Debian Codegraph @

src/f32-f16-vcvt/gen/f32-f16-vcvt-neon-u16.c

raw
1 // Auto-generated file. Do not edit!
2 // Template: src/f32-f16-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/vcvt.h"
16
17
18 void xnn_f32_f16_vcvt_ukernel__neon_u16(
19 size_t batch,
20 const float* input,
21 xnn_float16* output,
22 const void* params) XNN_OOB_READS
23 {
24 assert(batch != 0);
25 assert(batch % sizeof(float) == 0);
26 assert(input != NULL);
27 assert(output != NULL);
28
29 const uint32x4_t vexp_bias = vdupq_n_u32(UINT32_C(0x07800000));
30 const float32x4_t vscale_to_inf = vdupq_n_f32(0x1.0p+112f);
31 const uint32x4_t vexpw_max = vdupq_n_u32(UINT32_C(0x7F800000));
32 const float32x4_t vscale_to_zero = vdupq_n_f32(0x1.0p-110f);
33 const uint32x4_t vbias_min = vdupq_n_u32(UINT32_C(0x40000000));
34 const uint16x8_t vexph_mask = vdupq_n_u16(UINT16_C(0x7C00));
35 const uint16x8_t vmanth_mask = vdupq_n_u16(UINT16_C(0x0FFF));
36 const uint16x8_t vsignh_mask = vdupq_n_u16(UINT16_C(0x8000));
37 const uint16x8_t vnanh = vdupq_n_u16(UINT16_C(0x7E00));
38
39 // Only realizing a subset of these to match prior behavior.
40 XNN_FORCE_REALIZATION(vexp_bias);
41 XNN_FORCE_REALIZATION(vscale_to_inf);
42 XNN_FORCE_REALIZATION(vexpw_max);
43 XNN_FORCE_REALIZATION(vscale_to_zero);
44
45 uint16_t* o = (uint16_t*) output;
46 for (; batch >= 16 * sizeof(float); batch -= 16 * sizeof(float)) {
47 const float32x4_t vx0 = vld1q_f32(input); input += 4;
48 const float32x4_t vx1 = vld1q_f32(input); input += 4;
49 const float32x4_t vx2 = vld1q_f32(input); input += 4;
50 const float32x4_t vx3 = vld1q_f32(input); input += 4;
51
52 const float32x4_t vabsx0 = vabsq_f32(vx0);
53 const float32x4_t vabsx1 = vabsq_f32(vx1);
54 const float32x4_t vabsx2 = vabsq_f32(vx2);
55 const float32x4_t vabsx3 = vabsq_f32(vx3);
56
57 uint32x4_t vbias0 = vaddq_u32(vreinterpretq_u32_f32(vabsx0), vexp_bias);
58 uint32x4_t vbias1 = vaddq_u32(vreinterpretq_u32_f32(vabsx1), vexp_bias);
59 uint32x4_t vbias2 = vaddq_u32(vreinterpretq_u32_f32(vabsx2), vexp_bias);
60 uint32x4_t vbias3 = vaddq_u32(vreinterpretq_u32_f32(vabsx3), vexp_bias);
61
62 float32x4_t vf0 = vmulq_f32(vabsx0, vscale_to_inf);
63 float32x4_t vf1 = vmulq_f32(vabsx1, vscale_to_inf);
64 float32x4_t vf2 = vmulq_f32(vabsx2, vscale_to_inf);
65 float32x4_t vf3 = vmulq_f32(vabsx3, vscale_to_inf);
66 const uint32x4_t vnanmaskw0 = vcgtq_u32(vreinterpretq_u32_f32(vabsx0), vexpw_max);
67 const uint32x4_t vnanmaskw1 = vcgtq_u32(vreinterpretq_u32_f32(vabsx1), vexpw_max);
68 const uint32x4_t vnanmaskw2 = vcgtq_u32(vreinterpretq_u32_f32(vabsx2), vexpw_max);
69 const uint32x4_t vnanmaskw3 = vcgtq_u32(vreinterpretq_u32_f32(vabsx3), vexpw_max);
70
71 vbias0 = vandq_u32(vbias0, vexpw_max);
72 vbias1 = vandq_u32(vbias1, vexpw_max);
73 vbias2 = vandq_u32(vbias2, vexpw_max);
74 vbias3 = vandq_u32(vbias3, vexpw_max);
75 vf0 = vmulq_f32(vf0, vscale_to_zero);
76 vf1 = vmulq_f32(vf1, vscale_to_zero);
77 vf2 = vmulq_f32(vf2, vscale_to_zero);
78 vf3 = vmulq_f32(vf3, vscale_to_zero);
79
80 const uint16x8_t vnanmaskh0 = vcombine_u16(vmovn_u32(vnanmaskw0), vmovn_u32(vnanmaskw1));
81 const uint16x8_t vnanmaskh1 = vcombine_u16(vmovn_u32(vnanmaskw2), vmovn_u32(vnanmaskw3));
82 vbias0 = vmaxq_u32(vbias0, vbias_min);
83 vbias1 = vmaxq_u32(vbias1, vbias_min);
84 vbias2 = vmaxq_u32(vbias2, vbias_min);
85 vbias3 = vmaxq_u32(vbias3, vbias_min);
86
87 vf0 = vaddq_f32(vf0, vreinterpretq_f32_u32(vbias0));
88 vf1 = vaddq_f32(vf1, vreinterpretq_f32_u32(vbias1));
89 vf2 = vaddq_f32(vf2, vreinterpretq_f32_u32(vbias2));
90 vf3 = vaddq_f32(vf3, vreinterpretq_f32_u32(vbias3));
91
92 uint16x8_t vexph0 = vcombine_u16(vshrn_n_u32(vreinterpretq_u32_f32(vf0), 13), vshrn_n_u32(vreinterpretq_u32_f32(vf1), 13));
93 uint16x8_t vexph1 = vcombine_u16(vshrn_n_u32(vreinterpretq_u32_f32(vf2), 13), vshrn_n_u32(vreinterpretq_u32_f32(vf3), 13));
94 uint16x8_t vmanth0 = vcombine_u16(vmovn_u32(vreinterpretq_u32_f32(vf0)), vmovn_u32(vreinterpretq_u32_f32(vf1)));
95 uint16x8_t vmanth1 = vcombine_u16(vmovn_u32(vreinterpretq_u32_f32(vf2)), vmovn_u32(vreinterpretq_u32_f32(vf3)));
96 uint16x8_t vsignh0 = vcombine_u16(vshrn_n_u32(vreinterpretq_u32_f32(vx0), 16), vshrn_n_u32(vreinterpretq_u32_f32(vx1), 16));
97 uint16x8_t vsignh1 = vcombine_u16(vshrn_n_u32(vreinterpretq_u32_f32(vx2), 16), vshrn_n_u32(vreinterpretq_u32_f32(vx3), 16));
98
99 vexph0 = vandq_u16(vexph0, vexph_mask);
100 vexph1 = vandq_u16(vexph1, vexph_mask);
101 vmanth0 = vandq_u16(vmanth0, vmanth_mask);
102 vmanth1 = vandq_u16(vmanth1, vmanth_mask);
103 vsignh0 = vandq_u16(vsignh0, vsignh_mask);
104 vsignh1 = vandq_u16(vsignh1, vsignh_mask);
105
106 uint16x8_t vh0 = vaddq_u16(vmanth0, vexph0);
107 uint16x8_t vh1 = vaddq_u16(vmanth1, vexph1);
108
109 vh0 = vbslq_u16(vnanmaskh0, vnanh, vh0);
110 vh1 = vbslq_u16(vnanmaskh1, vnanh, vh1);
111
112 vh0 = vorrq_u16(vh0, vsignh0);
113 vh1 = vorrq_u16(vh1, vsignh1);
114
115 vst1q_u16(o, vh0); o += 8;
116 vst1q_u16(o, vh1); o += 8;
117 }
118 for (; batch >= 4 * sizeof(float); batch -= 4 * sizeof(float)) {
119 const float32x4_t vx = vld1q_f32(input); input += 4;
120
121 const float32x4_t vabsx = vabsq_f32(vx);
122
123 uint32x4_t vbias = vaddq_u32(vreinterpretq_u32_f32(vabsx), vexp_bias);
124
125 float32x4_t vf = vmulq_f32(vabsx, vscale_to_inf);
126 const uint32x4_t vnanmaskw = vcgtq_u32(vreinterpretq_u32_f32(vabsx), vexpw_max);
127
128 vbias = vandq_u32(vbias, vexpw_max);
129 vf = vmulq_f32(vf, vscale_to_zero);
130
131 const uint16x4_t vnanmaskh = vmovn_u32(vnanmaskw);
132 vbias = vmaxq_u32(vbias, vbias_min);
133
134 vf = vaddq_f32(vf, vreinterpretq_f32_u32(vbias));
135
136 uint16x4_t vexph = vshrn_n_u32(vreinterpretq_u32_f32(vf), 13);
137 uint16x4_t vmanth = vmovn_u32(vreinterpretq_u32_f32(vf));
138 uint16x4_t vsignh = vshrn_n_u32(vreinterpretq_u32_f32(vx), 16);
139
140 vexph = vand_u16(vexph, vget_low_u16(vexph_mask));
141 vmanth = vand_u16(vmanth, vget_low_u16(vmanth_mask));
142 vsignh = vand_u16(vsignh, vget_low_u16(vsignh_mask));
143
144 uint16x4_t vh = vadd_u16(vmanth, vexph);
145
146 vh = vbsl_u16(vnanmaskh, vget_low_u16(vnanh), vh);
147
148 vh = vorr_u16(vh, vsignh);
149
150 vst1_u16(o, vh); o += 4;
151 }
152 if XNN_UNLIKELY(batch != 0) {
153 assert(batch % sizeof(float) == 0);
154 assert(batch >= 1 * sizeof(float));
155 assert(batch <= 3 * sizeof(float));
156 const float32x4_t vx = vld1q_f32(input);
157
158 const float32x4_t vabsx = vabsq_f32(vx);
159
160 uint32x4_t vbias = vaddq_u32(vreinterpretq_u32_f32(vabsx), vexp_bias);
161
162 float32x4_t vf = vmulq_f32(vabsx, vscale_to_inf);
163 const uint32x4_t vnanmaskw = vcgtq_u32(vreinterpretq_u32_f32(vabsx), vexpw_max);
164
165 vbias = vandq_u32(vbias, vexpw_max);
166 vf = vmulq_f32(vf, vscale_to_zero);
167
168 const uint16x4_t vnanmaskh = vmovn_u32(vnanmaskw);
169 vbias = vmaxq_u32(vbias, vbias_min);
170
171 vf = vaddq_f32(vf, vreinterpretq_f32_u32(vbias));
172
173 uint16x4_t vexph = vshrn_n_u32(vreinterpretq_u32_f32(vf), 13);
174 uint16x4_t vmanth = vmovn_u32(vreinterpretq_u32_f32(vf));
175 uint16x4_t vsignh = vshrn_n_u32(vreinterpretq_u32_f32(vx), 16);
176
177 vexph = vand_u16(vexph, vget_low_u16(vexph_mask));
178 vmanth = vand_u16(vmanth, vget_low_u16(vmanth_mask));
179 vsignh = vand_u16(vsignh, vget_low_u16(vsignh_mask));
180
181 uint16x4_t vh = vadd_u16(vmanth, vexph);
182
183 vh = vbsl_u16(vnanmaskh, vget_low_u16(vnanh), vh);
184
185 vh = vorr_u16(vh, vsignh);
186
187 if (batch & (2 * sizeof(float))) {
188 vst1_lane_u32((void*) o, vreinterpret_u32_u16(vh), 0); o += 2;
189 vh = vext_u16(vh, vh, 2);
190 }
191 if (batch & (1 * sizeof(float))) {
192 vst1_lane_u16(o, vh, 0);
193 }
194 }
195 }