17template<
typename T, std::size_t... Is>
20 std::index_sequence<Is...>)
23 return {(
static_cast<void>(Is), element)...};
31 return CHAR_BIT *
sizeof(T);
34template<
typename T, std::
size_t N>
35inline constexpr std::array<T, N>
make_array(
const T& element)
40template<std::
integral DstType, std::
integral SrcType>
43 using SrcLimits = std::numeric_limits<SrcType>;
44 using DstLimits = std::numeric_limits<DstType>;
51 constexpr bool mayHavePositiveOverflow = std::cmp_greater(SrcLimits::max(), DstLimits::max());
52 constexpr bool mayHaveNegativeOverflow = std::cmp_less(SrcLimits::lowest(), DstLimits::lowest());
54 if constexpr(mayHavePositiveOverflow)
56 if(std::cmp_greater(src, DstLimits::max()))
59 src, DstLimits::max());
63 if constexpr(mayHaveNegativeOverflow)
65 if(std::cmp_less(src, DstLimits::lowest()))
68 src, DstLimits::lowest());
73 return static_cast<DstType
>(src);
76template<std::
floating_po
int DstType, std::
floating_po
int SrcType>
80 if constexpr(std::is_same_v<SrcType, DstType>)
85 else if constexpr(
sizeof(DstType) >
sizeof(SrcType))
88 static_assert(std::numeric_limits<SrcType>::is_iec559);
89 static_assert(std::numeric_limits<DstType>::is_iec559);
91 return static_cast<DstType
>(src);
96 const auto dst =
static_cast<DstType
>(src);
97 const auto dstBackToSrc =
static_cast<SrcType
>(dst);
98 if(src != dstBackToSrc)
108template<
typename DstType,
typename SrcType>
112 if constexpr(std::is_integral_v<SrcType> && std::is_integral_v<DstType>)
117 else if constexpr(std::is_integral_v<SrcType> && std::is_floating_point_v<DstType>)
124 else if constexpr(std::is_floating_point_v<SrcType> && std::is_integral_v<DstType>)
133 static_assert(std::is_floating_point_v<SrcType> && std::is_floating_point_v<DstType>);
139template<
typename DstType,
typename SrcType>
#define PH_ASSERT(condition)
Definition assertion.h:49
#define PH_ASSERT_UNREACHABLE_SECTION()
Definition assertion.h:52
constexpr std::array< T, sizeof...(Is)> make_array(T element, std::index_sequence< Is... >)
Definition utility.ipp:18
The root for all renderer implementations.
Definition assertion.h:9
DstType lossless_integer_cast(const SrcType &src)
Definition utility.ipp:41
void throw_formatted(const std::format_string< Args... > msgFormat, Args &&... args)
Definition exceptions.h:85
constexpr std::array< T, N > make_array(const T &element)
Creates an std::array filled with the same element.
Definition utility.ipp:35
DstType lossless_cast(const SrcType &src)
Cast numeric value to another type without any loss of information. If there is any possible overflow...
Definition utility.ipp:109
DstType lossless_float_cast(const SrcType &src)
Definition utility.ipp:77
consteval std::size_t sizeof_in_bits()
Calculates number of bits an instance of type T occupies.
Definition utility.ipp:29