Marat Dukhan | e6502da | 2020-07-15 16:31:36 -0700 | [diff] [blame] | 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/vunary.h> |
| 15 | |
| 16 | |
| 17 | void xnn_f32_vlrelu_ukernel__wasmsimd_bitselect_x${BATCH_TILE}( |
| 18 | size_t n, |
| 19 | const float* x, |
| 20 | float* y, |
Marat Dukhan | 7be427a | 2021-12-13 23:38:20 -0800 | [diff] [blame^] | 21 | const union xnn_f32_lrelu_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS |
Marat Dukhan | e6502da | 2020-07-15 16:31:36 -0700 | [diff] [blame] | 22 | { |
| 23 | assert(n != 0); |
| 24 | assert(n % sizeof(float) == 0); |
| 25 | |
Marat Dukhan | ee029b2 | 2021-06-30 12:47:02 -0700 | [diff] [blame] | 26 | const v128_t vslope = wasm_v128_load32_splat(¶ms->scalar.slope); |
Marat Dukhan | e6502da | 2020-07-15 16:31:36 -0700 | [diff] [blame] | 27 | $if BATCH_TILE > 4: |
| 28 | for (; n >= ${BATCH_TILE} * sizeof(float); n -= ${BATCH_TILE} * sizeof(float)) { |
| 29 | const v128_t vx${ABC[0:4]} = wasm_v128_load(x); |
| 30 | $for N in range(4, BATCH_TILE, 4): |
| 31 | const v128_t vx${ABC[N:N+4]} = wasm_v128_load(x + ${N}); |
| 32 | x += ${BATCH_TILE}; |
| 33 | |
| 34 | $for N in range(0, BATCH_TILE, 4): |
| 35 | v128_t vacc${ABC[N:N+4]} = wasm_f32x4_mul(vx${ABC[N:N+4]}, vslope); |
Marat Dukhan | d4db6af | 2021-08-05 14:42:26 -0700 | [diff] [blame] | 36 | const v128_t vmask${ABC[N:N+4]} = wasm_i32x4_shr(vx${ABC[N:N+4]}, 31); |
Marat Dukhan | e6502da | 2020-07-15 16:31:36 -0700 | [diff] [blame] | 37 | |
| 38 | $for N in range(0, BATCH_TILE, 4): |
| 39 | vacc${ABC[N:N+4]} = wasm_v128_bitselect(vacc${ABC[N:N+4]}, vx${ABC[N:N+4]}, vmask${ABC[N:N+4]}); |
| 40 | |
| 41 | wasm_v128_store(y, vacc${ABC[0:4]}); |
| 42 | $for N in range(4, BATCH_TILE, 4): |
| 43 | wasm_v128_store(y + ${N}, vacc${ABC[N:N+4]}); |
| 44 | y += ${BATCH_TILE}; |
| 45 | } |
| 46 | for (; n >= 4 * sizeof(float); n -= 4 * sizeof(float)) { |
| 47 | const v128_t vx = wasm_v128_load(x); |
| 48 | x += 4; |
| 49 | v128_t vacc = wasm_f32x4_mul(vx, vslope); |
Marat Dukhan | d4db6af | 2021-08-05 14:42:26 -0700 | [diff] [blame] | 50 | const v128_t vmask = wasm_i32x4_shr(vx, 31); |
Marat Dukhan | e6502da | 2020-07-15 16:31:36 -0700 | [diff] [blame] | 51 | vacc = wasm_v128_bitselect(vacc, vx, vmask); |
| 52 | wasm_v128_store(y, vacc); |
| 53 | y += 4; |
| 54 | } |
| 55 | if XNN_UNLIKELY(n != 0) { |
| 56 | const v128_t vx = wasm_v128_load(x); |
| 57 | v128_t vacc = wasm_f32x4_mul(vx, vslope); |
Marat Dukhan | d4db6af | 2021-08-05 14:42:26 -0700 | [diff] [blame] | 58 | const v128_t vmask = wasm_i32x4_shr(vx, 31); |
Marat Dukhan | e6502da | 2020-07-15 16:31:36 -0700 | [diff] [blame] | 59 | vacc = wasm_v128_bitselect(vacc, vx, vmask); |
| 60 | |
| 61 | if (n & (2 * sizeof(float))) { |
| 62 | *((double*) y) = wasm_f64x2_extract_lane(vacc, 0); |
| 63 | vacc = wasm_v32x4_shuffle(vacc, vacc, 2, 3, 2, 3); |
| 64 | y += 2; |
| 65 | } |
| 66 | if (n & (1 * sizeof(float))) { |
| 67 | *y = wasm_f32x4_extract_lane(vacc, 0); |
| 68 | } |
| 69 | } |
| 70 | } |