xref: /aosp_15_r20/external/XNNPACK/src/f32-qu8-vcvt/gen/vcvt-neonv8-x8.c (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-qs8-vcvt/neonv8.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__neonv8_x8(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__neonv8_x8(
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->neonv8.scale);
31   const int16x8_t voutput_zero_point = vld1q_dup_s16(&params->neonv8.output_zero_point);
32   const uint8x8_t voutput_min = vld1_dup_u8(&params->neonv8.output_min);
33   const uint8x8_t voutput_max = vld1_dup_u8(&params->neonv8.output_max);
34   for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
35     float32x4_t vx_lo = vld1q_f32(x); x += 4;
36     float32x4_t vx_hi = vld1q_f32(x); x += 4;
37 
38     vx_lo = vmulq_f32(vx_lo, vscale);
39     vx_hi = vmulq_f32(vx_hi, vscale);
40 
41     const int32x4_t vacc_lo = vcvtnq_s32_f32(vx_lo);
42     const int32x4_t vacc_hi = vcvtnq_s32_f32(vx_hi);
43 
44     int16x8_t vacc = vcombine_s16(vqmovn_s32(vacc_lo), vqmovn_s32(vacc_hi));
45     vacc = vqaddq_s16(vacc, voutput_zero_point);
46 
47     uint8x8_t vy = vqmovun_s16(vacc);
48     vy = vmax_u8(vy, voutput_min);
49     vy = vmin_u8(vy, voutput_max);
50     vst1_u8(y, vy); y += 8;
51   }
52   if XNN_UNLIKELY(n != 0) {
53     assert(n >= 1 * sizeof(float));
54     assert(n <= 7 * sizeof(float));
55     float32x4_t vx_lo = vld1q_f32(x);
56     const float* x_hi = (const float*) ((uintptr_t) x + (n & (4 * sizeof(float))));
57     float32x4_t vx_hi = vld1q_f32(x_hi);
58 
59     vx_lo = vmulq_f32(vx_lo, vscale);
60     vx_hi = vmulq_f32(vx_hi, vscale);
61 
62     const int32x4_t vacc_lo = vcvtnq_s32_f32(vx_lo);
63     const int32x4_t vacc_hi = vcvtnq_s32_f32(vx_hi);
64 
65     int16x8_t vacc = vcombine_s16(vqmovn_s32(vacc_lo), vqmovn_s32(vacc_hi));
66     vacc = vqaddq_s16(vacc, voutput_zero_point);
67 
68     uint8x8_t vy = vqmovun_s16(vacc);
69     vy = vmax_u8(vy, voutput_min);
70     vy = vmin_u8(vy, voutput_max);
71 
72     if (n & (4 * sizeof(float))) {
73       vst1_lane_u32((void*) y, vreinterpret_u32_u8(vy), 0); y += 4;
74       vy = vext_u8(vy, vy, 4);
75     }
76     if (n & (2 * sizeof(float))) {
77       vst1_lane_u16((void*) y, vreinterpret_u16_u8(vy), 0); y += 2;
78       vy = vext_u8(vy, vy, 2);
79     }
80     if (n & (1 * sizeof(float))) {
81       vst1_lane_u8(y, vy, 0);
82     }
83   }
84 }
85