jaulib v1.3.6
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
74std::ostream& operator<<(std::ostream& out, const AABBox& v) noexcept {
75 return out << "aabb[bl " << v.lo << ", tr " << v.hi << "]";
76}
77
78#include <random>
79
80TEST_CASE( "Float Math Bench 04a", "[intersect][benchmark][arithmetic][math]" ) {
81 std::mt19937 rng;
82 int32_t seed_val=0;
83 rng.seed(seed_val);
84 std::uniform_int_distribution<int32_t> rint(0,50);
85
86 const int loops = catch_auto_run ? 1000 : 1000000;
87 size_t isect_count=0;
88 std::vector<AABBox2f> va0, vb0;
89 std::vector<AABBox> va, vb;
90 for(int i=0; i<loops; ++i) {
91 Point2f lo((float)rint(rng), (float)rint(rng));
92 Point2f hi(lo.x+(float)rint(rng), lo.y+(float)rint(rng));
93 AABBox a { lo, hi };
94 AABBox2f a0 { lo, hi };
95 lo = Point2f((float)rint(rng), (float)rint(rng));
96 hi = Point2f(lo.x+(float)rint(rng), lo.y+(float)rint(rng));
97 AABBox2f b0 { lo, hi };
98 AABBox b { lo, hi };
99 va0.push_back(a0);
100 vb0.push_back(b0);
101 va.push_back(a);
102 vb.push_back(b);
103 bool i0 = a0.intersects(b0);
104 bool i1a = a.intersects1a(b);
105 bool i1b = a.intersects1b(b);
106 bool i1c = a.intersects1c(b);
107 if( i1a ) {
108 ++isect_count;
109 }
110 // std::cout << "# " << i << std::endl;
111 // std::cout << "A: " << a << std::endl;
112 // std::cout << "B: " << b << ", i " << i1a << std::endl;
113 REQUIRE( i1a == i1b );
114 REQUIRE( i1a == i1c );
115 REQUIRE( i1a == i0 );
116 // std::cout << std::endl;
117 bool i2a = a.intersects2a(b);
118 bool i2b = a.intersects2b(b);
119 bool i2c = a.intersects2c(b);
120 REQUIRE( i1a == i2a );
121 REQUIRE( i2a == i2b );
122 REQUIRE( i2a == i2c );
123 REQUIRE( i2a == i0 );
124 }
125 std::cout << "isect_count " << isect_count << "/" << va.size() << ", " << 100.0f*( (float)isect_count / (float)va.size() ) << "%" << std::endl;
126
127 BENCHMARK("Intersect0 Benchmark") {
128 size_t r = 0;
129 for(size_t i = 0; i < va0.size(); ++i) {
130 AABBox2f a = va0[i];
131 AABBox2f b = vb0[i];
132 r += a.intersects(b) ? 10 : 1;
133 }
134 return r;
135 };
136 BENCHMARK("Intersect1a Benchmark") {
137 size_t r = 0;
138 for(size_t i = 0; i < va.size(); ++i) {
139 AABBox a = va[i];
140 AABBox b = vb[i];
141 r += a.intersects1a(b) ? 10 : 1;
142 }
143 return r;
144 };
145 BENCHMARK("Intersect1b Benchmark") {
146 size_t r = 0;
147 for(size_t i = 0; i < va.size(); ++i) {
148 AABBox a = va[i];
149 AABBox b = vb[i];
150 r += a.intersects1b(b) ? 10 : 1;
151 }
152 return r;
153 };
154 BENCHMARK("Intersect1c Benchmark") {
155 size_t r = 0;
156 for(size_t i = 0; i < va.size(); ++i) {
157 AABBox a = va[i];
158 AABBox b = vb[i];
159 r += a.intersects1c(b) ? 10 : 1;
160 }
161 return r;
162 };
163 BENCHMARK("Intersect2a Benchmark") {
164 size_t r = 0;
165 for(size_t i = 0; i < va.size(); ++i) {
166 AABBox a = va[i];
167 AABBox b = vb[i];
168 r += a.intersects2a(b) ? 10 : 1;
169 }
170 return r;
171 };
172 BENCHMARK("Intersect2b Benchmark") {
173 size_t r = 0;
174 for(size_t i = 0; i < va.size(); ++i) {
175 AABBox a = va[i];
176 AABBox b = vb[i];
177 r += a.intersects2b(b) ? 10 : 1;
178 }
179 return r;
180 };
181 BENCHMARK("Intersect2c Benchmark") {
182 size_t r = 0;
183 for(size_t i = 0; i < va.size(); ++i) {
184 AABBox a = va[i];
185 AABBox b = vb[i];
186 r += a.intersects2c(b) ? 10 : 1;
187 }
188 return r;
189 };
190}
191
value_type x
Definition vec2f.hpp:75
value_type y
Definition vec2f.hpp:76
value_type x
Definition vec2i.hpp:77
value_type y
Definition vec2i.hpp:78
Axis Aligned Bounding Box.
Definition aabbox2f.hpp:34
bool intersects(const AABBox2f &o) const noexcept
Definition aabbox2f.hpp:151
std::ostream & operator<<(std::ostream &out, const cow_darray< Value_type, Size_type, Alloc_type > &c)
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:402
__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