Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
math_integer.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <concepts>
4#include <cstdint>
5#include <stdexcept>
6#include <type_traits>
8
9namespace uni20::detail
10{
11template <class T>
12concept MathInteger = std::integral<T> && !std::same_as<T, bool>;
13
14// Accept ordinary integer literals without an implicit narrowing conversion at
15// the public API boundary. Exponents and orders share a signed 64-bit domain.
16template <MathInteger I> std::int64_t math_integer(I value)
17{
18 if constexpr (sizeof(I) >= sizeof(std::int64_t) && !std::is_signed_v<I>)
19 if (value > static_cast<I>(numeric_limits<std::int64_t>::max()))
20 throw std::out_of_range("math exponent/order does not fit int64_t");
21 if constexpr (sizeof(I) > sizeof(std::int64_t) && std::is_signed_v<I>)
22 if (value < static_cast<I>(numeric_limits<std::int64_t>::min()) ||
23 value > static_cast<I>(numeric_limits<std::int64_t>::max()))
24 throw std::out_of_range("math exponent/order does not fit int64_t");
25 return static_cast<std::int64_t>(value);
26}
27} // namespace uni20::detail
locale_t value
Definition data_table.cpp:49