forked from samhocevar/z8lua
-
Notifications
You must be signed in to change notification settings - Fork 3
/
fix32.h
241 lines (200 loc) · 8.72 KB
/
fix32.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//
// ZEPTO-8 — Fantasy console emulator
//
// Copyright © 2016—2020 Sam Hocevar <[email protected]>
//
// This program is free software. It comes without any warranty, to
// the extent permitted by applicable law. You can redistribute it
// and/or modify it under the terms of the Do What the Fuck You Want
// to Public License, Version 2, as published by the WTFPL Task Force.
// See http://www.wtfpl.net/ for more details.
//
#pragma once
#include <stdint.h> // int32_t, int64_t, …
#include <cmath> // std::abs
#include <algorithm> // std::min
#include <type_traits> // std::enable_if
namespace z8
{
struct fix32
{
inline fix32() = default;
// Convert from/to double
inline fix32(double d)
: m_bits(int32_t(int64_t(d * 65536.0)))
{}
inline operator double() const
{
return double(m_bits) * (1.0 / 65536.0);
}
// Conversions up to int16_t are safe.
inline fix32(int8_t x) : m_bits(int32_t(x << 16)) {}
inline fix32(uint8_t x) : m_bits(int32_t(x << 16)) {}
inline fix32(int16_t x) : m_bits(int32_t(x << 16)) {}
// Anything above int16_t is risky because of precision loss, but Lua
// does too many implicit conversions from int that we can’t mark this
// one as explicit.
inline fix32(int32_t x) : m_bits(int32_t(x << 16)) {}
inline explicit fix32(uint16_t x) : m_bits(int32_t(x << 16)) {}
inline explicit fix32(uint32_t x) : m_bits(int32_t(x << 16)) {}
inline explicit fix32(int64_t x) : m_bits(int32_t(x << 16)) {}
inline explicit fix32(uint64_t x) : m_bits(int32_t(x << 16)) {}
// Support for long and unsigned long when it is a distinct
// type from the standard int*_t types, e.g. on Windows.
template<typename T,
typename std::enable_if<(std::is_same<T, long>::value ||
std::is_same<T, unsigned long>::value) &&
!std::is_same<T, int32_t>::value &&
!std::is_same<T, uint32_t>::value &&
!std::is_same<T, int64_t>::value &&
!std::is_same<T, uint64_t>::value>::type *...>
inline explicit fix32(T x) : m_bits(int32_t(x << 16)) {}
// Explicit casts are all allowed
inline explicit operator int8_t() const { return m_bits >> 16; }
inline explicit operator uint8_t() const { return m_bits >> 16; }
inline explicit operator int16_t() const { return m_bits >> 16; }
inline explicit operator uint16_t() const { return m_bits >> 16; }
inline explicit operator int32_t() const { return m_bits >> 16; }
inline explicit operator uint32_t() const { return m_bits >> 16; }
inline explicit operator int64_t() const { return m_bits >> 16; }
inline explicit operator uint64_t() const { return m_bits >> 16; }
// Additional casts for long and unsigned long on architectures where
// these are not the same types as their cstdint equivalents.
template<typename T,
typename std::enable_if<(std::is_same<T, long>::value ||
std::is_same<T, unsigned long>::value) &&
!std::is_same<T, int32_t>::value &&
!std::is_same<T, uint32_t>::value &&
!std::is_same<T, int64_t>::value &&
!std::is_same<T, uint64_t>::value>::type *...>
inline explicit operator T() const { return T(m_bits >> 16); }
// Directly initialise bits
static inline fix32 frombits(int32_t x)
{
fix32 ret; ret.m_bits = x; return ret;
}
inline int32_t bits() const { return m_bits; }
// Comparisons
inline explicit operator bool() const { return bool(m_bits); }
inline bool operator ==(fix32 x) const { return m_bits == x.m_bits; }
inline bool operator !=(fix32 x) const { return m_bits != x.m_bits; }
inline bool operator <(fix32 x) const { return m_bits < x.m_bits; }
inline bool operator >(fix32 x) const { return m_bits > x.m_bits; }
inline bool operator <=(fix32 x) const { return m_bits <= x.m_bits; }
inline bool operator >=(fix32 x) const { return m_bits >= x.m_bits; }
// Increments
inline fix32& operator ++() { m_bits += 0x10000; return *this; }
inline fix32& operator --() { m_bits -= 0x10000; return *this; }
inline fix32 operator ++(int) { fix32 ret = *this; ++*this; return ret; }
inline fix32 operator --(int) { fix32 ret = *this; --*this; return ret; }
// Math operations
inline fix32 const &operator +() const { return *this; }
inline fix32 operator -() const { return frombits(-m_bits); }
inline fix32 operator ~() const { return frombits(~m_bits); }
inline fix32 operator +(fix32 x) const { return frombits(m_bits + x.m_bits); }
inline fix32 operator -(fix32 x) const { return frombits(m_bits - x.m_bits); }
inline fix32 operator &(fix32 x) const { return frombits(m_bits & x.m_bits); }
inline fix32 operator |(fix32 x) const { return frombits(m_bits | x.m_bits); }
inline fix32 operator ^(fix32 x) const { return frombits(m_bits ^ x.m_bits); }
fix32 operator *(fix32 x) const
{
return frombits(int64_t(m_bits) * x.m_bits >> 16);
}
fix32 operator /(fix32 x) const
{
// This special case ensures 0x8000/0x1 = 0x8000, not 0x8000.0001
if (x.m_bits == 0x10000)
return *this;
if (x.m_bits)
{
using std::abs;
int64_t result = int64_t(m_bits) * 0x10000 / x.m_bits;
if (abs(result) <= 0x7fffffffu)
return frombits(int32_t(result));
}
// Return 0x8000.0001 (not 0x8000.0000) for -Inf, just like PICO-8
return frombits((m_bits ^ x.m_bits) >= 0 ? 0x7fffffffu : 0x80000001u);
}
fix32 operator %(fix32 x) const
{
// PICO-8 always returns positive values
x = abs(x);
int32_t result = x ? m_bits % x.m_bits : m_bits;
return frombits(result >= 0 ? result : result + x.m_bits);
}
inline fix32 operator <<(int y) const
{
// If y is negative, use lshr() instead.
return y < 0 ? lshr(*this, -y) : frombits(y >= 32 ? 0 : bits() << y);
}
inline fix32 operator >>(int y) const
{
using std::min;
// If y is negative, use << instead.
return y < 0 ? *this << -y : frombits(bits() >> min(y, 31));
}
inline fix32& operator +=(fix32 x) { return *this = *this + x; }
inline fix32& operator -=(fix32 x) { return *this = *this - x; }
inline fix32& operator &=(fix32 x) { return *this = *this & x; }
inline fix32& operator |=(fix32 x) { return *this = *this | x; }
inline fix32& operator ^=(fix32 x) { return *this = *this ^ x; }
inline fix32& operator *=(fix32 x) { return *this = *this * x; }
inline fix32& operator /=(fix32 x) { return *this = *this / x; }
inline fix32& operator %=(fix32 x) { return *this = *this % x; }
// Free functions
static inline fix32 abs(fix32 a) { return a.m_bits > 0 ? a : -a; }
static inline fix32 min(fix32 a, fix32 b) { return a < b ? a : b; }
static inline fix32 max(fix32 a, fix32 b) { return a > b ? a : b; }
static inline fix32 ceil(fix32 x) { return -floor(-x); }
static inline fix32 floor(fix32 x) { return frombits(x.m_bits & 0xffff0000); }
static inline fix32 modf(fix32 x) { return frombits(x.m_bits & 0x0000ffff); }
static fix32 pow(fix32 x, fix32 y)
{
if ((y.m_bits & 0xffff0000) == (uint32_t)y.m_bits) {
return pow(x, (int)y);
}
return fix32(std::pow(double(x), double(y)));
}
static fix32 pow(fix32 x, int y)
{
fix32 res = 1;
if (y > 0) {
for(int i = 0; i < y; i++) {
res *= x;
}
}
else {
for(int i = 0; i > y; i--){
res /= x;
}
}
return res;
}
static inline fix32 lshr(fix32 x, int y)
{
// If y is negative, use << instead.
return y < 0 ? x << -y : frombits(y >= 32 ? 0 : uint32_t(x.bits()) >> y);
}
static inline fix32 rotl(fix32 x, int y)
{
y &= 0x1f;
return frombits((x.bits() << y) | (uint32_t(x.bits()) >> (32 - y)));
}
static inline fix32 rotr(fix32 x, int y)
{
y &= 0x1f;
return frombits((uint32_t(x.bits()) >> y) | (x.bits() << (32 - y)));
}
#ifdef _3DS
inline explicit fix32(size_t x) : m_bits(int32_t(x << 16)) {}
inline fix32(int x) : m_bits(int(x << 16)) {}
inline explicit operator size_t() const { return m_bits >> 16; }
#endif
static inline fix32 ldexp(fix32 x, int y)
{
return fix32(std::ldexp((double)x, y));
}
private:
int32_t m_bits;
};
}