3#include "native/x86/instruction_storage.h"
4#include "native/isa_import.h"
5#include "native/config.h"
11#include "native/x86/detail/constant_lanes.h"
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);
18 constexpr std::uint64_t sigma1(std::uint64_t x)
noexcept {
19 return std::rotr(x, 19) ^ std::rotr(x, 61) ^ (x >> 6);
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]);
26 return x86_constant::pack<V>(x);
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);
35 return x86_constant::pack<V>(x);
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;
50 return x86_constant::pack<V>(std::array{f, e, b, a});
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")
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))); }
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);
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);
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))); }
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);
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);
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")
116 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 {
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))); }
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))
125 [[nodiscard]]
consteval 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 {
126 return detail::x86_sha512_constant::sha512rnds2(a, b, c);
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))
133 [[nodiscard]] hint_inline
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 {
134 return detail::x86_sha512_constant::sha512rnds2(a, b, c);
138 template<isa<x86> Arch,
class... Args>
void sha512msg1(Args...) =
delete;
141 template<isa<x86> Arch,
class... Args>
void sha512msg2(Args...) =
delete;
144 template<isa<x86> Arch,
class... Args>
void sha512rnds2(Args...) =
delete;
152#if NATIVE_HOST_X86 || defined(NATIVE_DOXYGEN)
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.