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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
|
//
// Fix.h : variable length fixed point data type
//
#ifndef _Fix_h
#ifdef __GNUG__
#pragma once
#pragma interface
#endif
#define _Fix_h 1
#include <stream.h>
#include <std.h>
#include <stddef.h>
#include <Integer.h>
typedef unsigned short uint16;
typedef short int16;
typedef unsigned long uint32;
typedef long int32;
#define _Fix_min_length 1
#define _Fix_max_length 65535
#define _Fix_min_value -1.0
#define _Fix_max_value 1.0
extern uint16 Fix_default_length;
extern int Fix_default_print_width;
struct _Frep // internal Fix representation
{
uint16 len; // length in bits
uint16 siz; // allocated storage
int16 ref; // reference count
uint16 s[1]; // start of ushort array represention
};
typedef struct _Frep* _Fix;
extern _Frep _Frep_0;
extern _Frep _Frep_m1;
extern _Frep _Frep_quotient_bump;
class Fix
{
_Fix rep;
Fix(_Fix);
void unique();
public:
Fix();
Fix(Fix&);
Fix(double&);
Fix(int);
Fix(int, Fix&);
Fix(int, double&);
Fix(int, _Frep*);
~Fix();
Fix operator = (Fix&);
Fix operator = (double&);
friend int operator == (Fix&, Fix& );
friend int operator != (Fix&, Fix&);
friend int operator < (Fix&, Fix&);
friend int operator <= (Fix&, Fix&);
friend int operator > (Fix&, Fix&);
friend int operator >= (Fix&, Fix&);
Fix& operator + ();
Fix operator - ();
friend Fix operator + (Fix&, Fix&);
friend Fix operator - (Fix&, Fix&);
friend Fix operator * (Fix&, Fix&);
friend Fix operator / (Fix&, Fix&);
friend Fix operator * (Fix&, int);
friend Fix operator * (int, Fix&);
friend Fix operator % (Fix&, int);
friend Fix operator << (Fix&, int);
friend Fix operator >> (Fix&, int);
#ifdef __GNUG__
friend Fix operator <? (Fix&, Fix&); // min
friend Fix operator >? (Fix&, Fix&); // max
#endif
Fix operator += (Fix&);
Fix operator -= (Fix&);
Fix operator *= (Fix&);
Fix operator /= (Fix&);
Fix operator *= (int);
Fix operator %= (int);
Fix operator <<=(int);
Fix operator >>=(int);
friend char* Ftoa(Fix&, int width = Fix_default_print_width);
friend Fix atoF(const char*, int len = Fix_default_length);
friend istream& operator >> (istream&, Fix&);
friend ostream& operator << (ostream&, Fix&);
// built-in functions
friend Fix abs(Fix); // absolute value
friend int sgn(Fix&); // -1, 0, +1
friend Integer mantissa(Fix&); // integer representation
friend double value(Fix&); // double value
friend int length(Fix&); // field length
friend void show(Fix&); // show contents
// error handlers
void error(const char* msg); // error handler
void range_error(const char* msg); // range error handler
// internal class functions
friend void mask(_Fix);
friend int compare(_Fix, _Fix = &_Frep_0);
friend _Fix new_Fix(uint16);
friend _Fix new_Fix(uint16, _Fix);
friend _Fix new_Fix(uint16, double);
friend _Fix copy(_Fix, _Fix);
friend _Fix negate(_Fix, _Fix = NULL);
friend _Fix add(_Fix, _Fix, _Fix = NULL);
friend _Fix subtract(_Fix, _Fix, _Fix = NULL);
friend _Fix multiply(_Fix, _Fix, _Fix = NULL);
friend _Fix multiply(_Fix, int, _Fix = NULL);
friend _Fix divide(_Fix, _Fix, _Fix = NULL, _Fix = NULL);
friend _Fix shift(_Fix, int, _Fix = NULL);
// non-operator versions for user
friend void negate(Fix& x, Fix& r);
friend void add(Fix& x, Fix& y, Fix& r);
friend void subtract(Fix& x, Fix& y, Fix& r);
friend void multiply(Fix& x, Fix& y, Fix& r);
friend void divide(Fix& x, Fix& y, Fix& q, Fix& r);
friend void shift(Fix& x, int y, Fix& r);
};
// error handlers
extern void
default_Fix_error_handler(const char*),
default_Fix_range_error_handler(const char*);
extern one_arg_error_handler_t
Fix_error_handler,
Fix_range_error_handler;
extern one_arg_error_handler_t
set_Fix_error_handler(one_arg_error_handler_t f),
set_Fix_range_error_handler(one_arg_error_handler_t f);
typedef void (*Fix_peh)(_Fix&);
extern Fix_peh Fix_overflow_handler;
extern void
Fix_overflow_saturate(_Fix&),
Fix_overflow_wrap(_Fix&),
Fix_overflow_warning_saturate(_Fix&),
Fix_overflow_warning(_Fix&),
Fix_overflow_error(_Fix&);
extern Fix_peh set_overflow_handler(Fix_peh);
extern int Fix_set_default_length(int);
// function definitions
#if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
inline void Fix::unique()
{
if ( rep->ref > 1 )
{
rep->ref--;
rep = new_Fix(rep->len,rep);
}
}
inline void mask (_Fix x)
{
int n = x->len & 0x0f;
if ( n )
x->s[x->siz - 1] &= 0xffff0000 >> n;
}
inline _Fix copy(_Fix from, _Fix to)
{
uint16 *ts = to->s, *fs = from->s;
int ilim = to->siz < from->siz ? to->siz : from->siz;
for ( int i=0; i < ilim; i++ )
*ts++ = *fs++;
for ( ; i < to->siz; i++ )
*ts++ = 0;
mask(to);
return to;
}
inline Fix::Fix(_Fix f)
{
rep = f;
}
inline Fix::Fix()
{
rep = new_Fix(Fix_default_length);
}
inline Fix::Fix(int len)
{
if ( len < _Fix_min_length || len > _Fix_max_length )
error("illegal length in declaration");
rep = new_Fix((uint16 )len);
}
inline Fix::Fix(double& d)
{
rep = new_Fix(Fix_default_length,d);
}
inline Fix::Fix(Fix& y)
{
rep = y.rep; rep->ref++;
}
inline Fix::Fix(int len, Fix& y)
{
if ( len < _Fix_min_length || len > _Fix_max_length )
error("illegal length in declaration");
rep = new_Fix((uint16 )len,y.rep);
}
inline Fix::Fix(int len, _Frep* fr)
{
if ( len < 1 || len > 65535 )
error("illegal length in declaration");
rep = new_Fix((uint16 )len,fr);
}
inline Fix::Fix(int len, double& d)
{
if ( len < _Fix_min_length || len > _Fix_max_length )
error("illegal length in declaration");
rep = new_Fix((uint16 )len,d);
}
inline Fix::~Fix()
{
if ( --rep->ref <= 0 ) delete rep;
}
inline Fix Fix::operator = (Fix& y)
{
if ( rep->len == y.rep->len ) {
++y.rep->ref;
if ( --rep->ref <= 0 ) delete rep;
rep = y.rep;
}
else {
unique();
copy(y.rep,rep);
}
return *this;
}
inline Fix Fix::operator = (double& d)
{
int oldlen = rep->len;
if ( --rep->ref <= 0 ) delete rep;
rep = new_Fix(oldlen,d);
return *this;
}
inline int operator == (Fix& x, Fix& y)
{
return compare(x.rep, y.rep) == 0;
}
inline int operator != (Fix& x, Fix& y)
{
return compare(x.rep, y.rep) != 0;
}
inline int operator < (Fix& x, Fix& y)
{
return compare(x.rep, y.rep) < 0;
}
inline int operator <= (Fix& x, Fix& y)
{
return compare(x.rep, y.rep) <= 0;
}
inline int operator > (Fix& x, Fix& y)
{
return compare(x.rep, y.rep) > 0;
}
inline int operator >= (Fix& x, Fix& y)
{
return compare(x.rep, y.rep) >= 0;
}
inline Fix& Fix::operator + ()
{
return *this;
}
inline Fix Fix::operator - ()
{
_Fix r = negate(rep); return r;
}
inline Fix operator + (Fix& x, Fix& y)
{
_Fix r = add(x.rep, y.rep); return r;
}
inline Fix operator - (Fix& x, Fix& y)
{
_Fix r = subtract(x.rep, y.rep); return r;
}
inline Fix operator * (Fix& x, Fix& y)
{
_Fix r = multiply(x.rep, y.rep); return r;
}
inline Fix operator * (Fix& x, int y)
{
_Fix r = multiply(x.rep, y); return r;
}
inline Fix operator * (int y, Fix& x)
{
_Fix r = multiply(x.rep, y); return r;
}
inline Fix operator / (Fix& x, Fix& y)
{
_Fix r = divide(x.rep, y.rep); return r;
}
inline Fix Fix::operator += (Fix& y)
{
unique(); add(rep, y.rep, rep); return *this;
}
inline Fix Fix::operator -= (Fix& y)
{
unique(); subtract(rep, y.rep, rep); return *this;
}
inline Fix Fix::operator *= (Fix& y)
{
unique(); multiply(rep, y.rep, rep); return *this;
}
inline Fix Fix::operator *= (int y)
{
unique(); multiply(rep, y, rep); return *this;
}
inline Fix Fix::operator /= (Fix& y)
{
unique(); divide(rep, y.rep, rep); return *this;
}
inline Fix operator % (Fix& x, int y)
{
Fix r((int )x.rep->len + y, x); return r;
}
inline Fix operator << (Fix& x, int y)
{
_Fix rep = shift(x.rep, y); return rep;
}
inline Fix operator >> (Fix& x, int y)
{
_Fix rep = shift(x.rep, -y); return rep;
}
inline Fix Fix::operator <<= (int y)
{
unique(); shift(rep, y, rep); return *this;
}
inline Fix Fix::operator >>= (int y)
{
unique(); shift(rep, -y, rep); return *this;
}
#ifdef __GNUG__
inline Fix operator <? (Fix& x, Fix& y)
{
if ( compare(x.rep, y.rep) <= 0 ) return x; else return y;
}
inline Fix operator >? (Fix& x, Fix& y)
{
if ( compare(x.rep, y.rep) >= 0 ) return x; else return y;
}
#endif
inline Fix abs(Fix x)
{
_Fix r = (compare(x.rep) >= 0 ? new_Fix(x.rep->len,x.rep) : negate(x.rep));
return r;
}
inline int sgn(Fix& x)
{
int a = compare(x.rep);
return a == 0 ? 0 : (a > 0 ? 1 : -1);
}
inline int length(Fix& x)
{
return x.rep->len;
}
inline ostream& operator << (ostream& s, Fix& y)
{
return s << Ftoa(y);
}
inline void negate (Fix& x, Fix& r)
{
negate(x.rep, r.rep);
}
inline void add (Fix& x, Fix& y, Fix& r)
{
add(x.rep, y.rep, r.rep);
}
inline void subtract (Fix& x, Fix& y, Fix& r)
{
subtract(x.rep, y.rep, r.rep);
}
inline void multiply (Fix& x, Fix& y, Fix& r)
{
multiply(x.rep, y.rep, r.rep);
}
inline void divide (Fix& x, Fix& y, Fix& q, Fix& r)
{
divide(x.rep, y.rep, q.rep, r.rep);
}
inline void shift (Fix& x, int y, Fix& r)
{
shift(x.rep, y, r.rep);
}
#endif
#endif
|