1 // Auto-generated file. Do not edit!
2 // Template: src/f16-f32-vcvt/wasmsimd-int16.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 <wasm_simd128.h>
13
14 #include <xnnpack/common.h>
15 #include <xnnpack/vcvt.h>
16
17
xnn_f16_f32_vcvt_ukernel__wasmsimd_int16_x8(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__wasmsimd_int16_x8(
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 v128_t vsign_mask = wasm_v128_load64_splat(params->wasmsimd_int16.sign_mask);
30 const v128_t vexp_offset = wasm_v128_load64_splat(params->wasmsimd_int16.exp_offset);
31 const v128_t vexp_scale = wasm_v128_load64_splat(params->wasmsimd_int16.exp_scale);
32 const v128_t vmagic_mask = wasm_v128_load64_splat(params->wasmsimd_int16.magic_mask);
33 const v128_t vmagic_bias = wasm_v128_load64_splat(params->wasmsimd_int16.magic_bias);
34 const v128_t vdenorm_cutoff = wasm_v128_load64_splat(params->wasmsimd_int16.denorm_cutoff);
35
36 const uint16_t* i = (const uint16_t*) input;
37 for (; n >= 8 * sizeof(uint16_t); n -= 8 * sizeof(uint16_t)) {
38 const v128_t vh = wasm_v128_load(i);
39 i += 8;
40
41 const v128_t vsign = wasm_v128_and(vh, vsign_mask);
42
43 const v128_t vnonsign = wasm_v128_xor(vh, vsign);
44
45 const v128_t vprenorm_lo = wasm_i16x8_shl(vnonsign, 13);
46 const v128_t vprenorm_hi = wasm_i16x8_add(wasm_u16x8_shr(vnonsign, 3), vexp_offset);
47
48 const v128_t vnorm_lo = wasm_f32x4_mul(wasm_v16x8_shuffle(vprenorm_lo, vprenorm_hi, 0, 8, 1, 9, 2, 10, 3, 11), vexp_scale);
49 const v128_t vnorm_hi = wasm_f32x4_mul(wasm_v16x8_shuffle(vprenorm_lo, vprenorm_hi, 4, 12, 5, 13, 6, 14, 7, 15), vexp_scale);
50
51 const v128_t vdenorm_lo = wasm_f32x4_sub(wasm_v16x8_shuffle(vnonsign, vmagic_mask, 0, 8, 1, 9, 2, 10, 3, 11), vmagic_bias);
52 const v128_t vdenorm_hi = wasm_f32x4_sub(wasm_v16x8_shuffle(vnonsign, vmagic_mask, 4, 12, 5, 13, 6, 14, 7, 15), vmagic_bias);
53
54 const v128_t vmask = wasm_i16x8_gt(vnonsign, vdenorm_cutoff);
55 const v128_t vzero = wasm_i16x8_const_splat(0);
56
57 const v128_t vxmask_lo = wasm_i32x4_extend_low_i16x8(vmask);
58 const v128_t vf_lo = wasm_v128_or(wasm_v16x8_shuffle(vzero, vsign, 0, 8, 1, 9, 2, 10, 3, 11),
59 wasm_v128_bitselect(vnorm_lo, vdenorm_lo, vxmask_lo));
60
61 const v128_t vxmask_hi = wasm_i32x4_extend_high_i16x8(vmask);
62 const v128_t vf_hi = wasm_v128_or(wasm_v16x8_shuffle(vzero, vsign, 4, 12, 5, 13, 6, 14, 7, 15),
63 wasm_v128_bitselect(vnorm_hi, vdenorm_hi, vxmask_hi));
64
65 wasm_v128_store(output, vf_lo);
66 wasm_v128_store(output + 4, vf_hi);
67 output += 8;
68 }
69 if XNN_UNLIKELY(n != 0) {
70 assert(n >= 1 * sizeof(uint16_t));
71 assert(n <= 7 * sizeof(uint16_t));
72 const v128_t vh = wasm_v128_load(i);
73
74 const v128_t vsign = wasm_v128_and(vh, vsign_mask);
75
76 const v128_t vnonsign = wasm_v128_xor(vh, vsign);
77
78 const v128_t vprenorm_lo = wasm_i16x8_shl(vnonsign, 13);
79 const v128_t vprenorm_hi = wasm_i16x8_add(wasm_u16x8_shr(vnonsign, 3), vexp_offset);
80
81 const v128_t vnorm_lo = wasm_f32x4_mul(wasm_v16x8_shuffle(vprenorm_lo, vprenorm_hi, 0, 8, 1, 9, 2, 10, 3, 11), vexp_scale);
82 const v128_t vnorm_hi = wasm_f32x4_mul(wasm_v16x8_shuffle(vprenorm_lo, vprenorm_hi, 4, 12, 5, 13, 6, 14, 7, 15), vexp_scale);
83
84 const v128_t vdenorm_lo = wasm_f32x4_sub(wasm_v16x8_shuffle(vnonsign, vmagic_mask, 0, 8, 1, 9, 2, 10, 3, 11), vmagic_bias);
85 const v128_t vdenorm_hi = wasm_f32x4_sub(wasm_v16x8_shuffle(vnonsign, vmagic_mask, 4, 12, 5, 13, 6, 14, 7, 15), vmagic_bias);
86
87 const v128_t vmask = wasm_i16x8_gt(vnonsign, vdenorm_cutoff);
88 const v128_t vzero = wasm_i16x8_const_splat(0);
89
90 const v128_t vxmask_lo = wasm_i32x4_extend_low_i16x8(vmask);
91 v128_t vf = wasm_v128_or(wasm_v16x8_shuffle(vzero, vsign, 0, 8, 1, 9, 2, 10, 3, 11),
92 wasm_v128_bitselect(vnorm_lo, vdenorm_lo, vxmask_lo));
93
94 if (n & (4 * sizeof(uint16_t))) {
95 wasm_v128_store(output, vf);
96 output += 4;
97
98 const v128_t vxmask_hi = wasm_i32x4_extend_high_i16x8(vmask);
99 vf = wasm_v128_or(wasm_v16x8_shuffle(vzero, vsign, 4, 12, 5, 13, 6, 14, 7, 15),
100 wasm_v128_bitselect(vnorm_hi, vdenorm_hi, vxmask_hi));
101 }
102 if (n & (2 * sizeof(uint16_t))) {
103 *((double*) output) = wasm_f64x2_extract_lane(vf, 0);
104 output += 2;
105
106 vf = wasm_v64x2_shuffle(vf, vf, 1, 1);
107 }
108 if (n & (1 * sizeof(uint16_t))) {
109 *((float*) output) = wasm_f32x4_extract_lane(vf, 0);
110 }
111 }
112 }
113