Debian Codegraph @

src/f32-f16-vcvt/gen/f32-f16-vcvt-neon-u8.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_u8(
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 >= 8 * sizeof(float); batch -= 8 * sizeof(float)) {
47 const float32x4_t vx0 = vld1q_f32(input); input += 4;
48 const float32x4_t vx1 = vld1q_f32(input); input += 4;
49
50 const float32x4_t vabsx0 = vabsq_f32(vx0);
51 const float32x4_t vabsx1 = vabsq_f32(vx1);
52
53 uint32x4_t vbias0 = vaddq_u32(vreinterpretq_u32_f32(vabsx0), vexp_bias);
54 uint32x4_t vbias1 = vaddq_u32(vreinterpretq_u32_f32(vabsx1), vexp_bias);
55
56 float32x4_t vf0 = vmulq_f32(vabsx0, vscale_to_inf);
57 float32x4_t vf1 = vmulq_f32(vabsx1, vscale_to_inf);
58 const uint32x4_t vnanmaskw0 = vcgtq_u32(vreinterpretq_u32_f32(vabsx0), vexpw_max);
59 const uint32x4_t vnanmaskw1 = vcgtq_u32(vreinterpretq_u32_f32(vabsx1), vexpw_max);
60
61 vbias0 = vandq_u32(vbias0, vexpw_max);
62 vbias1 = vandq_u32(vbias1, vexpw_max);
63 vf0 = vmulq_f32(vf0, vscale_to_zero);
64 vf1 = vmulq_f32(vf1, vscale_to_zero);
65
66 const uint16x8_t vnanmaskh0 = vcombine_u16(vmovn_u32(vnanmaskw0), vmovn_u32(vnanmaskw1));
67 vbias0 = vmaxq_u32(vbias0, vbias_min);
68 vbias1 = vmaxq_u32(vbias1, vbias_min);
69
70 vf0 = vaddq_f32(vf0, vreinterpretq_f32_u32(vbias0));
71 vf1 = vaddq_f32(vf1, vreinterpretq_f32_u32(vbias1));
72
73 uint16x8_t vexph0 = vcombine_u16(vshrn_n_u32(vreinterpretq_u32_f32(vf0), 13), vshrn_n_u32(vreinterpretq_u32_f32(vf1), 13));
74 uint16x8_t vmanth0 = vcombine_u16(vmovn_u32(vreinterpretq_u32_f32(vf0)), vmovn_u32(vreinterpretq_u32_f32(vf1)));
75 uint16x8_t vsignh0 = vcombine_u16(vshrn_n_u32(vreinterpretq_u32_f32(vx0), 16), vshrn_n_u32(vreinterpretq_u32_f32(vx1), 16));
76
77 vexph0 = vandq_u16(vexph0, vexph_mask);
78 vmanth0 = vandq_u16(vmanth0, vmanth_mask);
79 vsignh0 = vandq_u16(vsignh0, vsignh_mask);
80
81 uint16x8_t vh0 = vaddq_u16(vmanth0, vexph0);
82
83 vh0 = vbslq_u16(vnanmaskh0, vnanh, vh0);
84
85 vh0 = vorrq_u16(vh0, vsignh0);
86
87 vst1q_u16(o, vh0); o += 8;
88 }
89 for (; batch >= 4 * sizeof(float); batch -= 4 * sizeof(float)) {
90 const float32x4_t vx = vld1q_f32(input); input += 4;
91
92 const float32x4_t vabsx = vabsq_f32(vx);
93
94 uint32x4_t vbias = vaddq_u32(vreinterpretq_u32_f32(vabsx), vexp_bias);
95
96 float32x4_t vf = vmulq_f32(vabsx, vscale_to_inf);
97 const uint32x4_t vnanmaskw = vcgtq_u32(vreinterpretq_u32_f32(vabsx), vexpw_max);
98
99 vbias = vandq_u32(vbias, vexpw_max);
100 vf = vmulq_f32(vf, vscale_to_zero);
101
102 const uint16x4_t vnanmaskh = vmovn_u32(vnanmaskw);
103 vbias = vmaxq_u32(vbias, vbias_min);
104
105 vf = vaddq_f32(vf, vreinterpretq_f32_u32(vbias));
106
107 uint16x4_t vexph = vshrn_n_u32(vreinterpretq_u32_f32(vf), 13);
108 uint16x4_t vmanth = vmovn_u32(vreinterpretq_u32_f32(vf));
109 uint16x4_t vsignh = vshrn_n_u32(vreinterpretq_u32_f32(vx), 16);
110
111 vexph = vand_u16(vexph, vget_low_u16(vexph_mask));
112 vmanth = vand_u16(vmanth, vget_low_u16(vmanth_mask));
113 vsignh = vand_u16(vsignh, vget_low_u16(vsignh_mask));
114
115 uint16x4_t vh = vadd_u16(vmanth, vexph);
116
117 vh = vbsl_u16(vnanmaskh, vget_low_u16(vnanh), vh);
118
119 vh = vorr_u16(vh, vsignh);
120
121 vst1_u16(o, vh); o += 4;
122 }
123 if XNN_UNLIKELY(batch != 0) {
124 assert(batch % sizeof(float) == 0);
125 assert(batch >= 1 * sizeof(float));
126 assert(batch <= 3 * sizeof(float));
127 const float32x4_t vx = vld1q_f32(input);
128
129 const float32x4_t vabsx = vabsq_f32(vx);
130
131 uint32x4_t vbias = vaddq_u32(vreinterpretq_u32_f32(vabsx), vexp_bias);
132
133 float32x4_t vf = vmulq_f32(vabsx, vscale_to_inf);
134 const uint32x4_t vnanmaskw = vcgtq_u32(vreinterpretq_u32_f32(vabsx), vexpw_max);
135
136 vbias = vandq_u32(vbias, vexpw_max);
137 vf = vmulq_f32(vf, vscale_to_zero);
138
139 const uint16x4_t vnanmaskh = vmovn_u32(vnanmaskw);
140 vbias = vmaxq_u32(vbias, vbias_min);
141
142 vf = vaddq_f32(vf, vreinterpretq_f32_u32(vbias));
143
144 uint16x4_t vexph = vshrn_n_u32(vreinterpretq_u32_f32(vf), 13);
145 uint16x4_t vmanth = vmovn_u32(vreinterpretq_u32_f32(vf));
146 uint16x4_t vsignh = vshrn_n_u32(vreinterpretq_u32_f32(vx), 16);
147
148 vexph = vand_u16(vexph, vget_low_u16(vexph_mask));
149 vmanth = vand_u16(vmanth, vget_low_u16(vmanth_mask));
150 vsignh = vand_u16(vsignh, vget_low_u16(vsignh_mask));
151
152 uint16x4_t vh = vadd_u16(vmanth, vexph);
153
154 vh = vbsl_u16(vnanmaskh, vget_low_u16(vnanh), vh);
155
156 vh = vorr_u16(vh, vsignh);
157
158 if (batch & (2 * sizeof(float))) {
159 vst1_lane_u32((void*) o, vreinterpret_u32_u16(vh), 0); o += 2;
160 vh = vext_u16(vh, vh, 2);
161 }
162 if (batch & (1 * sizeof(float))) {
163 vst1_lane_u16(o, vh, 0);
164 }
165 }
166 }