xref: /aosp_15_r20/external/XNNPACK/src/f32-rmax/wasmsimd-x86.c (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1 // Copyright 2020 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 
8 #include <wasm_simd128.h>
9 
10 #include <xnnpack/math.h>
11 #include <xnnpack/rmax.h>
12 
13 
xnn_f32_rmax_ukernel__wasmsimd_x86(size_t n,const float * x,float * y)14 void xnn_f32_rmax_ukernel__wasmsimd_x86(
15     size_t n,
16     const float* x,
17     float* y)
18 {
19   assert(n != 0);
20   assert(n % sizeof(float) == 0);
21 
22   v128_t vmax0 = wasm_v128_load32_splat(x);
23   v128_t vmax1 = vmax0;
24   v128_t vmax2 = vmax0;
25   v128_t vmax3 = vmax0;
26   for (; n >= 16 * sizeof(float); n -= 16 * sizeof(float)) {
27     const v128_t vx0 = wasm_v128_load(x);
28     const v128_t vx1 = wasm_v128_load(x + 4);
29     const v128_t vx2 = wasm_v128_load(x + 8);
30     const v128_t vx3 = wasm_v128_load(x + 12);
31     x += 16;
32 
33     vmax0 = wasm_f32x4_pmax(vx0, vmax0);
34     vmax1 = wasm_f32x4_pmax(vx1, vmax1);
35     vmax2 = wasm_f32x4_pmax(vx2, vmax2);
36     vmax3 = wasm_f32x4_pmax(vx3, vmax3);
37   }
38   const v128_t vmax01 = wasm_f32x4_pmax(vmax1, vmax0);
39   const v128_t vmax23 = wasm_f32x4_pmax(vmax3, vmax2);
40   v128_t vmax0123 = wasm_f32x4_pmax(vmax23, vmax01);
41   for (; n >= 4 * sizeof(float); n -= 4 * sizeof(float)) {
42     const v128_t vx = wasm_v128_load(x);
43     vmax0123 = wasm_f32x4_pmax(vx, vmax0123);
44     x += 4;
45   }
46   const v128_t vmax2301 = wasm_v32x4_shuffle(vmax0123, vmax0123, 2, 3, 0, 1);
47   vmax0123 = wasm_f32x4_pmax(vmax2301, vmax0123);
48   float vmax = math_max_f32(wasm_f32x4_extract_lane(vmax0123, 0), wasm_f32x4_extract_lane(vmax0123, 1));
49   if XNN_UNLIKELY(n != 0) {
50     do {
51       const float vx = *x++;
52       vmax = math_max_f32(vx, vmax);
53       n -= 4;
54     } while (n != 0);
55   }
56   *y = vmax;
57 }
58