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_addsub_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 vabsx${ABC[N:N+4]} = wasm_v128_andnot(vx${ABC[N:N+4]}, vsign_mask); 39 40 $for N in range(0, BATCH_TILE, 4): 41 const v128_t vrndmask${ABC[N:N+4]} = wasm_v128_or(vsign_mask, wasm_f32x4_le(vmagic_number, vabsx${ABC[N:N+4]})); 42 43 $for N in range(0, BATCH_TILE, 4): 44 const v128_t vrndabsx${ABC[N:N+4]} = wasm_f32x4_sub(wasm_f32x4_add(vabsx${ABC[N:N+4]}, vmagic_number), vmagic_number); 45 46 $for N in range(0, BATCH_TILE, 4): 47 const v128_t vrndx${ABC[N:N+4]} = wasm_v128_bitselect(vx${ABC[N:N+4]}, vrndabsx${ABC[N:N+4]}, vrndmask${ABC[N:N+4]}); 48 49 $for N in range(0, BATCH_TILE, 4): 50 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]})); 51 52 $for N in range(0, BATCH_TILE, 4): 53 const v128_t vadjrndx${ABC[N:N+4]} = wasm_f32x4_add(vrndx${ABC[N:N+4]}, vone); 54 55 $for N in range(0, BATCH_TILE, 4): 56 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]}); 57 58 wasm_v128_store(y, vy${ABC[0:4]}); 59 $for N in range(4, BATCH_TILE, 4): 60 wasm_v128_store(y + ${N}, vy${ABC[N:N+4]}); 61 y += ${BATCH_TILE}; 62 } 63 for (; n >= 4 * sizeof(float); n -= 4 * sizeof(float)) { 64 const v128_t vx = wasm_v128_load(x); 65 x += 4; 66 67 const v128_t vabsx = wasm_v128_andnot(vx, vsign_mask); 68 const v128_t vrndmask = wasm_v128_or(vsign_mask, wasm_f32x4_le(vmagic_number, vabsx)); 69 const v128_t vrndabsx = wasm_f32x4_sub(wasm_f32x4_add(vabsx, vmagic_number), vmagic_number); 70 const v128_t vrndx = wasm_v128_bitselect(vx, vrndabsx, 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 vabsx = wasm_v128_andnot(vx, vsign_mask); 82 const v128_t vrndmask = wasm_v128_or(vsign_mask, wasm_f32x4_le(vmagic_number, vabsx)); 83 const v128_t vrndabsx = wasm_f32x4_sub(wasm_f32x4_add(vabsx, vmagic_number), vmagic_number); 84 const v128_t vrndx = wasm_v128_bitselect(vx, vrndabsx, vrndmask); 85 const v128_t vadjmask = wasm_v128_or(vsign_mask, wasm_f32x4_le(vx, vrndx)); 86 const v128_t vadjrndx = wasm_f32x4_add(vrndx, vone); 87 v128_t vy = wasm_v128_bitselect(vrndx, vadjrndx, vadjmask); 88 89 if (n & (2 * sizeof(float))) { 90 *((double*) y) = wasm_f64x2_extract_lane(vy, 0); 91 vy = wasm_v32x4_shuffle(vy, vy, 2, 3, 2, 3); 92 y += 2; 93 } 94 if (n & (1 * sizeof(float))) { 95 *y = wasm_f32x4_extract_lane(vy, 0); 96 } 97 } 98} 99