Google

Main Page   Class Hierarchy   Compound List   File List   Compound Members  

qint.h

00001 /*
00002     Copyright (C) 1998-2000 by Andrew Zabolotny
00003   
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Library General Public
00006     License as published by the Free Software Foundation; either
00007     version 2 of the License, or (at your option) any later version.
00008   
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Library General Public License for more details.
00013   
00014     You should have received a copy of the GNU Library General Public
00015     License along with this library; if not, write to the Free
00016     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017 */
00018 
00019 #ifndef __QINT_H__
00020 #define __QINT_H__
00021 
00022 #if defined (CS_IEEE_DOUBLE_FORMAT)
00023 
00106 #ifdef CS_BIG_ENDIAN
00107 #  define CS_LOWER_WORD_BYTE    4
00108 #else
00109 #  define CS_LOWER_WORD_BYTE    0
00110 #endif
00111 
00112 #define CS_LONG_AT_BYTE(x,b)    *(long *)(((char *)&x) + b)
00113 
00120 #define FIST_MAGIC_QINT   (65536.0 * 65536.0 * 16.0)
00121 
00123 
00128 #ifdef CS_QINT_WORKAROUND
00129 static inline long QInt (double inval)
00130 {
00131   union { double dtemp; long result; } x;
00132 
00133   x.dtemp = FIST_MAGIC_QINT + inval;
00134   x.result = CS_LONG_AT_BYTE (x.dtemp, 2);
00135   return x.result < 0 ? (x.result >> 1) + 1 : x.result;
00136 }
00137 #else
00138 static inline long QInt (double inval)
00139 {
00140   double dtemp = FIST_MAGIC_QINT + inval;
00141   // Note that on both low-endian (x86) and big-endian (m68k) we have
00142   // to shift by two bytes. So no need for an #ifdef.
00143   long result = CS_LONG_AT_BYTE (dtemp, 2);
00144   return result < 0 ? (result >> 1) + 1 : result;
00145 }
00146 #endif
00147 
00152 #define FIST_MAGIC_QROUND (((65536.0 * 65536.0 * 16.0) + (65536.0 * 0.5)) * 65536.0)
00153 
00155 static inline long QRound (double inval)
00156 {
00157   double dtemp = FIST_MAGIC_QROUND + inval;
00158   return CS_LONG_AT_BYTE (dtemp, CS_LOWER_WORD_BYTE) - 0x80000000;
00159 }
00160 
00165 #define FIST_MAGIC_QINT8 (((65536.0 * 16.0) + 0.5) * 65536.0 * 256.0)
00166 
00168 inline long QInt8 (float inval)
00169 {
00170   double dtemp = FIST_MAGIC_QINT8 + inval;
00171   return CS_LONG_AT_BYTE (dtemp, CS_LOWER_WORD_BYTE) - 0x80000000;
00172 }
00173 
00178 #define FIST_MAGIC_QINT16 (((65536.0 * 16.0) + 0.5) * 65536.0)
00179 
00181 inline long QInt16 (float inval)
00182 {
00183   double dtemp = FIST_MAGIC_QINT16 + inval;
00184   return CS_LONG_AT_BYTE (dtemp, CS_LOWER_WORD_BYTE) - 0x80000000;
00185 }
00186 
00191 #define FIST_MAGIC_QINT24 (((65536.0 * 16.0) + 0.5) * 256.0)
00192 
00193 inline long QInt24 (float inval)
00194 {
00195   double dtemp = FIST_MAGIC_QINT24 + inval;
00196   return CS_LONG_AT_BYTE (dtemp, CS_LOWER_WORD_BYTE) - 0x80000000;
00197 }
00198     
00199 #else /* not CS_IEEE_DOUBLE_FORMAT */
00200 
00201 #define QRound(x) (int ((x) + ((x < 0) ? -0.5 : +0.5)))
00202 #define QInt(x)   (int (x))
00203 #define QInt8(x)  (int ((x)*256.))
00204 #define QInt16(x) (int ((x)*65536.))
00205 #define QInt24(x) (int ((x)*16777216.))
00206 
00207 #endif /* CS_IEEE_DOUBLE_FORMAT */
00208 
00209 #endif // __QINT_H__

Generated for Crystal Space by doxygen 1.2.5 written by Dimitri van Heesch, ©1997-2000