Stratax 0.4.0rc1
Loading...
Searching...
No Matches
NumericValidation.hpp
Go to the documentation of this file.
1
4#pragma once
5
8
9#include <climits>
10#include <cstdint>
11#include <limits>
12#include <type_traits>
13
14namespace stratax::core::validation {
15
37template<Numeric L, Numeric R>
38void require_valid_division(const L& lhs, const R& rhs)
39{
40 if (rhs == R{})
41 {
42 throw Exceptions::ZeroDivisionError("Division by zero.");
43 }
44
45 if constexpr (Integral<L> && Integral<R>)
46 {
47 using expression_type = decltype(lhs / rhs);
48 if constexpr (std::is_signed_v<expression_type>)
49 {
50 if (static_cast<expression_type>(lhs) ==
51 std::numeric_limits<expression_type>::min() &&
52 static_cast<expression_type>(rhs) == expression_type{-1})
53 {
54 throw Exceptions::OverflowError("Signed integer division overflow.");
55 }
56 }
57 }
58}
59
71template<typename Value, Integral Count>
72constexpr bool valid_shift_count(const Count& count) noexcept
73{
74 if constexpr (std::is_signed_v<std::remove_cvref_t<Count>>)
75 {
76 if (count < 0)
77 {
78 return false;
79 }
80 }
81
82 using value_type = std::remove_cvref_t<Value>;
83
84 return static_cast<std::uintmax_t>(count) <
85 sizeof(value_type) * CHAR_BIT;
86}
87
96template<typename Value, Integral Count>
97void require_valid_shift_count(const Count& count)
98{
99 if (!valid_shift_count<Value>(count))
100 {
101 throw Exceptions::ValueError("Invalid shift count.");
102 }
103}
104
105} // namespace stratax::core::validation
Dtype and array interface concepts.
Stratax exception types derived from std::runtime_error.
void require_valid_division(const L &lhs, const R &rhs)
Rejects zero divisors and unrepresentable native signed division.
void require_valid_shift_count(const Count &count)
Rejects shift counts outside the stored value type's bit width.
constexpr bool valid_shift_count(const Count &count) noexcept
Tests whether a shift count is valid for a value type.
Explicitly detected numeric overflow, including signed division minimum by minus one; other arithmeti...
Invalid argument value, such as an invalid shift count or null nonempty view.
An explicitly checked divisor is zero.
Supported integer dtype, excluding bool.
Definition Concepts.hpp:62