forked from pioneerspacesim/pioneer
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fixed.h
191 lines (171 loc) · 6.69 KB
/
fixed.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
// Copyright © 2008-2012 Pioneer Developers. See AUTHORS.txt for details
// Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
#ifndef _FIXED_H
#define _FIXED_H
#include <SDL_stdinc.h>
#include <cassert>
template <int FRAC_BITS>
class fixedf {
public:
static const int FRAC = FRAC_BITS;
static const Uint64 MASK = (Uint64(1UL)<<FRAC_BITS)-1;
static const Sint64 DUMMY= -1 ; // -1 to ensure signed type (signedness warning otherwise)
fixedf(): v(0) {}
// template <int bits>
// fixedf(fixedf<bits> f) { *this = f; }
fixedf(Sint64 raw): v(raw) {}
fixedf(Sint64 num, Sint64 denom): v((num<<FRAC) / denom) {}
// ^^ this is fucking shit
fixedf Abs() const { return fixedf(v >= 0 ? v : -v); }
friend fixedf operator+(const fixedf a, const Sint64 b) { return a+fixedf(b<<FRAC); }
friend fixedf operator-(const fixedf a, const Sint64 b) { return a-fixedf(b<<FRAC); }
friend fixedf operator*(const fixedf a, const Sint64 b) { return a*fixedf(b<<FRAC); }
friend fixedf operator/(const fixedf a, const Sint64 b) { return a/fixedf(b<<FRAC); }
friend fixedf operator+(const Sint64 a, const fixedf b) { return fixedf(a<<FRAC)+b; }
friend fixedf operator-(const Sint64 a, const fixedf b) { return fixedf(a<<FRAC)-b; }
friend fixedf operator*(const Sint64 a, const fixedf b) { return fixedf(a<<FRAC)*b; }
friend fixedf operator/(const Sint64 a, const fixedf b) { return fixedf(a<<FRAC)/b; }
friend bool operator==(const fixedf a, const Sint64 b) { return a == fixedf(b<<FRAC); }
friend bool operator==(const Sint64 a, const fixedf b) { return b == fixedf(a<<FRAC); }
friend bool operator!=(const fixedf a, const Sint64 b) { return a != fixedf(b<<FRAC); }
friend bool operator!=(const Sint64 a, const fixedf b) { return b != fixedf(a<<FRAC); }
friend bool operator>=(const fixedf a, const Sint64 b) { return a >= fixedf(b<<FRAC); }
friend bool operator>=(const Sint64 a, const fixedf b) { return b >= fixedf(a<<FRAC); }
friend bool operator<=(const fixedf a, const Sint64 b) { return a <= fixedf(b<<FRAC); }
friend bool operator<=(const Sint64 a, const fixedf b) { return b <= fixedf(a<<FRAC); }
friend bool operator>(const fixedf a, const Sint64 b) { return a > fixedf(b<<FRAC); }
friend bool operator>(const Sint64 a, const fixedf b) { return b > fixedf(a<<FRAC); }
friend bool operator<(const fixedf a, const Sint64 b) { return a < fixedf(b<<FRAC); }
friend bool operator<(const Sint64 a, const fixedf b) { return b < fixedf(a<<FRAC); }
friend fixedf operator>>(const fixedf a, const int b) { return fixedf(a.v >> b); }
friend fixedf operator<<(const fixedf a, const int b) { return fixedf(a.v << b); }
fixedf &operator*=(const fixedf a) { (*this) = (*this)*a; return (*this); }
fixedf &operator*=(const Sint64 a) { (*this) = (*this)*a; return (*this); }
fixedf &operator/=(const fixedf a) { (*this) = (*this)/a; return (*this); }
fixedf &operator/=(const Sint64 a) { (*this) = (*this)/a; return (*this); }
fixedf &operator+=(const fixedf a) { (*this) = (*this)+a; return (*this); }
fixedf &operator+=(const Sint64 a) { (*this) = (*this)+a; return (*this); }
fixedf &operator-=(const fixedf a) { (*this) = (*this)-a; return (*this); }
fixedf &operator-=(const Sint64 a) { (*this) = (*this)-a; return (*this); }
fixedf &operator>>=(const int a) { v >>= a; return (*this); }
fixedf &operator<<=(const int a) { v <<= a; return (*this); }
friend fixedf operator-(const fixedf a) { return fixedf(-a.v); }
friend fixedf operator+(const fixedf a, const fixedf b) { return fixedf(a.v+b.v); }
friend fixedf operator-(const fixedf a, const fixedf b) { return fixedf(a.v-b.v); }
friend fixedf operator*(const fixedf a, const fixedf b) {
// 64*64 = (128bit>>FRAC) & ((1<<64)-1)
//return fixedf(a.v*b.v >> FRAC);
Sint64 hi = 0;
Uint64 a0, a1, b0, b1;
Uint64 lo = 0;
Uint64 oldlo;
int isneg = 0;
if (a.v < 0) {
a0 = (-a.v) & 0xffffffff;
a1 = (-a.v) >> 32;
isneg = !isneg;
} else {
a0 = a.v & 0xffffffff;
a1 = a.v >> 32;
}
if (b.v < 0) {
b0 = (-b.v) & 0xffffffff;
b1 = (-b.v) >> 32;
isneg = !isneg;
} else {
b0 = b.v & 0xffffffff;
b1 = b.v >> 32;
}
Uint64 x;
// a0 * b;
lo = a0*b0; oldlo = lo;
x = a0*b1;
lo += x<<32;
if (lo < oldlo) hi++;
oldlo = lo;
hi += (x>>32);
// a1 * b;
x = a1*b0;
lo += x<<32;
if (lo < oldlo) hi++;
oldlo = lo;
hi += x>>32;
hi += a1*b1;
Sint64 out = (lo>>FRAC) + ((hi&MASK)<<(64-FRAC));
return isneg ? -out : out;
}
friend fixedf operator/(const fixedf a, const fixedf b) {
// 128-bit divided by 64-bit, to make sure high bits are not lost
Sint64 quotient_hi = a.v>>(64-FRAC);
Uint64 quotient_lo = a.v<<FRAC;
Sint64 d = b.v;
int isneg = 0;
Sint64 remainder=0;
if (d < 0) {
d = -d;
isneg = 1;
}
for (int i=0; i<128; i++) {
Uint64 sbit = (Uint64(1)<<63) & quotient_hi;
remainder <<= 1;
if (sbit) remainder |= 1;
// shift quotient left 1
{
quotient_hi <<= 1;
if (quotient_lo & (Uint64(1)<<63)) quotient_hi |= 1;
quotient_lo <<= 1;
}
if (remainder >= d) {
remainder -= d;
quotient_lo |= 1;
}
}
return (isneg ? -Sint64(quotient_lo) : quotient_lo);
}
friend bool operator==(const fixedf a, const fixedf b) { return a.v == b.v; }
friend bool operator!=(const fixedf a, const fixedf b) { return a.v != b.v; }
friend bool operator>(const fixedf a, const fixedf b) { return a.v > b.v; }
friend bool operator<(const fixedf a, const fixedf b) { return a.v < b.v; }
friend bool operator>=(const fixedf a, const fixedf b) { return a.v >= b.v; }
friend bool operator<=(const fixedf a, const fixedf b) { return a.v <= b.v; }
/* implicit operator float() bad */
int ToInt32() const { return int(v>>FRAC); }
Sint64 ToInt64() const { return v>>FRAC; }
float ToFloat() const { return v/float(Sint64(1)<<FRAC); }
double ToDouble() const { return v/double(Sint64(1)<<FRAC); }
template <int NEW_FRAC_BITS>
operator fixedf<NEW_FRAC_BITS>() const {
int shift = NEW_FRAC_BITS - FRAC_BITS;
if (shift > 0) return fixedf<NEW_FRAC_BITS>(v<<shift);
else return fixedf<NEW_FRAC_BITS>(v>>(-shift));
}
static fixedf SqrtOf(fixedf a) {
/* only works on even-numbered fractional bits */
assert(!(FRAC & 1));
Uint64 root, remHi, remLo, testDiv, count;
root = 0;
remHi = 0;
remLo = a.v;
count = 32+(FRAC>>1)-1;
do {
remHi = (remHi<<2) | (remLo>>62); remLo <<= 2;
root <<= 1;
testDiv = (root << 1) + 1;
if (remHi >= testDiv) {
remHi -= testDiv;
root++;
}
} while (count-- != 0);
return(fixedf(root));
}
static fixedf CubeRootOf(fixedf a) {
/* NR method. XXX very bad initial estimate (we get there in
* the end... XXX */
fixedf x = a;
for (int i=0; i<48; i++) x = fixedf(1,3) * ((a / (x*x)) + 2*x);
return x;
}
Sint64 v;
};
typedef fixedf<32> fixed;
#endif /* _FIXED_H */