| 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 |
* 2021 Evan Nemerson <[email protected]> |
| 25 |
*/ |
| 26 |
|
| 27 |
#if !defined(SIMDE_MIPS_MSA_ADDS_A_H) |
| 28 |
#define SIMDE_MIPS_MSA_ADDS_A_H |
| 29 |
|
| 30 |
#include "types.h" |
| 31 |
|
| 32 |
HEDLEY_DIAGNOSTIC_PUSH |
| 33 |
SIMDE_DISABLE_UNWANTED_DIAGNOSTICS |
| 34 |
SIMDE_BEGIN_DECLS_ |
| 35 |
|
| 36 |
SIMDE_FUNCTION_ATTRIBUTES |
| 37 |
simde_v16i8 |
| 38 |
simde_msa_adds_a_b(simde_v16i8 a, simde_v16i8 b) { |
| 39 |
#if defined(SIMDE_MIPS_MSA_NATIVE) |
| 40 |
return __msa_adds_a_b(a, b); |
| 41 |
#elif defined(SIMDE_ARM_NEON_A32V7_NATIVE) |
| 42 |
return vqaddq_s8(vabsq_s8(a), vabsq_s8(b)); |
| 43 |
#elif defined(SIMDE_POWER_ALTIVEC_P6_NATIVE) |
| 44 |
return vec_adds(vec_abs(a), vec_abs(b)); |
| 45 |
#else |
| 46 |
simde_v16i8_private |
| 47 |
a_ = simde_v16i8_to_private(a), |
| 48 |
b_ = simde_v16i8_to_private(b), |
| 49 |
r_; |
| 50 |
|
| 51 |
#if defined(SIMDE_X86_SSSE3_NATIVE) |
| 52 |
r_.m128i = _mm_adds_epi8(_mm_abs_epi8(a_.m128i), _mm_abs_epi8(b_.m128i)); |
| 53 |
#elif defined(SIMDE_WASM_SIMD128_NATIVE) |
| 54 |
r_.v128 = wasm_i8x16_add_sat(wasm_i8x16_abs(a_.v128), wasm_i8x16_abs(b_.v128)); |
| 55 |
#elif defined(SIMDE_VECTOR_SCALAR) |
| 56 |
__typeof__(a_.values) amask = HEDLEY_REINTERPRET_CAST(__typeof__(a_.values), a_.values < 0); |
| 57 |
__typeof__(b_.values) bmask = HEDLEY_REINTERPRET_CAST(__typeof__(b_.values), b_.values < 0); |
| 58 |
__typeof__(a_.values) aabs = (-a_.values & amask) | (a_.values & ~amask); |
| 59 |
__typeof__(b_.values) babs = (-b_.values & bmask) | (b_.values & ~bmask); |
| 60 |
__typeof__(r_.values) sum = aabs + babs; |
| 61 |
__typeof__(r_.values) max = { INT8_MAX, INT8_MAX, INT8_MAX, INT8_MAX, INT8_MAX, INT8_MAX, INT8_MAX, INT8_MAX, INT8_MAX, INT8_MAX, INT8_MAX, INT8_MAX, INT8_MAX, INT8_MAX, INT8_MAX, INT8_MAX }; |
| 62 |
__typeof__(r_.values) smask = HEDLEY_REINTERPRET_CAST(__typeof__(r_.values), aabs > (max - babs)); |
| 63 |
r_.values = (max & smask) | (sum & ~smask); |
| 64 |
#else |
| 65 |
SIMDE_VECTORIZE |
| 66 |
for (size_t i = 0 ; i < (sizeof(r_.values) / sizeof(r_.values[0])) ; i++) { |
| 67 |
r_.values[i] = |
| 68 |
simde_math_adds_i8( |
| 69 |
((a_.values[i] < 0) ? -a_.values[i] : a_.values[i]), |
| 70 |
((b_.values[i] < 0) ? -b_.values[i] : b_.values[i]) |
| 71 |
); |
| 72 |
} |
| 73 |
#endif |
| 74 |
|
| 75 |
return simde_v16i8_from_private(r_); |
| 76 |
#endif |
| 77 |
} |
| 78 |
#if defined(SIMDE_MIPS_MSA_ENABLE_NATIVE_ALIASES) |
| 79 |
#undef __msa_adds_a_b |
| 80 |
#define __msa_adds_a_b(a, b) simde_msa_adds_a_b((a), (b)) |
| 81 |
#endif |
| 82 |
|
| 83 |
SIMDE_FUNCTION_ATTRIBUTES |
| 84 |
simde_v8i16 |
| 85 |
simde_msa_adds_a_h(simde_v8i16 a, simde_v8i16 b) { |
| 86 |
#if defined(SIMDE_MIPS_MSA_NATIVE) |
| 87 |
return __msa_adds_a_h(a, b); |
| 88 |
#elif defined(SIMDE_ARM_NEON_A32V7_NATIVE) |
| 89 |
return vqaddq_s16(vabsq_s16(a), vabsq_s16(b)); |
| 90 |
#elif defined(SIMDE_POWER_ALTIVEC_P6_NATIVE) |
| 91 |
return vec_adds(vec_abs(a), vec_abs(b)); |
| 92 |
#else |
| 93 |
simde_v8i16_private |
| 94 |
a_ = simde_v8i16_to_private(a), |
| 95 |
b_ = simde_v8i16_to_private(b), |
| 96 |
r_; |
| 97 |
|
| 98 |
#if defined(SIMDE_X86_SSSE3_NATIVE) |
| 99 |
r_.m128i = _mm_adds_epi16(_mm_abs_epi16(a_.m128i), _mm_abs_epi16(b_.m128i)); |
| 100 |
#elif defined(SIMDE_WASM_SIMD128_NATIVE) |
| 101 |
r_.v128 = wasm_i16x8_add_sat(wasm_i16x8_abs(a_.v128), wasm_i16x8_abs(b_.v128)); |
| 102 |
#elif defined(SIMDE_VECTOR_SCALAR) |
| 103 |
__typeof__(a_.values) amask = HEDLEY_REINTERPRET_CAST(__typeof__(a_.values), a_.values < 0); |
| 104 |
__typeof__(b_.values) bmask = HEDLEY_REINTERPRET_CAST(__typeof__(b_.values), b_.values < 0); |
| 105 |
__typeof__(a_.values) aabs = (-a_.values & amask) | (a_.values & ~amask); |
| 106 |
__typeof__(b_.values) babs = (-b_.values & bmask) | (b_.values & ~bmask); |
| 107 |
__typeof__(r_.values) sum = aabs + babs; |
| 108 |
__typeof__(r_.values) max = { INT16_MAX, INT16_MAX, INT16_MAX, INT16_MAX, INT16_MAX, INT16_MAX, INT16_MAX, INT16_MAX }; |
| 109 |
__typeof__(r_.values) smask = HEDLEY_REINTERPRET_CAST(__typeof__(r_.values), aabs > (max - babs)); |
| 110 |
r_.values = (max & smask) | (sum & ~smask); |
| 111 |
#else |
| 112 |
SIMDE_VECTORIZE |
| 113 |
for (size_t i = 0 ; i < (sizeof(r_.values) / sizeof(r_.values[0])) ; i++) { |
| 114 |
r_.values[i] = |
| 115 |
simde_math_adds_i16( |
| 116 |
((a_.values[i] < 0) ? -a_.values[i] : a_.values[i]), |
| 117 |
((b_.values[i] < 0) ? -b_.values[i] : b_.values[i]) |
| 118 |
); |
| 119 |
} |
| 120 |
#endif |
| 121 |
|
| 122 |
return simde_v8i16_from_private(r_); |
| 123 |
#endif |
| 124 |
} |
| 125 |
#if defined(SIMDE_MIPS_MSA_ENABLE_NATIVE_ALIASES) |
| 126 |
#undef __msa_adds_a_h |
| 127 |
#define __msa_adds_a_h(a, b) simde_msa_adds_a_h((a), (b)) |
| 128 |
#endif |
| 129 |
|
| 130 |
SIMDE_FUNCTION_ATTRIBUTES |
| 131 |
simde_v4i32 |
| 132 |
simde_msa_adds_a_w(simde_v4i32 a, simde_v4i32 b) { |
| 133 |
#if defined(SIMDE_MIPS_MSA_NATIVE) |
| 134 |
return __msa_adds_a_w(a, b); |
| 135 |
#elif defined(SIMDE_ARM_NEON_A32V7_NATIVE) |
| 136 |
return vqaddq_s32(vabsq_s32(a), vabsq_s32(b)); |
| 137 |
#elif defined(SIMDE_POWER_ALTIVEC_P6_NATIVE) |
| 138 |
return vec_adds(vec_abs(a), vec_abs(b)); |
| 139 |
#else |
| 140 |
simde_v4i32_private |
| 141 |
a_ = simde_v4i32_to_private(a), |
| 142 |
b_ = simde_v4i32_to_private(b), |
| 143 |
r_; |
| 144 |
|
| 145 |
#if defined(SIMDE_X86_SSSE3_NATIVE) |
| 146 |
__m128i aabs = _mm_abs_epi32(a_.m128i); |
| 147 |
__m128i babs = _mm_abs_epi32(b_.m128i); |
| 148 |
__m128i sum = _mm_add_epi32(aabs, babs); |
| 149 |
__m128i max = _mm_set1_epi32(INT32_MAX); |
| 150 |
__m128i smask = |
| 151 |
_mm_cmplt_epi32( |
| 152 |
_mm_sub_epi32(max, babs), |
| 153 |
aabs |
| 154 |
); |
| 155 |
|
| 156 |
#if defined(SIMDE_X86_SSE4_1_NATIVE) |
| 157 |
r_.m128i = _mm_blendv_epi8(sum, max, smask); |
| 158 |
#else |
| 159 |
r_.m128i = |
| 160 |
_mm_or_si128( |
| 161 |
_mm_and_si128(smask, max), |
| 162 |
_mm_andnot_si128(smask, sum) |
| 163 |
); |
| 164 |
#endif |
| 165 |
#elif defined(SIMDE_VECTOR_SCALAR) |
| 166 |
__typeof__(a_.values) amask = HEDLEY_REINTERPRET_CAST(__typeof__(a_.values), a_.values < 0); |
| 167 |
__typeof__(b_.values) bmask = HEDLEY_REINTERPRET_CAST(__typeof__(b_.values), b_.values < 0); |
| 168 |
__typeof__(a_.values) aabs = (-a_.values & amask) | (a_.values & ~amask); |
| 169 |
__typeof__(b_.values) babs = (-b_.values & bmask) | (b_.values & ~bmask); |
| 170 |
__typeof__(r_.values) sum = aabs + babs; |
| 171 |
__typeof__(r_.values) max = { INT32_MAX, INT32_MAX, INT32_MAX, INT32_MAX }; |
| 172 |
__typeof__(r_.values) smask = HEDLEY_REINTERPRET_CAST(__typeof__(r_.values), aabs > (max - babs)); |
| 173 |
r_.values = (max & smask) | (sum & ~smask); |
| 174 |
#else |
| 175 |
SIMDE_VECTORIZE |
| 176 |
for (size_t i = 0 ; i < (sizeof(r_.values) / sizeof(r_.values[0])) ; i++) { |
| 177 |
r_.values[i] = |
| 178 |
simde_math_adds_i32( |
| 179 |
((a_.values[i] < 0) ? -a_.values[i] : a_.values[i]), |
| 180 |
((b_.values[i] < 0) ? -b_.values[i] : b_.values[i]) |
| 181 |
); |
| 182 |
} |
| 183 |
#endif |
| 184 |
|
| 185 |
return simde_v4i32_from_private(r_); |
| 186 |
#endif |
| 187 |
} |
| 188 |
#if defined(SIMDE_MIPS_MSA_ENABLE_NATIVE_ALIASES) |
| 189 |
#undef __msa_adds_a_w |
| 190 |
#define __msa_adds_a_w(a, b) simde_msa_adds_a_w((a), (b)) |
| 191 |
#endif |
| 192 |
|
| 193 |
SIMDE_FUNCTION_ATTRIBUTES |
| 194 |
simde_v2i64 |
| 195 |
simde_msa_adds_a_d(simde_v2i64 a, simde_v2i64 b) { |
| 196 |
#if defined(SIMDE_MIPS_MSA_NATIVE) |
| 197 |
return __msa_adds_a_d(a, b); |
| 198 |
#elif defined(SIMDE_ARM_NEON_A64V8_NATIVE) |
| 199 |
return vqaddq_s64(vabsq_s64(a), vabsq_s64(b)); |
| 200 |
#else |
| 201 |
simde_v2i64_private |
| 202 |
a_ = simde_v2i64_to_private(a), |
| 203 |
b_ = simde_v2i64_to_private(b), |
| 204 |
r_; |
| 205 |
|
| 206 |
#if defined(SIMDE_VECTOR_SCALAR) |
| 207 |
__typeof__(a_.values) amask = HEDLEY_REINTERPRET_CAST(__typeof__(a_.values), a_.values < 0); |
| 208 |
__typeof__(b_.values) bmask = HEDLEY_REINTERPRET_CAST(__typeof__(b_.values), b_.values < 0); |
| 209 |
__typeof__(a_.values) aabs = (-a_.values & amask) | (a_.values & ~amask); |
| 210 |
__typeof__(b_.values) babs = (-b_.values & bmask) | (b_.values & ~bmask); |
| 211 |
__typeof__(r_.values) sum = aabs + babs; |
| 212 |
__typeof__(r_.values) max = { INT64_MAX, INT64_MAX }; |
| 213 |
__typeof__(r_.values) smask = HEDLEY_REINTERPRET_CAST(__typeof__(r_.values), aabs > (max - babs)); |
| 214 |
r_.values = (max & smask) | (sum & ~smask); |
| 215 |
#else |
| 216 |
SIMDE_VECTORIZE |
| 217 |
for (size_t i = 0 ; i < (sizeof(r_.values) / sizeof(r_.values[0])) ; i++) { |
| 218 |
r_.values[i] = |
| 219 |
simde_math_adds_i64( |
| 220 |
((a_.values[i] < 0) ? -a_.values[i] : a_.values[i]), |
| 221 |
((b_.values[i] < 0) ? -b_.values[i] : b_.values[i]) |
| 222 |
); |
| 223 |
} |
| 224 |
#endif |
| 225 |
|
| 226 |
return simde_v2i64_from_private(r_); |
| 227 |
#endif |
| 228 |
} |
| 229 |
#if defined(SIMDE_MIPS_MSA_ENABLE_NATIVE_ALIASES) |
| 230 |
#undef __msa_adds_a_d |
| 231 |
#define __msa_adds_a_d(a, b) simde_msa_adds_a_d((a), (b)) |
| 232 |
#endif |
| 233 |
|
| 234 |
SIMDE_END_DECLS_ |
| 235 |
HEDLEY_DIAGNOSTIC_POP |
| 236 |
|
| 237 |
#endif /* !defined(SIMDE_MIPS_MSA_ADDS_A_H) */ |