• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2021 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$assert OP in ["RNDNE", "RNDZ", "RNDU", "RNDD"]
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
19$WASM_F32X4_RND = {
20$  "RNDNE": "wasm_f32x4_nearest",
21$  "RNDZ": "wasm_f32x4_trunc",
22$  "RNDU": "wasm_f32x4_ceil",
23$  "RNDD": "wasm_f32x4_floor",
24$}[OP]
25void xnn_f32_v${OP.lower()}_ukernel__wasmsimd_x${BATCH_TILE}(
26    size_t n,
27    const float* x,
28    float* y,
29    const union xnn_f32_rnd_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
30{
31  assert(n != 0);
32  assert(n % sizeof(float) == 0);
33
34  for (; n >= ${BATCH_TILE} * sizeof(float); n -= ${BATCH_TILE} * sizeof(float)) {
35    $for N in range(0, BATCH_TILE, 4):
36      const v128_t vx${ABC[N:N+4]} = wasm_v128_load(x); x += 4;
37
38    $for N in range(0, BATCH_TILE, 4):
39      const v128_t vy${ABC[N:N+4]} = ${WASM_F32X4_RND}(vx${ABC[N:N+4]});
40
41    $for N in range(0, BATCH_TILE, 4):
42      wasm_v128_store(y, vy${ABC[N:N+4]}); y += 4;
43  }
44  $if BATCH_TILE > 4:
45    for (; n >= 4 * sizeof(float); n -= 4 * sizeof(float)) {
46      const v128_t vx = wasm_v128_load(x); x += 4;
47      const v128_t vy = ${WASM_F32X4_RND}(vx);
48      wasm_v128_store(y, vy); y += 4;
49    }
50  if XNN_UNLIKELY(n != 0) {
51    const v128_t vx = wasm_v128_load(x);
52    v128_t vy = ${WASM_F32X4_RND}(vx);
53
54    if (n & (2 * sizeof(float))) {
55      *((double*) y) = wasm_f64x2_extract_lane(vy, 0);
56      vy = wasm_v32x4_shuffle(vy, vy, 2, 3, 2, 3);
57      y += 2;
58    }
59    if (n & (1 * sizeof(float))) {
60      *y = wasm_f32x4_extract_lane(vy, 0);
61    }
62  }
63}
64