xref: /aosp_15_r20/external/XNNPACK/src/f32-qu8-vcvt/gen/vcvt-neon-x16.c (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-qs8-vcvt/neon.c.in
3 //   Generator: tools/xngen
4 //
5 // Copyright 2021 Google LLC
6 //
7 // This source code is licensed under the BSD-style license found in the
8 // LICENSE file in the root directory of this source tree.
9 
10 #include <assert.h>
11 
12 #include <arm_neon.h>
13 
14 #include <xnnpack/common.h>
15 #include <xnnpack/intrinsics-polyfill.h>
16 #include <xnnpack/vcvt.h>
17 
18 
xnn_f32_qu8_vcvt_ukernel__neon_x16(size_t n,const float * x,uint8_t * y,const union xnn_f32_qu8_cvt_params params[restrict XNN_MIN_ELEMENTS (1)])19 void xnn_f32_qu8_vcvt_ukernel__neon_x16(
20     size_t n,
21     const float* x,
22     uint8_t* y,
23     const union xnn_f32_qu8_cvt_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
24 {
25   assert(n != 0);
26   assert(n % sizeof(float) == 0);
27   assert(x != NULL);
28   assert(y != NULL);
29 
30   const float32x4_t vscale = vld1q_dup_f32(&params->neon.scale);
31   const float32x4_t vmagic_bias = vld1q_dup_f32(&params->neon.magic_bias);
32   const int32x4_t vmagic_bias_less_zero_point = vld1q_dup_s32(&params->neon.magic_bias_less_zero_point);
33   const uint8x16_t voutput_min = vld1q_dup_u8(&params->neon.output_min);
34   const uint8x16_t voutput_max = vld1q_dup_u8(&params->neon.output_max);
35   for (; n >= 16 * sizeof(float); n -= 16 * sizeof(float)) {
36     float32x4_t vx0123 = vld1q_f32(x); x += 4;
37     float32x4_t vx4567 = vld1q_f32(x); x += 4;
38     float32x4_t vx89AB = vld1q_f32(x); x += 4;
39     float32x4_t vxCDEF = vld1q_f32(x); x += 4;
40 
41     vx0123 = vmulq_f32(vx0123, vscale);
42     vx4567 = vmulq_f32(vx4567, vscale);
43     vx89AB = vmulq_f32(vx89AB, vscale);
44     vxCDEF = vmulq_f32(vxCDEF, vscale);
45 
46     vx0123 = vaddq_f32(vx0123, vmagic_bias);
47     vx4567 = vaddq_f32(vx4567, vmagic_bias);
48     vx89AB = vaddq_f32(vx89AB, vmagic_bias);
49     vxCDEF = vaddq_f32(vxCDEF, vmagic_bias);
50 
51     const int32x4_t vacc0123 = vqsubq_s32(vreinterpretq_s32_f32(vx0123), vmagic_bias_less_zero_point);
52     const int32x4_t vacc4567 = vqsubq_s32(vreinterpretq_s32_f32(vx4567), vmagic_bias_less_zero_point);
53     const int32x4_t vacc89AB = vqsubq_s32(vreinterpretq_s32_f32(vx89AB), vmagic_bias_less_zero_point);
54     const int32x4_t vaccCDEF = vqsubq_s32(vreinterpretq_s32_f32(vxCDEF), vmagic_bias_less_zero_point);
55 
56     const int16x8_t vacc01234567 = vcombine_s16(vqmovn_s32(vacc0123), vqmovn_s32(vacc4567));
57     const int16x8_t vacc89ABCDEF = vcombine_s16(vqmovn_s32(vacc89AB), vqmovn_s32(vaccCDEF));
58 
59     uint8x16_t vy0123456789ABCDEF = vcombine_u8(vqmovun_s16(vacc01234567), vqmovun_s16(vacc89ABCDEF));
60 
61     vy0123456789ABCDEF = vmaxq_u8(vy0123456789ABCDEF, voutput_min);
62 
63     vy0123456789ABCDEF = vminq_u8(vy0123456789ABCDEF, voutput_max);
64 
65     vst1q_u8(y, vy0123456789ABCDEF); y += 16;
66   }
67   for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
68     float32x4_t vx_lo = vld1q_f32(x); x += 4;
69     float32x4_t vx_hi = vld1q_f32(x); x += 4;
70 
71     vx_lo = vmulq_f32(vx_lo, vscale);
72     vx_hi = vmulq_f32(vx_hi, vscale);
73 
74     vx_lo = vaddq_f32(vx_lo, vmagic_bias);
75     vx_hi = vaddq_f32(vx_hi, vmagic_bias);
76 
77     const int32x4_t vacc_lo = vqsubq_s32(vreinterpretq_s32_f32(vx_lo), vmagic_bias_less_zero_point);
78     const int32x4_t vacc_hi = vqsubq_s32(vreinterpretq_s32_f32(vx_hi), vmagic_bias_less_zero_point);
79 
80     const int16x8_t vacc = vcombine_s16(vqmovn_s32(vacc_lo), vqmovn_s32(vacc_hi));
81 
82     uint8x8_t vy = vqmovun_s16(vacc);
83     vy = vmax_u8(vy, vget_low_u8(voutput_min));
84     vy = vmin_u8(vy, vget_low_u8(voutput_max));
85     vst1_u8(y, vy); y += 8;
86   }
87   if XNN_UNLIKELY(n != 0) {
88     assert(n >= 1 * sizeof(float));
89     assert(n <= 7 * sizeof(float));
90     float32x4_t vx_lo = vld1q_f32(x);
91     const float* x_hi = (const float*) ((uintptr_t) x + (n & (4 * sizeof(float))));
92     float32x4_t vx_hi = vld1q_f32(x_hi);
93 
94     vx_lo = vmulq_f32(vx_lo, vscale);
95     vx_hi = vmulq_f32(vx_hi, vscale);
96 
97     vx_lo = vaddq_f32(vx_lo, vmagic_bias);
98     vx_hi = vaddq_f32(vx_hi, vmagic_bias);
99 
100     const int32x4_t vacc_lo = vqsubq_s32(vreinterpretq_s32_f32(vx_lo), vmagic_bias_less_zero_point);
101     const int32x4_t vacc_hi = vqsubq_s32(vreinterpretq_s32_f32(vx_hi), vmagic_bias_less_zero_point);
102 
103     const int16x8_t vacc = vcombine_s16(vqmovn_s32(vacc_lo), vqmovn_s32(vacc_hi));
104 
105     uint8x8_t vy = vqmovun_s16(vacc);
106     vy = vmax_u8(vy, vget_low_u8(voutput_min));
107     vy = vmin_u8(vy, vget_low_u8(voutput_max));
108 
109     if (n & (4 * sizeof(float))) {
110       vst1_lane_u32((void*) y, vreinterpret_u32_u8(vy), 0); y += 4;
111       vy = vext_u8(vy, vy, 4);
112     }
113     if (n & (2 * sizeof(float))) {
114       vst1_lane_u16((void*) y, vreinterpret_u16_u8(vy), 0); y += 2;
115       vy = vext_u8(vy, vy, 2);
116     }
117     if (n & (1 * sizeof(float))) {
118       vst1_lane_u8(y, vy, 0);
119     }
120   }
121 }
122