1 //========================================================================
5 // Fixed point type, with C++ operators.
7 // Copyright 2004 Glyph & Cog, LLC
9 //========================================================================
15 #ifdef USE_GCC_PRAGMAS
16 #pragma implementation
19 #include "FixedPoint.h"
21 FixedPoint
FixedPoint::sqrt(FixedPoint x
) {
31 y1
.val
= (y0
.val
+ z
.val
) >> 1;
32 } while (::abs(y0
.val
- y1
.val
) > 1);
37 //~ this is not very accurate
38 FixedPoint
FixedPoint::pow(FixedPoint x
, FixedPoint y
) {
39 FixedPoint t
, t2
, lnx0
, lnx
, z0
, z
;
46 t
= (x
- 1) / (x
+ 1);
55 } while (::abs(lnx
.val
- lnx0
.val
) > 2);
69 } while (::abs(z
.val
- z0
.val
) > 2 && d
< (1 << fixptShift
));
74 int FixedPoint::mul(int x
, int y
) {
76 return ((FixPtInt64
)x
* y
) >> fixptShift
;
78 int ah0
, ah
, bh
, al
, bl
;
83 bl
= y
- (bh
<< fixptShift
);
84 return ah0
* bh
+ ah
* bl
+ al
* bh
+ ((al
* bl
) >> fixptShift
);
88 int FixedPoint::div(int x
, int y
) {
90 return ((FixPtInt64
)x
<< fixptShift
) / y
;
95 #endif // USE_FIXEDPOINT