native 0.0.1
Vectors, masks and wide register packs for C++26
Loading...
Searching...
No Matches
integer_constant.h
1// SPDX-License-Identifier: BSD-2-Clause OR Apache-2.0
2#pragma once
3
5#include "native/x86/detail/constant_lanes.h"
6#include <array>
7#include <bit>
8#include <cstdint>
9#include <limits>
10#include <type_traits>
11
12// Integer instruction semantics used only by the constant-evaluation branches.
13// Runtime wrappers continue to call their target-specific native helpers.
14namespace native::detail::x86_instruction_constant {
15 using x86_constant::lanes;
16
17 template<unsigned Imm8, class V>
18 constexpr V carryless(V a, V b) noexcept {
19 auto aa = lanes(a), bb = lanes(b);
20 decltype(aa) result{};
21 for (std::size_t lane = 0; lane < V::lanes; lane += 2) {
22 auto x = aa[lane + (Imm8 & 1)];
23 auto y = bb[lane + ((Imm8 >> 4) & 1)];
24 for (unsigned bit = 0; bit < 64; ++bit) if ((y >> bit) & 1) {
25 result[lane] ^= x << bit;
26 if (bit) result[lane + 1] ^= x >> (64 - bit);
27 }
28 }
29 return V::load(result.data());
30 }
31
32 template<class V>
33 constexpr V population(V value, V source, std::uint64_t mask) noexcept {
34 auto input = lanes(value), result = lanes(source);
35 for (std::size_t lane = 0; lane < V::lanes; ++lane)
36 if ((mask >> lane) & 1)
37 result[lane] = static_cast<typename V::value_type>(std::popcount(input[lane]));
38 return V::load(result.data());
39 }
40
41 using aes_constant::field_product;
42 using aes_constant::field_inverse;
43
44 template<class V>
45 constexpr V field_multiply(V a, V b, V source, std::uint64_t mask) noexcept {
46 auto aa = lanes(a), bb = lanes(b), result = lanes(source);
47 for (std::size_t lane = 0; lane < V::lanes; ++lane)
48 if ((mask >> lane) & 1) result[lane] = field_product(aa[lane], bb[lane]);
49 return V::load(result.data());
50 }
51
52 template<unsigned Imm8, bool Inverse, class V, class M>
53 constexpr V field_affine(V a, M matrix, V source, std::uint64_t mask) noexcept {
54 auto aa = lanes(a), result = lanes(source);
55 auto rows = lanes(matrix);
56 for (std::size_t lane = 0; lane < V::lanes; ++lane) if ((mask >> lane) & 1) {
57 auto value = Inverse ? field_inverse(aa[lane]) : aa[lane];
58 unsigned byte = Imm8;
59 for (unsigned bit = 0; bit < 8; ++bit) {
60 auto row = static_cast<unsigned>(rows[lane / 8] >> (8 * (7 - bit))) & 255;
61 byte ^= (std::popcount(row & value) & 1u) << bit;
62 }
63 result[lane] = static_cast<std::uint8_t>(byte);
64 }
65 return V::load(result.data());
66 }
67
68 template<bool Saturate, class V, class A, class B>
69 constexpr V dot(V accumulator, A a, B b, std::uint64_t mask, bool zero) noexcept {
70 using T = typename V::value_type;
71 auto result = lanes(accumulator);
72 auto aa = lanes(a);
73 auto bb = lanes(b);
74 constexpr auto group = A::lanes / V::lanes;
75 for (std::size_t lane = 0; lane < V::lanes; ++lane) {
76 if (!((mask >> lane) & 1)) {
77 if (zero) result[lane] = 0;
78 continue;
79 }
80 // Even two unsigned 16-bit products plus an unsigned accumulator fit
81 // in int64_t. Saturate the complete sum, never an intermediate pair.
82 std::int64_t sum = result[lane];
83 for (std::size_t part = 0; part < group; ++part)
84 sum += std::int64_t(aa[group * lane + part]) * std::int64_t(bb[group * lane + part]);
85 if constexpr (Saturate) {
86 if (sum < std::numeric_limits<T>::min()) sum = std::numeric_limits<T>::min();
87 if (sum > std::numeric_limits<T>::max()) sum = std::numeric_limits<T>::max();
88 }
89 result[lane] = std::bit_cast<T>(static_cast<std::uint32_t>(sum));
90 }
91 return V::load(result.data());
92 }
93}
Shared AES byte operations for constant evaluation on ARM and x86.
typename mask_traits< std::remove_cvref_t< T > >::type mask
Definition mask_traits.h:22