27#include <jau/test/catch2_ext.hpp>
35TEST_CASE(
"Int Math Bench 01a",
"[abs][benchmark][arithmetic][math]" ) {
36 BENCHMARK(
"jau::abs Benchmark") {
39 REQUIRE( 1_i64 ==
jau::abs( 1_i64) );
40 REQUIRE( 1_i64 ==
jau::abs(-1_i64) );
41 REQUIRE( std::numeric_limits<int64_t>::max() ==
jau::abs( std::numeric_limits<int64_t>::max() ) );
42 REQUIRE( INT32_MAX ==
jau::abs( INT32_MAX ) );
45TEST_CASE(
"Int Math Bench 01b",
"[ct_abs][benchmark][arithmetic][math]" ) {
46 BENCHMARK(
"jau::ct_abs Benchmark") {
51 REQUIRE( std::numeric_limits<int64_t>::max() ==
jau::ct_abs( std::numeric_limits<int64_t>::max() ) );
57TEST_CASE(
"Int Math Bench 01c",
"[abs][benchmark][arithmetic][math]" ) {
58 BENCHMARK(
"std::abs Benchmark") {
59 REQUIRE( 1 == std::abs( 1) );
60 REQUIRE( 1 == std::abs(-1) );
61 REQUIRE( 1_i64 == std::abs( 1_i64) );
62 REQUIRE( 1_i64 == std::abs(-1_i64) );
63 REQUIRE( std::numeric_limits<int64_t>::max() == std::abs( std::numeric_limits<int64_t>::max() ) );
64 REQUIRE( INT32_MAX == std::abs( INT32_MAX ) );
68TEST_CASE(
"Int Math Bench 02a",
"[min][max][benchmark][arithmetic][math]" ) {
69 BENCHMARK(
"MinMax Benchmark") {
70 REQUIRE( 0 ==
jau::min( 0, INT32_MAX ) );
71 REQUIRE( INT32_MAX ==
jau::max( 0, INT32_MAX ) );
72 REQUIRE( INT32_MAX-1==
jau::min( INT32_MAX-1, INT32_MAX ) );
73 REQUIRE( INT32_MAX ==
jau::max( INT32_MAX-1, INT32_MAX ) );
74 REQUIRE( INT32_MIN ==
jau::min( 0, INT32_MIN ) );
75 REQUIRE( 0 ==
jau::max( 0, INT32_MIN ) );
76 REQUIRE( INT32_MIN ==
jau::min( INT32_MIN+1, INT32_MIN ) );
77 REQUIRE( INT32_MIN+1==
jau::max( INT32_MIN+1, INT32_MIN ) );
80TEST_CASE(
"Int Math Bench 03a",
"[ct_min][ct_max][benchmark][arithmetic][math]" ) {
81 BENCHMARK(
"Min2Max2 Benchmark") {
83 REQUIRE( INT32_MAX ==
jau::ct_max( 0, INT32_MAX ) );
84 REQUIRE( INT32_MAX-1==
jau::ct_min( INT32_MAX-1, INT32_MAX ) );
85 REQUIRE( INT32_MAX ==
jau::ct_max( INT32_MAX-1, INT32_MAX ) );
86 REQUIRE( INT32_MIN+1 ==
jau::ct_min( 0, INT32_MIN+1 ) );
88 REQUIRE( INT32_MIN ==
jau::ct_min( INT32_MIN+1, INT32_MIN ) );
89 REQUIRE( INT32_MIN+1==
jau::ct_max( INT32_MIN+1, INT32_MIN ) );
98 bool __attribute__ ((noinline))
101 return hi.x >= o.lo.x &&
106 bool __attribute__ ((noinline))
109 return !(
hi.x < o.lo.x ||
114 bool __attribute__ ((noinline))
119 return lo_.
x <= hi_.
x && lo_.
y <= hi_.
y;
124 return hi.x >= o.
lo.
x &&
131 return !(
hi.x < o.
lo.
x ||
140 return lo_.
x <= hi_.
x && lo_.
y <= hi_.
y;
146TEST_CASE(
"Int Math Bench 04a",
"[intersect][benchmark][arithmetic][math]" ) {
150 std::uniform_int_distribution<int32_t> rint(0,50);
152 const int loops = catch_auto_run ? 1000 : 1000000;
153 size_t isect_count=0;
154 std::vector<AABBox> va, vb;
155 for(
int i=0; i<
loops; ++i) {
156 Point2i lo(rint(rng), rint(rng));
157 Point2i hi(lo.
x+rint(rng), lo.
y+rint(rng));
158 AABBox a { .lo=lo, .hi=hi };
159 lo =
Point2i(rint(rng), rint(rng));
160 hi =
Point2i(lo.
x+rint(rng), lo.
y+rint(rng));
161 AABBox b { .lo=lo, .hi=hi };
164 bool i1a = a.intersects1a(b);
165 bool i1b = a.intersects1b(b);
166 bool i1c = a.intersects1c(b);
173 REQUIRE( i1a == i1b );
174 REQUIRE( i1a == i1c );
176 bool i2a = a.intersects2a(b);
177 bool i2b = a.intersects2b(b);
178 bool i2c = a.intersects2c(b);
179 REQUIRE( i1a == i2a );
180 REQUIRE( i2a == i2b );
181 REQUIRE( i2a == i2c );
183 std::cout <<
"isect_count " << isect_count <<
"/" << va.size() <<
", " << 100.0f*( (float)isect_count / (float)va.size() ) <<
"%" << std::endl;
185 BENCHMARK(
"Intersect1a Benchmark") {
187 for(
size_t i = 0; i < va.size(); ++i) {
194 BENCHMARK(
"Intersect1b Benchmark") {
196 for(
size_t i = 0; i < va.size(); ++i) {
203 BENCHMARK(
"Intersect1c Benchmark") {
205 for(
size_t i = 0; i < va.size(); ++i) {
212 BENCHMARK(
"Intersect2a Benchmark") {
214 for(
size_t i = 0; i < va.size(); ++i) {
221 BENCHMARK(
"Intersect2b Benchmark") {
223 for(
size_t i = 0; i < va.size(); ++i) {
230 BENCHMARK(
"Intersect2c Benchmark") {
232 for(
size_t i = 0; i < va.size(); ++i) {
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 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 min(const T x, const T y) noexcept
Returns the minimum of two integrals (w/ 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 abs(const T x) noexcept
Returns the absolute value of an arithmetic number (w/ branching) in O(1)
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
bool intersects1b(const AABBox &o) const
bool intersects2c(const AABBox &o) const
bool intersects1c(const AABBox &o) const
bool intersects2b(const AABBox &o) const
bool intersects1a(const AABBox &o) const
bool intersects2a(const AABBox &o) const
TEST_CASE("Int Math Bench 01a", "[abs][benchmark][arithmetic][math]")