# sleeplessinc/sleepylib

Switch branches/tags
Nothing to show
Fetching contributors…
Cannot retrieve contributors at this time
executable file 109 lines (86 sloc) 2.55 KB
 #ifndef com_sleepless_fixedpoint #define com_sleepless_fixedpoint /* Copyright 2004 Sleepless Software Inc. All Rights Reserved */ typedef long fixed; fixed float2fixed(float x) { if (x > 32767.0f) { return 0x7FFFFFFF; } if (x < -32767.0) { return -0x7FFFFFFF; } return (long)(x * 65536.0f + (x < 0 ? -0.5f : 0.5f)); } float fixed2float(fixed x) { return (float)x / 65536.0f; } #if 0 struct fixed { long v; inline FixedPoint() { v = 0; } /* Only use this for initializing a FixedPoint with a constant. */ inline FixedPoint(float f) { v = (int)(f * (float)(1 << FIXEDPOINT_DECIMALS)); } /* Automatic conversion between FixedPoint and int */ inline FixedPoint(int i) { v = i << FIXEDPOINT_DECIMALS; } inline operator int() const { return (v + 0x80) >> FIXEDPOINT_DECIMALS; } // XXX? /* Automatic conversion between FixedPoint and float */ inline FixedPoint(float f) { int iprt = (int)f; int fprt = (int); v = i << FIXEDPOINT_DECIMALS; } inline operator float() const { return (v + 0x80) >> FIXEDPOINT_DECIMALS; } /* Basic fixed-point math operators. */ inline FixedPoint operator +(FixedPoint x) const { FixedPoint r; r.v = v + x.v; return r; } inline FixedPoint operator -(FixedPoint x) const { FixedPoint r; r.v = v - x.v; return r; } inline FixedPoint operator -() const { FixedPoint r; r.v = -v; return r; } inline FixedPoint operator *(int scale) const { FixedPoint r; r.v = v * scale; return r; } inline FixedPoint operator /(int scale) const { FixedPoint r; r.v = v / scale; return r; } inline FixedPoint operator <<(int nbits) const { FixedPoint r; r.v = v << nbits; return r; } inline FixedPoint operator >>(int nbits) const { FixedPoint r; r.v = v >> nbits; return r; } /* Comparison operators. */ inline int operator <(FixedPoint x) const { return v < x.v; } inline int operator <=(FixedPoint x) const { return v <= x.v; } inline int operator >(FixedPoint x) const { return v > x.v; } inline int operator >=(FixedPoint x) const { return v >= x.v; } inline int operator !=(FixedPoint x) const { return v != x.v; } inline int operator ==(FixedPoint x) const { return v == x.v; } /* In-place math operators. */ inline void operator +=(FixedPoint x) { v += x.v; } inline void operator -=(FixedPoint x) { v -= x.v; } inline void operator *=(int scale) { v *= scale; } inline void operator /=(int scale) { v /= scale; } inline void operator >>=(int scale) { v >>= scale; } inline void operator <<=(int scale) { v <<= scale; } }; #endif #endif //com_sleepless_fixedpoint