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_vrndu_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 const v128_t vone = wasm_f32x4_splat(1.0f); 30 $if BATCH_TILE > 4: 31 for (; n >= ${BATCH_TILE} * sizeof(float); n -= ${BATCH_TILE} * sizeof(float)) { 32 const v128_t vx${ABC[0:4]} = wasm_v128_load(x); 33 $for N in range(4, BATCH_TILE, 4): 34 const v128_t vx${ABC[N:N+4]} = wasm_v128_load(x + ${N}); 35 x += ${BATCH_TILE}; 36 37 $for N in range(0, BATCH_TILE, 4): 38 const v128_t vintx${ABC[N:N+4]} = wasm_i32x4_trunc_saturate_f32x4(vx${ABC[N:N+4]}); 39 const v128_t vabsx${ABC[N:N+4]} = wasm_f32x4_abs(vx${ABC[N:N+4]}); 40 41 $for N in range(0, BATCH_TILE, 4): 42 const v128_t vprerndx${ABC[N:N+4]} = wasm_f32x4_convert_i32x4(vintx${ABC[N:N+4]}); 43 const v128_t vrndmask${ABC[N:N+4]} = wasm_v128_andnot(wasm_f32x4_lt(vabsx${ABC[N:N+4]}, vmagic_number), vsign_mask); 44 45 $for N in range(0, BATCH_TILE, 4): 46 const v128_t vrndx${ABC[N:N+4]} = wasm_v128_bitselect(vprerndx${ABC[N:N+4]}, vx${ABC[N:N+4]}, vrndmask${ABC[N:N+4]}); 47 48 $for N in range(0, BATCH_TILE, 4): 49 const v128_t vadjmask${ABC[N:N+4]} = wasm_v128_or(vsign_mask, wasm_f32x4_le(vx${ABC[N:N+4]}, vrndx${ABC[N:N+4]})); 50 51 $for N in range(0, BATCH_TILE, 4): 52 const v128_t vadjrndx${ABC[N:N+4]} = wasm_f32x4_add(vrndx${ABC[N:N+4]}, vone); 53 54 $for N in range(0, BATCH_TILE, 4): 55 const v128_t vy${ABC[N:N+4]} = wasm_v128_bitselect(vrndx${ABC[N:N+4]}, vadjrndx${ABC[N:N+4]}, vadjmask${ABC[N:N+4]}); 56 57 wasm_v128_store(y, vy${ABC[0:4]}); 58 $for N in range(4, BATCH_TILE, 4): 59 wasm_v128_store(y + ${N}, vy${ABC[N:N+4]}); 60 y += ${BATCH_TILE}; 61 } 62 for (; n >= 4 * sizeof(float); n -= 4 * sizeof(float)) { 63 const v128_t vx = wasm_v128_load(x); 64 x += 4; 65 66 const v128_t vintx = wasm_i32x4_trunc_saturate_f32x4(vx); 67 const v128_t vabsx = wasm_f32x4_abs(vx); 68 const v128_t vprerndx = wasm_f32x4_convert_i32x4(vintx); 69 const v128_t vrndmask = wasm_v128_andnot(wasm_f32x4_lt(vabsx, vmagic_number), vsign_mask); 70 const v128_t vrndx = wasm_v128_bitselect(vprerndx, vx, vrndmask); 71 const v128_t vadjmask = wasm_v128_or(vsign_mask, wasm_f32x4_le(vx, vrndx)); 72 const v128_t vadjrndx = wasm_f32x4_add(vrndx, vone); 73 const v128_t vy = wasm_v128_bitselect(vrndx, vadjrndx, vadjmask); 74 75 wasm_v128_store(y, vy); 76 y += 4; 77 } 78 if XNN_UNLIKELY(n != 0) { 79 const v128_t vx = wasm_v128_load(x); 80 81 const v128_t vintx = wasm_i32x4_trunc_saturate_f32x4(vx); 82 const v128_t vabsx = wasm_f32x4_abs(vx); 83 const v128_t vprerndx = wasm_f32x4_convert_i32x4(vintx); 84 const v128_t vrndmask = wasm_v128_andnot(wasm_f32x4_lt(vabsx, vmagic_number), vsign_mask); 85 const v128_t vrndx = wasm_v128_bitselect(vprerndx, vx, vrndmask); 86 const v128_t vadjmask = wasm_v128_or(vsign_mask, wasm_f32x4_le(vx, vrndx)); 87 const v128_t vadjrndx = wasm_f32x4_add(vrndx, vone); 88 v128_t vy = wasm_v128_bitselect(vrndx, vadjrndx, vadjmask); 89 90 if (n & (2 * sizeof(float))) { 91 *((double*) y) = wasm_f64x2_extract_lane(vy, 0); 92 vy = wasm_v32x4_shuffle(vy, vy, 2, 3, 2, 3); 93 y += 2; 94 } 95 if (n & (1 * sizeof(float))) { 96 *y = wasm_f32x4_extract_lane(vy, 0); 97 } 98 } 99} 100