Botan 3.10.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
18// NOLINTBEGIN(portability-simd-intrinsics)
19
20class SIMD_8x64 final {
21 public:
22 SIMD_8x64& operator=(const SIMD_8x64& other) = default;
23 SIMD_8x64(const SIMD_8x64& other) = default;
24
25 SIMD_8x64& operator=(SIMD_8x64&& other) = default;
26 SIMD_8x64(SIMD_8x64&& other) = default;
27
28 ~SIMD_8x64() = default;
29
30 // zero initialized
31 BOTAN_FN_ISA_SIMD_8X64 SIMD_8x64() : m_simd(_mm512_setzero_si512()) {}
32
33 // Load two halves at different addresses
34 static BOTAN_FN_ISA_SIMD_8X64 SIMD_8x64 load_le4(const void* in0,
35 const void* in1,
36 const void* in2,
37 const void* in3) {
38 auto r = _mm512_setzero_si512();
39 r = _mm512_inserti32x4(r, _mm_loadu_si128(reinterpret_cast<const __m128i*>(in0)), 3);
40 r = _mm512_inserti32x4(r, _mm_loadu_si128(reinterpret_cast<const __m128i*>(in1)), 2);
41 r = _mm512_inserti32x4(r, _mm_loadu_si128(reinterpret_cast<const __m128i*>(in2)), 1);
42 r = _mm512_inserti32x4(r, _mm_loadu_si128(reinterpret_cast<const __m128i*>(in3)), 0);
43 return SIMD_8x64(r);
44 }
45
46 static BOTAN_FN_ISA_SIMD_8X64 SIMD_8x64 load_be4(const void* in0,
47 const void* in1,
48 const void* in2,
49 const void* in3) {
50 return SIMD_8x64::load_le4(in0, in1, in2, in3).bswap();
51 }
52
53 static BOTAN_FN_ISA_SIMD_8X64 SIMD_8x64 load_le(const void* in) {
54 return SIMD_8x64(_mm512_loadu_si512(reinterpret_cast<const __m512i*>(in)));
55 }
56
57 static BOTAN_FN_ISA_SIMD_8X64 SIMD_8x64 load_be(const void* in) { return SIMD_8x64::load_le(in).bswap(); }
58
59 SIMD_8x64 BOTAN_FN_ISA_SIMD_8X64 bswap() const {
60 // clang-format off
61 const auto idx = _mm512_set_epi8(
62 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,
63 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);
64 // clang-format on
65
66 return SIMD_8x64(_mm512_shuffle_epi8(m_simd, idx));
67 }
68
69 void store_le(uint64_t out[8]) const { this->store_le(reinterpret_cast<uint8_t*>(out)); }
70
71 BOTAN_FN_ISA_SIMD_8X64 void store_le(uint8_t out[]) const {
72 _mm512_storeu_si512(reinterpret_cast<__m512i*>(out), m_simd);
73 }
74
75 BOTAN_FN_ISA_SIMD_8X64 void store_le4(void* out0, void* out1, void* out2, void* out3) {
76 _mm_storeu_si128(reinterpret_cast<__m128i*>(out0), _mm512_extracti32x4_epi32(m_simd, 3));
77 _mm_storeu_si128(reinterpret_cast<__m128i*>(out1), _mm512_extracti32x4_epi32(m_simd, 2));
78 _mm_storeu_si128(reinterpret_cast<__m128i*>(out2), _mm512_extracti32x4_epi32(m_simd, 1));
79 _mm_storeu_si128(reinterpret_cast<__m128i*>(out3), _mm512_extracti32x4_epi32(m_simd, 0));
80 }
81
82 SIMD_8x64 operator+(const SIMD_8x64& other) const {
83 SIMD_8x64 retval(*this);
84 retval += other;
85 return retval;
86 }
87
88 SIMD_8x64 operator^(const SIMD_8x64& other) const {
89 SIMD_8x64 retval(*this);
90 retval ^= other;
91 return retval;
92 }
93
94 BOTAN_FN_ISA_SIMD_8X64 void operator+=(const SIMD_8x64& other) {
95 m_simd = _mm512_add_epi64(m_simd, other.m_simd);
96 }
97
98 BOTAN_FN_ISA_SIMD_8X64 void operator^=(const SIMD_8x64& other) {
99 m_simd = _mm512_xor_si512(m_simd, other.m_simd);
100 }
101
102 template <size_t ROT>
103 BOTAN_FN_ISA_SIMD_8X64 SIMD_8x64 rotr() const
104 requires(ROT > 0 && ROT < 64)
105 {
106 return SIMD_8x64(_mm512_ror_epi64(m_simd, ROT));
107 }
108
109 template <size_t ROT>
110 SIMD_8x64 rotl() const {
111 return this->rotr<64 - ROT>();
112 }
113
114 template <int SHIFT>
115 SIMD_8x64 BOTAN_FN_ISA_SIMD_8X64 shr() const noexcept {
116 return SIMD_8x64(_mm512_srli_epi64(m_simd, SHIFT));
117 }
118
119 static SIMD_8x64 BOTAN_FN_ISA_SIMD_8X64 alignr8(const SIMD_8x64& a, const SIMD_8x64& b) {
120 return SIMD_8x64(_mm512_alignr_epi8(a.m_simd, b.m_simd, 8));
121 }
122
123 explicit BOTAN_FN_ISA_SIMD_8X64 SIMD_8x64(__m512i x) : m_simd(x) {}
124
125 private:
126 __m512i m_simd;
127};
128
129// NOLINTEND(portability-simd-intrinsics)
130
131} // namespace Botan
132
133#endif
BOTAN_FN_ISA_SIMD_8X64 void operator+=(const SIMD_8x64 &other)
Definition simd_8x64.h:94
BOTAN_FN_ISA_SIMD_8X64 void store_le(uint8_t out[]) const
Definition simd_8x64.h:71
SIMD_8x64 BOTAN_FN_ISA_SIMD_8X64 bswap() const
Definition simd_8x64.h:59
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:34
SIMD_8x64(SIMD_8x64 &&other)=default
void store_le(uint64_t out[8]) const
Definition simd_8x64.h:69
~SIMD_8x64()=default
SIMD_8x64 operator^(const SIMD_8x64 &other) const
Definition simd_8x64.h:88
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:46
SIMD_8x64(const SIMD_8x64 &other)=default
SIMD_8x64 rotl() const
Definition simd_8x64.h:110
static BOTAN_FN_ISA_SIMD_8X64 SIMD_8x64 load_le(const void *in)
Definition simd_8x64.h:53
static SIMD_8x64 BOTAN_FN_ISA_SIMD_8X64 alignr8(const SIMD_8x64 &a, const SIMD_8x64 &b)
Definition simd_8x64.h:119
BOTAN_FN_ISA_SIMD_8X64 SIMD_8x64()
Definition simd_8x64.h:31
BOTAN_FN_ISA_SIMD_8X64 SIMD_8x64 rotr() const
Definition simd_8x64.h:103
BOTAN_FN_ISA_SIMD_8X64 SIMD_8x64(__m512i x)
Definition simd_8x64.h:123
BOTAN_FN_ISA_SIMD_8X64 void store_le4(void *out0, void *out1, void *out2, void *out3)
Definition simd_8x64.h:75
BOTAN_FN_ISA_SIMD_8X64 void operator^=(const SIMD_8x64 &other)
Definition simd_8x64.h:98
SIMD_8x64 & operator=(SIMD_8x64 &&other)=default
static BOTAN_FN_ISA_SIMD_8X64 SIMD_8x64 load_be(const void *in)
Definition simd_8x64.h:57
SIMD_8x64 operator+(const SIMD_8x64 &other) const
Definition simd_8x64.h:82
SIMD_8x64 BOTAN_FN_ISA_SIMD_8X64 shr() const noexcept
Definition simd_8x64.h:115
SIMD_8x64 & operator=(const SIMD_8x64 &other)=default