3#include "native/isa_import.h"
4#include "native/arm/detail/constant_lanes.h"
5#include "native/config.h"
12#include "native/targets.h"
16namespace native::detail::arm_sm3 {
17 template<isa<arm> Arch>
18 requires(Arch.has(arm_feature::sm3))
19 [[nodiscard]] hint_inline hint_const hint_target(
"sm4") uint32x4_t sm3ss1(
20 uint32x4_t a, uint32x4_t b, uint32x4_t c) noexcept {
21 return vsm3ss1q_u32(a, b, c);
24 template<isa<arm> Arch,
int Lane>
25 requires(Arch.has(arm_feature::sm3) && Lane >= 0 && Lane < 4)
26 [[nodiscard]] hint_inline hint_const hint_target(
"sm4") uint32x4_t sm3tt1a(
27 uint32x4_t state, uint32x4_t ss1, uint32x4_t words) noexcept {
28 return vsm3tt1aq_u32(state, ss1, words, Lane);
31 template<isa<arm> Arch,
int Lane>
32 requires(Arch.has(arm_feature::sm3) && Lane >= 0 && Lane < 4)
33 [[nodiscard]] hint_inline hint_const hint_target(
"sm4") uint32x4_t sm3tt1b(
34 uint32x4_t state, uint32x4_t ss1, uint32x4_t words) noexcept {
35 return vsm3tt1bq_u32(state, ss1, words, Lane);
38 template<isa<arm> Arch,
int Lane>
39 requires(Arch.has(arm_feature::sm3) && Lane >= 0 && Lane < 4)
40 [[nodiscard]] hint_inline hint_const hint_target(
"sm4") uint32x4_t sm3tt2a(
41 uint32x4_t state, uint32x4_t ss1, uint32x4_t words) noexcept {
42 return vsm3tt2aq_u32(state, ss1, words, Lane);
45 template<isa<arm> Arch,
int Lane>
46 requires(Arch.has(arm_feature::sm3) && Lane >= 0 && Lane < 4)
47 [[nodiscard]] hint_inline hint_const hint_target(
"sm4") uint32x4_t sm3tt2b(
48 uint32x4_t state, uint32x4_t ss1, uint32x4_t words) noexcept {
49 return vsm3tt2bq_u32(state, ss1, words, Lane);
52 template<isa<arm> Arch>
53 requires(Arch.has(arm_feature::sm3))
54 [[nodiscard]] hint_inline hint_const hint_target(
"sm4") uint32x4_t sm3partw1(
55 uint32x4_t a, uint32x4_t b, uint32x4_t c) noexcept {
56 return vsm3partw1q_u32(a, b, c);
59 template<isa<arm> Arch>
60 requires(Arch.has(arm_feature::sm3))
61 [[nodiscard]] hint_inline hint_const hint_target(
"sm4") uint32x4_t sm3partw2(
62 uint32x4_t a, uint32x4_t b, uint32x4_t c) noexcept {
63 return vsm3partw2q_u32(a, b, c);
74export namespace native::detail::arm_sm3_constant {
75 constexpr std::uint32_t p1(std::uint32_t value)
noexcept {
76 return value ^ std::rotl(value, 15) ^ std::rotl(value, 23);
79 template<
class V>
constexpr V sm3ss1(V a, V b, V c)
noexcept {
80 auto x = arm_constant::lanes(a);
81 auto y = arm_constant::lanes(b);
82 auto z = arm_constant::lanes(c);
83 std::array<std::uint32_t, 4> result{0, 0, 0, std::rotl(std::rotl(x[3], 12) + y[3] + z[3], 7)};
84 return arm_constant::pack<V>(result);
87 template<
int Lane,
bool Second,
bool Late,
class V>
88 constexpr V round(V state, V ss1, V words)
noexcept {
89 auto x = arm_constant::lanes(state);
90 auto s = arm_constant::lanes(ss1)[3];
91 auto word = arm_constant::lanes(words)[Lane];
92 auto boolean = x[3] ^ x[2] ^ x[1];
93 if constexpr (Late && Second)
94 boolean = (x[3] & x[2]) | (~x[3] & x[1]);
95 else if constexpr (Late)
96 boolean = (x[3] & x[2]) | (x[3] & x[1]) | (x[2] & x[1]);
97 if constexpr (!Second)
98 s ^= std::rotl(x[3], 12);
99 auto next =
boolean + x[0] + s + word;
100 if constexpr (Second)
101 next ^= std::rotl(next, 9) ^ std::rotl(next, 17);
102 std::array<std::uint32_t, 4> result{x[1], std::rotl(x[2], Second ? 19 : 9), x[3], next};
103 return arm_constant::pack<V>(result);
106 template<
int Lane,
class V>
constexpr V sm3tt1a(V a, V b, V c)
noexcept {
107 return round<Lane, false, false>(a, b, c);
110 template<
int Lane,
class V>
constexpr V sm3tt1b(V a, V b, V c)
noexcept {
111 return round<Lane, false, true>(a, b, c);
114 template<
int Lane,
class V>
constexpr V sm3tt2a(V a, V b, V c)
noexcept {
115 return round<Lane, true, false>(a, b, c);
118 template<
int Lane,
class V>
constexpr V sm3tt2b(V a, V b, V c)
noexcept {
119 return round<Lane, true, true>(a, b, c);
122 template<
class V>
constexpr V sm3partw1(V a, V b, V c)
noexcept {
123 auto x = arm_constant::lanes(a);
124 auto y = arm_constant::lanes(b);
125 auto z = arm_constant::lanes(c);
126 std::array<std::uint32_t, 4> result{};
127 for (
unsigned i = 0; i < 3; ++i)
128 result[i] = p1(x[i] ^ y[i] ^ std::rotl(z[i + 1], 15));
129 result[3] = p1(x[3] ^ y[3] ^ std::rotl(result[0], 15));
130 return arm_constant::pack<V>(result);
133 template<
class V>
constexpr V sm3partw2(V a, V b, V c)
noexcept {
134 auto x = arm_constant::lanes(a);
135 auto y = arm_constant::lanes(b);
136 auto z = arm_constant::lanes(c);
137 auto first = y[0] ^ std::rotl(z[0], 7);
138 for (
unsigned i = 0; i < 4; ++i)
139 x[i] ^= y[i] ^ std::rotl(z[i], 7);
140 x[3] ^= p1(std::rotl(first, 15));
141 return arm_constant::pack<V>(x);
146#if NATIVE_HOST_NEON || defined(NATIVE_DOXYGEN)
159 template<isa<arm> Arch>
160 requires(Arch.has(arm_feature::sm3))
161 [[nodiscard]] hint_inline hint_const
162 hint_target(
"sm4") constexpr
simd<std::uint32_t, 4, Arch>
164 simd<
std::uint32_t, 4, Arch> c) noexcept {
166 return detail::arm_sm3_constant::sm3ss1(a, b, c);
168 auto result = detail::arm_sm3::sm3ss1<Arch>(__builtin_bit_cast(uint32x4_t, a.to_native()),
169 __builtin_bit_cast(uint32x4_t, b.to_native()),
170 __builtin_bit_cast(uint32x4_t, c.to_native()));
177 template<isa<arm> Arch>
178 requires(!Arch.has(arm_feature::sm3) && !Arch.has(polyfill) &&
179 requires { sizeof(simd<std::uint32_t, 4, Arch>); })
182 return detail::arm_sm3_constant::sm3ss1(a, b, c);
186 template<isa<arm> Arch>
187 requires(!Arch.has(arm_feature::sm3) && Arch.has(polyfill) &&
188 requires { sizeof(simd<std::uint32_t, 4, Arch>); })
189 [[nodiscard]] hint_inline hint_const
192 return detail::arm_sm3_constant::sm3ss1(a, b, c);
197 template<isa<arm> Arch,
int Lane>
198 requires(Arch.has(arm_feature::sm3) && Lane >= 0 && Lane < 4)
199 [[nodiscard]] hint_inline hint_const
200 hint_target(
"sm4")
constexpr simd<std::uint32_t, 4, Arch>
204 return detail::arm_sm3_constant::sm3tt1a<Lane>(state, ss1, words);
207 detail::arm_sm3::sm3tt1a<Arch, Lane>(__builtin_bit_cast(uint32x4_t, state.to_native()),
208 __builtin_bit_cast(uint32x4_t, ss1.to_native()),
209 __builtin_bit_cast(uint32x4_t, words.to_native()));
216 template<isa<arm> Arch,
int Lane>
217 requires(!Arch.has(arm_feature::sm3) && !Arch.has(polyfill) && Lane >= 0 && Lane < 4 &&
218 requires { sizeof(simd<std::uint32_t, 4, Arch>); })
222 return detail::arm_sm3_constant::sm3tt1a<Lane>(state, ss1, words);
226 template<isa<arm> Arch,
int Lane>
227 requires(!Arch.has(arm_feature::sm3) && Arch.has(polyfill) && Lane >= 0 && Lane < 4 &&
228 requires { sizeof(simd<std::uint32_t, 4, Arch>); })
229 [[nodiscard]] hint_inline hint_const
233 return detail::arm_sm3_constant::sm3tt1a<Lane>(state, ss1, words);
238 template<isa<arm> Arch,
int Lane>
239 requires(Arch.has(arm_feature::sm3) && Lane >= 0 && Lane < 4)
240 [[nodiscard]] hint_inline hint_const
241 hint_target(
"sm4")
constexpr simd<std::uint32_t, 4, Arch>
245 return detail::arm_sm3_constant::sm3tt1b<Lane>(state, ss1, words);
248 detail::arm_sm3::sm3tt1b<Arch, Lane>(__builtin_bit_cast(uint32x4_t, state.to_native()),
249 __builtin_bit_cast(uint32x4_t, ss1.to_native()),
250 __builtin_bit_cast(uint32x4_t, words.to_native()));
257 template<isa<arm> Arch,
int Lane>
258 requires(!Arch.has(arm_feature::sm3) && !Arch.has(polyfill) && Lane >= 0 && Lane < 4 &&
259 requires { sizeof(simd<std::uint32_t, 4, Arch>); })
263 return detail::arm_sm3_constant::sm3tt1b<Lane>(state, ss1, words);
267 template<isa<arm> Arch,
int Lane>
268 requires(!Arch.has(arm_feature::sm3) && Arch.has(polyfill) && Lane >= 0 && Lane < 4 &&
269 requires { sizeof(simd<std::uint32_t, 4, Arch>); })
270 [[nodiscard]] hint_inline hint_const
274 return detail::arm_sm3_constant::sm3tt1b<Lane>(state, ss1, words);
279 template<isa<arm> Arch,
int Lane>
280 requires(Arch.has(arm_feature::sm3) && Lane >= 0 && Lane < 4)
281 [[nodiscard]] hint_inline hint_const
282 hint_target(
"sm4")
constexpr simd<std::uint32_t, 4, Arch>
286 return detail::arm_sm3_constant::sm3tt2a<Lane>(state, ss1, words);
289 detail::arm_sm3::sm3tt2a<Arch, Lane>(__builtin_bit_cast(uint32x4_t, state.to_native()),
290 __builtin_bit_cast(uint32x4_t, ss1.to_native()),
291 __builtin_bit_cast(uint32x4_t, words.to_native()));
298 template<isa<arm> Arch,
int Lane>
299 requires(!Arch.has(arm_feature::sm3) && !Arch.has(polyfill) && Lane >= 0 && Lane < 4 &&
300 requires { sizeof(simd<std::uint32_t, 4, Arch>); })
304 return detail::arm_sm3_constant::sm3tt2a<Lane>(state, ss1, words);
308 template<isa<arm> Arch,
int Lane>
309 requires(!Arch.has(arm_feature::sm3) && Arch.has(polyfill) && Lane >= 0 && Lane < 4 &&
310 requires { sizeof(simd<std::uint32_t, 4, Arch>); })
311 [[nodiscard]] hint_inline hint_const
315 return detail::arm_sm3_constant::sm3tt2a<Lane>(state, ss1, words);
320 template<isa<arm> Arch,
int Lane>
321 requires(Arch.has(arm_feature::sm3) && Lane >= 0 && Lane < 4)
322 [[nodiscard]] hint_inline hint_const
323 hint_target(
"sm4")
constexpr simd<std::uint32_t, 4, Arch>
327 return detail::arm_sm3_constant::sm3tt2b<Lane>(state, ss1, words);
330 detail::arm_sm3::sm3tt2b<Arch, Lane>(__builtin_bit_cast(uint32x4_t, state.to_native()),
331 __builtin_bit_cast(uint32x4_t, ss1.to_native()),
332 __builtin_bit_cast(uint32x4_t, words.to_native()));
339 template<isa<arm> Arch,
int Lane>
340 requires(!Arch.has(arm_feature::sm3) && !Arch.has(polyfill) && Lane >= 0 && Lane < 4 &&
341 requires { sizeof(simd<std::uint32_t, 4, Arch>); })
345 return detail::arm_sm3_constant::sm3tt2b<Lane>(state, ss1, words);
349 template<isa<arm> Arch,
int Lane>
350 requires(!Arch.has(arm_feature::sm3) && Arch.has(polyfill) && Lane >= 0 && Lane < 4 &&
351 requires { sizeof(simd<std::uint32_t, 4, Arch>); })
352 [[nodiscard]] hint_inline hint_const
356 return detail::arm_sm3_constant::sm3tt2b<Lane>(state, ss1, words);
360 template<isa<arm> Arch>
361 requires(Arch.has(arm_feature::sm3))
362 [[nodiscard]] hint_inline hint_const
363 hint_target(
"sm4")
constexpr simd<std::uint32_t, 4, Arch>
367 return detail::arm_sm3_constant::sm3partw1(a, b, c);
369 auto result = detail::arm_sm3::sm3partw1<Arch>(__builtin_bit_cast(uint32x4_t, a.to_native()),
370 __builtin_bit_cast(uint32x4_t, b.to_native()),
371 __builtin_bit_cast(uint32x4_t, c.to_native()));
378 template<isa<arm> Arch>
379 requires(!Arch.has(arm_feature::sm3) && !Arch.has(polyfill) &&
380 requires { sizeof(simd<std::uint32_t, 4, Arch>); })
383 return detail::arm_sm3_constant::sm3partw1(a, b, c);
387 template<isa<arm> Arch>
388 requires(!Arch.has(arm_feature::sm3) && Arch.has(polyfill) &&
389 requires { sizeof(simd<std::uint32_t, 4, Arch>); })
390 [[nodiscard]] hint_inline hint_const
393 return detail::arm_sm3_constant::sm3partw1(a, b, c);
397 template<isa<arm> Arch>
398 requires(Arch.has(arm_feature::sm3))
399 [[nodiscard]] hint_inline hint_const
400 hint_target(
"sm4")
constexpr simd<std::uint32_t, 4, Arch>
404 return detail::arm_sm3_constant::sm3partw2(a, b, c);
406 auto result = detail::arm_sm3::sm3partw2<Arch>(__builtin_bit_cast(uint32x4_t, a.to_native()),
407 __builtin_bit_cast(uint32x4_t, b.to_native()),
408 __builtin_bit_cast(uint32x4_t, c.to_native()));
415 template<isa<arm> Arch>
416 requires(!Arch.has(arm_feature::sm3) && !Arch.has(polyfill) &&
417 requires { sizeof(simd<std::uint32_t, 4, Arch>); })
420 return detail::arm_sm3_constant::sm3partw2(a, b, c);
424 template<isa<arm> Arch>
425 requires(!Arch.has(arm_feature::sm3) && Arch.has(polyfill) &&
426 requires { sizeof(simd<std::uint32_t, 4, Arch>); })
427 [[nodiscard]] hint_inline hint_const
430 return detail::arm_sm3_constant::sm3partw2(a, b, c);
constexpr simd< std::uint32_t, 4, Arch > sm3ss1(simd< std::uint32_t, 4, Arch > a, simd< std::uint32_t, 4, Arch > b, simd< std::uint32_t, 4, Arch > c) noexcept
Rotate and add the lane-three inputs; return SS1 in lane three and zero elsewhere.
constexpr simd< std::uint32_t, 4, Arch > sm3tt1b(simd< std::uint32_t, 4, Arch > state, simd< std::uint32_t, 4, Arch > ss1, simd< std::uint32_t, 4, Arch > words) noexcept
constexpr simd< std::uint32_t, 4, Arch > sm3partw1(simd< std::uint32_t, 4, Arch > a, simd< std::uint32_t, 4, Arch > b, simd< std::uint32_t, 4, Arch > c) noexcept
Compute the first four-word SM3 message-schedule update, including lane-zero feedback.
constexpr simd< std::uint32_t, 4, Arch > sm3tt1a(simd< std::uint32_t, 4, Arch > state, simd< std::uint32_t, 4, Arch > ss1, simd< std::uint32_t, 4, Arch > words) noexcept
constexpr simd< std::uint32_t, 4, Arch > sm3partw2(simd< std::uint32_t, 4, Arch > a, simd< std::uint32_t, 4, Arch > b, simd< std::uint32_t, 4, Arch > c) noexcept
Finish the four-word SM3 schedule update with the remaining XOR and rotation terms.
constexpr simd< std::uint32_t, 4, Arch > sm3tt2b(simd< std::uint32_t, 4, Arch > state, simd< std::uint32_t, 4, Arch > ss1, simd< std::uint32_t, 4, Arch > words) noexcept
constexpr simd< std::uint32_t, 4, Arch > sm3tt2a(simd< std::uint32_t, 4, Arch > state, simd< std::uint32_t, 4, Arch > ss1, simd< std::uint32_t, 4, Arch > words) noexcept
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.