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/common.h>
14#include <xnnpack/math.h>
15#include <xnnpack/vunary.h>
16
17
18void xnn_f32_vrndz_ukernel__wasmsimd_cvt_x${BATCH_TILE}(
19    size_t n,
20    const float* x,
21    float* y,
22    const union xnn_f32_rnd_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_DISABLE_TSAN
23{
24  assert(n != 0);
25  assert(n % sizeof(float) == 0);
26
27  const v128_t vsign_mask = wasm_f32x4_splat(-0.0f);
28  const v128_t vmagic_number = wasm_f32x4_splat(0x1.000000p+23f);
29  $if BATCH_TILE > 4:
30    for (; n >= ${BATCH_TILE} * sizeof(float); n -= ${BATCH_TILE} * sizeof(float)) {
31      const v128_t vx${ABC[0:4]} = wasm_v128_load(x);
32      $for N in range(4, BATCH_TILE, 4):
33        const v128_t vx${ABC[N:N+4]} = wasm_v128_load(x + ${N});
34      x += ${BATCH_TILE};
35
36      $for N in range(0, BATCH_TILE, 4):
37        const v128_t vintx${ABC[N:N+4]} = wasm_i32x4_trunc_saturate_f32x4(vx${ABC[N:N+4]});
38        const v128_t vabsx${ABC[N:N+4]} = wasm_f32x4_abs(vx${ABC[N:N+4]});
39
40      $for N in range(0, BATCH_TILE, 4):
41        const v128_t vrndx${ABC[N:N+4]} = wasm_f32x4_convert_i32x4(vintx${ABC[N:N+4]});
42        const v128_t vrndmask${ABC[N:N+4]} = wasm_v128_andnot(wasm_f32x4_lt(vabsx${ABC[N:N+4]}, vmagic_number), vsign_mask);
43
44      $for N in range(0, BATCH_TILE, 4):
45        const v128_t vy${ABC[N:N+4]} = wasm_v128_bitselect(vrndx${ABC[N:N+4]}, vx${ABC[N:N+4]}, vrndmask${ABC[N:N+4]});
46
47      wasm_v128_store(y, vy${ABC[0:4]});
48      $for N in range(4, BATCH_TILE, 4):
49        wasm_v128_store(y + ${N}, vy${ABC[N:N+4]});
50      y += ${BATCH_TILE};
51    }
52  for (; n >= 4 * sizeof(float); n -= 4 * sizeof(float)) {
53    const v128_t vx = wasm_v128_load(x);
54    x += 4;
55
56    const v128_t vintx = wasm_i32x4_trunc_saturate_f32x4(vx);
57    const v128_t vabsx = wasm_f32x4_abs(vx);
58    const v128_t vrndx = wasm_f32x4_convert_i32x4(vintx);
59    const v128_t vrndmask = wasm_v128_andnot(wasm_f32x4_lt(vabsx, vmagic_number), vsign_mask);
60    const v128_t vy = wasm_v128_bitselect(vrndx, vx, vrndmask);
61
62    wasm_v128_store(y, vy);
63    y += 4;
64  }
65  if XNN_UNLIKELY(n != 0) {
66    const v128_t vx = wasm_v128_load(x);
67
68    const v128_t vintx = wasm_i32x4_trunc_saturate_f32x4(vx);
69    const v128_t vabsx = wasm_f32x4_abs(vx);
70    const v128_t vrndx = wasm_f32x4_convert_i32x4(vintx);
71    const v128_t vrndmask = wasm_v128_andnot(wasm_f32x4_lt(vabsx, vmagic_number), vsign_mask);
72    v128_t vy = wasm_v128_bitselect(vrndx, vx, vrndmask);
73
74    if (n & (2 * sizeof(float))) {
75      *((double*) y) = wasm_f64x2_extract_lane(vy, 0);
76      vy = wasm_v32x4_shuffle(vy, vy, 2, 3, 2, 3);
77      y += 2;
78    }
79    if (n & (1 * sizeof(float))) {
80      *y = wasm_f32x4_extract_lane(vy, 0);
81    }
82  }
83}
84