| // Copyright 2022 Google LLC |
| // |
| // This source code is licensed under the BSD-style license found in the |
| // LICENSE file in the root directory of this source tree. |
| |
| $assert BATCH_TILE >= 8 |
| $assert BATCH_TILE == 8 or BATCH_TILE % 16 == 0 |
| $SIMD_TILE = BATCH_TILE // 16 |
| $ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| #include <assert.h> |
| |
| #include <wasm_simd128.h> |
| |
| #include <xnnpack/common.h> |
| #include <xnnpack/vcvt.h> |
| |
| |
| $XINT8_T = {"QS8": "int8_t", "QU8": "uint8_t"}[DATATYPE] |
| $WASM_X16X8_LOAD8X8 = {"QS8": "wasm_i16x8_load8x8", "QU8": "wasm_u16x8_load8x8"}[DATATYPE] |
| $WASM_I16X8_Q15MULR = "__builtin_wasm_relaxed_q15mulr_s_i16x8" if RELAXED else "wasm_i16x8_q15mulr_sat" |
| $WASM_X8X16_NARROW_I16X8 = {"QS8": "wasm_i8x16_narrow_i16x8", "QU8": "wasm_u8x16_narrow_i16x8"}[DATATYPE] |
| $ISA = "wasmrelaxedsimd" if RELAXED else "wasmsimd" |
| void xnn_${DATATYPE.lower()}_vlrelu_ukernel__${ISA}_x86_x${BATCH_TILE}( |
| size_t n, |
| const ${XINT8_T}* x, |
| ${XINT8_T}* y, |
| const union xnn_${DATATYPE.lower()}_lrelu_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS |
| { |
| assert(n != 0); |
| assert(n % sizeof(${XINT8_T}) == 0); |
| assert(x != NULL); |
| assert(y != NULL); |
| |
| const v128_t vinput_zero_point = wasm_v128_load64_splat(params->wasmsimd_x86.input_zero_point); |
| const v128_t vmultiplier_diff = wasm_v128_load64_splat(params->wasmsimd_x86.multiplier_diff); |
| const v128_t vmultiplier_base = wasm_v128_load64_splat(params->wasmsimd_x86.multiplier_base); |
| const v128_t voutput_zero_point = wasm_v128_load64_splat(params->wasmsimd_x86.output_zero_point); |
| $if BATCH_TILE > 8: |
| for (; n >= ${BATCH_TILE} * sizeof(${XINT8_T}); n -= ${BATCH_TILE} * sizeof(${XINT8_T})) { |
| v128_t vacc${ABC[0]} = ${WASM_X16X8_LOAD8X8}(x); |
| $for N in range(1, 2*SIMD_TILE): |
| v128_t vacc${ABC[N]} = ${WASM_X16X8_LOAD8X8}(x + ${N * 8}); |
| x += ${BATCH_TILE}; |
| |
| $for N in range(2*SIMD_TILE): |
| v128_t vmultiplier${ABC[N]} = wasm_i16x8_gt(vacc${ABC[N]}, vinput_zero_point); |
| vacc${ABC[N]} = wasm_i16x8_sub(vinput_zero_point, vacc${ABC[N]}); |
| |
| $for N in range(2*SIMD_TILE): |
| vmultiplier${ABC[N]} = wasm_v128_and(vmultiplier${ABC[N]}, vmultiplier_diff); |
| vacc${ABC[N]} = wasm_i16x8_shl(vacc${ABC[N]}, 7); |
| vmultiplier${ABC[N]} = wasm_v128_xor(vmultiplier${ABC[N]}, vmultiplier_base); |
| |
| $for N in range(2*SIMD_TILE): |
| vacc${ABC[N]} = ${WASM_I16X8_Q15MULR}(vacc${ABC[N]}, vmultiplier${ABC[N]}); |
| |
| $for N in range(2*SIMD_TILE): |
| vacc${ABC[N]} = wasm_i16x8_add_sat(vacc${ABC[N]}, voutput_zero_point); |
| |
| $for N in range(SIMD_TILE): |
| const v128_t vy${ABC[N]} = ${WASM_X8X16_NARROW_I16X8}(vacc${ABC[2*N]}, vacc${ABC[2*N+1]}); |
| |
| wasm_v128_store(y, vy${ABC[0]}); |
| $for N in range(1, SIMD_TILE): |
| wasm_v128_store((y + ${N * 16}), vy${ABC[N]}); |
| y += ${BATCH_TILE}; |
| } |
| for (; n >= 8 * sizeof(${XINT8_T}); n -= 8 * sizeof(${XINT8_T})) { |
| v128_t vacc = ${WASM_X16X8_LOAD8X8}(x); |
| v128_t vmultiplier = wasm_i16x8_gt(vacc, vinput_zero_point); |
| vacc = wasm_i16x8_sub(vinput_zero_point, vacc); |
| vmultiplier = wasm_v128_and(vmultiplier, vmultiplier_diff); |
| vacc = wasm_i16x8_shl(vacc, 7); |
| vmultiplier = wasm_v128_xor(vmultiplier, vmultiplier_base); |
| vacc = ${WASM_I16X8_Q15MULR}(vacc, vmultiplier); |
| vacc = wasm_i16x8_add_sat(vacc, voutput_zero_point); |
| x += 8; |
| |
| const v128_t vy = ${WASM_X8X16_NARROW_I16X8}(vacc, vacc); |
| wasm_v128_store64_lane(y, vy, 0); |
| y += 8; |
| } |
| if XNN_UNLIKELY(n != 0) { |
| assert(n >= 1 * sizeof(${XINT8_T})); |
| assert(n <= 7 * sizeof(${XINT8_T})); |
| |
| v128_t vacc = ${WASM_X16X8_LOAD8X8}(x); |
| v128_t vmultiplier = wasm_i16x8_gt(vacc, vinput_zero_point); |
| vacc = wasm_i16x8_sub(vinput_zero_point, vacc); |
| vmultiplier = wasm_v128_and(vmultiplier, vmultiplier_diff); |
| vacc = wasm_i16x8_shl(vacc, 7); |
| vmultiplier = wasm_v128_xor(vmultiplier, vmultiplier_base); |
| vacc = ${WASM_I16X8_Q15MULR}(vacc, vmultiplier); |
| vacc = wasm_i16x8_add_sat(vacc, voutput_zero_point); |
| |
| v128_t vy = ${WASM_X8X16_NARROW_I16X8}(vacc, vacc); |
| if (n & (4 * sizeof(${XINT8_T}))) { |
| wasm_v128_store32_lane(y, vy, 0); |
| vy = wasm_u64x2_shr(vy, 32); |
| y += 4; |
| } |
| if (n & (2 * sizeof(${XINT8_T}))) { |
| wasm_v128_store16_lane(y, vy, 0); |
| vy = wasm_u32x4_shr(vy, 16); |
| y += 2; |
| } |
| if (n & (1 * sizeof(${XINT8_T}))) { |
| wasm_v128_store8_lane(y, vy, 0); |
| } |
| } |
| } |