Debian Codegraph @

src/f16-f32-vcvt/gen/f16-f32-vcvt-wasmrelaxedsimd-int16-u8.c

raw
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
18 void xnn_f16_f32_vcvt_ukernel__wasmrelaxedsimd_int16_u8(
19 size_t batch,
20 const xnn_float16* input,
21 float* output,
22 const void* params) XNN_OOB_READS
23 {
24 assert(batch != 0);
25 assert(batch % sizeof(uint16_t) == 0);
26 assert(input != NULL);
27 assert(output != NULL);
28
29 const v128_t vsign_mask = wasm_u16x8_const_splat(UINT16_C(0x8000));
30 const v128_t vexp_offset = wasm_u16x8_const_splat(UINT16_C(0x7000));
31 const v128_t vexp_scale = wasm_f32x4_const_splat(0x1.0p-112f);
32 const v128_t vmagic_mask = wasm_u16x8_const_splat(UINT16_C(0x3F00));
33 const v128_t vmagic_bias = wasm_f32x4_const_splat(0.5f);
34 const v128_t vdenorm_cutoff = wasm_u16x8_const_splat(UINT16_C(0x0400));
35
36 XNN_FORCE_REALIZATION(vsign_mask);
37 XNN_FORCE_REALIZATION(vexp_offset);
38 XNN_FORCE_REALIZATION(vexp_scale);
39 XNN_FORCE_REALIZATION(vmagic_mask);
40 XNN_FORCE_REALIZATION(vmagic_bias);
41 XNN_FORCE_REALIZATION(vdenorm_cutoff);
42
43 const uint16_t* i = (const uint16_t*) input;
44 for (; batch >= 8 * sizeof(uint16_t); batch -= 8 * sizeof(uint16_t)) {
45 const v128_t vh = wasm_v128_load(i);
46 i += 8;
47
48 const v128_t vsign = wasm_v128_and(vh, vsign_mask);
49
50 const v128_t vnonsign = wasm_v128_xor(vh, vsign);
51
52 const v128_t vprenorm_lo = wasm_i16x8_shl(vnonsign, 13);
53 const v128_t vprenorm_hi = wasm_i16x8_add(wasm_u16x8_shr(vnonsign, 3), vexp_offset);
54
55 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);
56 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);
57
58 const v128_t vdenorm_lo = wasm_f32x4_sub(wasm_v16x8_shuffle(vnonsign, vmagic_mask, 0, 8, 1, 9, 2, 10, 3, 11), vmagic_bias);
59 const v128_t vdenorm_hi = wasm_f32x4_sub(wasm_v16x8_shuffle(vnonsign, vmagic_mask, 4, 12, 5, 13, 6, 14, 7, 15), vmagic_bias);
60
61 const v128_t vmask = wasm_i16x8_gt(vnonsign, vdenorm_cutoff);
62 const v128_t vzero = wasm_i16x8_const_splat(0);
63
64 const v128_t vxmask_lo = wasm_i32x4_extend_low_i16x8(vmask);
65 const v128_t vxmask_hi = wasm_i32x4_extend_high_i16x8(vmask);
66
67 const v128_t vabsf_lo = wasm_i32x4_relaxed_laneselect(vnorm_lo, vdenorm_lo, vxmask_lo);
68 const v128_t vsignf_lo = wasm_v16x8_shuffle(vzero, vsign, 0, 8, 1, 9, 2, 10, 3, 11);
69 const v128_t vabsf_hi = wasm_i32x4_relaxed_laneselect(vnorm_hi, vdenorm_hi, vxmask_hi);
70 const v128_t vsignf_hi = wasm_v16x8_shuffle(vzero, vsign, 4, 12, 5, 13, 6, 14, 7, 15);
71
72 const v128_t vf_lo = wasm_v128_or(vsignf_lo, vabsf_lo);
73 const v128_t vf_hi = wasm_v128_or(vsignf_hi, vabsf_hi);
74
75 wasm_v128_store(output, vf_lo);
76 wasm_v128_store(output + 4, vf_hi);
77 output += 8;
78 }
79 if XNN_UNLIKELY(batch != 0) {
80 assert(batch >= 1 * sizeof(uint16_t));
81 assert(batch <= 7 * sizeof(uint16_t));
82 const v128_t vh = wasm_v128_load(i);
83
84 const v128_t vsign = wasm_v128_and(vh, vsign_mask);
85
86 const v128_t vnonsign = wasm_v128_xor(vh, vsign);
87
88 const v128_t vprenorm_lo = wasm_i16x8_shl(vnonsign, 13);
89 const v128_t vprenorm_hi = wasm_i16x8_add(wasm_u16x8_shr(vnonsign, 3), vexp_offset);
90
91 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);
92 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);
93
94 const v128_t vdenorm_lo = wasm_f32x4_sub(wasm_v16x8_shuffle(vnonsign, vmagic_mask, 0, 8, 1, 9, 2, 10, 3, 11), vmagic_bias);
95 const v128_t vdenorm_hi = wasm_f32x4_sub(wasm_v16x8_shuffle(vnonsign, vmagic_mask, 4, 12, 5, 13, 6, 14, 7, 15), vmagic_bias);
96
97 const v128_t vmask = wasm_i16x8_gt(vnonsign, vdenorm_cutoff);
98 const v128_t vzero = wasm_i16x8_const_splat(0);
99
100 const v128_t vxmask_lo = wasm_i32x4_extend_low_i16x8(vmask);
101 const v128_t vxmask_hi = wasm_i32x4_extend_high_i16x8(vmask);
102
103 const v128_t vabsf_lo = wasm_i32x4_relaxed_laneselect(vnorm_lo, vdenorm_lo, vxmask_lo);
104 const v128_t vsignf_lo = wasm_v16x8_shuffle(vzero, vsign, 0, 8, 1, 9, 2, 10, 3, 11);
105 const v128_t vabsf_hi = wasm_i32x4_relaxed_laneselect(vnorm_hi, vdenorm_hi, vxmask_hi);
106 const v128_t vsignf_hi = wasm_v16x8_shuffle(vzero, vsign, 4, 12, 5, 13, 6, 14, 7, 15);
107
108 v128_t vf = wasm_v128_or(vsignf_lo, vabsf_lo);
109
110 if (batch & (4 * sizeof(uint16_t))) {
111 wasm_v128_store(output, vf);
112 output += 4;
113
114 vf = wasm_v128_or(vsignf_hi, vabsf_hi);
115 }
116 if (batch & (2 * sizeof(uint16_t))) {
117 wasm_v128_store64_lane(output, vf, 0);
118 vf = wasm_v64x2_shuffle(vf, vf, 1, 1);
119 output += 2;
120 }
121 if (batch & (1 * sizeof(uint16_t))) {
122 wasm_v128_store32_lane(output, vf, 0);
123 }
124 }
125 }