Botan 3.9.0
Crypto and TLS for C&
simd_8x64.h
Go to the documentation of this file.
1/*
2* (C) 2022,2025 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#ifndef BOTAN_SIMD_8X64_H_
8#define BOTAN_SIMD_8X64_H_
9
10#include <botan/compiler.h>
11#include <botan/types.h>
12#include <botan/internal/isa_extn.h>
13#include <botan/internal/target_info.h>
14#include <immintrin.h>
15
16namespace Botan {
17
18class SIMD_8x64 final {
19 public:
20 SIMD_8x64& operator=(const SIMD_8x64& other) = default;
21 SIMD_8x64(const SIMD_8x64& other) = default;
22
23 SIMD_8x64& operator=(SIMD_8x64&& other) = default;
24 SIMD_8x64(SIMD_8x64&& other) = default;
25
26 ~SIMD_8x64() = default;
27
28 // zero initialized
29 BOTAN_FN_ISA_SIMD_8X64 SIMD_8x64() : m_simd(_mm512_setzero_si512()) {}
30
31 // Load two halves at different addresses
32 static BOTAN_FN_ISA_SIMD_8X64 SIMD_8x64 load_le4(const void* in0,
33 const void* in1,
34 const void* in2,
35 const void* in3) {
36 auto r = _mm512_setzero_si512();
37 r = _mm512_inserti32x4(r, _mm_loadu_si128(reinterpret_cast<const __m128i*>(in0)), 3);
38 r = _mm512_inserti32x4(r, _mm_loadu_si128(reinterpret_cast<const __m128i*>(in1)), 2);
39 r = _mm512_inserti32x4(r, _mm_loadu_si128(reinterpret_cast<const __m128i*>(in2)), 1);
40 r = _mm512_inserti32x4(r, _mm_loadu_si128(reinterpret_cast<const __m128i*>(in3)), 0);
41 return SIMD_8x64(r);
42 }
43
44 static BOTAN_FN_ISA_SIMD_8X64 SIMD_8x64 load_be4(const void* in0,
45 const void* in1,
46 const void* in2,
47 const void* in3) {
48 return SIMD_8x64::load_le4(in0, in1, in2, in3).bswap();
49 }
50
51 static BOTAN_FN_ISA_SIMD_8X64 SIMD_8x64 load_le(const void* in) {
52 return SIMD_8x64(_mm512_loadu_si512(reinterpret_cast<const __m512i*>(in)));
53 }
54
55 static BOTAN_FN_ISA_SIMD_8X64 SIMD_8x64 load_be(const void* in) { return SIMD_8x64::load_le(in).bswap(); }
56
57 SIMD_8x64 BOTAN_FN_ISA_SIMD_8X64 bswap() const {
58 // clang-format off
59 const auto idx = _mm512_set_epi8(
60 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7,
61 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7);
62 // clang-format on
63
64 return SIMD_8x64(_mm512_shuffle_epi8(m_simd, idx));
65 }
66
67 void store_le(uint64_t out[8]) const { this->store_le(reinterpret_cast<uint8_t*>(out)); }
68
69 BOTAN_FN_ISA_SIMD_8X64 void store_le(uint8_t out[]) const {
70 _mm512_storeu_si512(reinterpret_cast<__m512i*>(out), m_simd);
71 }
72
73 BOTAN_FN_ISA_SIMD_8X64 void store_le4(void* out0, void* out1, void* out2, void* out3) {
74 _mm_storeu_si128(reinterpret_cast<__m128i*>(out0), _mm512_extracti32x4_epi32(m_simd, 3));
75 _mm_storeu_si128(reinterpret_cast<__m128i*>(out1), _mm512_extracti32x4_epi32(m_simd, 2));
76 _mm_storeu_si128(reinterpret_cast<__m128i*>(out2), _mm512_extracti32x4_epi32(m_simd, 1));
77 _mm_storeu_si128(reinterpret_cast<__m128i*>(out3), _mm512_extracti32x4_epi32(m_simd, 0));
78 }
79
80 SIMD_8x64 operator+(const SIMD_8x64& other) const {
81 SIMD_8x64 retval(*this);
82 retval += other;
83 return retval;
84 }
85
86 SIMD_8x64 operator^(const SIMD_8x64& other) const {
87 SIMD_8x64 retval(*this);
88 retval ^= other;
89 return retval;
90 }
91
92 BOTAN_FN_ISA_SIMD_8X64 void operator+=(const SIMD_8x64& other) {
93 m_simd = _mm512_add_epi64(m_simd, other.m_simd);
94 }
95
96 BOTAN_FN_ISA_SIMD_8X64 void operator^=(const SIMD_8x64& other) {
97 m_simd = _mm512_xor_si512(m_simd, other.m_simd);
98 }
99
100 template <size_t ROT>
101 BOTAN_FN_ISA_SIMD_8X64 SIMD_8x64 rotr() const
102 requires(ROT > 0 && ROT < 64)
103 {
104 return SIMD_8x64(_mm512_ror_epi64(m_simd, ROT));
105 }
106
107 template <size_t ROT>
108 SIMD_8x64 rotl() const {
109 return this->rotr<64 - ROT>();
110 }
111
112 template <int SHIFT>
113 SIMD_8x64 BOTAN_FN_ISA_SIMD_8X64 shr() const noexcept {
114 return SIMD_8x64(_mm512_srli_epi64(m_simd, SHIFT));
115 }
116
117 static SIMD_8x64 BOTAN_FN_ISA_SIMD_8X64 alignr8(const SIMD_8x64& a, const SIMD_8x64& b) {
118 return SIMD_8x64(_mm512_alignr_epi8(a.m_simd, b.m_simd, 8));
119 }
120
121 explicit BOTAN_FN_ISA_SIMD_8X64 SIMD_8x64(__m512i x) : m_simd(x) {}
122
123 private:
124 __m512i m_simd;
125};
126
127} // namespace Botan
128
129#endif
BOTAN_FN_ISA_SIMD_8X64 void operator+=(const SIMD_8x64 &other)
Definition simd_8x64.h:92
BOTAN_FN_ISA_SIMD_8X64 void store_le(uint8_t out[]) const
Definition simd_8x64.h:69
SIMD_8x64 BOTAN_FN_ISA_SIMD_8X64 bswap() const
Definition simd_8x64.h:57
static BOTAN_FN_ISA_SIMD_8X64 SIMD_8x64 load_le4(const void *in0, const void *in1, const void *in2, const void *in3)
Definition simd_8x64.h:32
SIMD_8x64(SIMD_8x64 &&other)=default
void store_le(uint64_t out[8]) const
Definition simd_8x64.h:67
~SIMD_8x64()=default
SIMD_8x64 operator^(const SIMD_8x64 &other) const
Definition simd_8x64.h:86
static BOTAN_FN_ISA_SIMD_8X64 SIMD_8x64 load_be4(const void *in0, const void *in1, const void *in2, const void *in3)
Definition simd_8x64.h:44
SIMD_8x64(const SIMD_8x64 &other)=default
SIMD_8x64 rotl() const
Definition simd_8x64.h:108
static BOTAN_FN_ISA_SIMD_8X64 SIMD_8x64 load_le(const void *in)
Definition simd_8x64.h:51
static SIMD_8x64 BOTAN_FN_ISA_SIMD_8X64 alignr8(const SIMD_8x64 &a, const SIMD_8x64 &b)
Definition simd_8x64.h:117
BOTAN_FN_ISA_SIMD_8X64 SIMD_8x64()
Definition simd_8x64.h:29
BOTAN_FN_ISA_SIMD_8X64 SIMD_8x64 rotr() const
Definition simd_8x64.h:101
BOTAN_FN_ISA_SIMD_8X64 SIMD_8x64(__m512i x)
Definition simd_8x64.h:121
BOTAN_FN_ISA_SIMD_8X64 void store_le4(void *out0, void *out1, void *out2, void *out3)
Definition simd_8x64.h:73
BOTAN_FN_ISA_SIMD_8X64 void operator^=(const SIMD_8x64 &other)
Definition simd_8x64.h:96
SIMD_8x64 & operator=(SIMD_8x64 &&other)=default
static BOTAN_FN_ISA_SIMD_8X64 SIMD_8x64 load_be(const void *in)
Definition simd_8x64.h:55
SIMD_8x64 operator+(const SIMD_8x64 &other) const
Definition simd_8x64.h:80
SIMD_8x64 BOTAN_FN_ISA_SIMD_8X64 shr() const noexcept
Definition simd_8x64.h:113
SIMD_8x64 & operator=(const SIMD_8x64 &other)=default