6#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MATH_ULP_DISTANCE_HPP_
7#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MATH_ULP_DISTANCE_HPP_
12#include "arene/base/detail/is_representable_as.hpp"
16#include "arene/base/stdlib_choice/cstdint.hpp"
17#include "arene/base/stdlib_choice/enable_if.hpp"
18#include "arene/base/stdlib_choice/is_integral.hpp"
19#include "arene/base/stdlib_choice/is_same.hpp"
20#include "arene/base/stdlib_choice/is_signed.hpp"
21#include "arene/base/stdlib_choice/make_signed.hpp"
22#include "arene/base/stdlib_choice/make_unsigned.hpp"
23#include "arene/base/stdlib_choice/numeric_limits.hpp"
51template <
class T,
class = constra
ints<>>
73namespace ulp_distance_detail {
79constexpr auto most_significant_bit(I
const& value)
noexcept ->
bool {
85template <
class ResultValue>
86class ulp_distance_fn {
99 static constexpr auto to_result(T
const& value)
noexcept -> result<ResultValue, ulp_distance_error> {
100 if (!detail::is_representable_as<ResultValue>(value)) {
103 return {in_place_value,
static_cast<ResultValue
>(value)};
111 using result_type = result<ResultValue, ulp_distance_error>;
149 if (::arene::base::isnan(left) || ::arene::base::isnan(right)) {
153 using unsigned_integer_type = unsigned_integer_for_t<T>;
158 auto const left_bits = as_bits(math::abs(left));
159 auto const right_bits = as_bits(math::abs(right));
161 auto const left_signbit = most_significant_bit(as_bits(left));
162 auto const right_signbit = most_significant_bit(as_bits(right));
164 if (left_signbit == right_signbit) {
167 auto const dist = left_bits - right_bits;
168 return left_signbit ? to_result(dist) : to_result(-dist);
171 auto const dist =
static_cast<unsigned_integer_type
>(
172 static_cast<unsigned_integer_type
>(left_bits) +
static_cast<unsigned_integer_type
>(right_bits)
177 constexpr unsigned_result max_abs_lowest{
181 if (cmp_less(max_abs_lowest, dist)) {
192 static_cast<ResultValue
>(
static_cast<unsigned_result
>(-
static_cast<unsigned_result
>(dist)))
196 return to_result(dist);
203template <
class ResultValue = std::
int64_t>
204extern constexpr auto ulp_distance = ulp_distance_detail::ulp_distance_fn<ResultValue>{};
#define ARENE_BIT_CAST_CONSTEXPR
Definition bit_cast.hpp:31
static constexpr value_type value
Definition integral_constant.hpp:34
static constexpr bool is_iec559
For floating point types, true if the type adheres to the IEC559 standard, otherwise false.
Definition numeric_limits.hpp:221
static constexpr auto max() noexcept -> T
The maximum finite value.
Definition numeric_limits.hpp:230
Definition accumulate.hpp:37
constexpr bool is_invocable_v
Determine if a functor type is invocable with the specified argument types.
constexpr bool enable_ulp_distance_comparable
determines if a type can be compared with ulp_distance
constexpr bit_cast_detail::do_bit_cast< To > bit_cast
Reinterpret the object representation of one type as that of another.
ulp_distance_error
enumeration used in the result error type of ulp_distance to
Definition ulp_distance.hpp:66
@ distance_not_representable
the ULP distance is not representable in the specifed result value type
Definition ulp_distance.hpp:70
@ unordered_argument
one of the arguments is incomparable
Definition ulp_distance.hpp:68
typename constraints_impl::constraints< EnableIfs... >::type constraints
Helper used to express template constraints in a consistent way using std::enable_if_t.
Definition constraints.hpp:51
constexpr auto ulp_distance
Compute the distance in units in the last place between two floating-point values.
smallest_unsigned_integer_for< 1ULL<<(sizeof(T) *CHAR_BIT - 1)> smallest_unsigned_integer_for_type
Calculates the smallest unsigned integer type that can be used to hold values of type T.
Definition smallest_integer_for.hpp:49
Definition accumulate.hpp:36
typename make_signed< Type >::type make_signed_t
A type alias for the equivalent signed type for integral and enumeration types other than bool.
Definition make_signed.hpp:252
::uint8_t uint8_t
An 8-bit unsigned integer type.
Definition cstdint.hpp:30
typename enable_if< Condition, Type >::type enable_if_t
Type alias to support constraining templates. Is an alias for the specified type if the Condition is ...
Definition enable_if.hpp:41
typename make_unsigned< Type >::type make_unsigned_t
A type alias for the equivalent unsigned type for integral and enumeration types other than bool.
Definition make_unsigned.hpp:252
Public export header for the result subpackage.
Provides backports of the safe integer comparison functions from C++20.