| // Copyright 2020 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. |
| |
| $ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| $assert CHANNEL_TILE % 8 == 0 |
| $assert CHANNEL_TILE >= 8 |
| $assert KERNEL_TILE >= 2 |
| #include <assert.h> |
| |
| #include <wasm_simd128.h> |
| |
| #include <xnnpack/dwconv.h> |
| |
| |
| void xnn_qs8_dwconv_minmax_ukernel_up${CHANNEL_TILE}x${KERNEL_TILE}__wasmsimd_mul16( |
| size_t channels, |
| size_t output_width, |
| const int8_t** input, |
| const void* weights, |
| int8_t* output, |
| size_t input_stride, |
| size_t output_increment, |
| size_t input_offset, |
| const int8_t* zero, |
| const union xnn_qs8_gemm_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_DISABLE_TSAN |
| { |
| assert(channels != 0); |
| assert(output_width != 0); |
| |
| do { |
| $for K in range(KERNEL_TILE): |
| const int8_t* i${K} = input[${K}]; |
| assert(i${K} != NULL); |
| if XNN_UNPREDICTABLE(i${K} != zero) { |
| i${K} = (const int8_t*) ((uintptr_t) i${K} + input_offset); |
| } |
| input = (const int8_t**) ((uintptr_t) input + input_stride); |
| |
| size_t c = channels; |
| const void* w = weights; |
| for (; c >= ${CHANNEL_TILE}; c -= ${CHANNEL_TILE}) { |
| v128_t vacc${ABC[0:4]} = wasm_v128_load(w); |
| $for C in range(4, CHANNEL_TILE, 4): |
| v128_t vacc${ABC[C:C+4]} = wasm_v128_load((const void*) ((uintptr_t) w + ${C} * sizeof(int32_t))); |
| |
| $for K in range(KERNEL_TILE): |
| |
| $for C in range(0, CHANNEL_TILE, 8): |
| $if C == 0: |
| const v128_t vi${K}x${ABC[0:8]} = wasm_i16x8_load_8x8(i${K}); |
| $else: |
| const v128_t vi${K}x${ABC[C:C+8]} = wasm_i16x8_load_8x8(i${K} + ${C}); |
| const v128_t vk${K}x${ABC[C:C+8]} = wasm_i16x8_load_8x8((const void*) ((uintptr_t) w + ${CHANNEL_TILE} * sizeof(int32_t) + ${K * CHANNEL_TILE + C} * sizeof(int8_t))); |
| i${K} += ${CHANNEL_TILE}; |
| |
| $for C in range(0, CHANNEL_TILE, 8): |
| const v128_t vprod${K}x${ABC[C:C+8]} = wasm_i16x8_mul(vi${K}x${ABC[C:C+8]}, vk${K}x${ABC[C:C+8]}); |
| |
| $for C in range(0, CHANNEL_TILE, 8): |
| vacc${ABC[C:C+4]} = wasm_i32x4_add(vacc${ABC[C:C+4]}, wasm_i32x4_widen_low_i16x8(vprod${K}x${ABC[C:C+8]})); |
| vacc${ABC[C+4:C+8]} = wasm_i32x4_add(vacc${ABC[C+4:C+8]}, wasm_i32x4_widen_high_i16x8(vprod${K}x${ABC[C:C+8]})); |
| |
| w = (const void*) ((uintptr_t) w + ${CHANNEL_TILE} * sizeof(int32_t) + ${KERNEL_TILE * CHANNEL_TILE} * sizeof(int8_t)); |
| |
| $for C in range(0, CHANNEL_TILE, 4): |
| const v128_t vsign${ABC[C:C+4]} = wasm_i32x4_shr(vacc${ABC[C:C+4]}, 31); |
| |
| $for C in range(0, CHANNEL_TILE, 4): |
| const v128_t vacc${ABC[C:C+2]} = wasm_v32x4_shuffle(vacc${ABC[C:C+4]}, vsign${ABC[C:C+4]}, 0, 4, 1, 5); |
| const v128_t vacc${ABC[C+2:C+4]} = wasm_v32x4_shuffle(vacc${ABC[C:C+4]}, vsign${ABC[C:C+4]}, 2, 6, 3, 7); |
| |
| const v128_t vmultiplier = wasm_v128_load(params->wasmsimd.multiplier); |
| const v128_t vrounding = wasm_v128_load(params->wasmsimd.rounding); |
| $for C in range(0, CHANNEL_TILE, 2): |
| const v128_t vprod${ABC[C:C+2]} = wasm_i64x2_add(wasm_i64x2_mul(vacc${ABC[C:C+2]}, vmultiplier), vrounding); |
| |
| $for C in range(0, CHANNEL_TILE, 4): |
| const v128_t vq31prod${ABC[C:C+4]} = wasm_v32x4_shuffle(vprod${ABC[C:C+2]}, vprod${ABC[C+2:C+4]}, 1, 3, 5, 7); |
| |
| const v128_t vremainder_mask = wasm_v128_load(params->wasmsimd.remainder_mask); |
| $for C in range(0, CHANNEL_TILE, 4): |
| const v128_t vrem${ABC[C:C+4]} = wasm_i32x4_add(wasm_v128_and(vq31prod${ABC[C:C+4]}, vremainder_mask), wasm_i32x4_shr(vq31prod${ABC[C:C+4]}, 31)); |
| |
| const v128_t vthreshold = wasm_v128_load(params->wasmsimd.remainder_threshold); |
| const int32_t vshift = params->wasmsimd.shift; |
| $for C in range(0, CHANNEL_TILE, 4): |
| vacc${ABC[C:C+4]} = wasm_i32x4_sub(wasm_i32x4_shr(vq31prod${ABC[C:C+4]}, vshift), wasm_i32x4_gt(vrem${ABC[C:C+4]}, vthreshold)); |
| |
| const v128_t voutput_zero_point = wasm_v128_load(params->wasmsimd.output_zero_point); |
| $for C in range(0, CHANNEL_TILE, 8): |
| v128_t vout${ABC[C:C+8]} = wasm_i16x8_add_saturate(wasm_i16x8_narrow_i32x4(vacc${ABC[C:C+4]}, vacc${ABC[C+4:C+8]}), voutput_zero_point); |
| |
| const v128_t voutput_min = wasm_v128_load(params->wasmsimd.output_min); |
| const v128_t voutput_max = wasm_v128_load(params->wasmsimd.output_max); |
| $for C in range(0, CHANNEL_TILE, 16): |
| $if C + 8 < CHANNEL_TILE: |
| v128_t vout${ABC[C:C+16]} = wasm_i8x16_min(wasm_i8x16_max(wasm_i8x16_narrow_i16x8(vout${ABC[C:C+8]}, vout${ABC[C+8:C+16]}), voutput_min), voutput_max); |
| $else: |
| v128_t vout${ABC[C:C+8]}${ABC[C:C+8]} = wasm_i8x16_min(wasm_i8x16_max(wasm_i8x16_narrow_i16x8(vout${ABC[C:C+8]}, vout${ABC[C:C+8]}), voutput_min), voutput_max); |
| |
| $if CHANNEL_TILE > 8: |
| wasm_v128_store(output, vout${ABC[0:16]}); |
| $else: |
| *((double*) output) = wasm_f64x2_extract_lane(vout${ABC[0:8]}${ABC[0:8]}, 0); |
| $for C in range(16, CHANNEL_TILE, 16): |
| $if C + 8 < CHANNEL_TILE: |
| wasm_v128_store(output + ${C}, vout${ABC[C:C+16]}); |
| $else: |
| *((double*) (output + ${C})) = wasm_f64x2_extract_lane(vout${ABC[C:C+8]}${ABC[C:C+8]}, 0); |
| output += ${CHANNEL_TILE}; |
| } |
| if XNN_UNLIKELY(c != 0) { |
| $if CHANNEL_TILE > 8: |
| const int8_t* k = (const int8_t*) ((uintptr_t) w + ${CHANNEL_TILE} * sizeof(int32_t)); |
| ${"do " if CHANNEL_TILE > 8 else ""}{ |
| v128_t vacc${ABC[0:4]} = wasm_v128_load(w); |
| v128_t vacc${ABC[4:8]} = wasm_v128_load((const void*) ((uintptr_t) w + 4 * sizeof(int32_t))); |
| |
| $for K in range(KERNEL_TILE): |
| |
| const v128_t vi${K}x${ABC[0:8]} = wasm_i16x8_load_8x8(i${K}); |
| $if CHANNEL_TILE > 8: |
| $if K == 0: |
| const v128_t vk${K}x${ABC[0:8]} = wasm_i16x8_load_8x8(k); |
| $else: |
| const v128_t vk${K}x${ABC[0:8]} = wasm_i16x8_load_8x8((const void*) (k + ${K * CHANNEL_TILE})); |
| $else: |
| const v128_t vk${K}x${ABC[0:8]} = wasm_i16x8_load_8x8((const void*) ((uintptr_t) w + ${CHANNEL_TILE} * sizeof(int32_t) + ${K * CHANNEL_TILE} * sizeof(int8_t))); |
| $if CHANNEL_TILE > 8: |
| i${K} += 8; |
| |
| const v128_t vprod${K}x${ABC[0:8]} = wasm_i16x8_mul(vi${K}x${ABC[0:8]}, vk${K}x${ABC[0:8]}); |
| |
| vacc${ABC[0:4]} = wasm_i32x4_add(vacc${ABC[0:4]}, wasm_i32x4_widen_low_i16x8(vprod${K}x${ABC[0:8]})); |
| vacc${ABC[4:8]} = wasm_i32x4_add(vacc${ABC[4:8]}, wasm_i32x4_widen_high_i16x8(vprod${K}x${ABC[0:8]})); |
| |
| $if CHANNEL_TILE > 8: |
| w = (const void*) ((uintptr_t) w + 8 * sizeof(int32_t)); |
| k += 8; |
| |
| const v128_t vsign${ABC[0:4]} = wasm_i32x4_shr(vacc${ABC[0:4]}, 31); |
| const v128_t vsign${ABC[4:8]} = wasm_i32x4_shr(vacc${ABC[4:8]}, 31); |
| |
| const v128_t vacc${ABC[0:2]} = wasm_v32x4_shuffle(vacc${ABC[0:4]}, vsign${ABC[0:4]}, 0, 4, 1, 5); |
| const v128_t vacc${ABC[2:4]} = wasm_v32x4_shuffle(vacc${ABC[0:4]}, vsign${ABC[0:4]}, 2, 6, 3, 7); |
| const v128_t vacc${ABC[4:6]} = wasm_v32x4_shuffle(vacc${ABC[4:8]}, vsign${ABC[4:8]}, 0, 4, 1, 5); |
| const v128_t vacc${ABC[6:8]} = wasm_v32x4_shuffle(vacc${ABC[4:8]}, vsign${ABC[4:8]}, 2, 6, 3, 7); |
| |
| const v128_t vmultiplier = wasm_v128_load(params->wasmsimd.multiplier); |
| const v128_t vrounding = wasm_v128_load(params->wasmsimd.rounding); |
| const v128_t vprod${ABC[0:2]} = wasm_i64x2_add(wasm_i64x2_mul(vacc${ABC[0:2]}, vmultiplier), vrounding); |
| const v128_t vprod${ABC[2:4]} = wasm_i64x2_add(wasm_i64x2_mul(vacc${ABC[2:4]}, vmultiplier), vrounding); |
| const v128_t vprod${ABC[4:6]} = wasm_i64x2_add(wasm_i64x2_mul(vacc${ABC[4:6]}, vmultiplier), vrounding); |
| const v128_t vprod${ABC[6:8]} = wasm_i64x2_add(wasm_i64x2_mul(vacc${ABC[6:8]}, vmultiplier), vrounding); |
| |
| const v128_t vq31prod${ABC[0:4]} = wasm_v32x4_shuffle(vprod${ABC[0:2]}, vprod${ABC[2:4]}, 1, 3, 5, 7); |
| const v128_t vq31prod${ABC[4:8]} = wasm_v32x4_shuffle(vprod${ABC[4:6]}, vprod${ABC[6:8]}, 1, 3, 5, 7); |
| |
| const v128_t vremainder_mask = wasm_v128_load(params->wasmsimd.remainder_mask); |
| const v128_t vrem${ABC[0:4]} = wasm_i32x4_add(wasm_v128_and(vq31prod${ABC[0:4]}, vremainder_mask), wasm_i32x4_shr(vq31prod${ABC[0:4]}, 31)); |
| const v128_t vrem${ABC[4:8]} = wasm_i32x4_add(wasm_v128_and(vq31prod${ABC[4:8]}, vremainder_mask), wasm_i32x4_shr(vq31prod${ABC[4:8]}, 31)); |
| |
| const v128_t vthreshold = wasm_v128_load(params->wasmsimd.remainder_threshold); |
| const int32_t vshift = params->wasmsimd.shift; |
| vacc${ABC[0:4]} = wasm_i32x4_sub(wasm_i32x4_shr(vq31prod${ABC[0:4]}, vshift), wasm_i32x4_gt(vrem${ABC[0:4]}, vthreshold)); |
| vacc${ABC[4:8]} = wasm_i32x4_sub(wasm_i32x4_shr(vq31prod${ABC[4:8]}, vshift), wasm_i32x4_gt(vrem${ABC[4:8]}, vthreshold)); |
| |
| const v128_t voutput_zero_point = wasm_v128_load(params->wasmsimd.output_zero_point); |
| v128_t vout${ABC[0:8]} = wasm_i16x8_add_saturate(wasm_i16x8_narrow_i32x4(vacc${ABC[0:4]}, vacc${ABC[4:8]}), voutput_zero_point); |
| |
| const v128_t voutput_min = wasm_v128_load(params->wasmsimd.output_min); |
| const v128_t voutput_max = wasm_v128_load(params->wasmsimd.output_max); |
| v128_t vout${ABC[0:8]}${ABC[0:8]} = wasm_i8x16_min(wasm_i8x16_max(wasm_i8x16_narrow_i16x8(vout${ABC[0:8]}, vout${ABC[0:8]}), voutput_min), voutput_max); |
| |
| $if CHANNEL_TILE > 8: |
| if XNN_LIKELY(c >= 8) { |
| *((double*) output) = wasm_f64x2_extract_lane(vout${ABC[0:8]}${ABC[0:8]}, 0); |
| output += 8; |
| c -= 8; |
| } else { |
| if (c & 4) { |
| *((float*) output) = wasm_f32x4_extract_lane(vout${ABC[0:8]}${ABC[0:8]}, 0); |
| vout${ABC[0:8]}${ABC[0:8]} = wasm_u64x2_shr(vout${ABC[0:8]}${ABC[0:8]}, 32); |
| output += 4; |
| } |
| if (c & 2) { |
| *((uint16_t*) output) = (uint16_t) wasm_i16x8_extract_lane(vout${ABC[0:8]}${ABC[0:8]}, 0); |
| vout${ABC[0:8]}${ABC[0:8]} = wasm_u32x4_shr(vout${ABC[0:8]}${ABC[0:8]}, 16); |
| output += 2; |
| } |
| if (c & 1) { |
| *output = (int8_t) wasm_i8x16_extract_lane(vout${ABC[0:8]}${ABC[0:8]}, 0); |
| output += 1; |
| } |
| c = 0; |
| } |
| $else: |
| if (c & 4) { |
| *((float*) output) = wasm_f32x4_extract_lane(vout${ABC[0:8]}${ABC[0:8]}, 0); |
| vout${ABC[0:8]}${ABC[0:8]} = wasm_u64x2_shr(vout${ABC[0:8]}${ABC[0:8]}, 32); |
| output += 4; |
| } |
| if (c & 2) { |
| *((uint16_t*) output) = (uint16_t) wasm_i16x8_extract_lane(vout${ABC[0:8]}${ABC[0:8]}, 0); |
| vout${ABC[0:8]}${ABC[0:8]} = wasm_u32x4_shr(vout${ABC[0:8]}${ABC[0:8]}, 16); |
| output += 2; |
| } |
| if (c & 1) { |
| *output = (int8_t) wasm_i8x16_extract_lane(vout${ABC[0:8]}${ABC[0:8]}, 0); |
| output += 1; |
| } |
| }${" while (c != 0);" if CHANNEL_TILE > 8 else ""} |
| } |
| |
| output = (int8_t*) ((uintptr_t) output + output_increment); |
| } while (--output_width != 0); |
| } |