summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--Stars45/Fix.h103
1 files changed, 22 insertions, 81 deletions
diff --git a/Stars45/Fix.h b/Stars45/Fix.h
index d3aade0..4369812 100644
--- a/Stars45/Fix.h
+++ b/Stars45/Fix.h
@@ -49,14 +49,7 @@ const double fix_sixty_five=65536.0;
inline int fast_f2i(double d)
{
- int i;
-
- _asm {
- fld d
- fistp i
- }
-
- return i;
+ return static_cast<int>(d);
}
// +--------------------------------------------------------------------+
@@ -73,18 +66,9 @@ public:
static const fix five;
static const fix ten;
- fix() { }
- fix(int n) : val(n<<Precision) { }
- fix(double d)
- {
- long ival;
- _asm {
- fld fix_sixty_five
- fmul d
- fistp ival
- }
- val=ival;
- }
+ fix() : val(0) { }
+ fix(int n) : val(n<<Precision) { }
+ fix(double d) : val(static_cast<long>(d * fix_sixty_five)) { }
fix(const fix& f) : val(f.val) { }
// conversion operators:
@@ -95,12 +79,7 @@ public:
// assignment operators:
fix& operator=(const fix& f) { val=f.val; return *this; }
fix& operator=(int n) { val=(n<<Precision); return *this; }
- fix& operator=(double d) { long ival;
- _asm { fld fix_sixty_five
- fmul d
- fistp ival }
- val = ival;
- return *this; }
+ fix& operator=(double d) { val = static_cast<long>(d * fix_sixty_five); return *this; }
// comparison operators:
int operator==(const fix& f) const { return val==f.val; }
@@ -113,48 +92,22 @@ public:
// arithmetic operators:
fix operator+(const fix& f) const { fix r; r.val = val+f.val; return r; }
fix operator-(const fix& f) const { fix r; r.val = val-f.val; return r; }
- fix operator*(const fix& f) const { long a=val; long b=f.val;
- _asm {
- mov eax, a
- mov edx, b
- imul edx
- shrd eax, edx, 16
- mov a, eax
- }
- fix r; r.val = a; return r; }
- fix operator/(const fix& f) const { long a=val; long b=f.val;
- _asm {
- mov eax, a
- mov ebx, b
- mov edx, eax
- sar edx, 16
- shl eax, 16
- idiv ebx
- mov a, eax
- }
- fix r; r.val = a; return r; }
+ fix operator*(const fix& f) const {
+ fix r;
+ r.val = (val * f.val) >> Precision;
+ return r; }
+ fix operator/(const fix& f) const {
+ fix r;
+ r.val = (val << Precision) / f.val;
+ return r; }
fix& operator+=(const fix& f) { val+=f.val; return *this; }
fix& operator-=(const fix& f) { val-=f.val; return *this; }
- fix& operator*=(const fix& f) { long a=val; long b=f.val;
- _asm {
- mov eax, a
- mov edx, b
- imul edx
- shrd eax, edx, 16
- mov a, eax
- }
- val=a; return *this; }
- fix& operator/=(const fix& f) { long a=val; long b=f.val;
- _asm {
- mov eax, a
- mov ebx, b
- mov edx, eax
- sar edx, 16
- shl eax, 16
- idiv ebx
- mov a, eax
- }
- val=a; return *this; }
+ fix& operator*=(const fix& f) {
+ val = (val * f.val) >> Precision;
+ return *this; }
+ fix& operator/=(const fix& f) {
+ val = (val << Precision) / f.val;
+ return *this; }
fix operator+(int n) const { fix r; r.val = val+(n<<Precision); return r; }
fix operator-(int n) const { fix r; r.val = val-(n<<Precision); return r; }
@@ -171,10 +124,8 @@ public:
fix operator/(double d) const { fix f(d); return (*this)/f; }
fix& operator+=(double d) { fix f(d); val+=f.val; return *this; }
fix& operator-=(double d) { fix f(d); val-=f.val; return *this; }
- fix& operator*=(double d) { int n; _asm { fld d
- fistp n } val*=n; return *this; }
- fix& operator/=(double d) { int n; _asm { fld d
- fistp n } val/=n; return *this; }
+ fix& operator*=(double d) { val*=static_cast<long>(d); return *this; }
+ fix& operator/=(double d) { val/=static_cast<long>(d); return *this; }
// misc. functions:
fix truncate() const { fix r; r.val = val&IntMask; return r; }
@@ -184,17 +135,7 @@ public:
fix adjust_up() const { fix r; r.val = val+FractMask; return r; }
fix adjust_down() const { fix r; r.val = val-FractMask; return r; }
- fix muldiv(const fix& num, const fix& den) const
- { long a=val, b=num.val, c=den.val;
- _asm {
- mov eax, a
- mov edx, b
- mov ebx, c
- imul edx
- idiv ebx
- mov a, eax
- }
- fix r; r.val = a; return r; }
+ fix muldiv(const fix& num, const fix& den) const { return (*this) * num / den; }
// data:
long val;