aboutsummaryrefslogtreecommitdiffstats
path: root/test/test_int_math_perf01.cpp
blob: 1ffb3158af6b81bd9ec419eb2aa86ca7d98162fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
 * Author: Sven Gothel <sgothel@jausoft.com>
 * Copyright (c) 2024 Gothel Software e.K.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#include <thread>
#include <cassert>
#include <cinttypes>
#include <cstring>

#include <jau/test/catch2_ext.hpp>

#include <jau/int_math.hpp>

using namespace jau;
using namespace jau::int_literals;

TEST_CASE( "Int Math Bench 01a", "[abs][benchmark][arithmetic][math]" ) {
    BENCHMARK("jau::abs Benchmark") {
        REQUIRE( 1 == jau::abs( 1) );
        REQUIRE( 1 == jau::abs(-1) );
        REQUIRE( 1_i64 == jau::abs( 1_i64) );
        REQUIRE( 1_i64 == jau::abs(-1_i64) );
        REQUIRE( std::numeric_limits<int64_t>::max()  == jau::abs( std::numeric_limits<int64_t>::max() ) );
        REQUIRE( INT32_MAX  == jau::abs( INT32_MAX ) );
    };
}
TEST_CASE( "Int Math Bench 01b", "[ct_abs][benchmark][arithmetic][math]" ) {
    BENCHMARK("jau::ct_abs Benchmark") {
        REQUIRE( 1 == jau::ct_abs( 1) );
        REQUIRE( 1 == jau::ct_abs(-1) );
        REQUIRE( 1_i64 == jau::ct_abs( 1_i64) );
        REQUIRE( 1_i64 == jau::ct_abs(-1_i64) );
        REQUIRE( std::numeric_limits<int64_t>::max()  == jau::ct_abs( std::numeric_limits<int64_t>::max() ) );
        // REQUIRE( std::numeric_limits<int64_t>::max()  == jau::ct_abs( std::numeric_limits<int64_t>::min() ) ); // UB
        REQUIRE( INT32_MAX  == jau::ct_abs( INT32_MAX ) );
        // REQUIRE( INT32_MAX  == jau::ct_abs( INT32_MIN ) ); // UB
    };
}
TEST_CASE( "Int Math Bench 01c", "[abs][benchmark][arithmetic][math]" ) {
    BENCHMARK("std::abs Benchmark") {
        REQUIRE( 1 == std::abs( 1) );
        REQUIRE( 1 == std::abs(-1) );
        REQUIRE( 1_i64 == std::abs( 1_i64) );
        REQUIRE( 1_i64 == std::abs(-1_i64) );
        REQUIRE( std::numeric_limits<int64_t>::max()  == std::abs( std::numeric_limits<int64_t>::max() ) );
        REQUIRE( INT32_MAX  == std::abs( INT32_MAX ) );
    };
}

TEST_CASE( "Int Math Bench 02a", "[min][max][benchmark][arithmetic][math]" ) {
    BENCHMARK("MinMax Benchmark") {
        REQUIRE(         0  == jau::min( 0, INT32_MAX ) );
        REQUIRE( INT32_MAX  == jau::max( 0, INT32_MAX ) );
        REQUIRE( INT32_MAX-1== jau::min( INT32_MAX-1, INT32_MAX ) );
        REQUIRE( INT32_MAX  == jau::max( INT32_MAX-1, INT32_MAX ) );
        REQUIRE( INT32_MIN  == jau::min( 0, INT32_MIN ) );
        REQUIRE(         0  == jau::max( 0, INT32_MIN ) );
        REQUIRE( INT32_MIN  == jau::min( INT32_MIN+1, INT32_MIN ) );
        REQUIRE( INT32_MIN+1== jau::max( INT32_MIN+1, INT32_MIN ) );
    };
}
TEST_CASE( "Int Math Bench 03a", "[ct_min][ct_max][benchmark][arithmetic][math]" ) {
    BENCHMARK("Min2Max2 Benchmark") {
        REQUIRE(         0  == jau::ct_min( 0, INT32_MAX ) );
        REQUIRE( INT32_MAX  == jau::ct_max( 0, INT32_MAX ) );
        REQUIRE( INT32_MAX-1== jau::ct_min( INT32_MAX-1, INT32_MAX ) );
        REQUIRE( INT32_MAX  == jau::ct_max( INT32_MAX-1, INT32_MAX ) );
        REQUIRE( INT32_MIN+1  == jau::ct_min( 0, INT32_MIN+1 ) ); // limitation: `MIN <= x - y <= MAX`
        REQUIRE(         0  == jau::ct_max( 0, INT32_MIN+1 ) );   // limitation: `MIN <= x - y <= MAX`
        REQUIRE( INT32_MIN  == jau::ct_min( INT32_MIN+1, INT32_MIN ) );
        REQUIRE( INT32_MIN+1== jau::ct_max( INT32_MIN+1, INT32_MIN ) );
    };
}