• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 >= 1
7$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
8#include <assert.h>
9
10#include <xnnpack/common.h>
11#include <xnnpack/math.h>
12#include <xnnpack/vunary.h>
13
14
15$MIN_F32 = "__builtin_wasm_min_f32" if WASM else "math_min_f32"
16$MAX_F32 = "__builtin_wasm_max_f32" if WASM else "math_max_f32"
17void xnn_f32_vclamp_ukernel__${"wasm" if WASM else "scalar"}_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)])
22{
23  assert(n != 0);
24  assert(n % sizeof(float) == 0);
25  assert(x != NULL);
26  assert(y != NULL);
27
28  const float vy_min = params->scalar.min;
29  const float vy_max = params->scalar.max;
30
31  $if BATCH_TILE > 1:
32    for (; n >= ${BATCH_TILE} * sizeof(float); n -= ${BATCH_TILE} * sizeof(float)) {
33      $for N in range(BATCH_TILE):
34        float vacc${ABC[N]} = x[${N}];
35      x += ${BATCH_TILE};
36
37      $for N in range(BATCH_TILE):
38        vacc${ABC[N]} = ${MAX_F32}(vacc${ABC[N]}, vy_min);
39
40      $for N in range(BATCH_TILE):
41        vacc${ABC[N]} = ${MIN_F32}(vacc${ABC[N]}, vy_max);
42
43      $for N in range(BATCH_TILE):
44        y[${N}] = vacc${ABC[N]};
45      y += ${BATCH_TILE};
46    }
47    if XNN_UNLIKELY(n != 0) {
48      $if BATCH_TILE > 2:
49        do {
50          float vacc = *x++;
51          vacc = ${MAX_F32}(vacc, vy_min);
52          vacc = ${MIN_F32}(vacc, vy_max);
53          *y++ = vacc;
54          n -= sizeof(float);
55        } while (n != 0);
56      $else:
57        float vacc = *x;
58        vacc = ${MAX_F32}(vacc, vy_min);
59        vacc = ${MIN_F32}(vacc, vy_max);
60        *y = vacc;
61    }
62  $else:
63    for (; n >= sizeof(float); n -= sizeof(float)) {
64      float vacc = *x++;
65      vacc = ${MAX_F32}(vacc, vy_min);
66      vacc = ${MIN_F32}(vacc, vy_max);
67      *y++ = vacc;
68    }
69}
70