Skip to content

Commit 1c33176

Browse files
authored
Some warning and other minor fixes (#2038)
* FastRNG: fix warnings, optimize bool01() * UTF8: limit warnings on Android due to jni.hpp include * Remove unused includes * UTF8: add missing fmt/format.h include * Tests: remove invalid test
1 parent 602a2ca commit 1c33176

File tree

6 files changed

+161
-30
lines changed

6 files changed

+161
-30
lines changed

core/base/UTF8.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@
2929
#define AXMOL__UTF8_H
3030

3131
#include "platform/PlatformMacros.h"
32+
#include <fmt/format.h>
3233
#include <vector>
3334
#include <string>
3435
#include <sstream>
3536

3637
#if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID)
37-
# include "platform/android/jni/JniHelper.h"
38+
# include <jni.h>
3839
#endif
3940

4041
NS_AX_BEGIN

core/math/FastRNG.h

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,38 +20,28 @@ struct FastRNG
2020
uint32_t _carry = 0;
2121
uint32_t _k = 0;
2222
uint32_t _m = 0;
23-
24-
#ifdef UINT64_C
2523
uint64_t _seed = 0;
26-
#else
27-
uint32_t _seed = 0;
28-
#endif
2924

3025
FastRNG() { seed_rng((uint32_t)time(NULL)); }
26+
FastRNG(uint64_t seed) { seed_rng_64(seed); }
3127

32-
// initialize this object with a uint32_t seed
33-
void seed_rng(uint32_t seed)
28+
// initialize this object with seed
29+
void seed_rng(uint64_t seed)
3430
{
31+
auto seed1 = static_cast<uint32_t>(seed);
32+
auto seed2 = static_cast<uint32_t>(seed >> 32);
33+
3534
_seed = seed;
36-
_x = seed | 1;
37-
_y = seed | 2;
38-
_z = seed | 4;
39-
_w = seed | 8;
35+
_x = static_cast<int32_t>(seed1) | 1;
36+
_y = static_cast<int32_t>(seed2) | 2;
37+
_z = static_cast<int32_t>(seed1) | 4;
38+
_w = static_cast<int32_t>(seed2) | 8;
4039
_carry = 0;
4140
}
4241

43-
#ifdef UINT64_C
4442
// initialize this object with a uint64_t seed
45-
void seed_rng_64(uint64_t seed)
46-
{
47-
_seed = seed;
48-
_x = static_cast<int32_t>(seed) | 1;
49-
_y = static_cast<int32_t>(seed) | 2;
50-
_z = static_cast<int32_t>(seed) | 4;
51-
_w = static_cast<int32_t>(seed) | 8;
52-
_carry = 0;
53-
}
54-
#endif
43+
// DEPRECATED: use seed_rng instead
44+
void seed_rng_64(uint64_t seed) { seed_rng(seed); }
5545

5646
// returns a random uint32_t value
5747
uint32_t rng()
@@ -83,7 +73,7 @@ struct FastRNG
8373
// returns a random integer from 0 to max
8474
int32_t max(int32_t max = INT_MAX)
8575
{
86-
return static_cast<int32_t>(rng() / static_cast<float>(RNG_RAND_MAX / (max - 0)));
76+
return static_cast<int32_t>(static_cast<float>(rng()) / static_cast<float>(RNG_RAND_MAX / (max - 0)));
8777
}
8878

8979
// returns a random unsigned integer from 0 to max
@@ -92,17 +82,17 @@ struct FastRNG
9282
// returns a random float from min to max
9383
float rangef(float min = -1.0F, float max = 1.0F)
9484
{
95-
return min + rng() / static_cast<float>(RNG_RAND_MAX / (max - min));
85+
return min + static_cast<float>(rng()) / (static_cast<float>(RNG_RAND_MAX) / (max - min));
9686
}
9787

9888
// returns a random float from 0.0 to max
99-
float maxf(float max) { return rng() / static_cast<float>(RNG_RAND_MAX / (max - 0)); }
89+
float maxf(float max) { return static_cast<float>(rng()) / (static_cast<float>(RNG_RAND_MAX) / (max - 0.0f)); }
10090

10191
// returns a random float from 0.0 to 1.0
102-
float float01() { return rng() / static_cast<float>(RNG_RAND_MAX / (1 - 0)); }
92+
float float01() { return static_cast<float>(rng()) / (static_cast<float>(RNG_RAND_MAX) / (1.0f - 0.0f)); }
10393

10494
// returns either false or true randomly
105-
bool bool01() { return static_cast<bool>(floor(rng() / static_cast<float>(RNG_RAND_MAX / (2 - 0)))); }
95+
bool bool01() { return static_cast<bool>(rng() & 1); }
10696
};
10797

10898
#endif // _FAST_RNG_H__

core/platform/android/jni/Java_org_axmol_lib_AxmolAccelerometer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/****************************************************************************
22
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
3+
Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md).
34
45
https://axmol.dev/
56
@@ -22,7 +23,6 @@
2223
THE SOFTWARE.
2324
****************************************************************************/
2425

25-
#include "platform/android/jni/JniHelper.h"
2626
#include <jni.h>
2727
#include "base/Director.h"
2828
#include "base/EventDispatcher.h"

core/platform/android/jni/Java_org_axmol_lib_AxmolRenderer.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
#include "base/EventDispatcher.h"
3131
#include "platform/Application.h"
3232
#include "platform/FileUtils.h"
33-
#include "platform/android/jni/JniHelper.h"
3433
#include <jni.h>
3534

