xref: /aosp_15_r20/external/XNNPACK/src/math/cvt-f16-f32-neon-int32.c (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
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 <arm_neon.h>
11 
12 #include <xnnpack/math-stubs.h>
13 
14 
xnn_math_f16_f32_cvt__neon_int32(size_t n,const void * input,float * output)15 void xnn_math_f16_f32_cvt__neon_int32(
16     size_t n,
17     const void* input,
18     float* output)
19 {
20   assert(n % (8 * sizeof(float)) == 0);
21 
22   const uint32x4_t vsign_mask = vmovq_n_u32(0x80000000);
23   const uint32x4_t vexp_offset = vmovq_n_u32(0x70000000);
24   const float32x4_t vexp_scale = vmovq_n_f32(0x1.0p-112f);
25   const uint32x4_t vmagic_mask = vmovq_n_u32(0x3F000000);
26   const float32x4_t vmagic_bias = vmovq_n_f32(0.5f);
27   const uint32x4_t vdenorm_cutoff = vmovq_n_u32(0x04000000);
28 
29   const uint16_t* i = (const uint16_t*) input;
30   for (; n != 0; n -= 8 * sizeof(float)) {
31     const uint16x8_t vh = vld1q_u16(i); i += 8;
32 
33     const uint32x4_t vw_lo = vshll_n_u16(vget_low_u16(vh), 16);
34     const uint32x4_t vw_hi = vshll_n_u16(vget_high_u16(vh), 16);
35 
36     const uint32x4_t vsign_lo = vandq_u32(vw_lo, vsign_mask);
37     const uint32x4_t vsign_hi = vandq_u32(vw_hi, vsign_mask);
38 
39     const uint32x4_t vnonsign_lo = veorq_u32(vw_lo, vsign_lo);
40     const uint32x4_t vnonsign_hi = veorq_u32(vw_hi, vsign_hi);
41 
42     const float32x4_t vnorm_lo = vmulq_f32(vreinterpretq_f32_u32(vsraq_n_u32(vexp_offset, vnonsign_lo, 3)), vexp_scale);
43     const float32x4_t vnorm_hi = vmulq_f32(vreinterpretq_f32_u32(vsraq_n_u32(vexp_offset, vnonsign_hi, 3)), vexp_scale);
44 
45     const float32x4_t vdenorm_lo = vsubq_f32(vreinterpretq_f32_u32(vsriq_n_u32(vmagic_mask, vnonsign_lo, 16)), vmagic_bias);
46     const float32x4_t vdenorm_hi = vsubq_f32(vreinterpretq_f32_u32(vsriq_n_u32(vmagic_mask, vnonsign_hi, 16)), vmagic_bias);
47 
48     const uint32x4_t vmask_lo = vcgtq_u32(vnonsign_lo, vdenorm_cutoff);
49     const uint32x4_t vmask_hi = vcgtq_u32(vnonsign_hi, vdenorm_cutoff);
50 
51     const uint32x4_t vf_lo = vorrq_u32(vsign_lo, vreinterpretq_u32_f32(vbslq_f32(vmask_lo, vnorm_lo, vdenorm_lo)));
52     const uint32x4_t vf_hi = vorrq_u32(vsign_hi, vreinterpretq_u32_f32(vbslq_f32(vmask_hi, vnorm_hi, vdenorm_hi)));
53 
54     vst1q_f32(output, vreinterpretq_f32_u32(vf_lo)); output += 4;
55     vst1q_f32(output, vreinterpretq_f32_u32(vf_hi)); output += 4;
56   }
57 }
58