Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
opcodeqint.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 1998-2000 by Andrew Zabolotny
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public
15  License along with this library; if not, write to the Free
16  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18 
19 #ifndef __QINT_H__
20 #define __QINT_H__
21 
22 #if defined (CS_IEEE_DOUBLE_FORMAT)
23 
106 #ifdef CS_BIG_ENDIAN
107 # define CS_LOWER_WORD_BYTE 4
108 #else
109 # define CS_LOWER_WORD_BYTE 0
110 #endif
111 
112 #define CS_LONG_AT_BYTE(x,b) *(long *)(((char *)&x) + b)
113 
120 #define FIST_MAGIC_QINT (65536.0 * 65536.0 * 16.0)
121 
123 
128 #ifdef CS_QINT_WORKAROUND
129 static inline long QInt (double inval)
130 {
131  union { double dtemp; long result; } x;
132 
133  x.dtemp = FIST_MAGIC_QINT + inval;
134  x.result = CS_LONG_AT_BYTE (x.dtemp, 2);
135  return x.result < 0 ? (x.result >> 1) + 1 : x.result;
136 }
137 #else
138 static inline long QInt (double inval)
139 {
140  double dtemp = FIST_MAGIC_QINT + inval;
141  // Note that on both low-endian (x86) and big-endian (m68k) we have
142  // to shift by two bytes. So no need for an #ifdef.
143  long result = CS_LONG_AT_BYTE (dtemp, 2);
144  return result < 0 ? (result >> 1) + 1 : result;
145 }
146 #endif
147 
152 #define FIST_MAGIC_QROUND (((65536.0 * 65536.0 * 16.0) + (65536.0 * 0.5)) * 65536.0)
153 
155 static inline long QRound (double inval)
156 {
157  double dtemp = FIST_MAGIC_QROUND + inval;
158  return CS_LONG_AT_BYTE (dtemp, CS_LOWER_WORD_BYTE) - 0x80000000;
159 }
160 
165 #define FIST_MAGIC_QINT8 (((65536.0 * 16.0) + 0.5) * 65536.0 * 256.0)
166 
168 inline long QInt8 (float inval)
169 {
170  double dtemp = FIST_MAGIC_QINT8 + inval;
171  return CS_LONG_AT_BYTE (dtemp, CS_LOWER_WORD_BYTE) - 0x80000000;
172 }
173 
178 #define FIST_MAGIC_QINT16 (((65536.0 * 16.0) + 0.5) * 65536.0)
179 
181 inline long QInt16 (float inval)
182 {
183  double dtemp = FIST_MAGIC_QINT16 + inval;
184  return CS_LONG_AT_BYTE (dtemp, CS_LOWER_WORD_BYTE) - 0x80000000;
185 }
186 
191 #define FIST_MAGIC_QINT24 (((65536.0 * 16.0) + 0.5) * 256.0)
192 
193 inline long QInt24 (float inval)
194 {
195  double dtemp = FIST_MAGIC_QINT24 + inval;
196  return CS_LONG_AT_BYTE (dtemp, CS_LOWER_WORD_BYTE) - 0x80000000;
197 }
198 
199 #else /* not CS_IEEE_DOUBLE_FORMAT */
200 
201 #define QRound(x) (int ((x) + ((x < 0) ? -0.5 : +0.5)))
202 #define QInt(x) (int (x))
203 #define QInt8(x) (int ((x)*256.))
204 #define QInt16(x) (int ((x)*65536.))
205 #define QInt24(x) (int ((x)*16777216.))
206 
207 #endif /* CS_IEEE_DOUBLE_FORMAT */
208 
209 #endif // __QINT_H__