Starshatter_Open
Open source Starshatter engine
Main Page
Classes
Files
File List
File Members
All
Classes
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Fix.h
Go to the documentation of this file.
1
/* Project nGenEx
2
Destroyer Studios LLC
3
Copyright © 1997-2004. All Rights Reserved.
4
5
SUBSYSTEM: nGenEx.lib
6
FILE: Fix.h
7
AUTHOR: John DiCamillo
8
9
10
OVERVIEW
11
========
12
Fixed point number class with 16 bits of fractional precision
13
*/
14
15
#ifndef Fix_h
16
#define Fix_h
17
18
// +--------------------------------------------------------------------+
19
20
#include "
Types.h
"
21
22
// +--------------------------------------------------------------------+
23
24
const
double
fix_sixty_five
=65536.0;
25
26
inline
int
fast_f2i
(
double
d)
27
{
28
int
i;
29
30
_asm {
31
fld d
32
fistp i
33
}
34
35
return
i;
36
}
37
38
// +--------------------------------------------------------------------+
39
40
class
fix
41
{
42
public
:
43
static
const
char
*
TYPENAME
() {
return
"fix"
; }
44
45
enum
FixDef
{
Precision
=16,
IntMask
=0xffff0000,
FractMask
=0x0000ffff };
46
static
const
fix
one
;
47
static
const
fix
two
;
48
static
const
fix
three
;
49
static
const
fix
five
;
50
static
const
fix
ten
;
51
52
fix
() { }
53
fix
(
int
n) :
val
(n<<
Precision
) { }
54
fix
(
double
d)
55
{
56
long
ival;
57
_asm {
58
fld
fix_sixty_five
59
fmul d
60
fistp ival
61
}
62
val
=ival;
63
}
64
fix
(
const
fix
& f) :
val
(f.
val
) { }
65
66
// conversion operators:
67
operator
int ()
const
{
return
(
val
>>
Precision
); }
68
operator
float ()
const
{
return
((
float
)
val
) / ((float)
fix_sixty_five
); }
69
operator
double()
const
{
return
((
double
)
val
) /
fix_sixty_five
; }
70
71
// assignment operators:
72
fix
&
operator=
(
const
fix
& f) {
val
=f.
val
;
return
*
this
; }
73
fix
&
operator=
(
int
n) {
val
=(n<<
Precision
);
return
*
this
; }
74
fix
&
operator=
(
double
d) {
long
ival;
75
_asm { fld
fix_sixty_five
76
fmul d
77
fistp ival }
78
val
= ival;
79
return
*
this
; }
80
81
// comparison operators:
82
int
operator==
(
const
fix
& f)
const
{
return
val
==f.
val
; }
83
int
operator!=
(
const
fix
& f)
const
{
return
val
!=f.
val
; }
84
int
operator<=
(
const
fix
& f)
const
{
return
val
<=f.
val
; }
85
int
operator>=
(
const
fix
& f)
const
{
return
val
>=f.
val
; }
86
int
operator<
(
const
fix
& f)
const
{
return
val
< f.
val
; }
87
int
operator>
(
const
fix
& f)
const
{
return
val
> f.
val
; }
88
89
// arithmetic operators:
90
fix
operator+
(
const
fix
& f)
const
{
fix
r; r.
val
=
val
+f.
val
;
return
r; }
91
fix
operator-
(
const
fix
& f)
const
{
fix
r; r.
val
=
val
-f.
val
;
return
r; }
92
fix
operator*
(
const
fix
& f)
const
{
long
a=
val
;
long
b=f.
val
;
93
_asm {
94
mov eax, a
95
mov edx, b
96
imul edx
97
shrd eax, edx, 16
98
mov a, eax
99
}
100
fix
r; r.
val
= a;
return
r; }
101
fix
operator/
(
const
fix
& f)
const
{
long
a=
val
;
long
b=f.
val
;
102
_asm {
103
mov eax, a
104
mov ebx, b
105
mov edx, eax
106
sar edx, 16
107
shl eax, 16
108
idiv ebx
109
mov a, eax
110
}
111
fix
r; r.
val
= a;
return
r; }
112
fix
&
operator+=
(
const
fix
& f) {
val
+=f.
val
;
return
*
this
; }
113
fix
&
operator-=
(
const
fix
& f) {
val
-=f.
val
;
return
*
this
; }
114
fix
&
operator*=
(
const
fix
& f) {
long
a=
val
;
long
b=f.
val
;
115
_asm {
116
mov eax, a
117
mov edx, b
118
imul edx
119
shrd eax, edx, 16
120
mov a, eax
121
}
122
val
=a;
return
*
this
; }
123
fix
&
operator/=
(
const
fix
& f) {
long
a=
val
;
long
b=f.
val
;
124
_asm {
125
mov eax, a
126
mov ebx, b
127
mov edx, eax
128
sar edx, 16
129
shl eax, 16
130
idiv ebx
131
mov a, eax
132
}
133
val
=a;
return
*
this
; }
134
135
fix
operator+
(
int
n)
const
{
fix
r; r.
val
=
val
+(n<<
Precision
);
return
r; }
136
fix
operator-
(
int
n)
const
{
fix
r; r.
val
=
val
-(n<<
Precision
);
return
r; }
137
fix
operator*
(
int
n)
const
{
fix
r; r.
val
=
val
*n;
return
r; }
138
fix
operator/
(
int
n)
const
{
fix
r; r.
val
=
val
/n;
return
r; }
139
fix
&
operator+=
(
int
n) {
val
+=(n<<
Precision
);
return
*
this
; }
140
fix
&
operator-=
(
int
n) {
val
-=(n<<
Precision
);
return
*
this
; }
141
fix
&
operator*=
(
int
n) {
val
*=n;
return
*
this
; }
142
fix
&
operator/=
(
int
n) {
val
/=n;
return
*
this
; }
143
144
fix
operator+
(
double
d)
const
{
fix
f(d);
return
(*
this
)+f; }
145
fix
operator-
(
double
d)
const
{
fix
f(d);
return
(*
this
)-f; }
146
fix
operator*
(
double
d)
const
{
fix
f(d);
return
(*
this
)*f; }
147
fix
operator/
(
double
d)
const
{
fix
f(d);
return
(*
this
)/f; }
148
fix
&
operator+=
(
double
d) {
fix
f(d);
val
+=f.
val
;
return
*
this
; }
149
fix
&
operator-=
(
double
d) {
fix
f(d);
val
-=f.
val
;
return
*
this
; }
150
fix
&
operator*=
(
double
d) {
int
n; _asm { fld d
151
fistp n }
val
*=n;
return
*
this
; }
152
fix
&
operator/=
(
double
d) {
int
n; _asm { fld d
153
fistp n }
val
/=n;
return
*
this
; }
154
155
// misc. functions:
156
fix
truncate
()
const
{
fix
r; r.
val
=
val
&
IntMask
;
return
r; }
157
fix
fraction
()
const
{
fix
r; r.
val
=
val
-
truncate
().
val
;
return
r; }
158
fix
floor
()
const
{
fix
r; r.
val
=
val
&
IntMask
;
return
r; }
159
fix
ceil
()
const
{
fix
r; r.
val
= (
val
+
FractMask
)&
IntMask
;
return
r; }
160
fix
adjust_up
()
const
{
fix
r; r.
val
=
val
+
FractMask
;
return
r; }
161
fix
adjust_down
()
const
{
fix
r; r.
val
=
val
-
FractMask
;
return
r; }
162
163
fix
muldiv
(
const
fix
& num,
const
fix
& den)
const
164
{
long
a=
val
, b=num.
val
, c=den.
val
;
165
_asm {
166
mov eax, a
167
mov edx, b
168
mov ebx, c
169
imul edx
170
idiv ebx
171
mov a, eax
172
}
173
fix
r; r.
val
= a;
return
r; }
174
175
// data:
176
long
val
;
177
};
178
179
#endif Fix_h
180
nGenEx
Fix.h
Generated on Thu May 31 2012 16:30:53 for Starshatter_Open by
1.8.1