1 // Copyright 2021 Google LLC
2 //
3 // This source code is licensed under the BSD-style license found in the
4 // LICENSE file in the root directory of this source tree.
5
6 #include <assert.h>
7 #include <stddef.h>
8 #include <stdint.h>
9
10 #include <emmintrin.h>
11
12 #include <xnnpack/math-stubs.h>
13
14
xnn_math_f16_f32_cvt__sse2_int16(size_t n,const void * input,float * output)15 void xnn_math_f16_f32_cvt__sse2_int16(
16 size_t n,
17 const void* input,
18 float* output)
19 {
20 assert(n % (8 * sizeof(float)) == 0);
21
22 const __m128i vsign_mask = _mm_set1_epi16(0x8000);
23 const __m128i vexp_offset = _mm_set1_epi16(0x7000);
24 const __m128 vexp_scale = _mm_set1_ps(0x1.0p-112f);
25 const __m128i vmagic_mask = _mm_set1_epi16(0x3F00);
26 const __m128 vmagic_bias = _mm_set1_ps(0.5f);
27 const __m128i vdenorm_cutoff = _mm_set1_epi16(0x0400);
28
29 const uint16_t* i = (const uint16_t*) input;
30 for (; n != 0; n -= 8 * sizeof(float)) {
31 const __m128i vh = _mm_loadu_si128((const __m128i*) i);
32 i += 8;
33
34 const __m128i vsign = _mm_and_si128(vh, vsign_mask);
35
36 const __m128i vnonsign = _mm_xor_si128(vh, vsign);
37
38 const __m128i vprenorm_lo = _mm_slli_epi16(vnonsign, 13);
39 const __m128i vprenorm_hi = _mm_add_epi16(_mm_srli_epi16(vnonsign, 3), vexp_offset);
40
41 const __m128i vnorm_lo = _mm_castps_si128(_mm_mul_ps(_mm_castsi128_ps(_mm_unpacklo_epi16(vprenorm_lo, vprenorm_hi)), vexp_scale));
42 const __m128i vnorm_hi = _mm_castps_si128(_mm_mul_ps(_mm_castsi128_ps(_mm_unpackhi_epi16(vprenorm_lo, vprenorm_hi)), vexp_scale));
43
44 const __m128i vdenorm_lo = _mm_castps_si128(_mm_sub_ps(_mm_castsi128_ps(_mm_unpacklo_epi16(vnonsign, vmagic_mask)), vmagic_bias));
45 const __m128i vdenorm_hi = _mm_castps_si128(_mm_sub_ps(_mm_castsi128_ps(_mm_unpackhi_epi16(vnonsign, vmagic_mask)), vmagic_bias));
46
47 const __m128i vmask = _mm_cmpgt_epi16(vnonsign, vdenorm_cutoff);
48 const __m128i vmask_lo = _mm_unpacklo_epi16(vmask, vmask);
49 const __m128i vmask_hi = _mm_unpackhi_epi16(vmask, vmask);
50
51 const __m128i vf_lo = _mm_or_si128(_mm_unpacklo_epi16(_mm_setzero_si128(), vsign),
52 _mm_or_si128(_mm_and_si128(vmask_lo, vnorm_lo), _mm_andnot_si128(vmask_lo, vdenorm_lo)));
53 const __m128i vf_hi = _mm_or_si128(_mm_unpackhi_epi16(_mm_setzero_si128(), vsign),
54 _mm_or_si128(_mm_and_si128(vmask_hi, vnorm_hi), _mm_andnot_si128(vmask_hi, vdenorm_hi)));
55
56 _mm_storeu_ps(output, _mm_castsi128_ps(vf_lo));
57 _mm_storeu_ps(output + 4, _mm_castsi128_ps(vf_hi));
58 output += 8;
59 }
60 }
61