openMSX
FixedPoint_test.cc
Go to the documentation of this file.
1 #include "catch.hpp"
2 #include "FixedPoint.hh"
3 
4 using namespace openmsx;
5 
6 template<unsigned BITS>
7 static void check(const FixedPoint<BITS>& fp, int expectedRaw)
8 {
9  REQUIRE(fp.getRawValue() == expectedRaw);
10  int i = expectedRaw >> BITS;
11  unsigned fi = expectedRaw & ((1 << BITS) - 1);
12  float ff = float (fi) / (1 << BITS);
13  double fd = double(fi) / (1 << BITS);
14  float f = i + ff;
15  double d = i + fd;
16  CHECK(fp.toInt() == i);
17  CHECK(fp.toFloat() == f);
18  CHECK(fp.toDouble() == d);
19  CHECK(fp.fractAsInt() == fi);
20  CHECK(fp.fractionAsFloat() == ff);
21  CHECK(fp.fractionAsDouble() == fd);
22  CHECK(fp.floor().getRawValue() == (i << BITS));
23  CHECK(fp.fract().getRawValue() == int(fi));
24 }
25 
26 TEST_CASE("FixedPoint")
27 {
28  FixedPoint<2> a(10 ); check(a, 40);
29  FixedPoint<2> b(3.5f); check(b, 14);
30  FixedPoint<2> c(1.75); check(c, 7);
31  FixedPoint<7> d(c); check(d, 224); // more fractional bits
32  FixedPoint<1> e(d); check(e, 3); // less fractional bits
33 
34  check(FixedPoint<3>::roundRatioDown(2, 3), 5); // 2/3 ~= 5/8
35 
36  CHECK(a.divAsInt(b) == 2);
37  CHECK(b.divAsInt(a) == 0);
38  CHECK(b.divAsInt(c) == 2);
39 
40  check(a + b, 54);
41  check(a + c, 47);
42  check(b + c, 21);
43 
44  check(a - b, 26); check(b - a, -26);
45  check(a - c, 33); check(c - a, -33);
46  check(b - c, 7); check(c - b, -7);
47 
48  check(a * b, 140);
49  check(a * c, 70);
50  check(b * c, 24);
51  check(a * 2, 80);
52  check(b * -3, -42);
53  check(c * 4, 28);
54 
55  check(a / b, 11); check(b / a, 1);
56  check(a / c, 22); check(c / a, 0);
57  check(b / c, 8); check(c / b, 2);
58  check(a / -5, -8);
59  check(b / 4, 3);
60  check(c / -3, -2);
61 
62  check(a << 1, 80); check(a >> 3, 5);
63  check(b << 2, 56); check(b >> 2, 3);
64  check(c << 3, 56); check(c >> 1, 3);
65 
66  CHECK(a == a); CHECK(!(a != a));
67  CHECK(a != b); CHECK(!(a == b));
68  CHECK(b < a); CHECK(!(b >= a));
69  CHECK(b <= a); CHECK(!(b > a));
70  CHECK(a > b); CHECK(!(a <= b));
71  CHECK(a >= b); CHECK(!(a < b));
72 
73  a += b; check(a, 54);
74  c -= a; check(c, -47);
75 
76  a.addQuantum(); check(a, 55);
77  b.addQuantum(); check(b, 15);
78  c.addQuantum(); check(c, -46);
79 }
constexpr int getRawValue() const
Definition: FixedPoint.hh:223
TEST_CASE("FixedPoint")
constexpr double toDouble() const
Returns the double value that corresponds to this fixed point number.
Definition: FixedPoint.hh:90
A fixed point number, implemented by a 32-bit signed integer.
Definition: FixedPoint.hh:15
CHECK(m3==m3)
constexpr FixedPoint fract() const
Returns the fractional part of this value.
Definition: FixedPoint.hh:136
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
void addQuantum()
Increase this value with the smallest possible amount.
Definition: FixedPoint.hh:217
constexpr unsigned fractAsInt() const
Returns the fractional part of this value as an integer.
Definition: FixedPoint.hh:144
constexpr int divAsInt(FixedPoint other) const
Returns the result of a division between this fixed point number and another, rounded towards zero...
Definition: FixedPoint.hh:120
constexpr FixedPoint floor() const
Returns this value rounded down.
Definition: FixedPoint.hh:128
constexpr float toFloat() const
Returns the float value that corresponds to this fixed point number.
Definition: FixedPoint.hh:83
constexpr float fractionAsFloat() const
Returns the fractional part of this fixed point number as a float.
Definition: FixedPoint.hh:100
constexpr int toInt() const
Returns the integer part (rounded down) of this fixed point number.
Definition: FixedPoint.hh:76
constexpr double fractionAsDouble() const
Returns the fractional part of this fixed point number as a double.
Definition: FixedPoint.hh:110