#ifndef _IOFIXED64_H
#define _IOFIXED64_H
#include <IOKit/IOTypes.h>
#include <machine/limits.h>
#ifndef INT_MAX
#define INT_MAX 2147483647L
#endif
#ifndef INT_MIN
#define INT_MIN (-2147483647L-1)
#endif
class IOFixed64 {
public:
IOFixed64() {
value = 0;
}
SInt64 as64() const {
return value / 65536LL;
}
SInt64 asFixed64() const {
return value;
}
SInt32 asFixed24x8() const {
return value / 256LL;
}
UInt16 fraction() const {
UInt64 result;
if( value < 0LL )
result = value | ~0xffffLL;
else
result = value & 0xffffLL;
return result;
}
SInt32 as32() const {
SInt64 result = as64();
if (result > INT_MAX)
return INT_MAX;
if (result < INT_MIN)
return INT_MIN;
return (SInt32)result;
}
IOFixed asFixed() const {
if (value > INT_MAX)
return INT_MAX;
if (value < INT_MIN)
return INT_MIN;
return IOFixed(value);
}
IOFixed64& fromIntFloor(SInt64 x) {
value = (x * 65536LL);
return *this;
}
static IOFixed64 withIntFloor(SInt64 x) {
IOFixed64 result;
return result.fromIntFloor(x);
}
IOFixed64& fromIntCeiling(SInt64 x) {
value = (x * 65536LL) + 65535LL;
return *this;
}
IOFixed64& fromFixed(IOFixed x) {
value = x;
return *this;
}
static IOFixed64 withFixed(IOFixed x) {
IOFixed64 result;
return result.fromFixed(x);
}
IOFixed64& fromFixed64(SInt64 x) {
value = x;
return *this;
}
IOFixed64& fromFixed24x8(SInt32 x) {
value = x * 256LL;
return *this;
}
IOFixed64& operator+=(IOFixed64 x) {
value += x.value;
return *this;
}
IOFixed64& operator-=(IOFixed64 x) {
value -= x.value;
return *this;
}
IOFixed64& operator*=(IOFixed64 x) {
value *= x.value;
value /= 65536LL;
return *this;
}
IOFixed64& operator/=(IOFixed64 x) {
value *= 65536LL;
value /= x.value;
return *this;
}
IOFixed64& operator+=(SInt64 x) {
value += x * 65536LL;
return *this;
}
IOFixed64& operator-=(SInt64 x) {
value -= x * 65536LL;
return *this;
}
IOFixed64& operator*=(SInt64 x) {
value *= x;
return *this;
}
IOFixed64& operator/=(SInt64 x) {
value /= x;
return *this;
}
operator const bool() {
return (value != 0);
}
#define BOOL_OPERATOR(X) \
bool operator X (const IOFixed64 b) const { return value X b.value; }; \
bool operator X (const SInt64 b) const { return value X (b * 65536LL); };
BOOL_OPERATOR(>)
BOOL_OPERATOR(>=)
BOOL_OPERATOR(<)
BOOL_OPERATOR(<=)
BOOL_OPERATOR(==)
BOOL_OPERATOR(!=)
#undef BOOL_OPERATOR
private:
SInt64 value;
};
IOFixed64 operator* (const IOFixed64 a, const IOFixed64 b);
IOFixed64 operator* (const IOFixed64 b, const SInt64 a);
IOFixed64 operator/ (const IOFixed64 a, const IOFixed64 b);
IOFixed64 operator/ (const IOFixed64 a, const SInt64 b);
IOFixed64 operator+ (const IOFixed64 a, const IOFixed64 b);
IOFixed64 operator+ (const IOFixed64 a, const SInt64 b);
IOFixed64 operator- (const IOFixed64 a, const IOFixed64 b);
IOFixed64 operator- (const IOFixed64 a, const SInt64 b);
IOFixed64 exponent(const IOFixed64 original, const UInt8 power);
UInt32 llsqrt(UInt64 x);
UInt16 lsqrt(UInt32 x);
IOFixed64 IOQuarticFunction( const IOFixed64 x, const IOFixed64 *gains );
IOFixed64 IOQuarticDerivative( const IOFixed64 x, const IOFixed64 *gains );
#endif // _IOFIXED64_H