| 1 |
/* SPDX-License-Identifier: MIT |
| 2 |
* |
| 3 |
* Permission is hereby granted, free of charge, to any person |
| 4 |
* obtaining a copy of this software and associated documentation |
| 5 |
* files (the "Software"), to deal in the Software without |
| 6 |
* restriction, including without limitation the rights to use, copy, |
| 7 |
* modify, merge, publish, distribute, sublicense, and/or sell copies |
| 8 |
* of the Software, and to permit persons to whom the Software is |
| 9 |
* furnished to do so, subject to the following conditions: |
| 10 |
* |
| 11 |
* The above copyright notice and this permission notice shall be |
| 12 |
* included in all copies or substantial portions of the Software. |
| 13 |
* |
| 14 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 15 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 16 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 17 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 18 |
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 19 |
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 20 |
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 |
* SOFTWARE. |
| 22 |
* |
| 23 |
* Copyright: |
| 24 |
* 2020 Evan Nemerson <[email protected]> |
| 25 |
* 2020 Sean Maher <[email protected]> (Copyright owned by Google, LLC) |
| 26 |
*/ |
| 27 |
|
| 28 |
/* Implementation notes (seanptmaher): |
| 29 |
* |
| 30 |
* It won't overflow during the multiplication, it'll ever only double |
| 31 |
* the bit length, we only care about the overflow during the shift, |
| 32 |
* so do the multiplication, then the shift with saturation |
| 33 |
*/ |
| 34 |
|
| 35 |
#if !defined(SIMDE_ARM_NEON_QDMULL_H) |
| 36 |
#define SIMDE_ARM_NEON_QDMULL_H |
| 37 |
|
| 38 |
#include "combine.h" |
| 39 |
|
| 40 |
HEDLEY_DIAGNOSTIC_PUSH |
| 41 |
SIMDE_DISABLE_UNWANTED_DIAGNOSTICS |
| 42 |
SIMDE_BEGIN_DECLS_ |
| 43 |
|
| 44 |
SIMDE_FUNCTION_ATTRIBUTES |
| 45 |
int32_t |
| 46 |
simde_vqdmullh_s16(int16_t a, int16_t b) { |
| 47 |
#if defined(SIMDE_ARM_NEON_A64V8_NATIVE) |
| 48 |
return vqdmullh_s16(a, b); |
| 49 |
#else |
| 50 |
int32_t mul = (HEDLEY_STATIC_CAST(int32_t, a) * HEDLEY_STATIC_CAST(int32_t, b)); |
| 51 |
return (simde_math_labs(mul) & (1 << 30)) ? ((mul < 0) ? INT32_MIN : INT32_MAX) : mul << 1; |
| 52 |
#endif |
| 53 |
} |
| 54 |
#if defined(SIMDE_ARM_NEON_A32V7_ENABLE_NATIVE_ALIASES) |
| 55 |
#undef vqdmullh_s16 |
| 56 |
#define vqdmullh_s16(a, b) simde_vqdmullh_s16((a), (b)) |
| 57 |
#endif |
| 58 |
|
| 59 |
SIMDE_FUNCTION_ATTRIBUTES |
| 60 |
int64_t |
| 61 |
simde_vqdmulls_s32(int32_t a, int32_t b) { |
| 62 |
#if defined(SIMDE_ARM_NEON_A64V8_NATIVE) |
| 63 |
return vqdmulls_s32(a, b); |
| 64 |
#else |
| 65 |
int64_t mul = (HEDLEY_STATIC_CAST(int64_t, a) * HEDLEY_STATIC_CAST(int64_t, b)); |
| 66 |
return ((a > 0 ? a : -a) & (HEDLEY_STATIC_CAST(int64_t, 1) << 62)) ? ((mul < 0) ? INT64_MIN : INT64_MAX) : mul << 1; |
| 67 |
#endif |
| 68 |
} |
| 69 |
#if defined(SIMDE_ARM_NEON_A32V7_ENABLE_NATIVE_ALIASES) |
| 70 |
#undef vqdmulls_s16 |
| 71 |
#define vqdmulls_s16(a, b) simde_vqdmulls_s16((a), (b)) |
| 72 |
#endif |
| 73 |
|
| 74 |
SIMDE_FUNCTION_ATTRIBUTES |
| 75 |
simde_int32x4_t |
| 76 |
simde_vqdmull_s16(simde_int16x4_t a, simde_int16x4_t b) { |
| 77 |
#if defined(SIMDE_ARM_NEON_A32V7_NATIVE) |
| 78 |
return vqdmull_s16(a, b); |
| 79 |
#elif defined(SIMDE_WASM_SIMD128_NATIVE) |
| 80 |
simde_int32x4_private r_; |
| 81 |
simde_int16x8_private v_ = simde_int16x8_to_private(simde_vcombine_s16(a, b)); |
| 82 |
|
| 83 |
const v128_t lo = wasm_i32x4_extend_low_i16x8(v_.v128); |
| 84 |
const v128_t hi = wasm_i32x4_extend_high_i16x8(v_.v128); |
| 85 |
|
| 86 |
const v128_t product = wasm_i32x4_mul(lo, hi); |
| 87 |
const v128_t uflow = wasm_i32x4_lt(product, wasm_i32x4_splat(-INT32_C(0x40000000))); |
| 88 |
const v128_t oflow = wasm_i32x4_gt(product, wasm_i32x4_splat( INT32_C(0x3FFFFFFF))); |
| 89 |
r_.v128 = wasm_i32x4_shl(product, 1); |
| 90 |
r_.v128 = wasm_v128_bitselect(wasm_i32x4_splat(INT32_MIN), r_.v128, uflow); |
| 91 |
r_.v128 = wasm_v128_bitselect(wasm_i32x4_splat(INT32_MAX), r_.v128, oflow); |
| 92 |
|
| 93 |
return simde_int32x4_from_private(r_); |
| 94 |
#else |
| 95 |
simde_int32x4_private r_; |
| 96 |
simde_int16x4_private |
| 97 |
a_ = simde_int16x4_to_private(a), |
| 98 |
b_ = simde_int16x4_to_private(b); |
| 99 |
|
| 100 |
SIMDE_VECTORIZE |
| 101 |
for (size_t i = 0 ; i < (sizeof(r_.values) / sizeof(r_.values[0])) ; i++) { |
| 102 |
r_.values[i] = simde_vqdmullh_s16(a_.values[i], b_.values[i]); |
| 103 |
} |
| 104 |
|
| 105 |
return simde_int32x4_from_private(r_); |
| 106 |
#endif |
| 107 |
} |
| 108 |
#if defined(SIMDE_ARM_NEON_A32V7_ENABLE_NATIVE_ALIASES) |
| 109 |
#undef vqdmull_s16 |
| 110 |
#define vqdmull_s16(a, b) simde_vqdmull_s16((a), (b)) |
| 111 |
#endif |
| 112 |
SIMDE_FUNCTION_ATTRIBUTES |
| 113 |
|
| 114 |
simde_int64x2_t |
| 115 |
simde_vqdmull_s32(simde_int32x2_t a, simde_int32x2_t b) { |
| 116 |
#if defined(SIMDE_ARM_NEON_A32V7_NATIVE) |
| 117 |
return vqdmull_s32(a, b); |
| 118 |
#elif defined(SIMDE_WASM_SIMD128_NATIVE) |
| 119 |
simde_int64x2_private r_; |
| 120 |
simde_int32x4_private v_ = simde_int32x4_to_private(simde_vcombine_s32(a, b)); |
| 121 |
|
| 122 |
const v128_t lo = wasm_i64x2_extend_low_i32x4(v_.v128); |
| 123 |
const v128_t hi = wasm_i64x2_extend_high_i32x4(v_.v128); |
| 124 |
|
| 125 |
const v128_t product = wasm_i64x2_mul(lo, hi); |
| 126 |
const v128_t uflow = wasm_i64x2_lt(product, wasm_i64x2_splat(-INT64_C(0x4000000000000000))); |
| 127 |
const v128_t oflow = wasm_i64x2_gt(product, wasm_i64x2_splat( INT64_C(0x3FFFFFFFFFFFFFFF))); |
| 128 |
r_.v128 = wasm_i64x2_shl(product, 1); |
| 129 |
r_.v128 = wasm_v128_bitselect(wasm_i64x2_splat(INT64_MIN), r_.v128, uflow); |
| 130 |
r_.v128 = wasm_v128_bitselect(wasm_i64x2_splat(INT64_MAX), r_.v128, oflow); |
| 131 |
|
| 132 |
return simde_int64x2_from_private(r_); |
| 133 |
#else |
| 134 |
simde_int64x2_private r_; |
| 135 |
simde_int32x2_private |
| 136 |
a_ = simde_int32x2_to_private(a), |
| 137 |
b_ = simde_int32x2_to_private(b); |
| 138 |
|
| 139 |
SIMDE_VECTORIZE |
| 140 |
for (size_t i = 0 ; i < (sizeof(r_.values) / sizeof(r_.values[0])) ; i++) { |
| 141 |
r_.values[i] = simde_vqdmulls_s32(a_.values[i], b_.values[i]); |
| 142 |
} |
| 143 |
|
| 144 |
return simde_int64x2_from_private(r_); |
| 145 |
#endif |
| 146 |
} |
| 147 |
#if defined(SIMDE_ARM_NEON_A32V7_ENABLE_NATIVE_ALIASES) |
| 148 |
#undef vqdmull_s32 |
| 149 |
#define vqdmull_s32(a, b) simde_vqdmull_s32((a), (b)) |
| 150 |
#endif |
| 151 |
|
| 152 |
SIMDE_END_DECLS_ |
| 153 |
HEDLEY_DIAGNOSTIC_POP |
| 154 |
|
| 155 |
#endif /* !defined(SIMDE_ARM_NEON_QDMULL_H) */ |