1 // Auto-generated file. Do not edit!
2 // Template: src/f16-f32-vcvt/sse-int32.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/vcvt.h>
16
17
xnn_f16_f32_vcvt_ukernel__sse2_int32_x16(size_t n,const void * input,float * output,const union xnn_f16_f32_cvt_params params[restrict XNN_MIN_ELEMENTS (1)])18 void xnn_f16_f32_vcvt_ukernel__sse2_int32_x16(
19 size_t n,
20 const void* input,
21 float* output,
22 const union xnn_f16_f32_cvt_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
23 {
24 assert(n != 0);
25 assert(n % sizeof(uint16_t) == 0);
26 assert(input != NULL);
27 assert(output != NULL);
28
29 const __m128i vsign_mask = _mm_load_si128((const __m128i*) params->sse_int32.sign_mask);
30 const __m128i vexp_offset = _mm_load_si128((const __m128i*) params->sse_int32.exp_offset);
31 const __m128 vexp_scale = _mm_load_ps(params->sse_int32.exp_scale);
32 const __m128i vmagic_bias = _mm_load_si128((const __m128i*) params->sse_int32.magic_bias);
33 const __m128i vdenorm_cutoff = _mm_load_si128((const __m128i*) params->sse_int32.denorm_cutoff);
34
35 const uint16_t* i = (const uint16_t*) input;
36 for (; n >= 16 * sizeof(uint16_t); n -= 16 * sizeof(uint16_t)) {
37 const __m128i vh0 = _mm_loadu_si128((const __m128i*) i);
38 const __m128i vh1 = _mm_loadu_si128((const __m128i*) (i + 8));
39 i += 16;
40
41 const __m128i vw0 = _mm_unpacklo_epi16(_mm_setzero_si128(), vh0);
42 const __m128i vw1 = _mm_unpackhi_epi16(_mm_setzero_si128(), vh0);
43 const __m128i vw2 = _mm_unpacklo_epi16(_mm_setzero_si128(), vh1);
44 const __m128i vw3 = _mm_unpackhi_epi16(_mm_setzero_si128(), vh1);
45
46 const __m128i vsign0 = _mm_and_si128(vw0, vsign_mask);
47 const __m128i vsign1 = _mm_and_si128(vw1, vsign_mask);
48 const __m128i vsign2 = _mm_and_si128(vw2, vsign_mask);
49 const __m128i vsign3 = _mm_and_si128(vw3, vsign_mask);
50
51 const __m128i vnonsign0 = _mm_xor_si128(vw0, vsign0);
52 const __m128i vnonsign1 = _mm_xor_si128(vw1, vsign1);
53 const __m128i vnonsign2 = _mm_xor_si128(vw2, vsign2);
54 const __m128i vnonsign3 = _mm_xor_si128(vw3, vsign3);
55
56 const __m128i vnorm0 = _mm_castps_si128(_mm_mul_ps(_mm_castsi128_ps(_mm_add_epi32(_mm_srli_epi32(vnonsign0, 3), vexp_offset)), vexp_scale));
57 const __m128i vnorm1 = _mm_castps_si128(_mm_mul_ps(_mm_castsi128_ps(_mm_add_epi32(_mm_srli_epi32(vnonsign1, 3), vexp_offset)), vexp_scale));
58 const __m128i vnorm2 = _mm_castps_si128(_mm_mul_ps(_mm_castsi128_ps(_mm_add_epi32(_mm_srli_epi32(vnonsign2, 3), vexp_offset)), vexp_scale));
59 const __m128i vnorm3 = _mm_castps_si128(_mm_mul_ps(_mm_castsi128_ps(_mm_add_epi32(_mm_srli_epi32(vnonsign3, 3), vexp_offset)), vexp_scale));
60
61 const __m128i vdenorm0 = _mm_castps_si128(_mm_sub_ps(_mm_castsi128_ps(_mm_or_si128(_mm_srli_epi32(vnonsign0, 16), vmagic_bias)), _mm_castsi128_ps(vmagic_bias)));
62 const __m128i vdenorm1 = _mm_castps_si128(_mm_sub_ps(_mm_castsi128_ps(_mm_or_si128(_mm_srli_epi32(vnonsign1, 16), vmagic_bias)), _mm_castsi128_ps(vmagic_bias)));
63 const __m128i vdenorm2 = _mm_castps_si128(_mm_sub_ps(_mm_castsi128_ps(_mm_or_si128(_mm_srli_epi32(vnonsign2, 16), vmagic_bias)), _mm_castsi128_ps(vmagic_bias)));
64 const __m128i vdenorm3 = _mm_castps_si128(_mm_sub_ps(_mm_castsi128_ps(_mm_or_si128(_mm_srli_epi32(vnonsign3, 16), vmagic_bias)), _mm_castsi128_ps(vmagic_bias)));
65
66 const __m128i vmask0 = _mm_cmpgt_epi32(vnonsign0, vdenorm_cutoff);
67 const __m128i vmask1 = _mm_cmpgt_epi32(vnonsign1, vdenorm_cutoff);
68 const __m128i vmask2 = _mm_cmpgt_epi32(vnonsign2, vdenorm_cutoff);
69 const __m128i vmask3 = _mm_cmpgt_epi32(vnonsign3, vdenorm_cutoff);
70
71 const __m128i vf0 = _mm_or_si128(vsign0,
72 _mm_or_si128(_mm_and_si128(vmask0, vnorm0), _mm_andnot_si128(vmask0, vdenorm0)));
73 const __m128i vf1 = _mm_or_si128(vsign1,
74 _mm_or_si128(_mm_and_si128(vmask1, vnorm1), _mm_andnot_si128(vmask1, vdenorm1)));
75 const __m128i vf2 = _mm_or_si128(vsign2,
76 _mm_or_si128(_mm_and_si128(vmask2, vnorm2), _mm_andnot_si128(vmask2, vdenorm2)));
77 const __m128i vf3 = _mm_or_si128(vsign3,
78 _mm_or_si128(_mm_and_si128(vmask3, vnorm3), _mm_andnot_si128(vmask3, vdenorm3)));
79
80 _mm_storeu_ps(output, _mm_castsi128_ps(vf0));
81 _mm_storeu_ps(output + 4, _mm_castsi128_ps(vf1));
82 _mm_storeu_ps(output + 8, _mm_castsi128_ps(vf2));
83 _mm_storeu_ps(output + 12, _mm_castsi128_ps(vf3));
84 output += 16;
85 }
86 for (; n >= 8 * sizeof(uint16_t); n -= 8 * sizeof(uint16_t)) {
87 const __m128i vh = _mm_loadu_si128((const __m128i*) i);
88 i += 8;
89
90 const __m128i vw_lo = _mm_unpacklo_epi16(_mm_setzero_si128(), vh);
91 const __m128i vw_hi = _mm_unpackhi_epi16(_mm_setzero_si128(), vh);
92
93 const __m128i vsign_lo = _mm_and_si128(vw_lo, vsign_mask);
94 const __m128i vsign_hi = _mm_and_si128(vw_hi, vsign_mask);
95
96 const __m128i vnonsign_lo = _mm_xor_si128(vw_lo, vsign_lo);
97 const __m128i vnonsign_hi = _mm_xor_si128(vw_hi, vsign_hi);
98
99 const __m128i vnorm_lo = _mm_castps_si128(_mm_mul_ps(_mm_castsi128_ps(_mm_add_epi32(_mm_srli_epi32(vnonsign_lo, 3), vexp_offset)), vexp_scale));
100 const __m128i vnorm_hi = _mm_castps_si128(_mm_mul_ps(_mm_castsi128_ps(_mm_add_epi32(_mm_srli_epi32(vnonsign_hi, 3), vexp_offset)), vexp_scale));
101
102 const __m128i vdenorm_lo = _mm_castps_si128(_mm_sub_ps(_mm_castsi128_ps(_mm_or_si128(_mm_srli_epi32(vnonsign_lo, 16), vmagic_bias)), _mm_castsi128_ps(vmagic_bias)));
103 const __m128i vdenorm_hi = _mm_castps_si128(_mm_sub_ps(_mm_castsi128_ps(_mm_or_si128(_mm_srli_epi32(vnonsign_hi, 16), vmagic_bias)), _mm_castsi128_ps(vmagic_bias)));
104
105 const __m128i vmask_lo = _mm_cmpgt_epi32(vnonsign_lo, vdenorm_cutoff);
106 const __m128i vf_lo = _mm_or_si128(vsign_lo,
107 _mm_or_si128(_mm_and_si128(vmask_lo, vnorm_lo), _mm_andnot_si128(vmask_lo, vdenorm_lo)));
108
109 const __m128i vmask_hi = _mm_cmpgt_epi32(vnonsign_hi, vdenorm_cutoff);
110 const __m128i vf_hi = _mm_or_si128(vsign_hi,
111 _mm_or_si128(_mm_and_si128(vmask_hi, vnorm_hi), _mm_andnot_si128(vmask_hi, vdenorm_hi)));
112
113 _mm_storeu_ps(output, _mm_castsi128_ps(vf_lo));
114 _mm_storeu_ps(output + 4, _mm_castsi128_ps(vf_hi));
115 output += 8;
116 }
117 if XNN_UNPREDICTABLE(n != 0) {
118 const __m128i vh = _mm_loadu_si128((const __m128i*) i);
119
120 const __m128i vw_lo = _mm_unpacklo_epi16(_mm_setzero_si128(), vh);
121 const __m128i vw_hi = _mm_unpackhi_epi16(_mm_setzero_si128(), vh);
122
123 const __m128i vsign_lo = _mm_and_si128(vw_lo, vsign_mask);
124 const __m128i vsign_hi = _mm_and_si128(vw_hi, vsign_mask);
125
126 const __m128i vnonsign_lo = _mm_xor_si128(vw_lo, vsign_lo);
127 const __m128i vnonsign_hi = _mm_xor_si128(vw_hi, vsign_hi);
128
129 const __m128i vnorm_lo = _mm_castps_si128(_mm_mul_ps(_mm_castsi128_ps(_mm_add_epi32(_mm_srli_epi32(vnonsign_lo, 3), vexp_offset)), vexp_scale));
130 const __m128i vnorm_hi = _mm_castps_si128(_mm_mul_ps(_mm_castsi128_ps(_mm_add_epi32(_mm_srli_epi32(vnonsign_hi, 3), vexp_offset)), vexp_scale));
131
132 const __m128i vdenorm_lo = _mm_castps_si128(_mm_sub_ps(_mm_castsi128_ps(_mm_or_si128(_mm_srli_epi32(vnonsign_lo, 16), vmagic_bias)), _mm_castsi128_ps(vmagic_bias)));
133 const __m128i vdenorm_hi = _mm_castps_si128(_mm_sub_ps(_mm_castsi128_ps(_mm_or_si128(_mm_srli_epi32(vnonsign_hi, 16), vmagic_bias)), _mm_castsi128_ps(vmagic_bias)));
134
135 const __m128i vmask_lo = _mm_cmpgt_epi32(vnonsign_lo, vdenorm_cutoff);
136 __m128i vf = _mm_or_si128(vsign_lo,
137 _mm_or_si128(_mm_and_si128(vmask_lo, vnorm_lo), _mm_andnot_si128(vmask_lo, vdenorm_lo)));
138
139 if (n & (4 * sizeof(uint16_t))) {
140 _mm_storeu_ps(output, _mm_castsi128_ps(vf));
141 output += 4;
142
143 const __m128i vmask_hi = _mm_cmpgt_epi32(vnonsign_hi, vdenorm_cutoff);
144 vf = _mm_or_si128(vsign_hi,
145 _mm_or_si128(_mm_and_si128(vmask_hi, vnorm_hi), _mm_andnot_si128(vmask_hi, vdenorm_hi)));
146 }
147 if (n & (2 * sizeof(uint16_t))) {
148 _mm_storel_pi((__m64*) output, _mm_castsi128_ps(vf));
149 output += 2;
150
151 vf = _mm_castps_si128(_mm_movehl_ps(_mm_castsi128_ps(vf), _mm_castsi128_ps(vf)));
152 }
153 if (n & (1 * sizeof(uint16_t))) {
154 _mm_store_ss(output, _mm_castsi128_ps(vf));
155 }
156 }
157 }
158