8 #ifndef BOTAN_SIMD_32_H_
9 #define BOTAN_SIMD_32_H_
11 #include <botan/types.h>
13 #if defined(BOTAN_TARGET_SUPPORTS_SSE2)
14 #include <emmintrin.h>
15 #define BOTAN_SIMD_USE_SSE2
17 #elif defined(BOTAN_TARGET_SUPPORTS_ALTIVEC)
18 #include <botan/bswap.h>
19 #include <botan/loadstor.h>
23 #define BOTAN_SIMD_USE_ALTIVEC
25 #elif defined(BOTAN_TARGET_SUPPORTS_NEON)
26 #include <botan/cpuid.h>
28 #define BOTAN_SIMD_USE_NEON
31 #error "No SIMD instruction set enabled"
34 #if defined(BOTAN_SIMD_USE_SSE2)
35 #define BOTAN_SIMD_ISA "sse2"
36 #define BOTAN_VPERM_ISA "ssse3"
37 #define BOTAN_CLMUL_ISA "pclmul"
38 #elif defined(BOTAN_SIMD_USE_NEON)
39 #if defined(BOTAN_TARGET_ARCH_IS_ARM64)
40 #define BOTAN_SIMD_ISA "+simd"
41 #define BOTAN_CLMUL_ISA "+crypto"
43 #define BOTAN_SIMD_ISA "fpu=neon"
45 #define BOTAN_VPERM_ISA BOTAN_SIMD_ISA
46 #elif defined(BOTAN_SIMD_USE_ALTIVEC)
47 #define BOTAN_SIMD_ISA "altivec"
48 #define BOTAN_VPERM_ISA "altivec"
53 #if defined(BOTAN_SIMD_USE_SSE2)
54 typedef __m128i native_simd_type;
55 #elif defined(BOTAN_SIMD_USE_ALTIVEC)
56 typedef __vector
unsigned int native_simd_type;
57 #elif defined(BOTAN_SIMD_USE_NEON)
58 typedef uint32x4_t native_simd_type;
86 #if defined(BOTAN_SIMD_USE_SSE2)
87 m_simd = _mm_setzero_si128();
88 #elif defined(BOTAN_SIMD_USE_ALTIVEC)
89 m_simd = vec_splat_u32(0);
90 #elif defined(BOTAN_SIMD_USE_NEON)
91 m_simd = vdupq_n_u32(0);
100 #if defined(BOTAN_SIMD_USE_SSE2)
101 m_simd = _mm_loadu_si128(reinterpret_cast<const __m128i*>(B));
102 #elif defined(BOTAN_SIMD_USE_ALTIVEC)
103 __vector
unsigned int val = { B[0], B[1], B[2], B[3]};
105 #elif defined(BOTAN_SIMD_USE_NEON)
106 m_simd = vld1q_u32(B);
113 SIMD_4x32(uint32_t B0, uint32_t B1, uint32_t B2, uint32_t B3)
115 #if defined(BOTAN_SIMD_USE_SSE2)
116 m_simd = _mm_set_epi32(B3, B2, B1, B0);
117 #elif defined(BOTAN_SIMD_USE_ALTIVEC)
118 __vector
unsigned int val = {B0, B1, B2, B3};
120 #elif defined(BOTAN_SIMD_USE_NEON)
122 const uint32_t B[4] = { B0, B1, B2, B3 };
123 m_simd = vld1q_u32(B);
132 #if defined(BOTAN_SIMD_USE_SSE2)
134 #elif defined(BOTAN_SIMD_USE_NEON)
146 #if defined(BOTAN_SIMD_USE_SSE2)
148 #elif defined(BOTAN_SIMD_USE_NEON)
149 return SIMD_4x32(vreinterpretq_u32_u8(vdupq_n_u8(B)));
161 #if defined(BOTAN_SIMD_USE_SSE2)
162 return SIMD_4x32(_mm_loadu_si128(reinterpret_cast<const __m128i*>(in)));
163 #elif defined(BOTAN_SIMD_USE_ALTIVEC)
167 #elif defined(BOTAN_SIMD_USE_NEON)
168 SIMD_4x32 l(vld1q_u32(static_cast<const uint32_t*>(in)));
178 #if defined(BOTAN_SIMD_USE_SSE2)
181 #elif defined(BOTAN_SIMD_USE_ALTIVEC)
186 #elif defined(BOTAN_SIMD_USE_NEON)
187 SIMD_4x32 l(vld1q_u32(static_cast<const uint32_t*>(in)));
194 this->
store_le(reinterpret_cast<uint8_t*>(out));
199 this->
store_le(reinterpret_cast<uint8_t*>(out));
207 #if defined(BOTAN_SIMD_USE_SSE2)
209 _mm_storeu_si128(reinterpret_cast<__m128i*>(out),
raw());
211 #elif defined(BOTAN_SIMD_USE_ALTIVEC)
214 __vector
unsigned int V;
220 #elif defined(BOTAN_SIMD_USE_NEON)
223 vst1q_u8(out, vreinterpretq_u8_u32(m_simd));
227 vst1q_u8(out, vreinterpretq_u8_u32(
bswap().m_simd));
237 #if defined(BOTAN_SIMD_USE_SSE2)
241 #elif defined(BOTAN_SIMD_USE_ALTIVEC)
244 __vector
unsigned int V;
250 #elif defined(BOTAN_SIMD_USE_NEON)
253 vst1q_u8(out, vreinterpretq_u8_u32(
bswap().m_simd));
257 vst1q_u8(out, vreinterpretq_u8_u32(m_simd));
266 template<
size_t ROT1,
size_t ROT2,
size_t ROT3>
269 const SIMD_4x32 rot1 = this->rotr<ROT1>();
270 const SIMD_4x32 rot2 = this->rotr<ROT2>();
271 const SIMD_4x32 rot3 = this->rotr<ROT3>();
272 return (rot1 ^ rot2 ^ rot3);
281 static_assert(ROT > 0 && ROT < 32,
"Invalid rotation constant");
283 #if defined(BOTAN_SIMD_USE_SSE2)
285 return SIMD_4x32(_mm_or_si128(_mm_slli_epi32(m_simd, static_cast<int>(ROT)),
286 _mm_srli_epi32(m_simd, static_cast<int>(32-ROT))));
288 #elif defined(BOTAN_SIMD_USE_ALTIVEC)
290 const unsigned int r =
static_cast<unsigned int>(ROT);
291 __vector
unsigned int rot = {r, r, r, r};
294 #elif defined(BOTAN_SIMD_USE_NEON)
296 #if defined(BOTAN_TARGET_ARCH_IS_ARM64)
300 const uint8_t maskb[16] = { 3,0,1,2, 7,4,5,6, 11,8,9,10, 15,12,13,14 };
301 const uint8x16_t mask = vld1q_u8(maskb);
302 return SIMD_4x32(vreinterpretq_u32_u8(vqtbl1q_u8(vreinterpretq_u8_u32(m_simd), mask)));
306 return SIMD_4x32(vreinterpretq_u32_u16(vrev32q_u16(vreinterpretq_u16_u32(m_simd))));
309 return SIMD_4x32(vorrq_u32(vshlq_n_u32(m_simd, static_cast<int>(ROT)),
310 vshrq_n_u32(m_simd, static_cast<int>(32-ROT))));
320 return this->
rotl<32-ROT>();
375 #if defined(BOTAN_SIMD_USE_SSE2)
376 m_simd = _mm_add_epi32(m_simd, other.m_simd);
377 #elif defined(BOTAN_SIMD_USE_ALTIVEC)
378 m_simd = vec_add(m_simd, other.m_simd);
379 #elif defined(BOTAN_SIMD_USE_NEON)
380 m_simd = vaddq_u32(m_simd, other.m_simd);
386 #if defined(BOTAN_SIMD_USE_SSE2)
387 m_simd = _mm_sub_epi32(m_simd, other.m_simd);
388 #elif defined(BOTAN_SIMD_USE_ALTIVEC)
389 m_simd = vec_sub(m_simd, other.m_simd);
390 #elif defined(BOTAN_SIMD_USE_NEON)
391 m_simd = vsubq_u32(m_simd, other.m_simd);
397 #if defined(BOTAN_SIMD_USE_SSE2)
398 m_simd = _mm_xor_si128(m_simd, other.m_simd);
400 #elif defined(BOTAN_SIMD_USE_ALTIVEC)
401 m_simd = vec_xor(m_simd, other.m_simd);
402 #elif defined(BOTAN_SIMD_USE_NEON)
403 m_simd = veorq_u32(m_simd, other.m_simd);
409 #if defined(BOTAN_SIMD_USE_SSE2)
410 m_simd = _mm_or_si128(m_simd, other.m_simd);
411 #elif defined(BOTAN_SIMD_USE_ALTIVEC)
412 m_simd = vec_or(m_simd, other.m_simd);
413 #elif defined(BOTAN_SIMD_USE_NEON)
414 m_simd = vorrq_u32(m_simd, other.m_simd);
420 #if defined(BOTAN_SIMD_USE_SSE2)
421 m_simd = _mm_and_si128(m_simd, other.m_simd);
422 #elif defined(BOTAN_SIMD_USE_ALTIVEC)
423 m_simd = vec_and(m_simd, other.m_simd);
424 #elif defined(BOTAN_SIMD_USE_NEON)
425 m_simd = vandq_u32(m_simd, other.m_simd);
432 static_assert(SHIFT > 0 && SHIFT <= 31,
"Invalid shift count");
434 #if defined(BOTAN_SIMD_USE_SSE2)
435 return SIMD_4x32(_mm_slli_epi32(m_simd, SHIFT));
437 #elif defined(BOTAN_SIMD_USE_ALTIVEC)
438 const unsigned int s =
static_cast<unsigned int>(SHIFT);
439 const __vector
unsigned int shifts = {s, s, s, s};
440 return SIMD_4x32(vec_sl(m_simd, shifts));
441 #elif defined(BOTAN_SIMD_USE_NEON)
442 return SIMD_4x32(vshlq_n_u32(m_simd, SHIFT));
448 #if defined(BOTAN_SIMD_USE_SSE2)
449 return SIMD_4x32(_mm_srli_epi32(m_simd, SHIFT));
451 #elif defined(BOTAN_SIMD_USE_ALTIVEC)
452 const unsigned int s =
static_cast<unsigned int>(SHIFT);
453 const __vector
unsigned int shifts = {s, s, s, s};
454 return SIMD_4x32(vec_sr(m_simd, shifts));
455 #elif defined(BOTAN_SIMD_USE_NEON)
456 return SIMD_4x32(vshrq_n_u32(m_simd, SHIFT));
462 #if defined(BOTAN_SIMD_USE_SSE2)
463 return SIMD_4x32(_mm_xor_si128(m_simd, _mm_set1_epi32(0xFFFFFFFF)));
464 #elif defined(BOTAN_SIMD_USE_ALTIVEC)
465 return SIMD_4x32(vec_nor(m_simd, m_simd));
466 #elif defined(BOTAN_SIMD_USE_NEON)
474 #if defined(BOTAN_SIMD_USE_SSE2)
475 return SIMD_4x32(_mm_andnot_si128(m_simd, other.m_simd));
476 #elif defined(BOTAN_SIMD_USE_ALTIVEC)
481 return SIMD_4x32(vec_andc(other.m_simd, m_simd));
482 #elif defined(BOTAN_SIMD_USE_NEON)
484 return SIMD_4x32(vbicq_u32(other.m_simd, m_simd));
493 #if defined(BOTAN_SIMD_USE_SSE2)
496 T = _mm_shufflehi_epi16(T, _MM_SHUFFLE(2, 3, 0, 1));
497 T = _mm_shufflelo_epi16(T, _MM_SHUFFLE(2, 3, 0, 1));
498 return SIMD_4x32(_mm_or_si128(_mm_srli_epi16(T, 8), _mm_slli_epi16(T, 8)));
500 #elif defined(BOTAN_SIMD_USE_ALTIVEC)
503 __vector
unsigned int V;
509 return SIMD_4x32(vec.R[0], vec.R[1], vec.R[2], vec.R[3]);
511 #elif defined(BOTAN_SIMD_USE_NEON)
512 return SIMD_4x32(vreinterpretq_u32_u8(vrev32q_u8(vreinterpretq_u8_u32(m_simd))));
519 static_assert(I <= 3,
"Invalid shift count");
521 #if defined(BOTAN_SIMD_USE_SSE2)
523 #elif defined(BOTAN_SIMD_USE_NEON)
524 return SIMD_4x32(vextq_u32(vdupq_n_u32(0),
raw(), 4-I));
525 #elif defined(BOTAN_SIMD_USE_ALTIVEC)
526 const __vector
unsigned int zero = vec_splat_u32(0);
528 const __vector
unsigned char shuf[3] = {
529 { 16, 17, 18, 19, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 },
530 { 16, 17, 18, 19, 20, 21, 22, 23, 0, 1, 2, 3, 4, 5, 6, 7 },
531 { 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 0, 1, 2, 3 },
541 static_assert(I <= 3,
"Invalid shift count");
543 #if defined(BOTAN_SIMD_USE_SSE2)
545 #elif defined(BOTAN_SIMD_USE_NEON)
547 #elif defined(BOTAN_SIMD_USE_ALTIVEC)
548 const __vector
unsigned int zero = vec_splat_u32(0);
550 const __vector
unsigned char shuf[3] = {
551 { 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 },
552 { 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 },
553 { 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27 },
566 #if defined(BOTAN_SIMD_USE_SSE2)
567 const __m128i T0 = _mm_unpacklo_epi32(B0.m_simd, B1.m_simd);
568 const __m128i T1 = _mm_unpacklo_epi32(B2.m_simd, B3.m_simd);
569 const __m128i T2 = _mm_unpackhi_epi32(B0.m_simd, B1.m_simd);
570 const __m128i T3 = _mm_unpackhi_epi32(B2.m_simd, B3.m_simd);
572 B0.m_simd = _mm_unpacklo_epi64(T0, T1);
573 B1.m_simd = _mm_unpackhi_epi64(T0, T1);
574 B2.m_simd = _mm_unpacklo_epi64(T2, T3);
575 B3.m_simd = _mm_unpackhi_epi64(T2, T3);
576 #elif defined(BOTAN_SIMD_USE_ALTIVEC)
577 const __vector
unsigned int T0 = vec_mergeh(B0.m_simd, B2.m_simd);
578 const __vector
unsigned int T1 = vec_mergeh(B1.m_simd, B3.m_simd);
579 const __vector
unsigned int T2 = vec_mergel(B0.m_simd, B2.m_simd);
580 const __vector
unsigned int T3 = vec_mergel(B1.m_simd, B3.m_simd);
582 B0.m_simd = vec_mergeh(T0, T1);
583 B1.m_simd = vec_mergel(T0, T1);
584 B2.m_simd = vec_mergeh(T2, T3);
585 B3.m_simd = vec_mergel(T2, T3);
587 #elif defined(BOTAN_SIMD_USE_NEON) && defined(BOTAN_TARGET_ARCH_IS_ARM32)
588 const uint32x4x2_t T0 = vzipq_u32(B0.m_simd, B2.m_simd);
589 const uint32x4x2_t T1 = vzipq_u32(B1.m_simd, B3.m_simd);
590 const uint32x4x2_t O0 = vzipq_u32(T0.val[0], T1.val[0]);
591 const uint32x4x2_t O1 = vzipq_u32(T0.val[1], T1.val[1]);
593 B0.m_simd = O0.val[0];
594 B1.m_simd = O0.val[1];
595 B2.m_simd = O1.val[0];
596 B3.m_simd = O1.val[1];
598 #elif defined(BOTAN_SIMD_USE_NEON) && defined(BOTAN_TARGET_ARCH_IS_ARM64)
599 const uint32x4_t T0 = vzip1q_u32(B0.m_simd, B2.m_simd);
600 const uint32x4_t T2 = vzip2q_u32(B0.m_simd, B2.m_simd);
601 const uint32x4_t T1 = vzip1q_u32(B1.m_simd, B3.m_simd);
602 const uint32x4_t T3 = vzip2q_u32(B1.m_simd, B3.m_simd);
604 B0.m_simd = vzip1q_u32(T0, T1);
605 B1.m_simd = vzip2q_u32(T0, T1);
606 B2.m_simd = vzip1q_u32(T2, T3);
607 B3.m_simd = vzip2q_u32(T2, T3);
615 native_simd_type m_simd;
SIMD_4x32(const uint32_t B[4])
SIMD_4x32 operator~() const
SIMD_4x32 operator&(const SIMD_4x32 &other) const
void store_be(uint16_t in, uint8_t out[2])
SIMD_4x32 shift_elems_left() const
#define BOTAN_IF_CONSTEXPR
static SIMD_4x32 load_le(const void *in)
int(* final)(unsigned char *, CTX *)
void store_le(uint64_t out[2]) const
native_simd_type raw() const BOTAN_FUNC_ISA(BOTAN_SIMD_ISA)
void operator^=(const SIMD_4x32 &other)
SIMD_4x32 operator^(const SIMD_4x32 &other) const
static void transpose(SIMD_4x32 &B0, SIMD_4x32 &B1, SIMD_4x32 &B2, SIMD_4x32 &B3)
constexpr uint32_t make_uint32(uint8_t i0, uint8_t i1, uint8_t i2, uint8_t i3)
void operator+=(const SIMD_4x32 &other)
T load_be(const uint8_t in[], size_t off)
SIMD_4x32 andc(const SIMD_4x32 &other) const
static SIMD_4x32 load_be(const void *in)
void store_le(uint8_t out[]) const
SIMD_4x32 operator-(const SIMD_4x32 &other) const
#define BOTAN_FUNC_ISA(isa)
T load_le(const uint8_t in[], size_t off)
static SIMD_4x32 splat_u8(uint8_t B)
static bool is_little_endian()
static SIMD_4x32 splat(uint32_t B)
SIMD_4x32 operator|(const SIMD_4x32 &other) const
SIMD_4x32(native_simd_type x)
SIMD_4x32 operator+(const SIMD_4x32 &other) const
void operator|=(const SIMD_4x32 &other)
void operator-=(const SIMD_4x32 &other)
SIMD_4x32 & operator=(const SIMD_4x32 &other)=default
SIMD_4x32(uint32_t B0, uint32_t B1, uint32_t B2, uint32_t B3)
static bool is_big_endian()
void operator&=(const SIMD_4x32 &other)
SIMD_4x32 shift_elems_right() const
void store_be(uint8_t out[]) const
void store_le(uint32_t out[4]) const
void store_le(uint16_t in, uint8_t out[2])