• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_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__neon_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->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 uint8x8_t voutput_min = vld1_dup_u8(&params->neon.output_min);
34   const uint8x8_t voutput_max = vld1_dup_u8(&params->neon.output_max);
35   for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
36     float32x4_t vx_lo = vld1q_f32(x); x += 4;
37     float32x4_t vx_hi = vld1q_f32(x); x += 4;
38 
39     vx_lo = vmulq_f32(vx_lo, vscale);
40     vx_hi = vmulq_f32(vx_hi, vscale);
41 
42     vx_lo = vaddq_f32(vx_lo, vmagic_bias);
43     vx_hi = vaddq_f32(vx_hi, vmagic_bias);
44 
45     const int32x4_t vacc_lo = vqsubq_s32(vreinterpretq_s32_f32(vx_lo), vmagic_bias_less_zero_point);
46     const int32x4_t vacc_hi = vqsubq_s32(vreinterpretq_s32_f32(vx_hi), vmagic_bias_less_zero_point);
47 
48     const int16x8_t vacc = vcombine_s16(vqmovn_s32(vacc_lo), vqmovn_s32(vacc_hi));
49 
50     uint8x8_t vy = vqmovun_s16(vacc);
51     vy = vmax_u8(vy, voutput_min);
52     vy = vmin_u8(vy, voutput_max);
53     vst1_u8(y, vy); y += 8;
54   }
55   if XNN_UNLIKELY(n != 0) {
56     assert(n >= 1 * sizeof(float));
57     assert(n <= 7 * sizeof(float));
58     float32x4_t vx_lo = vld1q_f32(x);
59     const float* x_hi = (const float*) ((uintptr_t) x + (n & (4 * sizeof(float))));
60     float32x4_t vx_hi = vld1q_f32(x_hi);
61 
62     vx_lo = vmulq_f32(vx_lo, vscale);
63     vx_hi = vmulq_f32(vx_hi, vscale);
64 
65     vx_lo = vaddq_f32(vx_lo, vmagic_bias);
66     vx_hi = vaddq_f32(vx_hi, vmagic_bias);
67 
68     const int32x4_t vacc_lo = vqsubq_s32(vreinterpretq_s32_f32(vx_lo), vmagic_bias_less_zero_point);
69     const int32x4_t vacc_hi = vqsubq_s32(vreinterpretq_s32_f32(vx_hi), vmagic_bias_less_zero_point);
70 
71     const int16x8_t vacc = vcombine_s16(vqmovn_s32(vacc_lo), vqmovn_s32(vacc_hi));
72 
73     uint8x8_t vy = vqmovun_s16(vacc);
74     vy = vmax_u8(vy, voutput_min);
75     vy = vmin_u8(vy, voutput_max);
76 
77     if (n & (4 * sizeof(float))) {
78       vst1_lane_u32((void*) y, vreinterpret_u32_u8(vy), 0); y += 4;
79       vy = vext_u8(vy, vy, 4);
80     }
81     if (n & (2 * sizeof(float))) {
82       vst1_lane_u16((void*) y, vreinterpret_u16_u8(vy), 0); y += 2;
83       vy = vext_u8(vy, vy, 2);
84     }
85     if (n & (1 * sizeof(float))) {
86       vst1_lane_u8(y, vy, 0);
87     }
88   }
89 }
90