36TEST_CASE(
"Int Math Test 00",
"[sign][arithmetic][math]" ) {
50TEST_CASE(
"Int Math Test 01",
"[round][align][arithmetic][math]" ) {
74TEST_CASE(
"Int Math Test 02",
"[abs][arithmetic][math]" ) {
77 REQUIRE( 1_u64 ==
jau::abs( 1_u64) );
93 REQUIRE( 1_i64 ==
jau::abs( 1_i64) );
94 REQUIRE( 1_i64 ==
jau::abs(-1_i64) );
107 REQUIRE( INT32_MAX ==
jau::abs( INT32_MAX ) );
108 REQUIRE( INT32_MAX ==
jau::abs( INT32_MAX ) );
113 REQUIRE( INT32_MAX ==
std::abs( INT32_MAX ) );
118TEST_CASE(
"Int Math Test 03a",
"[min][max][clip][arithmetic][math]" ) {
119 REQUIRE( 0 ==
jau::min( 0, INT32_MAX ) );
120 REQUIRE( INT32_MAX ==
jau::max( 0, INT32_MAX ) );
121 REQUIRE( INT32_MAX-1==
jau::min( INT32_MAX-1, INT32_MAX ) );
122 REQUIRE( INT32_MAX ==
jau::max( INT32_MAX-1, INT32_MAX ) );
123 REQUIRE( INT32_MIN ==
jau::min( 0, INT32_MIN ) );
124 REQUIRE( 0 ==
jau::max( 0, INT32_MIN ) );
125 REQUIRE( INT32_MIN ==
jau::min( INT32_MIN+1, INT32_MIN ) );
126 REQUIRE( INT32_MIN+1==
jau::max( INT32_MIN+1, INT32_MIN ) );
128 REQUIRE( -10 ==
jau::clamp( INT32_MIN, -10, 10 ) );
129 REQUIRE( 10 ==
jau::clamp( INT32_MAX, -10, 10 ) );
132TEST_CASE(
"Int Math Test 03b",
"[ct_min][ct_max][clip2][arithmetic][math]" ) {
134 REQUIRE( INT32_MAX ==
jau::ct_max( 0, INT32_MAX ) );
135 REQUIRE( INT32_MAX-1==
jau::ct_min( INT32_MAX-1, INT32_MAX ) );
136 REQUIRE( INT32_MAX ==
jau::ct_max( INT32_MAX-1, INT32_MAX ) );
137 REQUIRE( INT32_MIN+1 ==
jau::ct_min( 0, INT32_MIN+1 ) );
139 REQUIRE( INT32_MIN ==
jau::ct_min( INT32_MIN+1, INT32_MIN ) );
140 REQUIRE( INT32_MIN+1==
jau::ct_max( INT32_MIN+1, INT32_MIN ) );
147TEST_CASE(
"Int Math Test 10",
"[bits][arithmetic][math]" ) {
149 REQUIRE( 0b0000000000000000U ==
ct_masked_merge( 0b0000000000000000U, 0b0000000000000000U, 0b0000000000000000U ) );
150 REQUIRE( 0b1100000000000011U ==
ct_masked_merge( 0b1111111100000000U, 0b1100000000000000U, 0b0000000000000011U ) );
151 REQUIRE( 64_u32 ==
ct_masked_merge( 0b1111111111111111U, 64_u32, 256_u32 ) );
152 REQUIRE( 256_u32 ==
ct_masked_merge( 0b0000000000000000U, 64_u32, 256_u32 ) );
167 REQUIRE( 0 ==
ct_bit_count( 0b00000000000000000000000000000000UL ) );
168 REQUIRE( 1 ==
ct_bit_count( 0b00000000000000000000000000000001UL ) );
169 REQUIRE( 1 ==
ct_bit_count( 0b10000000000000000000000000000000UL ) );
170 REQUIRE( 16 ==
ct_bit_count( 0b10101010101010101010101010101010UL ) );
171 REQUIRE( 16 ==
ct_bit_count( 0b01010101010101010101010101010101UL ) );
172 REQUIRE( 32 ==
ct_bit_count( 0b11111111111111111111111111111111UL ) );
175 REQUIRE( 0 ==
high_bit( 0b00000000U ) );
176 REQUIRE( 1 ==
high_bit( 0b00000001U ) );
177 REQUIRE( 2 ==
high_bit( 0b00000010U ) );
178 REQUIRE( 2 ==
high_bit( 0b00000011U ) );
179 REQUIRE( 8 ==
high_bit( 0b11000011U ) );
181 REQUIRE( 64 ==
high_bit( 0b1100001111000011110000111100001111000011110000111100001111000011UL ) );
184TEST_CASE(
"Int Math Test 20",
"[add][sub][overflow][arithmetic][math]" ) {
188 uint64_t a = 1, b = 2, r;
190 REQUIRE( a + b == r );
195 REQUIRE( a + b == r );
204 uint64_t a = 2, b = 1, r;
206 REQUIRE( a - b == r );
211 REQUIRE( a - b == r );
214 uint64_t a = 1, b = 2, r;
226 int64_t a = 1, b = 2, r;
228 REQUIRE( a + b == r );
233 REQUIRE( a + b == r );
242 int64_t a = 2, b = 1, r;
244 REQUIRE( a - b == r );
249 REQUIRE( a - b == r );
252 int64_t a = 1, b = 2, r;
254 REQUIRE( a - b == r );
263TEST_CASE(
"Int Math Test 21",
"[mul][overflow][arithmetic][math]" ) {
266 uint64_t a = 1, b = 2, r;
268 REQUIRE( a * b == r );
273 REQUIRE( a * b == r );
282 int64_t a = 1, b = 2, r;
284 REQUIRE( a * b == r );
289 REQUIRE( a * b == r );
constexpr uint32_t ct_bit_count(uint32_t n) noexcept
Returns the number of set bits within given 32bit integer (w/o branching) in O(1) and constant time (...
constexpr T ct_clamp(const T x, const T min_val, const T max_val) noexcept
Returns constrained integral value to lie between given min- and maximum value for MIN <= x - y <= MA...
constexpr T ct_abs(const T x) noexcept
Returns the absolute value of an arithmetic number (w/o branching) in O(1) and constant time (CT),...
constexpr T ct_max(const T x, const T y) noexcept
Returns the maximum of two integrals for MIN <= x - y <= MAX (w/o branching) in O(1) and constant tim...
constexpr T ct_masked_merge(T mask, T a_if_masked, T b_if_unmasked)
Returns merged a_if_masked bits selected by mask 1 bits and b_if_unmasked bits selected by mask 0 bit...
constexpr T ct_min(const T x, const T y) noexcept
Returns the minimum of two integrals for MIN <= x - y <= MAX (w/o branching) in O(1) and constant tim...
constexpr T clamp(const T x, const T min_val, const T max_val) noexcept
Returns constrained integral value to lie between given min- and maximum value (w/ branching) in O(1)...
constexpr T round_up(const T n, const U align_to)
Round up w/ branching in O(1)
constexpr nsize_t high_bit(T x)
Return the index of the highest set bit w/ branching (loop) in O(n), actually O(n/2).
constexpr T min(const T x, const T y) noexcept
Returns the minimum of two integrals (w/ branching) in O(1)
constexpr bool mul_overflow(const T a, const T b, T &res) noexcept
Integer overflow aware multiplication returning true if overflow occurred, otherwise false having the...
constexpr bool sub_overflow(const T a, const T b, T &res) noexcept
Integer overflow aware subtraction returning true if overflow occurred, otherwise false having the re...
constexpr uint32_t round_to_power_of_2(const uint32_t n)
If the given n is not is_power_of_2() return next_power_of_2(), otherwise return n unchanged.
constexpr int sign(const T x) noexcept
Returns the value of the sign function (w/o branching ?) in O(1).
constexpr T max(const T x, const T y) noexcept
Returns the maximum of two integrals (w/ branching) in O(1)
constexpr T round_down(T n, U align_to)
Round down w/ branching in O(1)
constexpr bool add_overflow(const T a, const T b, T &res) noexcept
Integer overflow aware addition returning true if overflow occurred, otherwise false having the resul...
constexpr T abs(const T x) noexcept
Returns the absolute value of an arithmetic number (w/ branching) in O(1)
constexpr bool is_power_of_2(const T x) noexcept
Power of 2 test (w/o branching ?) in O(1)
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
TEST_CASE("Int Math Test 00", "[sign][arithmetic][math]")