native 0.0.1
Vectors, masks and wide register packs for C++26
Loading...
Searching...
No Matches
integer_body.h
1// SPDX-FileCopyrightText: 2026 Edward Kmett <ekmett@gmail.com>
2// SPDX-License-Identifier: BSD-2-Clause OR Apache-2.0
3
4
5namespace native {
9 template <simd_integer_element To, simd_integer_element From, std::size_t N, ::native::isa<> Arch>
10 requires NATIVE_ARCH_REQUIRES(Arch) && (!detail::polyfill_shape<From,N,Arch> &&
11 !detail::polyfill_shape<To,sizeof(From)*N/sizeof(To),Arch>) &&
12 (sizeof(From) * N % sizeof(To) == 0 && requires {
14 typename simd<To,sizeof(From)*N/sizeof(To),Arch>::native_type;
15 } && sizeof(typename simd<From,N,Arch>::native_type) ==
16 sizeof(typename simd<To,sizeof(From)*N/sizeof(To),Arch>::native_type) &&
17 (sizeof(typename simd<From,N,Arch>::native_type) <= 16
18#if NATIVE_HAS_AVX2
19 || sizeof(typename simd<From,N,Arch>::native_type) == 32
20#endif
21#if NATIVE_HAS_AVX512F
22 || sizeof(typename simd<From,N,Arch>::native_type) == 64
23#endif
24 ))
25 [[nodiscard]] hint_inline constexpr hint_const auto reinterpret_bits(simd<From,N,Arch> value) noexcept
26 -> simd<To,sizeof(From)*N/sizeof(To),Arch> {
27 using result = simd<To,sizeof(From)*N/sizeof(To),Arch>;
28 // Keep native vectors in this target scope: the standard-library wrapper
29 // can otherwise impose its baseline vector return ABI on SysV hosts.
30 return result::from_native(__builtin_bit_cast(typename result::native_type,value.to_native()));
31 }
32
35 template <simd_integer_element T, std::size_t N, ::native::isa<> Arch>
36 requires NATIVE_ARCH_REQUIRES(Arch) && (std::is_unsigned_v<T> && sizeof(T) <= 4 && N > 1 && N % 2 == 0 &&
37 detail::integer_arithmetic<T,N,Arch>)
38 [[nodiscard]] hint_inline constexpr hint_const auto pairwise_add_widened(simd<T,N,Arch> value) noexcept {
39 using U = std::conditional_t<sizeof(T)==1,std::uint16_t,
40 std::conditional_t<sizeof(T)==2,std::uint32_t,std::uint64_t>>;
41 using result = simd<U,N/2,Arch>;
42 if consteval {
43 std::array<T,N> source{};
44 value.store(source.data());
45 std::array<U,N/2> lanes{};
46 for(std::size_t i=0;i<N/2;++i) lanes[i]=U(source[2*i])+source[2*i+1];
47 return result(lanes);
48 }
49 if constexpr (sizeof(T)==4 && N==2) {
50 // A logical two-lane input has four physical lanes. Only the live pair
51 // contributes to the scalar result, regardless of padding bits.
52 auto lanes = value.to_native();
53 return result(std::uint64_t(lanes[0]) + lanes[1]);
54 } else
55#if NATIVE_HAS_ARM_NEON
56 if constexpr (sizeof(T)==1)
57 return result::from_native(vreinterpretq_u8_u16(vpaddlq_u8(value.to_native())));
58 else if constexpr (sizeof(T)==2)
59 return result::from_native(vreinterpretq_u8_u32(vpaddlq_u16(vreinterpretq_u16_u8(value.to_native()))));
60 else return result::from_native(vreinterpretq_u8_u64(vpaddlq_u32(vreinterpretq_u32_u8(value.to_native()))));
61#elif NATIVE_HAS_WASM_SIMD128
62 if constexpr (sizeof(T) == 1) {
63 return result::from_native(wasm_u16x8_extadd_pairwise_u8x16(value.to_native()));
64 } else if constexpr (sizeof(T) == 2) {
65 return result::from_native(wasm_u32x4_extadd_pairwise_u16x8(value.to_native()));
66 } else {
67 auto even = wasm_i32x4_shuffle(value.to_native(), value.to_native(), 0, 2, 0, 0);
68 auto odd = wasm_i32x4_shuffle(value.to_native(), value.to_native(), 1, 3, 0, 0);
69 return result::from_native(wasm_i64x2_add(
70 wasm_u64x2_extend_low_u32x4(even), wasm_u64x2_extend_low_u32x4(odd)));
71 }
72#else
73 // Two unsigned byte lanes sum to at most 510, well inside PMADDUBSW's
74 // signed saturation bound. Wider lanes use exact register shifts/adds.
75#if NATIVE_HAS_AVX2
76 if constexpr (sizeof(T)==1 && sizeof(T)*N==16)
77 return result::from_native(_mm_maddubs_epi16(value.to_native(),_mm_set1_epi8(1)));
78 else if constexpr (sizeof(T)==1 && sizeof(T)*N==32)
79 return result::from_native(_mm256_maddubs_epi16(value.to_native(),_mm256_set1_epi8(1)));
80 else
81#endif
82#if NATIVE_HAS_AVX512BW
83 if constexpr (sizeof(T)==1 && sizeof(T)*N==64)
84 return result::from_native(_mm512_maddubs_epi16(value.to_native(),_mm512_set1_epi8(1)));
85 else
86#endif
87 {
88 auto words = reinterpret_bits<U>(value);
89 return (words & result(U(std::numeric_limits<T>::max()))) + words.template right<8*sizeof(T)>();
90 }
91#endif
92 }
93
96 template <simd_integer_element T, std::size_t N, ::native::isa<> Arch>
97 requires NATIVE_ARCH_REQUIRES(Arch) && (std::is_unsigned_v<T> && detail::integer_arithmetic<T,N,Arch>)
98 [[nodiscard]] hint_inline constexpr hint_const simd<T,N,Arch> popcount(simd<T,N,Arch> value) noexcept {
99 using result = simd<T,N,Arch>;
100 if consteval {
101 std::array<T,N> lanes{};
102 value.store(lanes.data());
103 for(auto &lane:lanes) lane=T(std::popcount(lane));
104 return result(lanes);
105 }
106 if constexpr (N==1) return result(T(std::popcount(value.to_native())));
107 else if constexpr (sizeof(T)==4 && (N==2 || N==3))
108 return result::from_storage(popcount(value.to_storage()));
109#if NATIVE_HAS_AVX512F && !NATIVE_HAS_AVX512BW
110 else if constexpr (sizeof(T)*N==64) {
111 // F/DQ supports arithmetic on these lanes, while its byte/word
112 // repartitions provide storage only.
113 // Accumulate bit populations inside each original lane instead.
114 constexpr T all = std::numeric_limits<T>::max();
115 value = value - (value.template right<1>() & result(T(all/3)));
116 value = (value & result(T(all/5))) + (value.template right<2>() & result(T(all/5)));
117 value = (value + value.template right<4>()) & result(T(all/17));
118 value = value + value.template right<8>();
119 value = value + value.template right<16>();
120 if constexpr (sizeof(T)==8) value = value + value.template right<32>();
121 return value & result(T(127));
122 }
123#endif
124 else if constexpr (sizeof(T)>1)
126 std::conditional_t<sizeof(T)==2,std::uint8_t,
127 std::conditional_t<sizeof(T)==4,std::uint16_t,std::uint32_t>>>(value)));
128 else {
129#if NATIVE_HAS_WASM_SIMD128
130 return result::from_native(wasm_i8x16_popcnt(value.to_native()));
131#endif
132#if NATIVE_HAS_ARM_NEON
133 return result::from_native(vcntq_u8(value.to_native()));
134#endif
135#if NATIVE_HAS_AVX2
136 if constexpr (N==16) {
137 auto table = _mm_setr_epi8(0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4);
138 auto mask = _mm_set1_epi8(15);
139 auto data = value.to_native();
140 return result::from_native(_mm_add_epi8(
141 _mm_shuffle_epi8(table,_mm_and_si128(data,mask)),
142 _mm_shuffle_epi8(table,_mm_and_si128(_mm_srli_epi16(data,4),mask))));
143 } else if constexpr (N==32) {
144 auto table = _mm256_broadcastsi128_si256(_mm_setr_epi8(0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4));
145 auto mask = _mm256_set1_epi8(15);
146 auto data = value.to_native();
147 return result::from_native(_mm256_add_epi8(
148 _mm256_shuffle_epi8(table,_mm256_and_si256(data,mask)),
149 _mm256_shuffle_epi8(table,_mm256_and_si256(_mm256_srli_epi16(data,4),mask))));
150 }
151#endif
152#if NATIVE_HAS_AVX512BW
153 if constexpr (N==64) {
154 auto table = _mm512_broadcast_i32x4(_mm_setr_epi8(0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4));
155 auto mask = _mm512_set1_epi8(15);
156 auto data = value.to_native();
157 return result::from_native(_mm512_add_epi8(
158 _mm512_shuffle_epi8(table,_mm512_and_si512(data,mask)),
159 _mm512_shuffle_epi8(table,_mm512_and_si512(_mm512_srli_epi16(data,4),mask))));
160 }
161#endif
162 }
163 }
164
167 template <simd_integer_element T, std::size_t N, ::native::isa<> Arch>
168 requires NATIVE_ARCH_REQUIRES(Arch) && (std::is_unsigned_v<T> && sizeof(T)<=4 &&
169 detail::integer_arithmetic<T,N,Arch>)
170 [[nodiscard]] hint_inline constexpr hint_const std::uint64_t reduce_add_widened(simd<T,N,Arch> value) noexcept {
171 if consteval {
172 std::array<T,N> lanes{};
173 value.store(lanes.data());
174 std::uint64_t sum=0;
175 for(auto lane:lanes) sum+=lane;
176 return sum;
177 }
178 if constexpr (N==1) return value.to_native();
179 else if constexpr (sizeof(T)==4 && (N==2 || N==3)) {
180 auto lanes = value.to_native();
181 auto sum = std::uint64_t(lanes[0]) + lanes[1];
182 if constexpr (N==3) sum += lanes[2];
183 return sum;
184 }
185 else {
186#if NATIVE_HAS_WASM_SIMD128
187 auto sums = [&] {
188 if constexpr (sizeof(T) == 1) {
190 } else if constexpr (sizeof(T) == 2) {
192 } else {
193 return pairwise_add_widened(value);
194 }
195 }();
196 return sums.template get<0>() + sums.template get<1>();
197#endif
198#if NATIVE_HAS_ARM_NEON
199 if constexpr (sizeof(T)==1) return vaddlvq_u8(value.to_native());
200 else if constexpr (sizeof(T)==2) return vaddlvq_u16(vreinterpretq_u16_u8(value.to_native()));
201 else return vaddlvq_u32(vreinterpretq_u32_u8(value.to_native()));
202#endif
203#if NATIVE_HAS_AVX2
204 auto sums = [&] {
205 if constexpr (sizeof(T)==1) {
206 if constexpr (N==16) return _mm_sad_epu8(value.to_native(),_mm_setzero_si128());
207 else if constexpr (N==32) return _mm256_sad_epu8(value.to_native(),_mm256_setzero_si256());
208#if NATIVE_HAS_AVX512BW
209 else return _mm512_sad_epu8(value.to_native(),_mm512_setzero_si512());
210#endif
211 } else if constexpr (sizeof(T)==2)
212 return pairwise_add_widened(pairwise_add_widened(value)).to_native();
213 else return pairwise_add_widened(value).to_native();
214 }();
215 if constexpr (sizeof(T)*N==16)
216 return std::uint64_t(_mm_cvtsi128_si64(_mm_add_epi64(sums,_mm_srli_si128(sums,8))));
217 else if constexpr (sizeof(T)*N==32) {
218 auto halves = _mm_add_epi64(_mm256_castsi256_si128(sums),_mm256_extracti128_si256(sums,1));
219 return std::uint64_t(_mm_cvtsi128_si64(_mm_add_epi64(halves,_mm_srli_si128(halves,8))));
220 }
221#if NATIVE_HAS_AVX512F
222 else return std::uint64_t(_mm512_reduce_add_epi64(sums));
223#endif
224#endif
225 }
226 }
227
229 template<simd_integer_element T,std::size_t N,isa<> Arch>
230 requires NATIVE_ARCH_REQUIRES(Arch) && (std::is_unsigned_v<T> && detail::polyfill_operation_shape<T,N,Arch>)
231 constexpr simd<T,N,Arch> popcount(simd<T,N,Arch> value) noexcept { return popcount(value); }
232
235 template<simd_integer_element T,std::size_t N,isa<> Arch>
236 requires NATIVE_ARCH_REQUIRES(Arch) && (std::is_unsigned_v<T> && sizeof(T)<=4 &&
237 detail::polyfill_operation_shape<T,N,Arch>)
238 [[nodiscard]] hint_inline constexpr hint_const std::uint64_t reduce_add_widened(simd<T,N,Arch> value) noexcept {
239 using V=simd<T,N,Arch>;
240 if constexpr(detail::instruction_only_shape<T,N,Arch>) {
241 std::array<T,N> lanes{}; value.store(lanes.data());
242 std::uint64_t sum=0; for(auto lane:lanes) sum+=lane;
243 return sum;
244 } else {
245 using C=typename V::chunk_type;
246 constexpr auto width=V::register_lanes;
247 auto registers=value.to_native();
248 std::uint64_t sum=0;
249 for(std::size_t i=0;i<N/width;++i)
250 sum+=reduce_add_widened(C::from_native(registers[i]));
251 if constexpr(N%width) {
252 std::array<T,width> tail{};
253 C::from_native(registers.back()).store(tail.data());
254 for(std::size_t i=N%width;i<width;++i) tail[i]=0;
255 sum+=reduce_add_widened(C::load(tail.data()));
256 }
257 return sum;
258 }
259 }
260}
typename mask_traits< std::remove_cvref_t< T > >::type mask
Definition mask_traits.h:22
Architecture-tagged vectors, register packs and supporting value types. Native arithmetic follows its...
constexpr bool all(simd< T, N, A > a) noexcept
Test whether every integer lane is nonzero.
constexpr auto pairwise_add_widened(simd< T, N, Arch > value) noexcept
constexpr auto reinterpret_bits(simd< From, N, Arch > value) noexcept -> simd< To, sizeof(From) *N/sizeof(To), Arch >
constexpr simd< T, N, Arch > popcount(simd< T, N, Arch > value) noexcept
constexpr std::uint64_t reduce_add_widened(simd< T, N, Arch > value) noexcept
Omitted architecture arguments use the native.simd provider's baseline.