-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathFixPoint.i
executable file
·407 lines (292 loc) · 11 KB
/
FixPoint.i
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
* FixPoint.i
*
* Provides fixpoint arithmetic (for use in SID.cpp)
* You need to define FIXPOINT_PREC (number of fractional bits) and
* ldSINTAB (ld of the size of the sinus table) as well M_PI
* _before_ including this file.
* Requires at least 32bit ints!
* (C) 1997 Andreas Dehmel
*/
#define FIXPOINT_BITS 32
#define FIXPOINT_SIGN (1<<(FIXPOINT_BITS-1))
/*
* Elementary functions for the FixPoint class
*/
// Multiplies two fixpoint numbers, result is a fixpoint number.
static inline int32 fixmult(int32 x, int32 y)
{
register uint32 a,b;
register bool sign;
sign = (x ^ y) < 0;
if (x < 0) {x = -x;}
if (y < 0) {y = -y;}
// a, b : integer part; x, y : fractional part. All unsigned now (for shift right)!!!
a = (((uint32)x) >> FIXPOINT_PREC); x &= ~(a << FIXPOINT_PREC);
b = (((uint32)y) >> FIXPOINT_PREC); y &= ~(b << FIXPOINT_PREC);
x = ((a*b) << FIXPOINT_PREC) + (a*y + b*x) +
((uint32)((x*y) + (1 << (FIXPOINT_PREC-1))) >> FIXPOINT_PREC);
#ifdef FIXPOINT_SIGN
if (x < 0) {x ^= FIXPOINT_SIGN;}
#endif
if (sign) {x = -x;}
return(x);
}
// Multiplies a fixpoint number with an integer, result is a 32 bit (!) integer in
// contrast to using the standard member-functions which can provide only (32-FIXPOINT_PREC)
// valid bits.
static inline int32 intmult(int32 x, int32 y) // x is fixpoint, y integer
{
register uint32 i,j;
register bool sign;
sign = (x ^ y) < 0;
if (x < 0) {x = -x;}
if (y < 0) {y = -y;}
i = (((uint32)x) >> 16); x &= ~(i << 16); // split both into 16.16 parts
j = (((uint32)y) >> 16); y &= ~(j << 16);
#if FIXPOINT_PREC <= 16
// This '32' is independent of the number of bits used, it's due to the 16 bit shift
i = ((i*j) << (32 - FIXPOINT_PREC)) + ((i*y + j*x) << (16 - FIXPOINT_PREC)) +
((uint32)(x*y + (1 << (FIXPOINT_PREC - 1))) >> FIXPOINT_PREC);
#else
{
register uint32 h;
h = (i*y + j*x);
i = ((i*j) << (32 - FIXPOINT_PREC)) + (h >> (FIXPOINT_PREC - 16));
h &= ((1 << (FIXPOINT_PREC - 16)) - 1); x *= y;
i += (x >> FIXPOINT_PREC); x &= ((1 << FIXPOINT_PREC) - 1);
i += (((h + (x >> 16)) + (1 << (FIXPOINT_PREC - 17))) >> (FIXPOINT_PREC - 16));
}
#endif
#ifdef FIXPOINT_SIGN
if (i < 0) {i ^= FIXPOINT_SIGN;}
#endif
if (sign) {i = -i;}
return(i);
}
// Computes the product of a fixpoint number with itself.
static inline int32 fixsquare(int32 x)
{
register uint32 a;
if (x < 0) {x = -x;}
a = (((uint32)x) >> FIXPOINT_PREC); x &= ~(a << FIXPOINT_PREC);
x = ((a*a) << FIXPOINT_PREC) + ((a*x) << 1) +
((uint32)((x*x) + (1 << (FIXPOINT_PREC-1))) >> FIXPOINT_PREC);
#ifdef FIXPOINT_SIGN
if (x < 0) {x ^= FIXPOINT_SIGN;}
#endif
return(x);
}
// Computes the square root of a fixpoint number.
static inline int32 fixsqrt(int32 x)
{
register int test, step;
if (x < 0) return(-1); if (x == 0) return(0);
step = (x <= (1<<FIXPOINT_PREC)) ? (1<<FIXPOINT_PREC) : (1<<((FIXPOINT_BITS - 2 + FIXPOINT_PREC)>>1));
test = 0;
while (step != 0)
{
register int h;
h = fixsquare(test + step);
if (h <= x) {test += step;}
if (h == x) break;
step >>= 1;
}
return(test);
}
// Divides a fixpoint number by another fixpoint number, yielding a fixpoint result.
static inline int32 fixdiv(int32 x, int32 y)
{
register int32 res, mask;
register bool sign;
sign = (x ^ y) < 0;
if (x < 0) {x = -x;}
if (y < 0) {y = -y;}
mask = (1<<FIXPOINT_PREC); res = 0;
while (x > y) {y <<= 1; mask <<= 1;}
while (mask != 0)
{
if (x >= y) {res |= mask; x -= y;}
mask >>= 1; y >>= 1;
}
#ifdef FIXPOINT_SIGN
if (res < 0) {res ^= FIXPOINT_SIGN;}
#endif
if (sign) {res = -res;}
return(res);
}
/*
* The C++ Fixpoint class. By no means exhaustive...
* Since it contains only one int data, variables of type FixPoint can be
* passed directly rather than as a reference.
*/
class FixPoint
{
private:
int32 x;
public:
FixPoint(void);
FixPoint(int32 y);
~FixPoint(void);
// conversions
int32 Value(void);
int32 round(void);
operator int32(void);
// unary operators
FixPoint sqrt(void);
FixPoint sqr(void);
FixPoint abs(void);
FixPoint operator+(void);
FixPoint operator-(void);
FixPoint operator++(void);
FixPoint operator--(void);
// binary operators
int32 imul(int32 y);
FixPoint operator=(FixPoint y);
FixPoint operator=(int32 y);
FixPoint operator+(FixPoint y);
FixPoint operator+(int32 y);
FixPoint operator-(FixPoint y);
FixPoint operator-(int32 y);
FixPoint operator/(FixPoint y);
FixPoint operator/(int32 y);
FixPoint operator*(FixPoint y);
FixPoint operator*(int32 y);
FixPoint operator+=(FixPoint y);
FixPoint operator+=(int32 y);
FixPoint operator-=(FixPoint y);
FixPoint operator-=(int32 y);
FixPoint operator*=(FixPoint y);
FixPoint operator*=(int32 y);
FixPoint operator/=(FixPoint y);
FixPoint operator/=(int32 y);
FixPoint operator<<(int8 y);
FixPoint operator>>(int8 y);
FixPoint operator<<=(int8 y);
FixPoint operator>>=(int8 y);
// conditional operators
bool operator<(FixPoint y);
bool operator<(int32 y);
bool operator<=(FixPoint y);
bool operator<=(int32 y);
bool operator>(FixPoint y);
bool operator>(int32 y);
bool operator>=(FixPoint y);
bool operator>=(int32 y);
bool operator==(FixPoint y);
bool operator==(int32 y);
bool operator!=(FixPoint y);
bool operator!=(int32 y);
};
/*
* int gets treated differently according to the case:
*
* a) Equations (=) or condition checks (==, <, <= ...): raw int (i.e. no conversion)
* b) As an argument for an arithmetic operation: conversion to fixpoint by shifting
*
* Otherwise loading meaningful values into FixPoint variables would be very awkward.
*/
FixPoint::FixPoint(void) {x = 0;}
FixPoint::FixPoint(int32 y) {x = y;}
FixPoint::~FixPoint(void) {;}
inline int32 FixPoint::Value(void) {return(x);}
inline int32 FixPoint::round(void) {return((x + (1 << (FIXPOINT_PREC-1))) >> FIXPOINT_PREC);}
inline FixPoint::operator int32(void) {return(x);}
// unary operators
inline FixPoint FixPoint::sqrt(void) {return(fixsqrt(x));}
inline FixPoint FixPoint::sqr(void) {return(fixsquare(x));}
inline FixPoint FixPoint::abs(void) {return((x < 0) ? -x : x);}
inline FixPoint FixPoint::operator+(void) {return(x);}
inline FixPoint FixPoint::operator-(void) {return(-x);}
inline FixPoint FixPoint::operator++(void) {x += (1 << FIXPOINT_PREC); return x;}
inline FixPoint FixPoint::operator--(void) {x -= (1 << FIXPOINT_PREC); return x;}
// binary operators
inline int32 FixPoint::imul(int32 y) {return(intmult(x,y));}
inline FixPoint FixPoint::operator=(FixPoint y) {x = y.Value(); return x;}
inline FixPoint FixPoint::operator=(int32 y) {x = y; return x;}
inline FixPoint FixPoint::operator+(FixPoint y) {return(x + y.Value());}
inline FixPoint FixPoint::operator+(int32 y) {return(x + (y << FIXPOINT_PREC));}
inline FixPoint FixPoint::operator-(FixPoint y) {return(x - y.Value());}
inline FixPoint FixPoint::operator-(int32 y) {return(x - (y << FIXPOINT_PREC));}
inline FixPoint FixPoint::operator/(FixPoint y) {return(fixdiv(x,y.Value()));}
inline FixPoint FixPoint::operator/(int32 y) {return(x/y);}
inline FixPoint FixPoint::operator*(FixPoint y) {return(fixmult(x,y.Value()));}
inline FixPoint FixPoint::operator*(int32 y) {return(x*y);}
inline FixPoint FixPoint::operator+=(FixPoint y) {x += y.Value(); return x;}
inline FixPoint FixPoint::operator+=(int32 y) {x += (y << FIXPOINT_PREC); return x;}
inline FixPoint FixPoint::operator-=(FixPoint y) {x -= y.Value(); return x;}
inline FixPoint FixPoint::operator-=(int32 y) {x -= (y << FIXPOINT_PREC); return x;}
inline FixPoint FixPoint::operator*=(FixPoint y) {x = fixmult(x,y.Value()); return x;}
inline FixPoint FixPoint::operator*=(int32 y) {x *= y; return x;}
inline FixPoint FixPoint::operator/=(FixPoint y) {x = fixdiv(x,y.Value()); return x;}
inline FixPoint FixPoint::operator/=(int32 y) {x /= y; return x;}
inline FixPoint FixPoint::operator<<(int8 y) {return(x << y);}
inline FixPoint FixPoint::operator>>(int8 y) {return(x >> y);}
inline FixPoint FixPoint::operator<<=(int8 y) {x <<= y; return x;}
inline FixPoint FixPoint::operator>>=(int8 y) {x >>= y; return x;}
// conditional operators
inline bool FixPoint::operator<(FixPoint y) {return(x < y.Value());}
inline bool FixPoint::operator<(int32 y) {return(x < y);}
inline bool FixPoint::operator<=(FixPoint y) {return(x <= y.Value());}
inline bool FixPoint::operator<=(int32 y) {return(x <= y);}
inline bool FixPoint::operator>(FixPoint y) {return(x > y.Value());}
inline bool FixPoint::operator>(int32 y) {return(x > y);}
inline bool FixPoint::operator>=(FixPoint y) {return(x >= y.Value());}
inline bool FixPoint::operator>=(int32 y) {return(x >= y);}
inline bool FixPoint::operator==(FixPoint y) {return(x == y.Value());}
inline bool FixPoint::operator==(int32 y) {return(x == y);}
inline bool FixPoint::operator!=(FixPoint y) {return(x != y.Value());}
inline bool FixPoint::operator!=(int32 y) {return(x != y);}
/*
* In case the first argument is an int (i.e. member-operators not applicable):
* Not supported: things like int/FixPoint. The same difference in conversions
* applies as mentioned above.
*/
// binary operators
inline FixPoint operator+(int32 x, FixPoint y) {return((x << FIXPOINT_PREC) + y.Value());}
inline FixPoint operator-(int32 x, FixPoint y) {return((x << FIXPOINT_PREC) - y.Value());}
inline FixPoint operator*(int32 x, FixPoint y) {return(x*y.Value());}
// conditional operators
inline bool operator==(int32 x, FixPoint y) {return(x == y.Value());}
inline bool operator!=(int32 x, FixPoint y) {return(x != y.Value());}
inline bool operator<(int32 x, FixPoint y) {return(x < y.Value());}
inline bool operator<=(int32 x, FixPoint y) {return(x <= y.Value());}
inline bool operator>(int32 x, FixPoint y) {return(x > y.Value());}
inline bool operator>=(int32 x, FixPoint y) {return(x >= y.Value());}
/*
* For more convenient creation of constant fixpoint numbers from constant floats.
*/
#define FixNo(n) (FixPoint)((int)(n*(1<<FIXPOINT_PREC)))
/*
* Stuff re. the sinus table used with fixpoint arithmetic
*/
// define as global variable
FixPoint SinTable[(1<<ldSINTAB)];
#define FIXPOINT_SIN_COS_GENERIC \
if (angle >= 3*(1<<ldSINTAB)) {return(-SinTable[(1<<(ldSINTAB+2)) - angle]);}\
if (angle >= 2*(1<<ldSINTAB)) {return(-SinTable[angle - 2*(1<<ldSINTAB)]);}\
if (angle >= (1<<ldSINTAB)) {return(SinTable[2*(1<<ldSINTAB) - angle]);}\
return(SinTable[angle]);
// sin and cos: angle is fixpoint number 0 <= angle <= 2 (*PI)
static inline FixPoint fixsin(FixPoint x)
{
int32 angle = x;
angle = (angle >> (FIXPOINT_PREC - ldSINTAB - 1)) & ((1<<(ldSINTAB+2))-1);
FIXPOINT_SIN_COS_GENERIC
}
static inline FixPoint fixcos(FixPoint x)
{
int32 angle = x;
// cos(x) = sin(x+PI/2)
angle = (angle + (1<<(FIXPOINT_PREC-1)) >> (FIXPOINT_PREC - ldSINTAB - 1)) & ((1<<(ldSINTAB+2))-1);
FIXPOINT_SIN_COS_GENERIC
}
static inline void InitFixSinTab(void)
{
int i;
float step;
for (i=0, step=0; i<(1<<ldSINTAB); i++, step+=0.5/(1<<ldSINTAB))
{
SinTable[i] = FixNo(sin(M_PI * step));
}
}