Debian Codegraph @

src/f32-f16-vcvt/gen/f32-f16-vcvt-sse2-u8.c

raw
1 // Auto-generated file. Do not edit!
2 // Template: src/f32-f16-vcvt/sse.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 <emmintrin.h>
13
14 #include "xnnpack/common.h"
15 #include "xnnpack/unaligned.h"
16 #include "xnnpack/vcvt.h"
17
18
19 void xnn_f32_f16_vcvt_ukernel__sse2_u8(
20 size_t batch,
21 const float* input,
22 xnn_float16* output,
23 const void* params) XNN_OOB_READS
24 {
25 assert(batch != 0);
26 assert(batch % sizeof(float) == 0);
27 assert(input != NULL);
28 assert(output != NULL);
29
30 const __m128 vnonsign_mask = _mm_castsi128_ps(_mm_set1_epi32(UINT32_C(0x7FFFFFFF)));
31 const __m128i vexp_bias = _mm_set1_epi32(UINT32_C(0x07800000));
32 const __m128 vscale_to_inf = _mm_set1_ps(0x1.0p+112f);
33 const __m128i vexpw_max = _mm_set1_epi32(UINT32_C(0x7F800000));
34 const __m128 vscale_to_zero = _mm_set1_ps(0x1.0p-110f);
35 const __m128i vbias_min = _mm_set1_epi32(UINT32_C(0x40008000)); // 0x8000, 0x4000, 0x8000, 0x4000, ...
36 const __m128i vmanth_mask = _mm_set1_epi32(UINT32_C(0x00000FFF));
37 const __m128i vexph_mask = _mm_set1_epi32(UINT32_C(0x00007C00));
38 const __m128i vnanh = _mm_set1_epi16(UINT16_C(0x7E00));
39
40 XNN_FORCE_REALIZATION(vnonsign_mask);
41 XNN_FORCE_REALIZATION(vexp_bias);
42 XNN_FORCE_REALIZATION(vscale_to_inf);
43 XNN_FORCE_REALIZATION(vexpw_max);
44 XNN_FORCE_REALIZATION(vscale_to_zero);
45 XNN_FORCE_REALIZATION(vbias_min);
46 XNN_FORCE_REALIZATION(vmanth_mask);
47 XNN_FORCE_REALIZATION(vexph_mask);
48 XNN_FORCE_REALIZATION(vnanh);
49
50 uint16_t* o = (uint16_t*) output;
51 for (; batch >= 8 * sizeof(float); batch -= 8 * sizeof(float)) {
52 const __m128 vx_lo = _mm_loadu_ps(input);
53 const __m128 vx_hi = _mm_loadu_ps(input + 4);
54 input += 8;
55
56 const __m128 vabsx_lo = _mm_and_ps(vx_lo, vnonsign_mask);
57 const __m128 vabsx_hi = _mm_and_ps(vx_hi, vnonsign_mask);
58
59 const __m128 vsignx_lo = _mm_xor_ps(vx_lo, vabsx_lo);
60 const __m128 vsignx_hi = _mm_xor_ps(vx_hi, vabsx_hi);
61 __m128i vbias_lo = _mm_add_epi32(_mm_castps_si128(vabsx_lo), vexp_bias);
62 __m128i vbias_hi = _mm_add_epi32(_mm_castps_si128(vabsx_hi), vexp_bias);
63 __m128 vf_lo = _mm_mul_ps(vabsx_lo, vscale_to_inf);
64 __m128 vf_hi = _mm_mul_ps(vabsx_hi, vscale_to_inf);
65 const __m128i vnanmaskw_lo = _mm_cmpgt_epi32(_mm_castps_si128(vabsx_lo), vexpw_max);
66 const __m128i vnanmaskw_hi = _mm_cmpgt_epi32(_mm_castps_si128(vabsx_hi), vexpw_max);
67
68 vbias_lo = _mm_and_si128(vbias_lo, vexpw_max);
69 vbias_hi = _mm_and_si128(vbias_hi, vexpw_max);
70 vf_lo = _mm_mul_ps(vf_lo, vscale_to_zero);
71 vf_hi = _mm_mul_ps(vf_hi, vscale_to_zero);
72 const __m128i vnanmaskh = _mm_packs_epi32(vnanmaskw_lo, vnanmaskw_hi);
73 const __m128i vsignh = _mm_packs_epi32(_mm_castps_si128(vsignx_lo), _mm_castps_si128(vsignx_hi));
74
75 vbias_lo = _mm_max_epi16(vbias_lo, vbias_min);
76 vbias_hi = _mm_max_epi16(vbias_hi, vbias_min);
77 __m128i vh = _mm_and_si128(vnanh, vnanmaskh);
78
79 vf_lo = _mm_add_ps(vf_lo, _mm_castsi128_ps(vbias_lo));
80 vf_hi = _mm_add_ps(vf_hi, _mm_castsi128_ps(vbias_hi));
81 vh = _mm_or_si128(vh, vsignh);
82
83 __m128i vexpw_lo = _mm_srli_epi32(_mm_castps_si128(vf_lo), 13);
84 __m128i vexpw_hi = _mm_srli_epi32(_mm_castps_si128(vf_hi), 13);
85 const __m128i vmantw_lo = _mm_and_si128(_mm_castps_si128(vf_lo), vmanth_mask);
86 const __m128i vmantw_hi = _mm_and_si128(_mm_castps_si128(vf_hi), vmanth_mask);
87
88 vexpw_lo = _mm_and_si128(vexpw_lo, vexph_mask);
89 vexpw_hi = _mm_and_si128(vexpw_hi, vexph_mask);
90
91 const __m128i vnonsignw_lo = _mm_add_epi32(vmantw_lo, vexpw_lo);
92 const __m128i vnonsignw_hi = _mm_add_epi32(vmantw_hi, vexpw_hi);
93
94 const __m128i vnonsignh = _mm_packs_epi32(vnonsignw_lo, vnonsignw_hi);
95
96 vh = _mm_or_si128(vh, _mm_andnot_si128(vnanmaskh, vnonsignh));
97
98 _mm_storeu_si128((__m128i*) o, vh);
99 o += 8;
100 }
101 if XNN_UNPREDICTABLE(batch != 0) {
102 const __m128 vx_lo = _mm_loadu_ps(input);
103 const float* input_hi = (const float*) ((uintptr_t) input + (batch & (4 * sizeof(float))));
104 const __m128 vx_hi = _mm_loadu_ps(input_hi);
105
106 const __m128 vabsx_lo = _mm_and_ps(vx_lo, vnonsign_mask);
107 const __m128 vabsx_hi = _mm_and_ps(vx_hi, vnonsign_mask);
108
109 const __m128 vsignx_lo = _mm_xor_ps(vx_lo, vabsx_lo);
110 const __m128 vsignx_hi = _mm_xor_ps(vx_hi, vabsx_hi);
111 __m128i vbias_lo = _mm_add_epi32(_mm_castps_si128(vabsx_lo), vexp_bias);
112 __m128i vbias_hi = _mm_add_epi32(_mm_castps_si128(vabsx_hi), vexp_bias);
113 __m128 vf_lo = _mm_mul_ps(vabsx_lo, vscale_to_inf);
114 __m128 vf_hi = _mm_mul_ps(vabsx_hi, vscale_to_inf);
115 const __m128i vnanmaskw_lo = _mm_cmpgt_epi32(_mm_castps_si128(vabsx_lo), vexpw_max);
116 const __m128i vnanmaskw_hi = _mm_cmpgt_epi32(_mm_castps_si128(vabsx_hi), vexpw_max);
117
118 vbias_lo = _mm_and_si128(vbias_lo, vexpw_max);
119 vbias_hi = _mm_and_si128(vbias_hi, vexpw_max);
120 vf_lo = _mm_mul_ps(vf_lo, vscale_to_zero);
121 vf_hi = _mm_mul_ps(vf_hi, vscale_to_zero);
122 const __m128i vnanmaskh = _mm_packs_epi32(vnanmaskw_lo, vnanmaskw_hi);
123 const __m128i vsignh = _mm_packs_epi32(_mm_castps_si128(vsignx_lo), _mm_castps_si128(vsignx_hi));
124
125 vbias_lo = _mm_max_epi16(vbias_lo, vbias_min);
126 vbias_hi = _mm_max_epi16(vbias_hi, vbias_min);
127 __m128i vh = _mm_and_si128(vnanh, vnanmaskh);
128
129 vf_lo = _mm_add_ps(vf_lo, _mm_castsi128_ps(vbias_lo));
130 vf_hi = _mm_add_ps(vf_hi, _mm_castsi128_ps(vbias_hi));
131 vh = _mm_or_si128(vh, vsignh);
132
133 __m128i vexpw_lo = _mm_srli_epi32(_mm_castps_si128(vf_lo), 13);
134 __m128i vexpw_hi = _mm_srli_epi32(_mm_castps_si128(vf_hi), 13);
135 const __m128i vmantw_lo = _mm_and_si128(_mm_castps_si128(vf_lo), vmanth_mask);
136 const __m128i vmantw_hi = _mm_and_si128(_mm_castps_si128(vf_hi), vmanth_mask);
137
138 vexpw_lo = _mm_and_si128(vexpw_lo, vexph_mask);
139 vexpw_hi = _mm_and_si128(vexpw_hi, vexph_mask);
140
141 const __m128i vnonsignw_lo = _mm_add_epi32(vmantw_lo, vexpw_lo);
142 const __m128i vnonsignw_hi = _mm_add_epi32(vmantw_hi, vexpw_hi);
143
144 const __m128i vnonsignh = _mm_packs_epi32(vnonsignw_lo, vnonsignw_hi);
145
146 vh = _mm_or_si128(vh, _mm_andnot_si128(vnanmaskh, vnonsignh));
147
148 if (batch & (4 * sizeof(float))) {
149 _mm_storel_epi64((__m128i*) o, vh);
150 vh = _mm_unpackhi_epi64(vh, vh);
151 o += 4;
152 }
153 if (batch & (2 * sizeof(float))) {
154 unaligned_store_u32(o, (uint32_t) _mm_cvtsi128_si32(vh));
155 vh = _mm_srli_epi64(vh, 32);
156 o += 2;
157 }
158 if (batch & (1 * sizeof(float))) {
159 *o = (uint16_t) _mm_cvtsi128_si32(vh);
160 }
161 }
162 }