native 0.0.1
Vectors, masks and wide register packs for C++26
Loading...
Searching...
No Matches
native.arm.sm3.ccm
1// SPDX-License-Identifier: BSD-2-Clause OR Apache-2.0
2module;
3#include "native/isa_import.h"
4#include "native/arm/detail/constant_lanes.h"
5#include "native/config.h"
6#include <hint.h>
7#include "native/isa.h"
8#include <cstdint>
9#if NATIVE_HOST_NEON
10#include <arm_neon.h>
11#endif
12#include "native/targets.h"
14#if NATIVE_HOST_NEON
15
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);
22 }
23
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);
29 }
30
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);
36 }
37
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);
43 }
44
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);
50 }
51
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);
57 }
58
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);
64 }
65
66} // namespace native::detail::arm_sm3
67#endif
69
70export module native.arm.sm3;
71export import native.arm.features;
72export import native.simd;
73// Export the semantic implementations so they can also be checked at runtime.
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);
77 }
78
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);
85 }
86
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);
104 }
105
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);
108 }
109
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);
112 }
113
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);
116 }
117
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);
120 }
121
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);
131 }
132
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);
142 }
143} // namespace native::detail::arm_sm3_constant
144
145
146#if NATIVE_HOST_NEON || defined(NATIVE_DOXYGEN)
147export namespace native {
157
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>
163 sm3ss1(simd<std::uint32_t, 4, Arch> a, simd<std::uint32_t, 4, Arch> b,
164 simd<std::uint32_t, 4, Arch> c) noexcept {
165 if consteval {
166 return detail::arm_sm3_constant::sm3ss1(a, b, c);
167 } else {
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()));
172 __builtin_bit_cast(typename simd<std::uint32_t, 4, Arch>::native_type, result));
173 }
174 }
175
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);
183 }
184
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);
193 }
194
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>
202 simd<std::uint32_t, 4, Arch> words) noexcept {
203 if consteval {
204 return detail::arm_sm3_constant::sm3tt1a<Lane>(state, ss1, words);
205 } else {
206 auto result =
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()));
211 __builtin_bit_cast(typename simd<std::uint32_t, 4, Arch>::native_type, result));
212 }
213 }
214
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>); })
219 [[nodiscard]] consteval simd<std::uint32_t, 4, Arch> sm3tt1a(
221 simd<std::uint32_t, 4, Arch> words) noexcept {
222 return detail::arm_sm3_constant::sm3tt1a<Lane>(state, ss1, words);
223 }
224
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
232 simd<std::uint32_t, 4, Arch> words) noexcept {
233 return detail::arm_sm3_constant::sm3tt1a<Lane>(state, ss1, words);
234 }
235
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>
243 simd<std::uint32_t, 4, Arch> words) noexcept {
244 if consteval {
245 return detail::arm_sm3_constant::sm3tt1b<Lane>(state, ss1, words);
246 } else {
247 auto result =
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()));
252 __builtin_bit_cast(typename simd<std::uint32_t, 4, Arch>::native_type, result));
253 }
254 }
255
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>); })
260 [[nodiscard]] consteval simd<std::uint32_t, 4, Arch> sm3tt1b(
262 simd<std::uint32_t, 4, Arch> words) noexcept {
263 return detail::arm_sm3_constant::sm3tt1b<Lane>(state, ss1, words);
264 }
265
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
273 simd<std::uint32_t, 4, Arch> words) noexcept {
274 return detail::arm_sm3_constant::sm3tt1b<Lane>(state, ss1, words);
275 }
276
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>
284 simd<std::uint32_t, 4, Arch> words) noexcept {
285 if consteval {
286 return detail::arm_sm3_constant::sm3tt2a<Lane>(state, ss1, words);
287 } else {
288 auto result =
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()));
293 __builtin_bit_cast(typename simd<std::uint32_t, 4, Arch>::native_type, result));
294 }
295 }
296
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>); })
301 [[nodiscard]] consteval simd<std::uint32_t, 4, Arch> sm3tt2a(
303 simd<std::uint32_t, 4, Arch> words) noexcept {
304 return detail::arm_sm3_constant::sm3tt2a<Lane>(state, ss1, words);
305 }
306
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
314 simd<std::uint32_t, 4, Arch> words) noexcept {
315 return detail::arm_sm3_constant::sm3tt2a<Lane>(state, ss1, words);
316 }
317
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>
325 simd<std::uint32_t, 4, Arch> words) noexcept {
326 if consteval {
327 return detail::arm_sm3_constant::sm3tt2b<Lane>(state, ss1, words);
328 } else {
329 auto result =
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()));
334 __builtin_bit_cast(typename simd<std::uint32_t, 4, Arch>::native_type, result));
335 }
336 }
337
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>); })
342 [[nodiscard]] consteval simd<std::uint32_t, 4, Arch> sm3tt2b(
344 simd<std::uint32_t, 4, Arch> words) noexcept {
345 return detail::arm_sm3_constant::sm3tt2b<Lane>(state, ss1, words);
346 }
347
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
355 simd<std::uint32_t, 4, Arch> words) noexcept {
356 return detail::arm_sm3_constant::sm3tt2b<Lane>(state, ss1, words);
357 }
358
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>
365 simd<std::uint32_t, 4, Arch> c) noexcept {
366 if consteval {
367 return detail::arm_sm3_constant::sm3partw1(a, b, c);
368 } else {
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()));
373 __builtin_bit_cast(typename simd<std::uint32_t, 4, Arch>::native_type, result));
374 }
375 }
376
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);
384 }
385
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);
394 }
395
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>
402 simd<std::uint32_t, 4, Arch> c) noexcept {
403 if consteval {
404 return detail::arm_sm3_constant::sm3partw2(a, b, c);
405 } else {
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()));
410 __builtin_bit_cast(typename simd<std::uint32_t, 4, Arch>::native_type, result));
411 }
412 }
413
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);
421 }
422
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);
431 }
432
434} // namespace native
435#endif
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.