xref: /aosp_15_r20/external/XNNPACK/src/qu8-vlrelu/gen/vlrelu-neon-x32.c (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1 // Auto-generated file. Do not edit!
2 //   Template: src/qs8-vlrelu/neon.c.in
3 //   Generator: tools/xngen
4 //
5 // Copyright 2022 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/vlrelu.h>
16 
17 
xnn_qu8_vlrelu_ukernel__neon_x32(size_t n,const uint8_t * x,uint8_t * y,const union xnn_qu8_lrelu_params params[restrict XNN_MIN_ELEMENTS (1)])18 void xnn_qu8_vlrelu_ukernel__neon_x32(
19     size_t n,
20     const uint8_t* x,
21     uint8_t* y,
22     const union xnn_qu8_lrelu_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
23 {
24   assert(n != 0);
25   assert(n % sizeof(uint8_t) == 0);
26   assert(x != NULL);
27   assert(y != NULL);
28 
29   const uint16x8_t vinput_zero_point = vld1q_dup_u16(&params->neon.input_zero_point);
30   const int16x8_t vpositive_multiplier = vld1q_dup_s16(&params->neon.positive_multiplier);
31   const int16x8_t vnegative_multiplier = vld1q_dup_s16(&params->neon.negative_multiplier);
32   const int16x8_t voutput_zero_point = vld1q_dup_s16(&params->neon.output_zero_point);
33   for (; n >= 32 * sizeof(uint8_t); n -= 32 * sizeof(uint8_t)) {
34     const uint8x16_t vx0 = vld1q_u8(x); x += 16;
35     const uint8x16_t vx1 = vld1q_u8(x); x += 16;
36 
37     int16x8_t vacc0 = vreinterpretq_s16_u16(vsubw_u8(vinput_zero_point, vget_low_u8(vx0)));
38     int16x8_t vacc1 = vreinterpretq_s16_u16(vsubw_u8(vinput_zero_point, vget_high_u8(vx0)));
39     int16x8_t vacc2 = vreinterpretq_s16_u16(vsubw_u8(vinput_zero_point, vget_low_u8(vx1)));
40     int16x8_t vacc3 = vreinterpretq_s16_u16(vsubw_u8(vinput_zero_point, vget_high_u8(vx1)));
41 
42     const uint16x8_t vmask0 = vcltq_s16(vacc0, vmovq_n_s16(0));
43     const uint16x8_t vmask1 = vcltq_s16(vacc1, vmovq_n_s16(0));
44     const uint16x8_t vmask2 = vcltq_s16(vacc2, vmovq_n_s16(0));
45     const uint16x8_t vmask3 = vcltq_s16(vacc3, vmovq_n_s16(0));
46 
47     vacc0 = vshlq_n_s16(vacc0, 7);
48     vacc1 = vshlq_n_s16(vacc1, 7);
49     vacc2 = vshlq_n_s16(vacc2, 7);
50     vacc3 = vshlq_n_s16(vacc3, 7);
51 
52     const int16x8_t vmultiplier0 = vbslq_s16(vmask0, vpositive_multiplier, vnegative_multiplier);
53     const int16x8_t vmultiplier1 = vbslq_s16(vmask1, vpositive_multiplier, vnegative_multiplier);
54     const int16x8_t vmultiplier2 = vbslq_s16(vmask2, vpositive_multiplier, vnegative_multiplier);
55     const int16x8_t vmultiplier3 = vbslq_s16(vmask3, vpositive_multiplier, vnegative_multiplier);
56 
57     vacc0 = vqrdmulhq_s16(vacc0, vmultiplier0);
58     vacc1 = vqrdmulhq_s16(vacc1, vmultiplier1);
59     vacc2 = vqrdmulhq_s16(vacc2, vmultiplier2);
60     vacc3 = vqrdmulhq_s16(vacc3, vmultiplier3);
61 
62     vacc0 = vqaddq_s16(vacc0, voutput_zero_point);
63     vacc1 = vqaddq_s16(vacc1, voutput_zero_point);
64     vacc2 = vqaddq_s16(vacc2, voutput_zero_point);
65     vacc3 = vqaddq_s16(vacc3, voutput_zero_point);
66 
67     const uint8x16_t vy0 = vcombine_u8(vqmovun_s16(vacc0), vqmovun_s16(vacc1));
68     const uint8x16_t vy1 = vcombine_u8(vqmovun_s16(vacc2), vqmovun_s16(vacc3));
69 
70     vst1q_u8(y, vy0); y += 16;
71     vst1q_u8(y, vy1); y += 16;
72   }
73   for (; n >= 8 * sizeof(uint8_t); n -= 8 * sizeof(uint8_t)) {
74     const uint8x8_t vx = vld1_u8(x); x += 8;
75     int16x8_t vacc = vreinterpretq_s16_u16(vsubw_u8(vinput_zero_point, vx));
76     const uint16x8_t vmask = vcltq_s16(vacc, vmovq_n_s16(0));
77     vacc = vshlq_n_s16(vacc, 7);
78     const int16x8_t vmultiplier = vbslq_s16(vmask, vpositive_multiplier, vnegative_multiplier);
79     vacc = vqrdmulhq_s16(vacc, vmultiplier);
80     vacc = vqaddq_s16(vacc, voutput_zero_point);
81     const uint8x8_t vy = vqmovun_s16(vacc);
82     vst1_u8(y, vy); y += 8;
83   }
84   if XNN_UNLIKELY(n != 0) {
85     assert(n >= 1 * sizeof(uint8_t));
86     assert(n <= 7 * sizeof(uint8_t));
87 
88     const uint8x8_t vx = vld1_u8(x);
89     int16x8_t vacc = vreinterpretq_s16_u16(vsubw_u8(vinput_zero_point, vx));
90     const uint16x8_t vmask = vcltq_s16(vacc, vmovq_n_s16(0));
91     vacc = vshlq_n_s16(vacc, 7);
92     const int16x8_t vmultiplier = vbslq_s16(vmask, vpositive_multiplier, vnegative_multiplier);
93     vacc = vqrdmulhq_s16(vacc, vmultiplier);
94     vacc = vqaddq_s16(vacc, voutput_zero_point);
95     uint8x8_t vy = vqmovun_s16(vacc);
96 
97     if (n & (4 * sizeof(uint8_t))) {
98       vst1_lane_u32((void*) y, vreinterpret_u32_u8(vy), 0); y += 4;
99       vy = vext_u8(vy, vy, 4);
100     }
101     if (n & (2 * sizeof(uint8_t))) {
102       vst1_lane_u16((void*) y, vreinterpret_u16_u8(vy), 0); y += 2;
103       vy = vext_u8(vy, vy, 2);
104     }
105     if (n & (1 * sizeof(uint8_t))) {
106       vst1_lane_u8(y, vy, 0);
107     }
108   }
109 }
110