Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
test_int_math_perf01.cpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2024 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24#include <thread>
25#include <cassert>
26#include <cinttypes>
27#include <cstring>
28
30
31#include <jau/int_math.hpp>
32
33using namespace jau;
34using namespace jau::int_literals;
35
36TEST_CASE( "Int Math Bench 01a", "[abs][benchmark][arithmetic][math]" ) {
37 BENCHMARK("jau::abs Benchmark") {
38 REQUIRE( 1 == jau::abs( 1) );
39 REQUIRE( 1 == jau::abs(-1) );
40 REQUIRE( 1_i64 == jau::abs( 1_i64) );
41 REQUIRE( 1_i64 == jau::abs(-1_i64) );
43 REQUIRE( INT32_MAX == jau::abs( INT32_MAX ) );
44 };
45}
46TEST_CASE( "Int Math Bench 01b", "[ct_abs][benchmark][arithmetic][math]" ) {
47 BENCHMARK("jau::ct_abs Benchmark") {
48 REQUIRE( 1 == jau::ct_abs( 1) );
49 REQUIRE( 1 == jau::ct_abs(-1) );
50 REQUIRE( 1_i64 == jau::ct_abs( 1_i64) );
51 REQUIRE( 1_i64 == jau::ct_abs(-1_i64) );
53 // REQUIRE( std::numeric_limits<int64_t>::max() == jau::ct_abs( std::numeric_limits<int64_t>::min() ) ); // UB
54 REQUIRE( INT32_MAX == jau::ct_abs( INT32_MAX ) );
55 // REQUIRE( INT32_MAX == jau::ct_abs( INT32_MIN ) ); // UB
56 };
57}
58TEST_CASE( "Int Math Bench 01c", "[abs][benchmark][arithmetic][math]" ) {
59 BENCHMARK("std::abs Benchmark") {
60 REQUIRE( 1 == std::abs( 1) );
61 REQUIRE( 1 == std::abs(-1) );
62 REQUIRE( 1_i64 == std::abs( 1_i64) );
63 REQUIRE( 1_i64 == std::abs(-1_i64) );
65 REQUIRE( INT32_MAX == std::abs( INT32_MAX ) );
66 };
67}
68
69TEST_CASE( "Int Math Bench 02a", "[min][max][benchmark][arithmetic][math]" ) {
70 BENCHMARK("MinMax Benchmark") {
71 REQUIRE( 0 == jau::min( 0, INT32_MAX ) );
72 REQUIRE( INT32_MAX == jau::max( 0, INT32_MAX ) );
73 REQUIRE( INT32_MAX-1== jau::min( INT32_MAX-1, INT32_MAX ) );
74 REQUIRE( INT32_MAX == jau::max( INT32_MAX-1, INT32_MAX ) );
75 REQUIRE( INT32_MIN == jau::min( 0, INT32_MIN ) );
76 REQUIRE( 0 == jau::max( 0, INT32_MIN ) );
77 REQUIRE( INT32_MIN == jau::min( INT32_MIN+1, INT32_MIN ) );
78 REQUIRE( INT32_MIN+1== jau::max( INT32_MIN+1, INT32_MIN ) );
79 };
80}
81TEST_CASE( "Int Math Bench 03a", "[ct_min][ct_max][benchmark][arithmetic][math]" ) {
82 BENCHMARK("Min2Max2 Benchmark") {
83 REQUIRE( 0 == jau::ct_min( 0, INT32_MAX ) );
84 REQUIRE( INT32_MAX == jau::ct_max( 0, INT32_MAX ) );
85 REQUIRE( INT32_MAX-1== jau::ct_min( INT32_MAX-1, INT32_MAX ) );
86 REQUIRE( INT32_MAX == jau::ct_max( INT32_MAX-1, INT32_MAX ) );
87 REQUIRE( INT32_MIN+1 == jau::ct_min( 0, INT32_MIN+1 ) ); // limitation: `MIN <= x - y <= MAX`
88 REQUIRE( 0 == jau::ct_max( 0, INT32_MIN+1 ) ); // limitation: `MIN <= x - y <= MAX`
89 REQUIRE( INT32_MIN == jau::ct_min( INT32_MIN+1, INT32_MIN ) );
90 REQUIRE( INT32_MIN+1== jau::ct_max( INT32_MIN+1, INT32_MIN ) );
91 };
92}
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),...
Definition: int_math_ct.hpp:95
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_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 min(const T x, const T y) noexcept
Returns the minimum of two integrals (w/ branching) in O(1)
Definition: base_math.hpp:177
constexpr T max(const T x, const T y) noexcept
Returns the maximum of two integrals (w/ branching) in O(1)
Definition: base_math.hpp:191
constexpr T abs(const T x) noexcept
Returns the absolute value of an arithmetic number (w/ branching) in O(1)
Definition: base_math.hpp:155
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
Definition: backtrace.hpp:32
TEST_CASE("Int Math Bench 01a", "[abs][benchmark][arithmetic][math]")