jaulib v1.4.1
Jau Support Library (C++, Java, ..)
Loading...
Searching...
No Matches
test_math_float_perf01.cpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright Gothel Software e.K.
4 *
5 * SPDX-License-Identifier: MIT
6 *
7 * This Source Code Form is subject to the terms of the MIT License
8 * If a copy of the MIT was not distributed with this file,
9 * you can obtain one at https://opensource.org/license/mit/.
10 */
11#include <cassert>
12#include <cstring>
13
15#include <jau/test/catch2_ext.hpp>
16
17#include <jau/math/vec2f.hpp>
18
19using namespace jau;
20using namespace jau::int_literals;
21
22using namespace jau::math;
23using namespace jau::math::geom;
24
25struct AABBox {
27
28 bool __attribute__ ((noinline))
29 intersects1a(const AABBox& o) const
30 {
31 return hi.x >= o.lo.x &&
32 hi.y >= o.lo.y &&
33 lo.x <= o.hi.x &&
34 lo.y <= o.hi.y;
35 }
36 bool __attribute__ ((noinline))
37 intersects1b(const AABBox& o) const
38 {
39 return !( hi.x < o.lo.x ||
40 hi.y < o.lo.y ||
41 lo.x > o.hi.x ||
42 lo.y > o.hi.y );
43 }
44 bool __attribute__ ((noinline))
45 intersects1c(const AABBox& o) const
46 {
47 const Point2f lo_ = max(lo, o.lo);
48 const Point2f hi_ = min(hi, o.hi);
49 return lo_.x <= hi_.x && lo_.y <= hi_.y;
50 }
51
52 bool intersects2a(const AABBox& o) const
53 {
54 return hi.x >= o.lo.x &&
55 hi.y >= o.lo.y &&
56 lo.x <= o.hi.x &&
57 lo.y <= o.hi.y;
58 }
59 bool intersects2b(const AABBox& o) const
60 {
61 return !( hi.x < o.lo.x ||
62 hi.y < o.lo.y ||
63 lo.x > o.hi.x ||
64 lo.y > o.hi.y );
65 }
66 bool intersects2c(const AABBox& o) const
67 {
68 const Point2f lo_ = max(lo, o.lo);
69 const Point2f hi_ = min(hi, o.hi);
70 return lo_.x <= hi_.x && lo_.y <= hi_.y;
71 }
72};
73
74#include <random>
75
76TEST_CASE( "Float Math Bench 04a", "[intersect][benchmark][arithmetic][math]" ) {
77 std::mt19937 rng;
78 int32_t seed_val=0;
79 rng.seed(seed_val);
80 std::uniform_int_distribution<int32_t> rint(0,50);
81
82 const int loops = catch_auto_run ? 1000 : 1000000;
83 size_t isect_count=0;
84 std::vector<AABBox2f> va0, vb0;
85 std::vector<AABBox> va, vb;
86 for(int i=0; i<loops; ++i) {
87 Point2f lo((float)rint(rng), (float)rint(rng));
88 Point2f hi(lo.x+(float)rint(rng), lo.y+(float)rint(rng));
89 AABBox a { .lo=lo, .hi=hi };
90 AABBox2f a0 { lo, hi };
91 lo = Point2f((float)rint(rng), (float)rint(rng));
92 hi = Point2f(lo.x+(float)rint(rng), lo.y+(float)rint(rng));
93 AABBox2f b0 { lo, hi };
94 AABBox b { .lo=lo, .hi=hi };
95 va0.push_back(a0);
96 vb0.push_back(b0);
97 va.push_back(a);
98 vb.push_back(b);
99 bool i0 = a0.intersects(b0);
100 bool i1a = a.intersects1a(b);
101 bool i1b = a.intersects1b(b);
102 bool i1c = a.intersects1c(b);
103 if( i1a ) {
104 ++isect_count;
105 }
106 // std::cout << "# " << i << std::endl;
107 // std::cout << "A: " << a << std::endl;
108 // std::cout << "B: " << b << ", i " << i1a << std::endl;
109 REQUIRE( i1a == i1b );
110 REQUIRE( i1a == i1c );
111 REQUIRE( i1a == i0 );
112 // std::cout << std::endl;
113 bool i2a = a.intersects2a(b);
114 bool i2b = a.intersects2b(b);
115 bool i2c = a.intersects2c(b);
116 REQUIRE( i1a == i2a );
117 REQUIRE( i2a == i2b );
118 REQUIRE( i2a == i2c );
119 REQUIRE( i2a == i0 );
120 }
121 std::cout << "isect_count " << isect_count << "/" << va.size() << ", " << 100.0f*( (float)isect_count / (float)va.size() ) << "%" << std::endl;
122
123 BENCHMARK("Intersect0 Benchmark") {
124 size_t r = 0;
125 for(size_t i = 0; i < va0.size(); ++i) {
126 AABBox2f a = va0[i];
127 AABBox2f b = vb0[i];
128 r += a.intersects(b) ? 10 : 1;
129 }
130 return r;
131 };
132 BENCHMARK("Intersect1a Benchmark") {
133 size_t r = 0;
134 for(size_t i = 0; i < va.size(); ++i) {
135 AABBox a = va[i];
136 AABBox b = vb[i];
137 r += a.intersects1a(b) ? 10 : 1;
138 }
139 return r;
140 };
141 BENCHMARK("Intersect1b Benchmark") {
142 size_t r = 0;
143 for(size_t i = 0; i < va.size(); ++i) {
144 AABBox a = va[i];
145 AABBox b = vb[i];
146 r += a.intersects1b(b) ? 10 : 1;
147 }
148 return r;
149 };
150 BENCHMARK("Intersect1c Benchmark") {
151 size_t r = 0;
152 for(size_t i = 0; i < va.size(); ++i) {
153 AABBox a = va[i];
154 AABBox b = vb[i];
155 r += a.intersects1c(b) ? 10 : 1;
156 }
157 return r;
158 };
159 BENCHMARK("Intersect2a Benchmark") {
160 size_t r = 0;
161 for(size_t i = 0; i < va.size(); ++i) {
162 AABBox a = va[i];
163 AABBox b = vb[i];
164 r += a.intersects2a(b) ? 10 : 1;
165 }
166 return r;
167 };
168 BENCHMARK("Intersect2b Benchmark") {
169 size_t r = 0;
170 for(size_t i = 0; i < va.size(); ++i) {
171 AABBox a = va[i];
172 AABBox b = vb[i];
173 r += a.intersects2b(b) ? 10 : 1;
174 }
175 return r;
176 };
177 BENCHMARK("Intersect2c Benchmark") {
178 size_t r = 0;
179 for(size_t i = 0; i < va.size(); ++i) {
180 AABBox a = va[i];
181 AABBox b = vb[i];
182 r += a.intersects2c(b) ? 10 : 1;
183 }
184 return r;
185 };
186}
187
value_type x
Definition vec2f.hpp:62
value_type y
Definition vec2f.hpp:63
value_type y
Definition vec2i.hpp:64
value_type x
Definition vec2i.hpp:63
Axis Aligned Bounding Box.
Definition aabbox2f.hpp:34
constexpr bool intersects(const AABBox2f &o) const noexcept
Definition aabbox2f.hpp:151
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)
Point2F< float > Point2f
Definition vec2f.hpp:417
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
Definition backtrace.hpp:32
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("Float Math Bench 04a", "[intersect][benchmark][arithmetic][math]")
static int loops