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$assert BATCH_TILE % 4 == 0
7$assert BATCH_TILE >= 4
8$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
9#include <assert.h>
10
11#include <wasm_simd128.h>
12
13#include <xnnpack/clamp.h>
14#include <xnnpack/common.h>
15
16
17void xnn_f32_clamp_ukernel__wasmsimd_${"x86" if X86 else "arm"}_x${BATCH_TILE}(
18    size_t n,
19    const float* x,
20    float* y,
21    const union xnn_f32_minmax_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_DISABLE_TSAN
22{
23  assert(n != 0);
24  assert(n % sizeof(float) == 0);
25  assert(x != NULL);
26  assert(y != NULL);
27
28  const v128_t vy_min = wasm_v32x4_load_splat(&params->scalar.min);
29  const v128_t vy_max = wasm_v32x4_load_splat(&params->scalar.max);
30
31  $if BATCH_TILE > 4:
32    for (; n >= ${BATCH_TILE} * sizeof(float); n -= ${BATCH_TILE} * sizeof(float)) {
33      v128_t vacc${ABC[0:4]} = wasm_v128_load(x);
34      $for N in range(4, BATCH_TILE, 4):
35        v128_t vacc${ABC[N:N+4]} = wasm_v128_load(x + ${N});
36      x += ${BATCH_TILE};
37
38      $if X86:
39        $for N in range(0, BATCH_TILE, 4):
40          const v128_t vmaskmin${ABC[N:N+4]} = wasm_f32x4_lt(vacc${ABC[N:N+4]}, vy_min);
41
42        $for N in range(0, BATCH_TILE, 4):
43          const v128_t vmaskmax${ABC[N:N+4]} = wasm_f32x4_le(vy_max, vacc${ABC[N:N+4]});
44          vacc${ABC[N:N+4]} = wasm_v128_bitselect(vy_min, vacc${ABC[N:N+4]}, vmaskmin${ABC[N:N+4]});
45
46        $for N in range(0, BATCH_TILE, 4):
47          vacc${ABC[N:N+4]} = wasm_v128_bitselect(vy_max, vacc${ABC[N:N+4]}, vmaskmax${ABC[N:N+4]});
48      $else:
49        $for N in range(0, BATCH_TILE, 4):
50          vacc${ABC[N:N+4]} = wasm_f32x4_max(vacc${ABC[N:N+4]}, vy_min);
51
52        $for N in range(0, BATCH_TILE, 4):
53          vacc${ABC[N:N+4]} = wasm_f32x4_min(vacc${ABC[N:N+4]}, vy_max);
54
55      wasm_v128_store(y, vacc${ABC[0:4]});
56      $for N in range(4, BATCH_TILE, 4):
57        wasm_v128_store(y + ${N}, vacc${ABC[N:N+4]});
58      y += ${BATCH_TILE};
59    }
60  for (; n >= 4 * sizeof(float); n -= 4 * sizeof(float)) {
61    v128_t vacc = wasm_v128_load(x);
62    x += 4;
63
64    $if X86:
65      const v128_t vmaskmin = wasm_f32x4_lt(vacc, vy_min);
66      const v128_t vmaskmax = wasm_f32x4_le(vy_max, vacc);
67      vacc = wasm_v128_bitselect(vy_min, vacc, vmaskmin);
68      vacc = wasm_v128_bitselect(vy_max, vacc, vmaskmax);
69    $else:
70      vacc = wasm_f32x4_max(vacc, vy_min);
71      vacc = wasm_f32x4_min(vacc, vy_max);
72
73    wasm_v128_store(y, vacc);
74    y += 4;
75  }
76  if XNN_UNLIKELY(n != 0) {
77    v128_t vacc = wasm_v128_load(x);
78
79    $if X86:
80      const v128_t vmaskmin = wasm_f32x4_lt(vacc, vy_min);
81      const v128_t vmaskmax = wasm_f32x4_le(vy_max, vacc);
82      vacc = wasm_v128_bitselect(vy_min, vacc, vmaskmin);
83      vacc = wasm_v128_bitselect(vy_max, vacc, vmaskmax);
84    $else:
85      vacc = wasm_f32x4_max(vacc, vy_min);
86      vacc = wasm_f32x4_min(vacc, vy_max);
87
88    if (n & (2 * sizeof(float))) {
89      *((double*) y) = wasm_f64x2_extract_lane(vacc, 0);
90      vacc = wasm_v32x4_shuffle(vacc, vacc, 2, 3, 2, 3);
91      y += 2;
92    }
93    if (n & (1 * sizeof(float))) {
94      *y = wasm_f32x4_extract_lane(vacc, 0);
95    }
96  }
97}
98