-
Notifications
You must be signed in to change notification settings - Fork 5
/
fix.h
186 lines (156 loc) · 6.71 KB
/
fix.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
#ifndef FIX_H
#define FIX_H
#include <cstdint>
#include <cmath>
#include <limits>
#include <cstring>
#include <cstdio>
template <unsigned int s, typename T=int_fast32_t> class Fix
{
public:
constexpr Fix() : value(0) {}
template <unsigned int s2, typename T2> Fix(const Fix<s2, T2> f) : value((s2 > s) ? (f.value >> (s2 - s)) : (f.value << (s-s2))) {}
constexpr Fix(const float v) : value(static_cast<T>(v * static_cast<float>(1<<s))) {}
constexpr Fix(const unsigned int v) : value(v << s) {}
constexpr Fix(const int v) : value(static_cast<unsigned int>(v) << s) {}
constexpr Fix(const long int v) : value(static_cast<unsigned long int>(v) << s) {}
constexpr operator float() const { return toFloat(); }
constexpr operator int() const { return toInteger<int>(); }
constexpr operator unsigned int() const { return toInteger<unsigned int>(); }
constexpr operator uint16_t() const { return toInteger<uint16_t>(); }
T round() const { T ret = value >> (s-1); return (ret>>1) + (ret&1); }
constexpr T floor() const { return value >> s; }
Fix<s,T> wholes() const { Fix<s,T> ret; ret.value = value & ~((1<<s)-1); return ret; }
Fix<s,T> operator -() const { Fix<s,T> ret; ret.value = -value; return ret; }
Fix<s,T>& operator +() const { return *this; }
Fix<s,T>& operator ++() { value += 1 << s; return *this; }
Fix<s,T>& operator --() { value -= 1 << s; return *this; }
template <typename U> Fix<s,T> operator >>(const U other) const { Fix<s,T> ret; ret.value = value >> other; return ret; }
template <typename U> Fix<s,T> operator <<(const U other) const { Fix<s,T> ret; ret.value = value << other; return ret; }
Fix<s,T> operator +(const Fix<s,T>& other) const { Fix<s,T> ret; ret.value = value + other.value; return ret; }
template <typename U> Fix<s,T> operator +(const U other) const { Fix<s,T> ret; ret.value = value + (other<<s); return ret; }
Fix<s,T> operator -(const Fix<s,T>& other) const { Fix<s,T> ret; ret.value = value - other.value; return ret; }
template <typename U> Fix<s,T> operator -(const U other) const { Fix<s,T> ret; ret.value = value - (other<<s); return ret; }
template <unsigned int s2, typename T2> Fix<s,T> operator *(const Fix<s2,T2>& other) const { Fix<s,T> ret; ret.value = (value * other.value) >> s2; return ret; }
template <typename U> Fix<s,T> operator *(const U other) const { Fix<s,T> ret; ret.value = value * other; return ret; }
Fix<s,T> operator /(const Fix<s,T>& other) const { Fix<s,T> ret; ret.value = (value << s) / other.value; return ret; }
template <typename U> Fix<s,T> operator /(const U other) const { Fix<s,T> ret; ret.value = value / other; return ret; }
Fix<s,T>& operator +=(const Fix<s,T>& other) { value += other.value; return *this; }
Fix<s,T>& operator -=(const Fix<s,T>& other) { value -= other.value; return *this; }
Fix<s,T>& operator *=(const float other) { value *= other; return *this; }
Fix<s,T>& operator *=(const int other) { value *= other; return *this; }
Fix<s,T>& operator *=(const Fix<s,T>& other) { value = (value * other.value) >> s; return *this; }
Fix<s,T>& operator /=(const float other) { value /= other; return *this; }
Fix<s,T>& operator /=(const int other) { value /= other; return *this; }
Fix<s,T>& operator /=(const Fix<s,T>& other) { value = (value << s) / other.value; return *this; }
constexpr bool operator >(const Fix<s,T>& other) const { return value > other.value; }
constexpr bool operator <(const Fix<s,T>& other) const { return value < other.value; }
template <typename U> constexpr bool operator >=(const U other) const { return value >= other<<s; }
constexpr bool operator >=(const Fix<s,T>& other) const { return value >= other.value; }
constexpr bool operator <=(const Fix<s,T>& other) const { return value <= other.value; }
constexpr bool operator ==(const Fix<s,T>& other) const { return value == other.value; }
constexpr bool operator !=(const Fix<s,T>& other) const { return value != other.value; }
template <typename U>
constexpr U toInteger() const
{
return value >> s;
}
constexpr float toFloat() const
{
return static_cast<float>(value) / static_cast<float>(1<<s);
}
template <typename U>
void fromInteger(const U v)
{
value = v << s;
}
void fromFloat(const float v)
{
value = static_cast<T>(v * static_cast<float>(1<<s));
}
Fix<s,T>& normaliseAngle()
{
while(*this < Fix<s,T>(0))
*this += Fix<s,T>(360);
while(*this >= 360)
*this -= Fix<s,T>(360);
return *this;
}
static Fix<s,T> minStep()
{
Fix<s,T> ret;
ret.value = 1;
return ret;
}
static Fix<s,T> minValue()
{
Fix<s,T> ret;
ret.value = std::numeric_limits<T>::min();
return ret;
}
static Fix<s,T> maxValue()
{
Fix<s,T> ret;
ret.value = std::numeric_limits<T>::max();
return ret;
}
void print() const
{
char str[32];
double n = toFloat();
if (n == 0.0)
strcpy(str, "0");
else {
int digit, m, m1;
char *c = str;
int neg = (n < 0);
if (neg)
n = -n;
m = log10(n);
int useExp = (m >= 14 || (neg && m >= 9) || m <= -9);
if (neg)
*(c++) = '-';
if (useExp) {
if (m < 0)
m -= 1.0;
n = n / pow(10.0, m);
m1 = m;
m = 0;}
if (m < 1.0)
m = 0;
while (n > 0.001 || m >= 0) {
double weight = pow(10.0, m);
if (weight > 0 && !__builtin_isinf(weight)) {
digit = ::floor(n / weight);
n -= (digit * weight);
*(c++) = '0' + digit; }
if (m == 0 && n > 0)
*(c++) = '.';
m--; }
if (useExp) {
int i, j;
*(c++) = 'e';
if (m1 > 0) {
*(c++) = '+';
} else {
*(c++) = '-';
m1 = -m1; }
m = 0;
while (m1 > 0) {
*(c++) = '0' + m1 % 10;
m1 /= 10;
m++; }
c -= m;
for (i = 0, j = m-1; i<j; i++, j--) {
c[i] ^= c[j];
c[j] ^= c[i];
c[i] ^= c[j];}
c += m;}
*(c) = '\0'; }
fputs(str, stdout);
}
using type = T;
constexpr static unsigned int precision = s;
T value;
};
#endif // FIX_H