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_int16(size_t n,const void * input,float * output)15 void xnn_math_f16_f32_cvt__neon_int16(
16 size_t n,
17 const void* input,
18 float* output)
19 {
20 assert(n % (8 * sizeof(float)) == 0);
21
22 const uint16x8_t vsign_mask = vmovq_n_u16(0x8000);
23 const uint16x8_t vexp_offset = vmovq_n_u16(0x7000);
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 uint16x8_t vdenorm_cutoff = vmovq_n_u16(0x0400);
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 uint16x8_t vsign = vandq_u16(vh, vsign_mask);
34
35 const uint16x8_t vnonsign = veorq_u16(vh, vsign);
36
37 const uint16x8x2_t vprenorm = vzipq_u16(vshlq_n_u16(vnonsign, 13), vsraq_n_u16(vexp_offset, vnonsign, 3));
38 const float32x4_t vnorm_lo = vmulq_f32(vreinterpretq_f32_u16(vprenorm.val[0]), vexp_scale);
39 const float32x4_t vnorm_hi = vmulq_f32(vreinterpretq_f32_u16(vprenorm.val[1]), vexp_scale);
40
41 const float32x4_t vdenorm_lo = vsubq_f32(vreinterpretq_f32_u32(vaddw_u16(vmagic_mask, vget_low_u16(vnonsign))), vmagic_bias);
42 const float32x4_t vdenorm_hi = vsubq_f32(vreinterpretq_f32_u32(vaddw_u16(vmagic_mask, vget_high_u16(vnonsign))), vmagic_bias);
43
44 const uint16x8_t vmask = vcgtq_u16(vnonsign, vdenorm_cutoff);
45 const uint32x4_t vmask_lo = vreinterpretq_u32_s32(vmovl_s16(vreinterpret_s16_u16(vget_low_u16(vmask))));
46 const uint32x4_t vmask_hi = vreinterpretq_u32_s32(vmovl_s16(vreinterpret_s16_u16(vget_high_u16(vmask))));
47
48 const uint32x4_t vf_lo = vorrq_u32(vshll_n_u16(vget_low_u16(vsign), 16),
49 vreinterpretq_u32_f32(vbslq_f32(vmask_lo, vnorm_lo, vdenorm_lo)));
50 const uint32x4_t vf_hi = vorrq_u32(vshll_n_u16(vget_high_u16(vsign), 16),
51 vreinterpretq_u32_f32(vbslq_f32(vmask_hi, vnorm_hi, vdenorm_hi)));
52
53 vst1q_f32(output, vreinterpretq_f32_u32(vf_lo)); output += 4;
54 vst1q_f32(output, vreinterpretq_f32_u32(vf_hi)); output += 4;
55 }
56 }
57