Debian Codegraph @

src/f16-f32-vcvt/gen/f16-f32-vcvt-wasmsimd-int32-u8.c

raw
1 // Auto-generated file. Do not edit!
2 // Template: src/f16-f32-vcvt/wasmsimd-int32.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__wasmsimd_int32_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_i32x4_const_splat(UINT32_C(0x80000000));
30 const v128_t vexp_offset = wasm_i32x4_const_splat(UINT32_C(0x70000000));
31 const v128_t vexp_scale = wasm_f32x4_const_splat(0x1.0p-112f);
32 const v128_t vmagic_bias = wasm_f32x4_const_splat(0.5f);
33 const v128_t vdenorm_cutoff = wasm_i32x4_const_splat(UINT32_C(0x08000000));
34
35 XNN_FORCE_REALIZATION(vsign_mask);
36 XNN_FORCE_REALIZATION(vexp_offset);
37 XNN_FORCE_REALIZATION(vexp_scale);
38 XNN_FORCE_REALIZATION(vmagic_bias);
39 XNN_FORCE_REALIZATION(vdenorm_cutoff);
40
41 const uint16_t* i = (const uint16_t*) input;
42 for (; batch >= 8 * sizeof(uint16_t); batch -= 8 * sizeof(uint16_t)) {
43 const v128_t vh = wasm_v128_load(i);
44 i += 8;
45
46 const v128_t vzero = wasm_i16x8_const_splat(0);
47 const v128_t vw_lo = wasm_v16x8_shuffle(vzero, vh, 0, 8, 1, 9, 2, 10, 3, 11);
48 const v128_t vw_hi = wasm_v16x8_shuffle(vzero, vh, 4, 12, 5, 13, 6, 14, 7, 15);
49
50 const v128_t vsign_lo = wasm_v128_and(vw_lo, vsign_mask);
51 const v128_t vsign_hi = wasm_v128_and(vw_hi, vsign_mask);
52
53 const v128_t vnonsign_lo = wasm_v128_xor(vw_lo, vsign_lo);
54 const v128_t vnonsign_hi = wasm_v128_xor(vw_hi, vsign_hi);
55
56 const v128_t vnorm_lo = wasm_f32x4_mul(wasm_i32x4_add(wasm_u32x4_shr(vnonsign_lo, 3), vexp_offset), vexp_scale);
57 const v128_t vnorm_hi = wasm_f32x4_mul(wasm_i32x4_add(wasm_u32x4_shr(vnonsign_hi, 3), vexp_offset), vexp_scale);
58
59 const v128_t vdenorm_lo = wasm_f32x4_sub(wasm_v128_or(wasm_u32x4_shr(vnonsign_lo, 16), vmagic_bias), vmagic_bias);
60 const v128_t vdenorm_hi = wasm_f32x4_sub(wasm_v128_or(wasm_u32x4_shr(vnonsign_hi, 16), vmagic_bias), vmagic_bias);
61
62 const v128_t vxmask_lo = wasm_i32x4_gt(vnonsign_lo, vdenorm_cutoff);
63 const v128_t vxmask_hi = wasm_i32x4_gt(vnonsign_hi, vdenorm_cutoff);
64
65 const v128_t vf_lo = wasm_v128_or(vsign_lo, wasm_v128_bitselect(vnorm_lo, vdenorm_lo, vxmask_lo));
66 const v128_t vf_hi = wasm_v128_or(vsign_hi, wasm_v128_bitselect(vnorm_hi, vdenorm_hi, vxmask_hi));
67
68 wasm_v128_store(output, vf_lo);
69 wasm_v128_store(output + 4, vf_hi);
70 output += 8;
71 }
72 if XNN_UNLIKELY(batch != 0) {
73 assert(batch >= 1 * sizeof(uint16_t));
74 assert(batch <= 7 * sizeof(uint16_t));
75 const v128_t vh = wasm_v128_load(i);
76
77 const v128_t vzero = wasm_i16x8_const_splat(0);
78 const v128_t vw_lo = wasm_v16x8_shuffle(vzero, vh, 0, 8, 1, 9, 2, 10, 3, 11);
79 const v128_t vw_hi = wasm_v16x8_shuffle(vzero, vh, 4, 12, 5, 13, 6, 14, 7, 15);
80
81 const v128_t vsign_lo = wasm_v128_and(vw_lo, vsign_mask);
82 const v128_t vsign_hi = wasm_v128_and(vw_hi, vsign_mask);
83
84 const v128_t vnonsign_lo = wasm_v128_xor(vw_lo, vsign_lo);
85 const v128_t vnonsign_hi = wasm_v128_xor(vw_hi, vsign_hi);
86
87 const v128_t vnorm_lo = wasm_f32x4_mul(wasm_i32x4_add(wasm_u32x4_shr(vnonsign_lo, 3), vexp_offset), vexp_scale);
88 const v128_t vnorm_hi = wasm_f32x4_mul(wasm_i32x4_add(wasm_u32x4_shr(vnonsign_hi, 3), vexp_offset), vexp_scale);
89
90 const v128_t vdenorm_lo = wasm_f32x4_sub(wasm_v128_or(wasm_u32x4_shr(vnonsign_lo, 16), vmagic_bias), vmagic_bias);
91 const v128_t vdenorm_hi = wasm_f32x4_sub(wasm_v128_or(wasm_u32x4_shr(vnonsign_hi, 16), vmagic_bias), vmagic_bias);
92
93 const v128_t vxmask_lo = wasm_i32x4_gt(vnonsign_lo, vdenorm_cutoff);
94 v128_t vf = wasm_v128_or(vsign_lo, wasm_v128_bitselect(vnorm_lo, vdenorm_lo, vxmask_lo));
95
96 if (batch & (4 * sizeof(uint16_t))) {
97 wasm_v128_store(output, vf);
98 output += 4;
99
100 const v128_t vxmask_hi = wasm_i32x4_gt(vnonsign_hi, vdenorm_cutoff);
101 vf = wasm_v128_or(vsign_hi, wasm_v128_bitselect(vnorm_hi, vdenorm_hi, vxmask_hi));
102 }
103 if (batch & (2 * sizeof(uint16_t))) {
104 wasm_v128_store64_lane(output, vf, 0);
105 vf = wasm_v64x2_shuffle(vf, vf, 1, 1);
106 output += 2;
107 }
108 if (batch & (1 * sizeof(uint16_t))) {
109 wasm_v128_store32_lane(output, vf, 0);
110 }
111 }
112 }