| 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 Christopher Moore <[email protected]> |
| 26 |
*/ |
| 27 |
|
| 28 |
/* The GFNI implementation is based on Wojciech Muła's work at |
| 29 |
* http://0x80.pl/articles/avx512-galois-field-for-bit-shuffling.html#bit-shuffling via |
| 30 |
* https://github.com/InstLatx64/InstLatX64_Demo/blob/49c27effdfd5a45f27e0ccb6e2f3be5f27c3845d/GFNI_Demo.h#L173 */ |
| 31 |
|
| 32 |
#if !defined(SIMDE_ARM_NEON_RBIT_H) |
| 33 |
#define SIMDE_ARM_NEON_RBIT_H |
| 34 |
|
| 35 |
#include "reinterpret.h" |
| 36 |
#include "types.h" |
| 37 |
|
| 38 |
HEDLEY_DIAGNOSTIC_PUSH |
| 39 |
SIMDE_DISABLE_UNWANTED_DIAGNOSTICS |
| 40 |
SIMDE_BEGIN_DECLS_ |
| 41 |
|
| 42 |
SIMDE_FUNCTION_ATTRIBUTES |
| 43 |
simde_uint8x8_t |
| 44 |
simde_vrbit_u8(simde_uint8x8_t a) { |
| 45 |
#if defined(SIMDE_ARM_NEON_A64V8_NATIVE) |
| 46 |
return vrbit_u8(a); |
| 47 |
#else |
| 48 |
simde_uint8x8_private |
| 49 |
r_, |
| 50 |
a_ = simde_uint8x8_to_private(a); |
| 51 |
|
| 52 |
#if defined(SIMDE_X86_MMX_NATIVE) && defined(SIMDE_X86_GFNI_NATIVE) |
| 53 |
__m128i tmp = _mm_movpi64_epi64(a_.m64); |
| 54 |
tmp = _mm_gf2p8affine_epi64_epi8(tmp, _mm_set1_epi64x(HEDLEY_STATIC_CAST(int64_t, UINT64_C(0x8040201008040201))), 0); |
| 55 |
r_.m64 = _mm_movepi64_pi64(tmp); |
| 56 |
#elif defined(SIMDE_X86_MMX_NATIVE) |
| 57 |
__m64 mask; |
| 58 |
mask = _mm_set1_pi8(0x55); |
| 59 |
a_.m64 = _mm_or_si64(_mm_andnot_si64(mask, _mm_slli_pi16(a_.m64, 1)), _mm_and_si64(mask, _mm_srli_pi16(a_.m64, 1))); |
| 60 |
mask = _mm_set1_pi8(0x33); |
| 61 |
a_.m64 = _mm_or_si64(_mm_andnot_si64(mask, _mm_slli_pi16(a_.m64, 2)), _mm_and_si64(mask, _mm_srli_pi16(a_.m64, 2))); |
| 62 |
mask = _mm_set1_pi8(0x0F); |
| 63 |
r_.m64 = _mm_or_si64(_mm_andnot_si64(mask, _mm_slli_pi16(a_.m64, 4)), _mm_and_si64(mask, _mm_srli_pi16(a_.m64, 4))); |
| 64 |
#else |
| 65 |
SIMDE_VECTORIZE |
| 66 |
for (size_t i = 0 ; i < (sizeof(r_.values) / sizeof(r_.values[0])) ; i++) { |
| 67 |
#if HEDLEY_HAS_BUILTIN(__builtin_bitreverse8) && !defined(HEDLEY_IBM_VERSION) |
| 68 |
r_.values[i] = __builtin_bitreverse8(a_.values[i]); |
| 69 |
#else |
| 70 |
r_.values[i] = HEDLEY_STATIC_CAST(uint8_t, (((a_.values[i] * UINT64_C(0x80200802)) & UINT64_C(0x0884422110)) * UINT64_C(0x0101010101)) >> 32); |
| 71 |
#endif |
| 72 |
} |
| 73 |
#endif |
| 74 |
|
| 75 |
return simde_uint8x8_from_private(r_); |
| 76 |
#endif |
| 77 |
} |
| 78 |
#if defined(SIMDE_ARM_NEON_A64V8_ENABLE_NATIVE_ALIASES) |
| 79 |
#undef vrbit_u8 |
| 80 |
#define vrbit_u8(a) simde_vrbit_u8(a) |
| 81 |
#endif |
| 82 |
|
| 83 |
SIMDE_FUNCTION_ATTRIBUTES |
| 84 |
simde_int8x8_t |
| 85 |
simde_vrbit_s8(simde_int8x8_t a) { |
| 86 |
#if defined(SIMDE_ARM_NEON_A64V8_NATIVE) |
| 87 |
return vrbit_s8(a); |
| 88 |
#else |
| 89 |
return simde_vreinterpret_s8_u8(simde_vrbit_u8(simde_vreinterpret_u8_s8(a))); |
| 90 |
#endif |
| 91 |
} |
| 92 |
#if defined(SIMDE_ARM_NEON_A64V8_ENABLE_NATIVE_ALIASES) |
| 93 |
#undef vrbit_s8 |
| 94 |
#define vrbit_s8(a) simde_vrbit_s8(a) |
| 95 |
#endif |
| 96 |
|
| 97 |
SIMDE_FUNCTION_ATTRIBUTES |
| 98 |
simde_uint8x16_t |
| 99 |
simde_vrbitq_u8(simde_uint8x16_t a) { |
| 100 |
#if defined(SIMDE_ARM_NEON_A64V8_NATIVE) |
| 101 |
return vrbitq_u8(a); |
| 102 |
#elif defined(SIMDE_POWER_ALTIVEC_P6_NATIVE) |
| 103 |
SIMDE_POWER_ALTIVEC_VECTOR(unsigned char) shift; |
| 104 |
shift = vec_splat_u8(1); |
| 105 |
a = vec_sel(vec_sl(a, shift), vec_sr(a, shift), vec_splats(HEDLEY_STATIC_CAST(unsigned char, 0x55))); |
| 106 |
shift = vec_splat_u8(2); |
| 107 |
a = vec_sel(vec_sl(a, shift), vec_sr(a, shift), vec_splats(HEDLEY_STATIC_CAST(unsigned char, 0x33))); |
| 108 |
shift = vec_splat_u8(4); |
| 109 |
return vec_or(vec_sl(a, shift), vec_sr(a, shift)); |
| 110 |
#else |
| 111 |
simde_uint8x16_private |
| 112 |
r_, |
| 113 |
a_ = simde_uint8x16_to_private(a); |
| 114 |
|
| 115 |
#if defined(SIMDE_X86_GFNI_NATIVE) |
| 116 |
r_.m128i = _mm_gf2p8affine_epi64_epi8(a_.m128i, _mm_set1_epi64x(HEDLEY_STATIC_CAST(int64_t, UINT64_C(0x8040201008040201))), 0); |
| 117 |
#elif defined(SIMDE_X86_SSE2_NATIVE) |
| 118 |
__m128i mask; |
| 119 |
mask = _mm_set1_epi8(0x55); |
| 120 |
a_.m128i = _mm_or_si128(_mm_andnot_si128(mask, _mm_slli_epi16(a_.m128i, 1)), _mm_and_si128(mask, _mm_srli_epi16(a_.m128i, 1))); |
| 121 |
mask = _mm_set1_epi8(0x33); |
| 122 |
a_.m128i = _mm_or_si128(_mm_andnot_si128(mask, _mm_slli_epi16(a_.m128i, 2)), _mm_and_si128(mask, _mm_srli_epi16(a_.m128i, 2))); |
| 123 |
mask = _mm_set1_epi8(0x0F); |
| 124 |
r_.m128i = _mm_or_si128(_mm_andnot_si128(mask, _mm_slli_epi16(a_.m128i, 4)), _mm_and_si128(mask, _mm_srli_epi16(a_.m128i, 4))); |
| 125 |
#elif defined(SIMDE_WASM_SIMD128_NATIVE) |
| 126 |
a_.v128 = wasm_v128_bitselect(wasm_u8x16_shr(a_.v128, 1), wasm_i8x16_shl(a_.v128, 1), wasm_i8x16_splat(0x55)); |
| 127 |
a_.v128 = wasm_v128_bitselect(wasm_u8x16_shr(a_.v128, 2), wasm_i8x16_shl(a_.v128, 2), wasm_i8x16_splat(0x33)); |
| 128 |
r_.v128 = wasm_v128_or(wasm_u8x16_shr(a_.v128, 4), wasm_i8x16_shl(a_.v128, 4)); |
| 129 |
#else |
| 130 |
SIMDE_VECTORIZE |
| 131 |
for (size_t i = 0 ; i < (sizeof(r_.values) / sizeof(r_.values[0])) ; i++) { |
| 132 |
#if HEDLEY_HAS_BUILTIN(__builtin_bitreverse8) && !defined(HEDLEY_IBM_VERSION) |
| 133 |
r_.values[i] = __builtin_bitreverse8(a_.values[i]); |
| 134 |
#else |
| 135 |
r_.values[i] = HEDLEY_STATIC_CAST(uint8_t, (((a_.values[i] * UINT64_C(0x80200802)) & UINT64_C(0x0884422110)) * UINT64_C(0x0101010101)) >> 32); |
| 136 |
#endif |
| 137 |
} |
| 138 |
#endif |
| 139 |
|
| 140 |
return simde_uint8x16_from_private(r_); |
| 141 |
#endif |
| 142 |
} |
| 143 |
#if defined(SIMDE_ARM_NEON_A64V8_ENABLE_NATIVE_ALIASES) |
| 144 |
#undef vrbitq_u8 |
| 145 |
#define vrbitq_u8(a) simde_vrbitq_u8(a) |
| 146 |
#endif |
| 147 |
|
| 148 |
SIMDE_FUNCTION_ATTRIBUTES |
| 149 |
simde_int8x16_t |
| 150 |
simde_vrbitq_s8(simde_int8x16_t a) { |
| 151 |
#if defined(SIMDE_ARM_NEON_A64V8_NATIVE) |
| 152 |
return vrbitq_s8(a); |
| 153 |
#else |
| 154 |
return simde_vreinterpretq_s8_u8(simde_vrbitq_u8(simde_vreinterpretq_u8_s8(a))); |
| 155 |
#endif |
| 156 |
} |
| 157 |
#if defined(SIMDE_ARM_NEON_A64V8_ENABLE_NATIVE_ALIASES) |
| 158 |
#undef vrbitq_s8 |
| 159 |
#define vrbitq_s8(a) simde_vrbitq_s8(a) |
| 160 |
#endif |
| 161 |
|
| 162 |
SIMDE_END_DECLS_ |
| 163 |
HEDLEY_DIAGNOSTIC_POP |
| 164 |
|
| 165 |
#endif /* !defined(SIMDE_ARM_NEON_RBIT_H) */ |