3635
#include "base/UTF8.h"

tests/unit-tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ set(GAME_SOURCE
4949
Source/core/base/ValueTests.cpp
5050
Source/core/base/VectorTests.cpp
5151

52+
Source/core/math/FastRNGTests.cpp
5253
Source/core/math/MathUtilTests.cpp
5354

5455
Source/core/network/UriTests.cpp
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/****************************************************************************
2+
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
3+
Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md).
4+
5+
https://axmol.dev/
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
****************************************************************************/
25+
26+
#include <doctest.h>
27+
#include <float.h>
28+
#include "math/FastRNG.h"
29+
30+
31+
TEST_SUITE("math/FastRNG") {
32+
TEST_CASE("rng") {
33+
auto rng = FastRNG(1);
34+
35+
CHECK_EQ(622173, rng.rng());
36+
CHECK_EQ(1462482235, rng.rng());
37+
CHECK_EQ(3913629391, rng.rng());
38+
39+
auto rng2 = FastRNG(2);
40+
41+
CHECK_EQ(760312, rng2.rng());
42+
CHECK_EQ(2413601167, rng2.rng());
43+
CHECK_EQ(927245950, rng2.rng());
44+
}
45+
46+
47+
TEST_CASE("range") {
48+
auto rng = FastRNG(12345);
49+
50+
CHECK_EQ(0, rng.range(0, 1));
51+
CHECK_EQ(-1, rng.range(-1, 0));
52+
CHECK_EQ(13, rng.range(10, 20));
53+
CHECK_EQ(12, rng.range(10, 20));
54+
CHECK_EQ(-11, rng.range(-20, -10));
55+
CHECK_EQ(-18, rng.range(-20, -10));
56+
57+
CHECK_EQ(260724215, rng.range(-INT_MAX, INT_MAX));
58+
CHECK_EQ(1458015400, rng.range(-INT_MAX, INT_MAX));
59+
}
60+
61+
62+
TEST_CASE("rangeu") {
63+
auto rng = FastRNG(1);
64+
65+
CHECK_EQ(0u, rng.rangeu(0u, 1u));
66+
CHECK_EQ(0u, rng.rangeu(0u, 1u));
67+
CHECK_EQ(19u, rng.rangeu(10u, 20u));
68+
69+
CHECK_EQ(128334119, rng.rangeu(0u, UINT_MAX));
70+
}
71+
72+
73+
TEST_CASE("max") {
74+
auto rng = FastRNG(1);
75+
76+
CHECK_EQ(0, rng.max(1));
77+
CHECK_EQ(3, rng.max(10));
78+
CHECK_EQ(1956814720, rng.max());
79+
}
80+
81+
82+
TEST_CASE("maxu") {
83+
auto rng = FastRNG(1);
84+
85+
CHECK_EQ(0u, rng.maxu(1));
86+
CHECK_EQ(3u, rng.maxu(10));
87+
CHECK_EQ(3913629391, rng.maxu());
88+
}
89+
90+
91+
TEST_CASE("rangef") {
92+
auto rng = FastRNG(1);
93+
94+
CHECK_EQ(doctest::Approx(-0.99971), rng.rangef(-1.0f, 1.0f));
95+
CHECK_EQ(doctest::Approx(-0.318979), rng.rangef(-1.0f, 1.0f));
96+
CHECK_EQ(doctest::Approx(19.1121), rng.rangef(10.0f, 20.0f));
97+
// These overflow
98+
//CHECK_EQ(doctest::Approx(1.0), rng.rangef(-FLT_MAX, FLT_MAX));
99+
//CHECK_EQ(doctest::Approx(1.0), rng.rangef(-FLT_MAX, FLT_MAX));
100+
}
101+
102+
103+
TEST_CASE("maxf") {
104+
auto rng = FastRNG(1);
105+
106+
CHECK_EQ(doctest::Approx(0.000144861), rng.maxf(1.0f));
107+
CHECK_EQ(doctest::Approx(0.340511), rng.maxf(1.0f));
108+
CHECK_EQ(doctest::Approx(9.11213), rng.maxf(10.0f));
109+
CHECK_EQ(doctest::Approx(1.01677e+37), rng.maxf(FLT_MAX));
110+
CHECK_EQ(doctest::Approx(1.50114e+38), rng.maxf(FLT_MAX));
111+
}
112+
113+
114+
TEST_CASE("float01") {
115+
auto rng = FastRNG(1);
116+
117+
CHECK_EQ(doctest::Approx(0.000144861), rng.float01());
118+
CHECK_EQ(doctest::Approx(0.340511), rng.float01());
119+
CHECK_EQ(doctest::Approx(0.911213), rng.float01());
120+
CHECK_EQ(doctest::Approx(0.0298801), rng.float01());
121+
}
122+
123+
124+
TEST_CASE("bool01") {
125+
auto rng = FastRNG(1);
126+
127+
auto t = 0;
128+
auto f = 0;
129+
for (auto i = 0; i < 100; i++) {
130+
if (rng.bool01()) {
131+
t++;
132+
} else {
133+
f++;
134+
}
135+
}
136+
137+
CHECK_EQ(45, t);
138+
CHECK_EQ(55, f);
139+
}
140+
}

0 commit comments

Comments
 (0)