1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-vrnd/vrndu-wasmsimd-addsub.c.in
3 //   Generator: tools/xngen
4 //
5 // Copyright 2020 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/math.h>
16 #include <xnnpack/vunary.h>
17 
18 
xnn_f32_vrndu_ukernel__wasmsimd_addsub_x8(size_t n,const float * x,float * y,const union xnn_f32_rnd_params params[restrict XNN_MIN_ELEMENTS (1)])19 void xnn_f32_vrndu_ukernel__wasmsimd_addsub_x8(
20     size_t n,
21     const float* x,
22     float* y,
23     const union xnn_f32_rnd_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_DISABLE_TSAN
24 {
25   assert(n != 0);
26   assert(n % sizeof(float) == 0);
27 
28   const v128_t vsign_mask = wasm_f32x4_splat(-0.0f);
29   const v128_t vmagic_number = wasm_f32x4_splat(0x1.000000p+23f);
30   const v128_t vone = wasm_f32x4_splat(1.0f);
31   for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
32     const v128_t vx0123 = wasm_v128_load(x);
33     const v128_t vx4567 = wasm_v128_load(x + 4);
34     x += 8;
35 
36     const v128_t vabsx0123 = wasm_v128_andnot(vx0123, vsign_mask);
37     const v128_t vabsx4567 = wasm_v128_andnot(vx4567, vsign_mask);
38 
39     const v128_t vrndmask0123 = wasm_v128_or(vsign_mask, wasm_f32x4_le(vmagic_number, vabsx0123));
40     const v128_t vrndmask4567 = wasm_v128_or(vsign_mask, wasm_f32x4_le(vmagic_number, vabsx4567));
41 
42     const v128_t vrndabsx0123 = wasm_f32x4_sub(wasm_f32x4_add(vabsx0123, vmagic_number), vmagic_number);
43     const v128_t vrndabsx4567 = wasm_f32x4_sub(wasm_f32x4_add(vabsx4567, vmagic_number), vmagic_number);
44 
45     const v128_t vrndx0123 = wasm_v128_bitselect(vx0123, vrndabsx0123, vrndmask0123);
46     const v128_t vrndx4567 = wasm_v128_bitselect(vx4567, vrndabsx4567, vrndmask4567);
47 
48     const v128_t vadjmask0123 = wasm_v128_or(vsign_mask, wasm_f32x4_le(vx0123, vrndx0123));
49     const v128_t vadjmask4567 = wasm_v128_or(vsign_mask, wasm_f32x4_le(vx4567, vrndx4567));
50 
51     const v128_t vadjrndx0123 = wasm_f32x4_add(vrndx0123, vone);
52     const v128_t vadjrndx4567 = wasm_f32x4_add(vrndx4567, vone);
53 
54     const v128_t vy0123 = wasm_v128_bitselect(vrndx0123, vadjrndx0123, vadjmask0123);
55     const v128_t vy4567 = wasm_v128_bitselect(vrndx4567, vadjrndx4567, vadjmask4567);
56 
57     wasm_v128_store(y, vy0123);
58     wasm_v128_store(y + 4, vy4567);
59     y += 8;
60   }
61   for (; n >= 4 * sizeof(float); n -= 4 * sizeof(float)) {
62     const v128_t vx = wasm_v128_load(x);
63     x += 4;
64 
65     const v128_t vabsx = wasm_v128_andnot(vx, vsign_mask);
66     const v128_t vrndmask = wasm_v128_or(vsign_mask, wasm_f32x4_le(vmagic_number, vabsx));
67     const v128_t vrndabsx = wasm_f32x4_sub(wasm_f32x4_add(vabsx, vmagic_number), vmagic_number);
68     const v128_t vrndx = wasm_v128_bitselect(vx, vrndabsx, vrndmask);
69     const v128_t vadjmask = wasm_v128_or(vsign_mask, wasm_f32x4_le(vx, vrndx));
70     const v128_t vadjrndx = wasm_f32x4_add(vrndx, vone);
71     const v128_t vy = wasm_v128_bitselect(vrndx, vadjrndx, vadjmask);
72 
73     wasm_v128_store(y, vy);
74     y += 4;
75   }
76   if XNN_UNLIKELY(n != 0) {
77     const v128_t vx = wasm_v128_load(x);
78 
79     const v128_t vabsx = wasm_v128_andnot(vx, vsign_mask);
80     const v128_t vrndmask = wasm_v128_or(vsign_mask, wasm_f32x4_le(vmagic_number, vabsx));
81     const v128_t vrndabsx = wasm_f32x4_sub(wasm_f32x4_add(vabsx, vmagic_number), vmagic_number);
82     const v128_t vrndx = wasm_v128_bitselect(vx, vrndabsx, vrndmask);
83     const v128_t vadjmask = wasm_v128_or(vsign_mask, wasm_f32x4_le(vx, vrndx));
84     const v128_t vadjrndx = wasm_f32x4_add(vrndx, vone);
85     v128_t vy = wasm_v128_bitselect(vrndx, vadjrndx, vadjmask);
86 
87     if (n & (2 * sizeof(float))) {
88       *((double*) y) = wasm_f64x2_extract_lane(vy, 0);
89       vy = wasm_v32x4_shuffle(vy, vy, 2, 3, 2, 3);
90       y += 2;
91     }
92     if (n & (1 * sizeof(float))) {
93       *y = wasm_f32x4_extract_lane(vy, 0);
94     }
95   }
96 }
97