-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fixed.cs
242 lines (199 loc) · 7.04 KB
/
Fixed.cs
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
241
242
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace SkiaSharpOpenGLBenchmark
{
public struct Fixed
{
// 22:10 fixed point math
const int CSS_RADIX_POINT = 10;
public int RawValue;
// Useful values
public static readonly Fixed F_PI_2 = new Fixed(0x00000648, true); /* 1.5708 (PI/2) */
public static readonly Fixed F_PI = new Fixed(0x00000c91, true); /* 3.1415 (PI) */
public static readonly Fixed F_3PI_2 = new Fixed(0x000012d9, true); /* 4.7124 (3PI/2) */
public static readonly Fixed F_2PI = new Fixed(0x00001922, true); /* 6.2831 (2 PI) */
public static readonly Fixed F_90 = new Fixed(0x00016800, true); /* 90 */
public static readonly Fixed F_180 = new Fixed(0x0002d000, true); /* 180 */
public static readonly Fixed F_270 = new Fixed(0x00043800, true); /* 270 */
public static readonly Fixed F_360 = new Fixed(0x0005a000, true); /* 360 */
public static readonly Fixed F_0 = new Fixed(0x00000000, true); /* 0 */
public static readonly Fixed F_0_5 = new Fixed(0x00000200, true); /* 0.5 */
public static readonly Fixed F_1 = new Fixed(0x00000400, true); /* 1 */
public static readonly Fixed F_10 = new Fixed(0x00002800, true); /* 10 */
public static readonly Fixed F_72 = new Fixed(0x00012000, true); /* 72 */
public static readonly Fixed F_96 = new Fixed(0x00018000, true); /* 96 */
public static readonly Fixed F_100 = new Fixed(0x00019000, true); /* 100 */
public static readonly Fixed F_200 = new Fixed(0x00032000, true); /* 200 */
public static readonly Fixed F_255 = new Fixed(0x0003FC00, true); /* 255 */
public static readonly Fixed F_300 = new Fixed(0x0004b000, true); /* 300 */
public static readonly Fixed F_400 = new Fixed(0x00064000, true); /* 400 */
public Fixed(int a, bool fromRaw = false)
{
if (fromRaw)
RawValue = a;
else
RawValue = FromInt(a);
}
public Fixed(double a)
{
double xx = a * (double)(1 << CSS_RADIX_POINT);
if (xx < Int32.MinValue)
xx = Int32.MinValue;
if (xx > Int32.MaxValue)
xx = Int32.MaxValue;
RawValue = (int)xx;
}
static int FromInt(int a)
{
long xx = ((long)a) * (1 << CSS_RADIX_POINT);
if (xx < Int32.MinValue)
xx = Int32.MinValue;
if (xx > Int32.MaxValue)
xx = Int32.MaxValue;
return (int)xx;
//RawValue = (int)xx;
}
public int ToInt()
{
return (int)(RawValue >> CSS_RADIX_POINT);
}
public double ToDouble()
{
return ((double)(RawValue) / (double)(1 << CSS_RADIX_POINT));
}
public override string ToString()
{
int iPart = RawValue >> CSS_RADIX_POINT;
long fPart = RawValue & ((1 << CSS_RADIX_POINT) - 1);
fPart = (fPart * 1000000000) / ((long)1 << CSS_RADIX_POINT);
return $"{iPart}.{fPart}";
}
public Fixed Truncate()
{
int a = RawValue;
int raw = (a & ~((1 << CSS_RADIX_POINT) - 1));
return new Fixed(raw, true);
}
// Fixed point percentage (a) of an integer (b), to an integer
// FPCT_OF_INT_TOINT()
public int PercentageToInt(int b)
{
Fixed r = (this * b) / F_100;
return r.ToInt();
}
#region +
public static Fixed operator +(Fixed one, Fixed other)
{
Fixed fInt;
fInt.RawValue = one.RawValue + other.RawValue;
return fInt;
}
public static Fixed operator +(Fixed one, int other)
{
return one + (Fixed)other;
}
public static Fixed operator +(int other, Fixed one)
{
return one + (Fixed)other;
}
#endregion
#region -
public static Fixed operator -(Fixed one)
{
Fixed fint;
fint.RawValue = -one.RawValue;
return fint;
}
#endregion
#region *
public static Fixed operator *(Fixed one, Fixed other)
{
long xx = ((long)one.RawValue * (long)other.RawValue) >> CSS_RADIX_POINT;
if (xx < Int32.MinValue)
xx = Int32.MinValue;
if (xx > Int32.MaxValue)
xx = Int32.MaxValue;
Fixed fInt;
fInt.RawValue = (int)xx;
return fInt;
}
public static Fixed operator *(Fixed one, int other)
{
long orv = ((long)other) * (1 << CSS_RADIX_POINT);
long xx = ((long)one.RawValue * orv) >> CSS_RADIX_POINT;
if (xx < Int32.MinValue)
xx = Int32.MinValue;
if (xx > Int32.MaxValue)
xx = Int32.MaxValue;
Fixed fInt;
fInt.RawValue = (int)xx;
return fInt;
}
#endregion
#region /
public static Fixed operator /(Fixed x, Fixed y)
{
long xx = ((long)x.RawValue * (1 << CSS_RADIX_POINT)) / y.RawValue;
if (xx < Int32.MinValue)
xx = Int32.MinValue;
if (xx > Int32.MaxValue)
xx = Int32.MaxValue;
Fixed fInt;
fInt.RawValue = (int)xx;
return fInt;
}
#endregion
#region <>
public static bool operator <(Fixed one, int other)
{
Fixed o = (Fixed)other;
return one.RawValue < o.RawValue;
}
public static bool operator >(Fixed one, int other)
{
Fixed o = (Fixed)other;
return one.RawValue > o.RawValue;
}
public static bool operator <(Fixed one, Fixed o)
{
return one.RawValue < o.RawValue;
}
public static bool operator >(Fixed one, Fixed o)
{
return one.RawValue > o.RawValue;
}
#endregion
public static explicit operator int(Fixed src)
{
return src.ToInt();
}
public static explicit operator Fixed(int src)
{
// ?!?1
return new Fixed(src);
}
#region ==
public static bool operator ==(Fixed b1, Fixed b2)
{
if ((object)b1 == null)
return (object)b2 == null;
return b1.Equals(b2);
}
public static bool operator !=(Fixed b1, Fixed b2)
{
return !(b1 == b2);
}
public override bool Equals(object obj)
{
if (obj is Fixed)
return ((Fixed)obj).RawValue == this.RawValue;
else
return false;
}
#endregion
}
}