scoobydoo
scoobydoo

Reputation: 123

typecast float32 to int16 using arm neon intrinsics

I'm a newbie to arm neon intrinsics and I would like to scale the float32 array with a scalar (2^13 = 8192) and typecast to int16_t array. I believe I need to perform the below steps:

  1. Load the float buffer array
  2. Multiply with the scalar (2^13 = 8192)
  3. Convert them to 32-bit integers
  4. Convert 32-bit to 16-bit integers
  5. Store them into 16-bit buffer

Could you please check and correct the below code:

    // convert float to int16
    uint32_t blk_cnt;
    float32x4_t f32x4;
    int32x4_t i32x4;
    int16x4_t i16x4;
    float32_t scale = 8192.0;
    /* Compute 4 complex samples at a time */
    blk_cnt = sz >> 2U;
    while (blk_cnt > 0U) {
        f32x4 = vld1q_f32 ((float32_t *) inpout);
        f32x4 = vmulq_n_f32(f32x4, scale);
        i32x4 = vcvtq_s32_f32 (f32x4);
        i16x4 = vmovn_s32 (i32x4);
        vst1_s16 (out, i16x4);      
        /* Increment pointers */
        out += 4;
        inpout += 4;
        /* Decrement the loop counter */
        blk_cnt--;
    }

Upvotes: 0

Views: 328

Answers (1)

You are most probably dealing with q13 (13 fraction bits) fixed point values. You just need to convert the floats to q13 int32 (vcvt_n_s32_f32), then shrink them to int16 by vqmovn.

https://developer.arm.com/documentation/dui0473/m/vfp-instructions/vcvt--between-floating-point-and-fixed-point-

Upvotes: 1

Related Questions