native 0.0.1
Vectors, masks and wide register packs for C++26
Loading...
Searching...
No Matches
native.x86.sha512.ccm
1// SPDX-License-Identifier: BSD-2-Clause OR Apache-2.0
2module;
3#include "native/x86/instruction_storage.h"
4#include "native/isa_import.h"
5#include "native/config.h"
6#include <hint.h>
7#include <array>
8#include <bit>
9#include <cstdint>
10#include <immintrin.h>
11#include "native/x86/detail/constant_lanes.h"
12import native.simd;
13
14namespace native::detail::x86_sha512_constant {
15 constexpr std::uint64_t sigma0(std::uint64_t x) noexcept {
16 return std::rotr(x, 1) ^ std::rotr(x, 8) ^ (x >> 7);
17 }
18 constexpr std::uint64_t sigma1(std::uint64_t x) noexcept {
19 return std::rotr(x, 19) ^ std::rotr(x, 61) ^ (x >> 6);
20 }
21 template<class V, class W> constexpr V sha512msg1(V a, W b) noexcept {
22 auto x = x86_constant::lanes(a);
23 auto y = x86_constant::lanes(b);
24 for (unsigned i = 0; i < 3; ++i) x[i] += sigma0(x[i + 1]);
25 x[3] += sigma0(y[0]);
26 return x86_constant::pack<V>(x);
27 }
28 template<class V> constexpr V sha512msg2(V a, V b) noexcept {
29 auto x = x86_constant::lanes(a);
30 auto y = x86_constant::lanes(b);
31 x[0] += sigma1(y[2]);
32 x[1] += sigma1(y[3]);
33 x[2] += sigma1(x[0]);
34 x[3] += sigma1(x[1]);
35 return x86_constant::pack<V>(x);
36 }
37 template<class V, class W> constexpr V sha512rnds2(V cdgh, V abef, W message) noexcept {
38 auto x = x86_constant::lanes(cdgh), y = x86_constant::lanes(abef);
39 auto words = x86_constant::lanes(message);
40 auto a = y[3], b = y[2], c = x[3], d = x[2];
41 auto e = y[1], f = y[0], g = x[1], h = x[0];
42 for (unsigned i = 0; i < 2; ++i) {
43 auto t1 = h + (std::rotr(e, 14) ^ std::rotr(e, 18) ^ std::rotr(e, 41)) +
44 ((e & f) ^ (~e & g)) + words[i];
45 auto t2 = (std::rotr(a, 28) ^ std::rotr(a, 34) ^ std::rotr(a, 39)) +
46 ((a & b) ^ (a & c) ^ (b & c));
47 h = g; g = f; f = e; e = d + t1;
48 d = c; c = b; b = a; a = t1 + t2;
49 }
50 return x86_constant::pack<V>(std::array{f, e, b, a});
51 }
52}
53
54namespace native {
61
63 template<isa<x86> Arch>
64 requires(Arch.has(x86_feature::sha512) && Arch.has(x86_feature::avx))
65 [[nodiscard]] hint_inline hint_const hint_target("sha512")
66 constexpr simd<std::uint64_t, 4, Arch> sha512msg1(simd<std::uint64_t, 4, Arch> a, simd<std::uint64_t, 2, Arch> b) noexcept {
67 if consteval { return detail::x86_sha512_constant::sha512msg1(a, b); }
68 else { return detail::x86_instruction_result<simd<std::uint64_t, 4, Arch>>(_mm256_sha512msg1_epi64(detail::x86_integer_register(a), detail::x86_integer_register(b))); }
69 }
70
72 template<isa<x86> Arch>
73 requires(!(Arch.has(x86_feature::sha512) && Arch.has(x86_feature::avx)) &&
74 requires { sizeof(simd<std::uint64_t, 2, Arch>); sizeof(simd<std::uint64_t, 4, Arch>); } && !Arch.has(polyfill))
76 return detail::x86_sha512_constant::sha512msg1(a, b);
77 }
78
80 template<isa<x86> Arch>
81 requires(!(Arch.has(x86_feature::sha512) && Arch.has(x86_feature::avx)) &&
82 requires { sizeof(simd<std::uint64_t, 2, Arch>); sizeof(simd<std::uint64_t, 4, Arch>); } && Arch.has(polyfill))
84 return detail::x86_sha512_constant::sha512msg1(a, b);
85 }
86
88 template<isa<x86> Arch>
89 requires(Arch.has(x86_feature::sha512) && Arch.has(x86_feature::avx))
90 [[nodiscard]] hint_inline hint_const hint_target("sha512")
92 if consteval { return detail::x86_sha512_constant::sha512msg2(a, b); }
93 else { return detail::x86_instruction_result<simd<std::uint64_t, 4, Arch>>(_mm256_sha512msg2_epi64(detail::x86_integer_register(a), detail::x86_integer_register(b))); }
94 }
95
97 template<isa<x86> Arch>
98 requires(!(Arch.has(x86_feature::sha512) && Arch.has(x86_feature::avx)) &&
99 requires { sizeof(simd<std::uint64_t, 4, Arch>); } && !Arch.has(polyfill))
101 return detail::x86_sha512_constant::sha512msg2(a, b);
102 }
103
105 template<isa<x86> Arch>
106 requires(!(Arch.has(x86_feature::sha512) && Arch.has(x86_feature::avx)) &&
107 requires { sizeof(simd<std::uint64_t, 4, Arch>); } && Arch.has(polyfill))
109 return detail::x86_sha512_constant::sha512msg2(a, b);
110 }
111
113 template<isa<x86> Arch>
114 requires(Arch.has(x86_feature::sha512) && Arch.has(x86_feature::avx))
115 [[nodiscard]] hint_inline hint_const hint_target("sha512")
117 if consteval { return detail::x86_sha512_constant::sha512rnds2(a, b, c); }
118 else { return detail::x86_instruction_result<simd<std::uint64_t, 4, Arch>>(_mm256_sha512rnds2_epi64(detail::x86_integer_register(a), detail::x86_integer_register(b), detail::x86_integer_register(c))); }
119 }
120
122 template<isa<x86> Arch>
123 requires(!(Arch.has(x86_feature::sha512) && Arch.has(x86_feature::avx)) &&
124 requires { sizeof(simd<std::uint64_t, 2, Arch>); sizeof(simd<std::uint64_t, 4, Arch>); } && !Arch.has(polyfill))
126 return detail::x86_sha512_constant::sha512rnds2(a, b, c);
127 }
128
130 template<isa<x86> Arch>
131 requires(!(Arch.has(x86_feature::sha512) && Arch.has(x86_feature::avx)) &&
132 requires { sizeof(simd<std::uint64_t, 2, Arch>); sizeof(simd<std::uint64_t, 4, Arch>); } && Arch.has(polyfill))
134 return detail::x86_sha512_constant::sha512rnds2(a, b, c);
135 }
136
138 template<isa<x86> Arch, class... Args> void sha512msg1(Args...) = delete;
139
141 template<isa<x86> Arch, class... Args> void sha512msg2(Args...) = delete;
142
144 template<isa<x86> Arch, class... Args> void sha512rnds2(Args...) = delete;
146}
147
148export module native.x86.sha512;
149export import native.x86.features;
150export import native.simd;
151
152#if NATIVE_HOST_X86 || defined(NATIVE_DOXYGEN)
153export namespace native {
154 using ::native::sha512msg1;
155 using ::native::sha512msg2;
156 using ::native::sha512rnds2;
157}
158#endif
constexpr simd< std::uint64_t, 4, Arch > sha512rnds2(simd< std::uint64_t, 4, Arch > a, simd< std::uint64_t, 4, Arch > b, simd< std::uint64_t, 2, Arch > c) noexcept
Two SHA-512 rounds: a is CDGH, b is ABEF, c contains two message-plus-constant words.
constexpr simd< std::uint64_t, 4, Arch > sha512msg2(simd< std::uint64_t, 4, Arch > a, simd< std::uint64_t, 4, Arch > b) noexcept
Final SHA-512 schedule step with recurrence through the low output lanes.
constexpr simd< std::uint64_t, 4, Arch > sha512msg1(simd< std::uint64_t, 4, Arch > a, simd< std::uint64_t, 2, Arch > b) noexcept
First SHA-512 schedule step; only the low lane of b is used.
Architecture-tagged vectors, register packs and supporting value types. Native arithmetic follows its...
Standard-library adaptations documented here for SIMD value types.
Omitted architecture arguments use the native.simd provider's baseline.