7#ifndef BOTAN_INT_UTILS_H_
8#define BOTAN_INT_UTILS_H_
10#include <botan/concepts.h>
11#include <botan/exceptn.h>
12#include <botan/strong_type.h>
13#include <botan/types.h>
18template <std::
unsigned_
integral T>
27template <std::
unsigned_
integral T>
35template <std::unsigned_integral
T, std::unsigned_integral... Ts>
36 requires all_same_v<
T, Ts...>
45template <std::
unsigned_
integral T>
50 const T r = (1U * a) *
b;
53 if(a != 0 && r / a !=
b) {
59template <
typename RT,
typename ExceptionType,
typename AT>
60 requires std::integral<strong_type_wrapped_type<RT>> && std::integral<strong_type_wrapped_type<AT>>
66 throw ExceptionType(error_msg_on_fail);
72template <
typename RT,
typename AT>
73 requires std::integral<strong_type_wrapped_type<RT>> && std::integral<strong_type_wrapped_type<AT>>
90template <std::
unsigned_
integral T>
93 constexpr T hi1 = (
static_cast<T>(-1) / 255) << 7;
95 constexpr T lo7 =
static_cast<T>(~hi1);
96 T r = (lo7 - a +
b) & hi1;
98 return (r << 1) - (r >> 7);
113template <std::
unsigned_
integral T>
116 constexpr T hi1 = (
static_cast<T>(-1) / 255) << 7;
118 constexpr T lo7 = ~hi1;
120 const T sub = ((v | hi1) - (lower & lo7)) ^ ((v ^ (~lower)) & hi1);
121 const T a_lo = sub & lo7;
122 const T a_hi = sub & hi1;
123 return (lo7 - a_lo + upper) & hi1 & ~a_hi;
129template <std::
unsigned_
integral T>
132 constexpr T lo1 = (
static_cast<T>(-1) / 255);
134 constexpr T hi1 = lo1 << 7;
136 constexpr size_t bits = (
sizeof(
T) * 8) - 8;
138 return static_cast<size_t>((((((v & hi1) - 1) & lo1) * lo1) >> bits) - 1);
constexpr std::optional< T > checked_add(T a, T b)
constexpr T swar_lt(T a, T b)
constexpr std::optional< T > checked_sub(T a, T b)
constexpr RT checked_cast_to_or_throw(AT i, std::string_view error_msg_on_fail)
constexpr RT checked_cast_to(AT i)
constexpr T swar_in_range(T v, T lower, T upper)
constexpr size_t index_of_first_set_byte(T v)
constexpr decltype(auto) unwrap_strong_type(T &&t)
Generically unwraps a strong type to its underlying type.
typename detail::wrapped_type_helper< std::remove_cvref_t< T > >::type strong_type_wrapped_type
Extracts the wrapped type from a strong type.
constexpr std::optional< T > checked_mul(T a, T b)
constexpr decltype(auto) wrap_strong_type(ParamT &&t)
Wraps a value into a caller-defined (strong) type.