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_sm3_constant {
15 template<
class V>
constexpr V sm3msg1(V a, V b, V c)
noexcept {
16 auto x = x86_constant::lanes(a), y = x86_constant::lanes(b), z = x86_constant::lanes(c);
17 for (
unsigned i = 0; i < 4; ++i) {
19 if (i < 3) t ^= std::rotl(y[i], 15);
20 x[i] = t ^ std::rotl(t, 15) ^ std::rotl(t, 23);
22 return x86_constant::pack<V>(x);
24 template<
class V>
constexpr V sm3msg2(V a, V b, V c)
noexcept {
25 auto x = x86_constant::lanes(a), y = x86_constant::lanes(b), z = x86_constant::lanes(c);
26 for (
unsigned i = 0; i < 4; ++i) x[i] ^= std::rotl(y[i], 7) ^ z[i];
27 x[3] ^= std::rotl(x[0], 6) ^ std::rotl(x[0], 15) ^ std::rotl(x[0], 30);
28 return x86_constant::pack<V>(x);
30 template<
unsigned Imm8,
class V>
constexpr V sm3rnds2(V cdgh, V abef, V message)
noexcept {
31 auto x = x86_constant::lanes(cdgh), y = x86_constant::lanes(abef);
32 auto words = x86_constant::lanes(message);
33 auto a = y[3], b = y[2], c = std::rotl(x[3], 9), d = std::rotl(x[2], 9);
34 auto e = y[1], f = y[0], g = std::rotl(x[1], 19), h = std::rotl(x[0], 19);
35 constexpr unsigned round = Imm8 & 62;
36 auto k = std::rotl(std::uint32_t(round < 16 ? 0x79cc4519 : 0x7a879d8a),
int(round));
37 for (
unsigned i = 0; i < 2; ++i) {
38 auto ss1 = std::rotl(std::rotl(a, 12) + e + k, 7);
39 auto ss2 = ss1 ^ std::rotl(a, 12);
40 auto ff = round < 16 ? a ^ b ^ c : (a & b) | (a & c) | (b & c);
41 auto gg = round < 16 ? e ^ f ^ g : (e & f) | (~e & g);
42 auto tt1 = ff + d + ss2 + (words[i] ^ words[i + 2]);
43 auto tt2 = gg + h + ss1 + words[i];
44 d = c; c = std::rotl(b, 9); b = a; a = tt1;
45 h = g; g = std::rotl(f, 19); f = e;
46 e = tt2 ^ std::rotl(tt2, 9) ^ std::rotl(tt2, 17);
49 return x86_constant::pack<V>(std::array{f, e, b, a});
62 template<isa<x86> Arch>
63 requires(Arch.has(x86_feature::sm3) && Arch.has(x86_feature::avx))
64 [[nodiscard]] hint_inline hint_const hint_target(
"sm3")
65 constexpr
simd<
std::uint32_t, 4, Arch>
sm3msg1(
simd<
std::uint32_t, 4, Arch> a,
simd<
std::uint32_t, 4, Arch> b,
simd<
std::uint32_t, 4, Arch> c) noexcept {
66 if consteval {
return detail::x86_sm3_constant::sm3msg1(a, b, c); }
67 else {
return detail::x86_instruction_result<simd<std::uint32_t, 4, Arch>>(_mm_sm3msg1_epi32(detail::x86_integer_register(a), detail::x86_integer_register(b), detail::x86_integer_register(c))); }
71 template<isa<x86> Arch>
72 requires(!(Arch.has(x86_feature::sm3) && Arch.has(x86_feature::avx)) &&
73 requires { sizeof(simd<std::uint32_t, 4, Arch>); } && !Arch.has(polyfill))
74 [[nodiscard]]
consteval simd<std::uint32_t, 4, Arch> sm3msg1(
simd<std::uint32_t, 4, Arch> a,
simd<std::uint32_t, 4, Arch> b,
simd<std::uint32_t, 4, Arch> c)
noexcept {
75 return detail::x86_sm3_constant::sm3msg1(a, b, c);
79 template<isa<x86> Arch>
80 requires(!(Arch.has(x86_feature::sm3) && Arch.has(x86_feature::avx)) &&
81 requires { sizeof(simd<std::uint32_t, 4, Arch>); } && Arch.has(polyfill))
82 [[nodiscard]] hint_inline
constexpr simd<std::uint32_t, 4, Arch> sm3msg1(
simd<std::uint32_t, 4, Arch> a,
simd<std::uint32_t, 4, Arch> b,
simd<std::uint32_t, 4, Arch> c)
noexcept {
83 return detail::x86_sm3_constant::sm3msg1(a, b, c);
87 template<isa<x86> Arch>
88 requires(Arch.has(x86_feature::sm3) && Arch.has(x86_feature::avx))
89 [[nodiscard]] hint_inline hint_const hint_target(
"sm3")
90 constexpr simd<std::uint32_t, 4, Arch> sm3msg2(
simd<std::uint32_t, 4, Arch> a,
simd<std::uint32_t, 4, Arch> b,
simd<std::uint32_t, 4, Arch> c)
noexcept {
91 if consteval {
return detail::x86_sm3_constant::sm3msg2(a, b, c); }
92 else {
return detail::x86_instruction_result<simd<std::uint32_t, 4, Arch>>(_mm_sm3msg2_epi32(detail::x86_integer_register(a), detail::x86_integer_register(b), detail::x86_integer_register(c))); }
96 template<isa<x86> Arch>
97 requires(!(Arch.has(x86_feature::sm3) && Arch.has(x86_feature::avx)) &&
98 requires { sizeof(simd<std::uint32_t, 4, Arch>); } && !Arch.has(polyfill))
99 [[nodiscard]]
consteval simd<std::uint32_t, 4, Arch> sm3msg2(
simd<std::uint32_t, 4, Arch> a,
simd<std::uint32_t, 4, Arch> b,
simd<std::uint32_t, 4, Arch> c)
noexcept {
100 return detail::x86_sm3_constant::sm3msg2(a, b, c);
104 template<isa<x86> Arch>
105 requires(!(Arch.has(x86_feature::sm3) && Arch.has(x86_feature::avx)) &&
106 requires { sizeof(simd<std::uint32_t, 4, Arch>); } && Arch.has(polyfill))
107 [[nodiscard]] hint_inline
constexpr simd<std::uint32_t, 4, Arch> sm3msg2(
simd<std::uint32_t, 4, Arch> a,
simd<std::uint32_t, 4, Arch> b,
simd<std::uint32_t, 4, Arch> c)
noexcept {
108 return detail::x86_sm3_constant::sm3msg2(a, b, c);
112 template<isa<x86> Arch,
unsigned Imm8>
113 requires(Arch.has(x86_feature::sm3) && Arch.has(x86_feature::avx) && Imm8 <= 255)
114 [[nodiscard]] hint_inline hint_const hint_target(
"sm3")
115 constexpr simd<std::uint32_t, 4, Arch> sm3rnds2(
simd<std::uint32_t, 4, Arch> a,
simd<std::uint32_t, 4, Arch> b,
simd<std::uint32_t, 4, Arch> c)
noexcept {
116 if consteval {
return detail::x86_sm3_constant::sm3rnds2<Imm8>(a, b, c); }
117 else {
return detail::x86_instruction_result<simd<std::uint32_t, 4, Arch>>(_mm_sm3rnds2_epi32(detail::x86_integer_register(a), detail::x86_integer_register(b), detail::x86_integer_register(c), Imm8)); }
121 template<isa<x86> Arch,
unsigned Imm8>
122 requires(!(Arch.has(x86_feature::sm3) && Arch.has(x86_feature::avx)) && Imm8 <= 255 &&
123 requires { sizeof(simd<std::uint32_t, 4, Arch>); } && !Arch.has(polyfill))
124 [[nodiscard]]
consteval simd<std::uint32_t, 4, Arch> sm3rnds2(
simd<std::uint32_t, 4, Arch> a,
simd<std::uint32_t, 4, Arch> b,
simd<std::uint32_t, 4, Arch> c)
noexcept {
125 return detail::x86_sm3_constant::sm3rnds2<Imm8>(a, b, c);
129 template<isa<x86> Arch,
unsigned Imm8>
130 requires(!(Arch.has(x86_feature::sm3) && Arch.has(x86_feature::avx)) && Imm8 <= 255 &&
131 requires { sizeof(simd<std::uint32_t, 4, Arch>); } && Arch.has(polyfill))
132 [[nodiscard]] hint_inline
constexpr simd<std::uint32_t, 4, Arch> sm3rnds2(
simd<std::uint32_t, 4, Arch> a,
simd<std::uint32_t, 4, Arch> b,
simd<std::uint32_t, 4, Arch> c)
noexcept {
133 return detail::x86_sm3_constant::sm3rnds2<Imm8>(a, b, c);
137 template<isa<x86> Arch,
class... Args>
void sm3msg1(Args...) =
delete;
140 template<isa<x86> Arch,
class... Args>
void sm3msg2(Args...) =
delete;
143 template<isa<x86> Arch,
unsigned Imm8,
class... Args>
void sm3rnds2(Args...) =
delete;
151#if NATIVE_HOST_X86 || defined(NATIVE_DOXYGEN)
constexpr simd< std::uint32_t, 4, Arch > sm3msg2(simd< std::uint32_t, 4, Arch > a, simd< std::uint32_t, 4, Arch > b, simd< std::uint32_t, 4, Arch > c) noexcept
Final SM3 schedule step.
constexpr simd< std::uint32_t, 4, Arch > sm3msg1(simd< std::uint32_t, 4, Arch > a, simd< std::uint32_t, 4, Arch > b, simd< std::uint32_t, 4, Arch > c) noexcept
First SM3 schedule step.
constexpr simd< std::uint32_t, 4, Arch > sm3rnds2(simd< std::uint32_t, 4, Arch > a, simd< std::uint32_t, 4, Arch > b, simd< std::uint32_t, 4, Arch > c) noexcept
Two SM3 rounds; Imm8 is masked with 0x3e. a holds unrotated CDGH and b holds ABEF.
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.