1#ifndef SANISIZER_FLOAT_HPP
2#define SANISIZER_FLOAT_HPP
30template<
typename Float_>
32 static_assert(std::is_floating_point<Float_>::value);
34 assert(std::isfinite(x));
35 assert(std::trunc(x) == x);
37#ifndef SANISIZER_FLOAT_FORCE_FREXP
38 if constexpr(std::numeric_limits<Float_>::radix == 2) {
43 return std::ilogb(x) + 1;
52#ifndef SANISIZER_FLOAT_FORCE_FREXP
69template<
typename Integer_,
typename Float_>
71 static_assert(std::is_floating_point<Float_>::value);
72 static_assert(std::is_integral<Integer_>::value);
74 if (!std::isfinite(x)) {
75 throw std::range_error(
"invalid conversion of non-finite value in sanisizer::from_float");
78 throw std::out_of_range(
"negative input value in sanisizer::from_float");
82 constexpr auto output_precision = std::numeric_limits<Integer_>::digits;
84 throw std::overflow_error(
"overflow detected in sanisizer::from_float");
106template<
typename Float_,
typename Integer_>
111 constexpr auto xmax =
get_max<I<
decltype(x)> >();
112 if constexpr(xmax == 0) {
114 }
else if (val == 0) {
118 constexpr auto frad = std::numeric_limits<Float_>::radix;
119 constexpr auto fdig = std::numeric_limits<Float_>::digits;
120#ifndef SANISIZER_FLOAT_FORCE_MANUAL
121 if constexpr(frad == 2) {
122 if constexpr(std::numeric_limits<I<
decltype(val)> >::digits > fdig) {
123 if constexpr((xmax - 1) >> fdig) {
124 const auto y = (val - 1) >> fdig;
126 throw std::overflow_error(
"overflow detected in sanisizer::to_float");
133 I<
decltype(val)> working = val - 1;
134 for (I<
decltype(fdig)> d = 0; d < fdig && working; ++d) {
138 throw std::overflow_error(
"overflow detected in sanisizer::to_float");
140#ifndef SANISIZER_FLOAT_FORCE_MANUAL
Create compile-time attestations.
Sanitize sizes to avoid integer overflow.
Definition arithmetic.hpp:16
constexpr auto get_value(Value_ x)
Definition attest.hpp:105
Float_ to_float(Integer_ x)
Definition float.hpp:107
Integer_ from_float(Float_ x)
Definition float.hpp:70
int required_bits_for_float(Float_ x)
Definition float.hpp:31
constexpr auto get_max()
Definition attest.hpp:119