12#ifndef BOTAN_BIT_OPS_H_
13#define BOTAN_BIT_OPS_H_
15#include <botan/types.h>
24 requires(std::is_integral<T>::value)
26 return static_cast<T>(0) - (a >> (
sizeof(
T) * 8 - 1));
34 requires(std::is_integral<T>::value)
46 requires(std::is_unsigned<T>::value)
48 return (arg != 0) && (arg != 1) && ((arg &
static_cast<T>(arg - 1)) == 0);
59 requires(std::is_unsigned<T>::value)
63 for(
size_t s = 8 *
sizeof(
T) / 2; s > 0; s /= 2) {
64 const size_t z = s * ((~ct_is_zero(n >> s)) & 1);
81 requires(std::is_integral<T>::value)
85 for(
size_t s = 8 *
sizeof(n) / 2; s >= 8; s /= 2) {
86 const size_t z = s * (~ct_is_zero(n >> s) & 1);
102inline constexpr size_t ctz(
T n)
103 requires(std::is_integral<T>::value)
111 for(
size_t s = 8 *
sizeof(
T) / 2; s > 0; s /= 2) {
112 const T mask = (
static_cast<T>(1) << s) - 1;
113 const size_t z = s * (
ct_is_zero(n & mask) & 1);
123 requires(std::is_integral<T>::value &&
sizeof(
T) < 32)
125 if(x >> (
sizeof(
T) * 8 - 1)) {
126 return sizeof(
T) * 8;
148template <std::
unsigned_
integral T>
150 return (a +
b - 1) /
b;
158 requires(std::is_integral<T>::value)
160 return (bits + 7) / 8;
165#if BOTAN_COMPILER_HAS_BUILTIN(__builtin_ctz)
169 return __builtin_ctz(n);
181 const T swap = ((x >> shift) ^ x) & mask;
182 return (x ^ swap) ^ (swap << shift);
187 const T swap = ((x >> shift) ^ y) & mask;
195 return (
b ^ (mask & (a ^
b)));
constexpr bool is_power_of_2(T arg)
constexpr size_t var_ctz32(uint32_t n)
constexpr T choose(T mask, T a, T b)
constexpr size_t high_bit(T n)
constexpr void swap_bits(T &x, T &y, T mask, size_t shift)
constexpr T ceil_division(T a, T b)
constexpr size_t ctz(T n)
constexpr T majority(T a, T b, T c)
constexpr T ceil_tobytes(T bits)
constexpr T ct_is_zero(T x)
constexpr size_t significant_bytes(T n)
constexpr uint8_t ceil_log2(T x)
constexpr T expand_top_bit(T a)
constexpr T bit_permute_step(T x, T mask, size_t shift)