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_vrndne_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 $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 vabsx${ABC[N:N+4]} = wasm_v128_andnot(vx${ABC[N:N+4]}, vsign_mask); 38 39 $for N in range(0, BATCH_TILE, 4): 40 const v128_t vrndmask${ABC[N:N+4]} = wasm_v128_or(vsign_mask, wasm_f32x4_le(vmagic_number, vabsx${ABC[N:N+4]})); 41 42 $for N in range(0, BATCH_TILE, 4): 43 const v128_t vrndabsx${ABC[N:N+4]} = wasm_f32x4_sub(wasm_f32x4_add(vabsx${ABC[N:N+4]}, vmagic_number), vmagic_number); 44 45 $for N in range(0, BATCH_TILE, 4): 46 const v128_t vy${ABC[N:N+4]} = wasm_v128_bitselect(vx${ABC[N:N+4]}, vrndabsx${ABC[N:N+4]}, vrndmask${ABC[N:N+4]}); 47 48 wasm_v128_store(y, vy${ABC[0:4]}); 49 $for N in range(4, BATCH_TILE, 4): 50 wasm_v128_store(y + ${N}, vy${ABC[N:N+4]}); 51 y += ${BATCH_TILE}; 52 } 53 for (; n >= 4 * sizeof(float); n -= 4 * sizeof(float)) { 54 const v128_t vx = wasm_v128_load(x); 55 x += 4; 56 57 const v128_t vabsx = wasm_v128_andnot(vx, vsign_mask); 58 const v128_t vrndmask = wasm_v128_or(vsign_mask, wasm_f32x4_le(vmagic_number, vabsx)); 59 const v128_t vrndabsx = wasm_f32x4_sub(wasm_f32x4_add(vabsx, vmagic_number), vmagic_number); 60 const v128_t vy = wasm_v128_bitselect(vx, vrndabsx, 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 vabsx = wasm_v128_andnot(vx, vsign_mask); 69 const v128_t vrndmask = wasm_v128_or(vsign_mask, wasm_f32x4_le(vmagic_number, vabsx)); 70 const v128_t vrndabsx = wasm_f32x4_sub(wasm_f32x4_add(vabsx, vmagic_number), vmagic_number); 71 v128_t vy = wasm_v128_bitselect(vx, vrndabsx, vrndmask); 72 73 if (n & (2 * sizeof(float))) { 74 *((double*) y) = wasm_f64x2_extract_lane(vy, 0); 75 vy = wasm_v32x4_shuffle(vy, vy, 2, 3, 2, 3); 76 y += 2; 77 } 78 if (n & (1 * sizeof(float))) { 79 *y = wasm_f32x4_extract_lane(vy, 0); 80 } 81 } 82} 83