Intel xvec
Experimental C++ simd class library
Loading...
Searching...
No Matches
std_bit.hpp
1//===----------------------------------------------------------------------===//
2//
3// Copyright (C) 2021 Intel Corporation
4//
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#pragma once
10
11#include <xvec/detail/config.hpp>
12
13#include <cstdint>
14#include <cstddef>
15#include <concepts>
16#include <limits> // std::numeric_limits
17
18namespace _XVEC_NAMESPACE::simd
19{
20
28
37template<vec_unsigned_integer _Up, vec_integral _Sp>
38 requires (_Up::size() == _Sp::size() &&
39 sizeof(typename _Up::value_type) == sizeof(typename _Sp::value_type))
40constexpr _Up rotl(const _Up& u, const _Sp& s)
41{
42 if (std::is_constant_evaluated())
43 return _Up([=](auto i) { return typename _Up::value_type(std::rotl(u[i], s[i])); });
44 else
45 return detail::rotate_left(target, u, s);
46}
47
52template<vec_unsigned_integer _Up, std::integral _Sp>
53constexpr _Up rotl(const _Up& u, _Sp s) {
54 using _Rp = std::make_signed_t<typename _Up::value_type>;
55 return rotl(u, rebind_t<_Rp, _Up>(_Rp(s))); }
56
65template<vec_unsigned_integer _Up, vec_integral _Sp>
66 requires (_Up::size() == _Sp::size() &&
67 sizeof(typename _Up::value_type) == sizeof(typename _Sp::value_type))
68constexpr _Up rotr(const _Up& u, const _Sp& s)
69{
70 if (std::is_constant_evaluated())
71 return _Up([=](auto i) { return typename _Up::value_type(std::rotr(u[i], s[i])); });
72 else
73 return detail::rotate_right(target, u, s);
74}
75
80template<vec_unsigned_integer _Up, std::integral _Sp>
81constexpr _Up rotr(const _Up& u, _Sp s) {
82 using _Rp = std::make_signed_t<typename _Up::value_type>;
83 return rotr(u, rebind_t<_Rp, _Up>(_Rp(s))); }
84
89template<vec_unsigned_integer _Vp>
90constexpr auto countl_zero(const _Vp& v) noexcept {
91 using _Rp = std::make_signed_t<typename _Vp::value_type>;
92 if (std::is_constant_evaluated())
93 return rebind_t<_Rp, _Vp>([=](auto i){ return _Rp(std::countl_zero(v[i])); });
94 else
95 return rebind_t<_Rp, _Vp>(detail::clz(target, v));
96}
97
102template<vec_unsigned_integer _Vp> constexpr auto countl_one(const _Vp& x) noexcept { return countl_zero(~x); }
103
108template<vec_unsigned_integer _Vp>
109constexpr auto countr_zero(const _Vp& v) noexcept {
110 using _Rp = std::make_signed_t<typename _Vp::value_type>;
111 if (std::is_constant_evaluated())
112 return rebind_t<_Rp, _Vp>([&](auto i){ return _Rp(std::countr_zero(v[i])); });
113 else
114 {
115 // Remove all bits except those to right of the lowest set bit
116 // http://0x80.pl/notesen/2023-01-31-avx512-bsf.html
117 auto lsbBits = ~v & (v - detail::cw<1>);
118 return rebind_t<_Rp, _Vp>(detail::popcount(target, lsbBits));
119 }
120}
121
126template<vec_unsigned_integer _Vp> constexpr auto countr_one(const _Vp& x) noexcept { return countr_zero(~x); }
127
132template<vec_unsigned_integer _Vp>
133constexpr auto popcount(const _Vp& x) noexcept {
134 using _Rp = std::make_signed_t<typename _Vp::value_type>;
135 if (std::is_constant_evaluated())
136 return rebind_t<_Rp, _Vp>([=](auto i) { return _Rp(std::popcount(x[i])); });
137 else
138 return rebind_t<_Rp, _Vp>(detail::popcount(target, x));
139}
140
145template<vec_unsigned_integer _Vp>
146constexpr typename _Vp::mask_type has_single_bit(const _Vp& x) noexcept {
147 return detail::has_single_bit(target, x);
148}
149
154template<vec_unsigned_integer _Vp>
155constexpr rebind_t<std::make_signed_t<typename _Vp::value_type>, _Vp>
156bit_width(const _Vp& x) noexcept {
157 using _R = rebind_t<std::make_signed_t<typename _Vp::value_type>, _Vp>;
158 return _R(detail::cw<std::numeric_limits<typename _Vp::value_type>::digits> - countl_zero(x));
159}
160
165template<vec_unsigned_integer _Vp>
166constexpr _Vp bit_ceil(const _Vp& x) noexcept {
167 if (std::is_constant_evaluated())
168 return _Vp([=](auto i) { return std::bit_ceil(x[i]); });
169 else
170 {
171 // Normally bit_ceil would need to be wary of shifts, since some types get
172 // promoted and would give the wrong result. This is't a problem for SIMD
173 // operations since promotion doesn't occur.
174 constexpr auto one = _Vp(std::uint8_t(1));
175 auto shift_amount = _Vp(bit_width(x - one));
176 return select(x < std::uint8_t(2), one, one << shift_amount);
177 }
178}
179
185template<vec_unsigned_integer _Vp>
186constexpr _Vp bit_floor(const _Vp& v) noexcept {
187 using _Tp = typename _Vp::value_type;
188 if (std::is_constant_evaluated())
189 return _Vp([=](auto i) { return std::bit_floor(v[i]); });
190 else
191 {
192 auto log2 = rebind_cast<_Tp>(detail::cw<std::numeric_limits<_Tp>::digits - 1> - countl_zero(v));
193 return select(v != _Vp(), std::uint8_t(1) << log2, _Vp());
194 }
195}
196
198
207
213template<vec_unsigned_integer _Vp>
214constexpr _Vp bit_reverse(const _Vp& x) noexcept {
215 if (std::is_constant_evaluated())
216 return detail::bitreverse(generic_tag{}, x);
217 else
218 return detail::bitreverse(target, x); }
219
225template<vec_integral _Vp>
226constexpr _Vp byteswap(const _Vp& v) noexcept {
227 if (std::is_constant_evaluated())
228 {
229 // byteswap only exists in C++23, so until then make our own.
230 auto bswap = [](auto b) {
231 auto value_representation = std::bit_cast<std::array<std::byte, sizeof(b)>>(b);
232 std::ranges::reverse(value_representation);
233 return std::bit_cast<decltype(b)>(value_representation);
234 };
235 return _Vp([=](auto i) { return bswap(v[i]); });
236 }
237
238 return detail::byteswap(target, v);
239}
240
249
252
263template<vec_unsigned_integer _Up, vec_integral _Sp>
264 requires (sizeof(typename _Up::value_type) == sizeof(typename _Sp::value_type))
265constexpr _Up funnel_shift_right(const _Up& high, const _Up& low, const _Sp& s)
266{
267 if (std::is_constant_evaluated())
268 return _Up([=](auto i) { return typename _Up::value_type(detail::funnel_shift_right(high[i], low[i], s[i])); });
269 else
270 return detail::fsr(target, high, low, s);
271}
272
279template<vec_unsigned_integer _Up, std::integral _Sp>
280constexpr _Up funnel_shift_right(const _Up& high, const _Up& low, _Sp s)
281 { return funnel_shift_right(high, low, _Up(typename _Up::value_type(s))); }
282
293template<vec_unsigned_integer _Up, vec_integral _Sp>
294 requires (sizeof(typename _Up::value_type) == sizeof(typename _Sp::value_type))
295constexpr _Up funnel_shift_left(const _Up& high, const _Up& low, const _Sp& s)
296{
297 if (std::is_constant_evaluated())
298 return _Up([=](auto i) { return typename _Up::value_type(detail::funnel_shift_left(high[i], low[i], s[i])); });
299 else
300 return detail::fsl(target, high, low, s);
301}
302
309template<vec_unsigned_integer _Up, std::integral _Sp>
310constexpr _Up funnel_shift_left(const _Up& high, const _Up& low, _Sp s)
311 { return funnel_shift_left(high, low, _Up(typename _Up::value_type(s))); }
312
314
315} // namespace _XVEC_NAMESPACE::simd
316
317namespace _XVEC_NAMESPACE
318{
319 using simd::byteswap;
320 using simd::bit_floor;
321 using simd::bit_ceil;
322 using simd::has_single_bit;
323 using simd::rotl;
324 using simd::rotr;
325 using simd::bit_width;
326 using simd::countl_zero;
327 using simd::countl_one;
328 using simd::countr_zero;
329 using simd::countr_one;
330 using simd::popcount;
331 using simd::funnel_shift_left;
332 using simd::funnel_shift_right;
333}
constexpr auto select(const _M &m, const _T &if_true, const _U &if_false) noexcept -> decltype(simd_select_impl(m, if_true, if_false))
Select between corresponding elements according to a mask.
Definition std_algorithm.hpp:88
constexpr rebind_t< std::make_signed_t< typename _Vp::value_type >, _Vp > bit_width(const _Vp &x) noexcept
Compute the number of bits needed to represent each element.
Definition std_bit.hpp:156
constexpr auto countl_zero(const _Vp &v) noexcept
Count consecutive zero bits from the most significant bit of each element.
Definition std_bit.hpp:90
constexpr auto countr_one(const _Vp &x) noexcept
Count consecutive one bits from the least significant bit of each element.
Definition std_bit.hpp:126
constexpr _Vp bit_floor(const _Vp &v) noexcept
Compute the largest integral power of two not greater than each element. Zero input elements produce ...
Definition std_bit.hpp:186
constexpr auto countl_one(const _Vp &x) noexcept
Count consecutive one bits from the most significant bit of each element.
Definition std_bit.hpp:102
constexpr auto popcount(const _Vp &x) noexcept
Count the one bits in each element.
Definition std_bit.hpp:133
constexpr _Up rotr(const _Up &u, const _Sp &s)
Rotate each unsigned element right by its corresponding offset. Bits rotated off one end reappear at ...
Definition std_bit.hpp:68
constexpr auto countr_zero(const _Vp &v) noexcept
Count consecutive zero bits from the least significant bit of each element.
Definition std_bit.hpp:109
constexpr _Vp::mask_type has_single_bit(const _Vp &x) noexcept
Identify elements that are integral powers of two.
Definition std_bit.hpp:146
constexpr _Vp bit_ceil(const _Vp &x) noexcept
Compute the smallest integral power of two not smaller than each element.
Definition std_bit.hpp:166
constexpr _Vp byteswap(const _Vp &v) noexcept
Reverse the bytes within each integral element.
Definition std_bit.hpp:226
constexpr _Up rotl(const _Up &u, const _Sp &s)
Rotate each unsigned element left by its corresponding offset. Bits rotated off one end reappear at t...
Definition std_bit.hpp:40
constexpr _Vp bit_reverse(const _Vp &x) noexcept
Reverse the bits within each element.
Definition std_bit.hpp:214
constexpr detail::RebindCast< _Up > rebind_cast
Rebinding CPO.
Definition std_rebind.hpp:55
constexpr _Up funnel_shift_left(const _Up &high, const _Up &low, const _Sp &s)
Funnel shift corresponding element pairs left. Conceptually concatenate each high element with its co...
Definition std_bit.hpp:295
constexpr _Up funnel_shift_right(const _Up &high, const _Up &low, const _Sp &s)
Funnel shift corresponding element pairs right. Conceptually concatenate each high element with its c...
Definition std_bit.hpp:265
constexpr basic_vec< _Tp, _Abi > popcount(x86_sse_tag, const basic_vec< _Tp, _Abi > &x) noexcept
}@
Definition x86_impl.hpp:1232
constexpr T funnel_shift_right(T high, T low, S s) noexcept
Definition compat.hpp:203
constexpr basic_vec< _Tp, _Abi > clz(x86_sse_tag, const basic_vec< _Tp, _Abi > &x) noexcept
Count the number of leading zero bits in each element.
Definition x86_impl.hpp:1303
constexpr auto has_single_bit(x86_avxsnc_tag, const basic_vec< _Tp, _Abi > &x) noexcept
Query whether each element has exactly one bit set.
Definition x86_impl.hpp:1294
constexpr T funnel_shift_left(T high, T low, S s) noexcept
Definition compat.hpp:217
constexpr basic_vec< _Tp, _Abi > bitreverse(x86_sse_tag, const basic_vec< _Tp, _Abi > &x) noexcept
Count the number of leading zero bits in each element.
Definition x86_impl.hpp:1413
Definition target_config.hpp:23