sanisizer
Sanitize sizes to avoid integer overflow
Loading...
Searching...
No Matches
comparisons.hpp
Go to the documentation of this file.
1#ifndef SANISIZER_COMPARISONS_HPP
2#define SANISIZER_COMPARISONS_HPP
3
4#include "utils.hpp"
5#include "attest.hpp"
6
12namespace sanisizer {
13
25template<typename Left_, typename Right_>
26constexpr bool is_equal(Left_ left, Right_ right) {
27 return as_unsigned(get_value(left)) == as_unsigned(get_value(right));
28}
29
41template<typename Left_, typename Right_>
42constexpr bool is_less_than(Left_ left, Right_ right) {
43 return as_unsigned(get_value(left)) < as_unsigned(get_value(right));
44}
45
57template<typename Left_, typename Right_>
58constexpr bool is_less_than_or_equal(Left_ left, Right_ right) {
59 return as_unsigned(get_value(left)) <= as_unsigned(get_value(right));
60}
61
73template<typename Left_, typename Right_>
74constexpr bool is_greater_than(Left_ left, Right_ right) {
75 return as_unsigned(get_value(left)) > as_unsigned(get_value(right));
76}
77
89template<typename Left_, typename Right_>
90constexpr bool is_greater_than_or_equal(Left_ left, Right_ right) {
91 return as_unsigned(get_value(left)) >= as_unsigned(get_value(right));
92}
93
105template<typename First_, typename Second_>
106constexpr auto min(First_ first, Second_ second) {
107 const auto fval = get_value(first);
108 const auto sval = get_value(second);
109 const bool first_larger = as_unsigned(fval) > as_unsigned(sval);
110
111 if constexpr(as_unsigned(get_max<First_>()) > as_unsigned(get_max<Second_>())) {
112 if (first_larger) {
113 return sval;
114 } else {
115 return static_cast<I<decltype(sval)> >(fval);
116 }
117 } else {
118 if (first_larger) {
119 return static_cast<I<decltype(fval)> >(sval);
120 } else {
121 return fval;
122 }
123 }
124}
125
137template<typename First_, typename Second_>
138constexpr auto max(First_ first, Second_ second) {
139 const auto fval = get_value(first);
140 const auto sval = get_value(second);
141 const bool first_larger = as_unsigned(fval) > as_unsigned(sval);
142
143 if constexpr(as_unsigned(get_max<First_>()) > as_unsigned(get_max<Second_>())) {
144 if (first_larger) {
145 return fval;
146 } else {
147 return static_cast<I<decltype(fval)> >(sval);
148 }
149 } else {
150 if (first_larger) {
151 return static_cast<I<decltype(sval)> >(fval);
152 } else {
153 return sval;
154 }
155 }
156}
157
158}
159
160#endif
Create compile-time attestations.
Sanitize sizes to avoid integer overflow.
Definition arithmetic.hpp:16
constexpr bool is_greater_than(Left_ left, Right_ right)
Definition comparisons.hpp:74
constexpr auto get_value(Value_ x)
Definition attest.hpp:105
constexpr bool is_equal(Left_ left, Right_ right)
Definition comparisons.hpp:26
constexpr auto max(First_ first, Second_ second)
Definition comparisons.hpp:138
constexpr bool is_greater_than_or_equal(Left_ left, Right_ right)
Definition comparisons.hpp:90
constexpr auto get_max()
Definition attest.hpp:119
constexpr bool is_less_than_or_equal(Left_ left, Right_ right)
Definition comparisons.hpp:58
constexpr auto min(First_ first, Second_ second)
Definition comparisons.hpp:106
constexpr bool is_less_than(Left_ left, Right_ right)
Definition comparisons.hpp:42