Botan 3.11.0
Crypto and TLS for C&
simd_2x64.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_2X64_H_
8#define BOTAN_SIMD_2X64_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 <span>
15
16// TODO: extend this to support NEON / AltiVec / LSX
17
18#if defined(BOTAN_TARGET_ARCH_SUPPORTS_SSSE3)
19 #include <emmintrin.h>
20 #include <tmmintrin.h>
21 #define BOTAN_SIMD_USE_SSSE3
22#elif defined(BOTAN_TARGET_ARCH_SUPPORTS_SIMD128)
23 #include <wasm_simd128.h>
24 #define BOTAN_SIMD_USE_SIMD128
25#endif
26
27namespace Botan {
28
29// NOLINTBEGIN(portability-simd-intrinsics)
30
31class SIMD_2x64 final {
32 public:
33#if defined(BOTAN_SIMD_USE_SSSE3)
34 using native_simd_type = __m128i;
35#elif defined(BOTAN_SIMD_USE_SIMD128)
36 using native_simd_type = v128_t;
37#endif
38
39 SIMD_2x64& operator=(const SIMD_2x64& other) = default;
40 SIMD_2x64(const SIMD_2x64& other) = default;
41
42 SIMD_2x64& operator=(SIMD_2x64&& other) = default;
43 SIMD_2x64(SIMD_2x64&& other) = default;
44
45 ~SIMD_2x64() = default;
46
47 // zero initialized
48 BOTAN_FN_ISA_SIMD_2X64 SIMD_2x64() :
49#if defined(BOTAN_SIMD_USE_SSSE3)
50 m_simd(_mm_setzero_si128())
51#elif defined(BOTAN_SIMD_USE_SIMD128)
52 m_simd(wasm_u64x2_const_splat(0))
53#endif
54 {
55 }
56
57 static SIMD_2x64 BOTAN_FN_ISA_SIMD_2X64 all_ones() {
58#if defined(BOTAN_SIMD_USE_SSSE3)
59 return SIMD_2x64(_mm_set1_epi8(-1));
60#elif defined(BOTAN_SIMD_USE_SIMD128)
61 return SIMD_2x64(wasm_i8x16_splat(0xFF));
62#endif
63 }
64
65 BOTAN_FN_ISA_SIMD_2X64 SIMD_2x64(uint64_t low, uint64_t high) :
66#if defined(BOTAN_SIMD_USE_SSSE3)
67 m_simd(_mm_set_epi64x(high, low))
68#elif defined(BOTAN_SIMD_USE_SIMD128)
69 m_simd(wasm_u64x2_make(low, high))
70#endif
71 {
72 }
73
74 static SIMD_2x64 BOTAN_FN_ISA_SIMD_2X64 load_le(const void* in) {
75#if defined(BOTAN_SIMD_USE_SSSE3)
76 return SIMD_2x64(_mm_loadu_si128(reinterpret_cast<const __m128i*>(in)));
77#elif defined(BOTAN_SIMD_USE_SIMD128)
78 return SIMD_2x64(wasm_v128_load(in));
79#endif
80 }
81
82 static SIMD_2x64 BOTAN_FN_ISA_SIMD_2X64 load_be(const void* in) { return SIMD_2x64::load_le(in).bswap(); }
83
84 static SIMD_2x64 BOTAN_FN_ISA_SIMD_2X64 load_le(std::span<const uint8_t, 16> in) {
85 return SIMD_2x64::load_le(in.data());
86 }
87
88 static SIMD_2x64 BOTAN_FN_ISA_SIMD_2X64 load_be(std::span<const uint8_t, 16> in) {
89 return SIMD_2x64::load_be(in.data());
90 }
91
92 SIMD_2x64 BOTAN_FN_ISA_SIMD_2X64 bswap() const {
93#if defined(BOTAN_SIMD_USE_SSSE3)
94 const auto idx = _mm_set_epi8(8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7);
95 return SIMD_2x64(_mm_shuffle_epi8(m_simd, idx));
96#elif defined(BOTAN_SIMD_USE_SIMD128)
97 return SIMD_2x64(wasm_i8x16_shuffle(m_simd, m_simd, 7, 6, 5, 4, 3, 2, 1, 0, 15, 14, 13, 12, 11, 10, 9, 8));
98#endif
99 }
100
101 SIMD_2x64 BOTAN_FN_ISA_SIMD_2X64 swap_lanes() const {
102#if defined(BOTAN_SIMD_USE_SSSE3)
103 return SIMD_2x64(_mm_shuffle_epi32(m_simd, _MM_SHUFFLE(1, 0, 3, 2)));
104#elif defined(BOTAN_SIMD_USE_SIMD128)
105 return SIMD_2x64(wasm_i64x2_shuffle(m_simd, m_simd, 1, 0));
106#endif
107 }
108
109 SIMD_2x64 BOTAN_FN_ISA_SIMD_2X64 reverse_all_bytes() const {
110#if defined(BOTAN_SIMD_USE_SSSE3)
111 const auto idx = _mm_set_epi8(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
112 return SIMD_2x64(_mm_shuffle_epi8(m_simd, idx));
113#elif defined(BOTAN_SIMD_USE_SIMD128)
114 return SIMD_2x64(wasm_i8x16_shuffle(m_simd, m_simd, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0));
115#endif
116 }
117
118 void BOTAN_FN_ISA_SIMD_2X64 store_le(uint64_t out[2]) const { this->store_le(reinterpret_cast<uint8_t*>(out)); }
119
120 void BOTAN_FN_ISA_SIMD_2X64 store_le(uint8_t out[]) const {
121#if defined(BOTAN_SIMD_USE_SSSE3)
122 _mm_storeu_si128(reinterpret_cast<__m128i*>(out), m_simd);
123#elif defined(BOTAN_SIMD_USE_SIMD128)
124 wasm_v128_store(out, m_simd);
125#endif
126 }
127
128 void BOTAN_FN_ISA_SIMD_2X64 store_be(uint64_t out[2]) const { this->store_be(reinterpret_cast<uint8_t*>(out)); }
129
130 void BOTAN_FN_ISA_SIMD_2X64 store_be(uint8_t out[]) const { bswap().store_le(out); }
131
132 void BOTAN_FN_ISA_SIMD_2X64 store_be(std::span<uint8_t, 16> out) const { this->store_be(out.data()); }
133
134 void BOTAN_FN_ISA_SIMD_2X64 store_le(std::span<uint8_t, 16> out) const { this->store_le(out.data()); }
135
136 SIMD_2x64 BOTAN_FN_ISA_SIMD_2X64 operator+(const SIMD_2x64& other) const {
137 SIMD_2x64 retval(*this);
138 retval += other;
139 return retval;
140 }
141
142 SIMD_2x64 BOTAN_FN_ISA_SIMD_2X64 operator^(const SIMD_2x64& other) const {
143 SIMD_2x64 retval(*this);
144 retval ^= other;
145 return retval;
146 }
147
148 void BOTAN_FN_ISA_SIMD_2X64 operator+=(const SIMD_2x64& other) {
149#if defined(BOTAN_SIMD_USE_SSSE3)
150 m_simd = _mm_add_epi64(m_simd, other.m_simd);
151#elif defined(BOTAN_SIMD_USE_SIMD128)
152 m_simd = wasm_i64x2_add(m_simd, other.m_simd);
153#endif
154 }
155
156 void BOTAN_FN_ISA_SIMD_2X64 operator^=(const SIMD_2x64& other) {
157#if defined(BOTAN_SIMD_USE_SSSE3)
158 m_simd = _mm_xor_si128(m_simd, other.m_simd);
159#elif defined(BOTAN_SIMD_USE_SIMD128)
160 m_simd = wasm_v128_xor(m_simd, other.m_simd);
161#endif
162 }
163
164 SIMD_2x64 BOTAN_FN_ISA_SIMD_2X64 andc(const SIMD_2x64& other) const noexcept {
165#if defined(BOTAN_SIMD_USE_SSSE3)
166 return SIMD_2x64(_mm_andnot_si128(m_simd, other.m_simd));
167#elif defined(BOTAN_SIMD_USE_SIMD128)
168 // SIMD128 is a & ~b
169 return SIMD_2x64(wasm_v128_andnot(other.m_simd, m_simd));
170#endif
171 }
172
173 template <size_t ROT>
174 BOTAN_FN_ISA_SIMD_2X64 SIMD_2x64 rotr() const
175 requires(ROT > 0 && ROT < 64)
176 {
177#if defined(BOTAN_SIMD_USE_SSSE3)
178 if constexpr(ROT == 8) {
179 auto tab = _mm_setr_epi8(1, 2, 3, 4, 5, 6, 7, 0, 9, 10, 11, 12, 13, 14, 15, 8);
180 return SIMD_2x64(_mm_shuffle_epi8(m_simd, tab));
181 } else if constexpr(ROT == 16) {
182 auto tab = _mm_setr_epi8(2, 3, 4, 5, 6, 7, 0, 1, 10, 11, 12, 13, 14, 15, 8, 9);
183 return SIMD_2x64(_mm_shuffle_epi8(m_simd, tab));
184 } else if constexpr(ROT == 24) {
185 auto tab = _mm_setr_epi8(3, 4, 5, 6, 7, 0, 1, 2, 11, 12, 13, 14, 15, 8, 9, 10);
186 return SIMD_2x64(_mm_shuffle_epi8(m_simd, tab));
187 } else if constexpr(ROT == 32) {
188 auto tab = _mm_setr_epi8(4, 5, 6, 7, 0, 1, 2, 3, 12, 13, 14, 15, 8, 9, 10, 11);
189 return SIMD_2x64(_mm_shuffle_epi8(m_simd, tab));
190 } else {
191 return SIMD_2x64(_mm_or_si128(_mm_srli_epi64(m_simd, static_cast<int>(ROT)),
192 _mm_slli_epi64(m_simd, static_cast<int>(64 - ROT))));
193 }
194#elif defined(BOTAN_SIMD_USE_SIMD128)
195 if constexpr(ROT == 8) {
196 return SIMD_2x64(wasm_i8x16_shuffle(m_simd, m_simd, 1, 2, 3, 4, 5, 6, 7, 0, 9, 10, 11, 12, 13, 14, 15, 8));
197 } else if constexpr(ROT == 16) {
198 return SIMD_2x64(wasm_i8x16_shuffle(m_simd, m_simd, 2, 3, 4, 5, 6, 7, 0, 1, 10, 11, 12, 13, 14, 15, 8, 9));
199 } else if constexpr(ROT == 24) {
200 return SIMD_2x64(wasm_i8x16_shuffle(m_simd, m_simd, 3, 4, 5, 6, 7, 0, 1, 2, 11, 12, 13, 14, 15, 8, 9, 10));
201 } else if constexpr(ROT == 32) {
202 return SIMD_2x64(wasm_i8x16_shuffle(m_simd, m_simd, 4, 5, 6, 7, 0, 1, 2, 3, 12, 13, 14, 15, 8, 9, 10, 11));
203 } else {
204 return SIMD_2x64(wasm_v128_or(wasm_u64x2_shr(m_simd, ROT), wasm_i64x2_shl(m_simd, 64 - ROT)));
205 }
206#endif
207 }
208
209 template <size_t ROT>
210 SIMD_2x64 BOTAN_FN_ISA_SIMD_2X64 rotl() const {
211 return this->rotr<64 - ROT>();
212 }
213
214 template <int SHIFT>
215 SIMD_2x64 BOTAN_FN_ISA_SIMD_2X64 shr() const noexcept {
216#if defined(BOTAN_SIMD_USE_SSSE3)
217 return SIMD_2x64(_mm_srli_epi64(m_simd, SHIFT));
218#elif defined(BOTAN_SIMD_USE_SIMD128)
219 return SIMD_2x64(wasm_u64x2_shr(m_simd, SHIFT));
220#endif
221 }
222
223 template <int SHIFT>
224 SIMD_2x64 BOTAN_FN_ISA_SIMD_2X64 shl() const noexcept {
225#if defined(BOTAN_SIMD_USE_SSSE3)
226 return SIMD_2x64(_mm_slli_epi64(m_simd, SHIFT));
227#elif defined(BOTAN_SIMD_USE_SIMD128)
228 return SIMD_2x64(wasm_i64x2_shl(m_simd, SHIFT));
229#endif
230 }
231
232 static SIMD_2x64 BOTAN_FN_ISA_SIMD_2X64 alignr8(const SIMD_2x64& a, const SIMD_2x64& b) {
233#if defined(BOTAN_SIMD_USE_SSSE3)
234 return SIMD_2x64(_mm_alignr_epi8(a.m_simd, b.m_simd, 8));
235#elif defined(BOTAN_SIMD_USE_SIMD128)
236 return SIMD_2x64(
237 wasm_i8x16_shuffle(b.m_simd, a.m_simd, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23));
238#endif
239 }
240
241 static SIMD_2x64 BOTAN_FN_ISA_SIMD_2X64 interleave_low(const SIMD_2x64& a, const SIMD_2x64& b) {
242#if defined(BOTAN_SIMD_USE_SSSE3)
243 return SIMD_2x64(_mm_unpacklo_epi64(a.m_simd, b.m_simd));
244#elif defined(BOTAN_SIMD_USE_SIMD128)
245 return SIMD_2x64(wasm_u64x2_extract_lane(a.m_simd, 0), wasm_u64x2_extract_lane(b.m_simd, 0));
246#endif
247 }
248
249 static SIMD_2x64 BOTAN_FN_ISA_SIMD_2X64 interleave_high(const SIMD_2x64& a, const SIMD_2x64& b) {
250#if defined(BOTAN_SIMD_USE_SSSE3)
251 return SIMD_2x64(_mm_unpackhi_epi64(a.m_simd, b.m_simd));
252#elif defined(BOTAN_SIMD_USE_SIMD128)
253 return SIMD_2x64(wasm_u64x2_extract_lane(a.m_simd, 1), wasm_u64x2_extract_lane(b.m_simd, 1));
254#endif
255 }
256
257 // Argon2 specific operation
258 static void BOTAN_FN_ISA_SIMD_2X64
260 auto T0 = SIMD_2x64::alignr8(B1, B0);
261 auto T1 = SIMD_2x64::alignr8(B0, B1);
262 B0 = T0;
263 B1 = T1;
264
265 T0 = C0;
266 C0 = C1;
267 C1 = T0;
268
269 T0 = SIMD_2x64::alignr8(D0, D1);
270 T1 = SIMD_2x64::alignr8(D1, D0);
271 D0 = T0;
272 D1 = T1;
273 }
274
275 // Argon2 specific operation
276 static void BOTAN_FN_ISA_SIMD_2X64
278 auto T0 = SIMD_2x64::alignr8(B0, B1);
279 auto T1 = SIMD_2x64::alignr8(B1, B0);
280 B0 = T0;
281 B1 = T1;
282
283 T0 = C0;
284 C0 = C1;
285 C1 = T0;
286
287 T0 = SIMD_2x64::alignr8(D1, D0);
288 T1 = SIMD_2x64::alignr8(D0, D1);
289 D0 = T0;
290 D1 = T1;
291 }
292
293 // Argon2 specific operation
294 static SIMD_2x64 BOTAN_FN_ISA_SIMD_2X64 mul2_32(SIMD_2x64 x, SIMD_2x64 y) {
295#if defined(BOTAN_SIMD_USE_SSSE3)
296 const __m128i m = _mm_mul_epu32(x.m_simd, y.m_simd);
297 return SIMD_2x64(_mm_add_epi64(m, m));
298#elif defined(BOTAN_SIMD_USE_SIMD128)
299 const auto m = wasm_u64x2_extmul_low_u32x4(wasm_i32x4_shuffle(x.m_simd, x.m_simd, 0, 2, 0, 2),
300 wasm_i32x4_shuffle(y.m_simd, y.m_simd, 0, 2, 0, 2));
301
302 return SIMD_2x64(wasm_i64x2_add(m, m));
303#endif
304 }
305
306 native_simd_type BOTAN_FN_ISA_SIMD_2X64 raw() const noexcept { return m_simd; }
307
308 explicit BOTAN_FN_ISA_SIMD_2X64 SIMD_2x64(native_simd_type x) : m_simd(x) {}
309
310 private:
311 native_simd_type m_simd;
312};
313
314// NOLINTEND(portability-simd-intrinsics)
315
316} // namespace Botan
317
318#endif
SIMD_2x64 BOTAN_FN_ISA_SIMD_2X64 shr() const noexcept
Definition simd_2x64.h:215
static SIMD_2x64 BOTAN_FN_ISA_SIMD_2X64 load_le(std::span< const uint8_t, 16 > in)
Definition simd_2x64.h:84
static SIMD_2x64 BOTAN_FN_ISA_SIMD_2X64 mul2_32(SIMD_2x64 x, SIMD_2x64 y)
Definition simd_2x64.h:294
SIMD_2x64 BOTAN_FN_ISA_SIMD_2X64 operator^(const SIMD_2x64 &other) const
Definition simd_2x64.h:142
BOTAN_FN_ISA_SIMD_2X64 SIMD_2x64()
Definition simd_2x64.h:48
native_simd_type BOTAN_FN_ISA_SIMD_2X64 raw() const noexcept
Definition simd_2x64.h:306
void BOTAN_FN_ISA_SIMD_2X64 store_be(uint8_t out[]) const
Definition simd_2x64.h:130
SIMD_2x64 & operator=(const SIMD_2x64 &other)=default
SIMD_2x64(const SIMD_2x64 &other)=default
static SIMD_2x64 BOTAN_FN_ISA_SIMD_2X64 load_be(std::span< const uint8_t, 16 > in)
Definition simd_2x64.h:88
void BOTAN_FN_ISA_SIMD_2X64 store_le(uint64_t out[2]) const
Definition simd_2x64.h:118
SIMD_2x64(SIMD_2x64 &&other)=default
static SIMD_2x64 BOTAN_FN_ISA_SIMD_2X64 load_be(const void *in)
Definition simd_2x64.h:82
BOTAN_FN_ISA_SIMD_2X64 SIMD_2x64(uint64_t low, uint64_t high)
Definition simd_2x64.h:65
SIMD_2x64 BOTAN_FN_ISA_SIMD_2X64 reverse_all_bytes() const
Definition simd_2x64.h:109
SIMD_2x64 BOTAN_FN_ISA_SIMD_2X64 shl() const noexcept
Definition simd_2x64.h:224
void BOTAN_FN_ISA_SIMD_2X64 store_be(std::span< uint8_t, 16 > out) const
Definition simd_2x64.h:132
static void BOTAN_FN_ISA_SIMD_2X64 untwist(SIMD_2x64 &B0, SIMD_2x64 &B1, SIMD_2x64 &C0, SIMD_2x64 &C1, SIMD_2x64 &D0, SIMD_2x64 &D1)
Definition simd_2x64.h:277
BOTAN_FN_ISA_SIMD_2X64 SIMD_2x64(native_simd_type x)
Definition simd_2x64.h:308
~SIMD_2x64()=default
SIMD_2x64 BOTAN_FN_ISA_SIMD_2X64 swap_lanes() const
Definition simd_2x64.h:101
void BOTAN_FN_ISA_SIMD_2X64 store_be(uint64_t out[2]) const
Definition simd_2x64.h:128
static void BOTAN_FN_ISA_SIMD_2X64 twist(SIMD_2x64 &B0, SIMD_2x64 &B1, SIMD_2x64 &C0, SIMD_2x64 &C1, SIMD_2x64 &D0, SIMD_2x64 &D1)
Definition simd_2x64.h:259
SIMD_2x64 BOTAN_FN_ISA_SIMD_2X64 andc(const SIMD_2x64 &other) const noexcept
Definition simd_2x64.h:164
SIMD_2x64 BOTAN_FN_ISA_SIMD_2X64 operator+(const SIMD_2x64 &other) const
Definition simd_2x64.h:136
static SIMD_2x64 BOTAN_FN_ISA_SIMD_2X64 all_ones()
Definition simd_2x64.h:57
SIMD_2x64 & operator=(SIMD_2x64 &&other)=default
static SIMD_2x64 BOTAN_FN_ISA_SIMD_2X64 load_le(const void *in)
Definition simd_2x64.h:74
void BOTAN_FN_ISA_SIMD_2X64 store_le(std::span< uint8_t, 16 > out) const
Definition simd_2x64.h:134
static SIMD_2x64 BOTAN_FN_ISA_SIMD_2X64 interleave_high(const SIMD_2x64 &a, const SIMD_2x64 &b)
Definition simd_2x64.h:249
SIMD_2x64 BOTAN_FN_ISA_SIMD_2X64 rotl() const
Definition simd_2x64.h:210
static SIMD_2x64 BOTAN_FN_ISA_SIMD_2X64 interleave_low(const SIMD_2x64 &a, const SIMD_2x64 &b)
Definition simd_2x64.h:241
void BOTAN_FN_ISA_SIMD_2X64 operator+=(const SIMD_2x64 &other)
Definition simd_2x64.h:148
static SIMD_2x64 BOTAN_FN_ISA_SIMD_2X64 alignr8(const SIMD_2x64 &a, const SIMD_2x64 &b)
Definition simd_2x64.h:232
SIMD_2x64 BOTAN_FN_ISA_SIMD_2X64 bswap() const
Definition simd_2x64.h:92
BOTAN_FN_ISA_SIMD_2X64 SIMD_2x64 rotr() const
Definition simd_2x64.h:174
void BOTAN_FN_ISA_SIMD_2X64 operator^=(const SIMD_2x64 &other)
Definition simd_2x64.h:156
void BOTAN_FN_ISA_SIMD_2X64 store_le(uint8_t out[]) const
Definition simd_2x64.h:120