CrystalSpace

Public API Reference

Main Page   Modules   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

qint.h

Go to the documentation of this file.
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 __CS_QINT_H__
00020 #define __CS_QINT_H__
00021 
00022 #if defined (CS_IEEE_DOUBLE_FORMAT)
00023 
00110 #ifdef CS_BIG_ENDIAN
00111 #  define CS_LOWER_WORD_BYTE    4
00112 #else
00113 #  define CS_LOWER_WORD_BYTE    0
00114 #endif
00115 
00116 #define CS_LONG_AT_BYTE(x,b)    *(long *)(((char *)&x) + b)
00117 
00124 #define FIST_MAGIC_QINT   (65536.0 * 65536.0 * 16.0)
00125 
00132 #ifdef CS_QINT_WORKAROUND
00133 static inline long QInt (double inval)
00134 {
00135   union { double dtemp; long result; } x;
00136 
00137   x.dtemp = FIST_MAGIC_QINT + inval;
00138   x.result = CS_LONG_AT_BYTE (x.dtemp, 2);
00139   return x.result < 0 ? (x.result >> 1) + 1 : x.result;
00140 }
00141 #else
00142 static inline long QInt (double inval)
00143 {
00144   double dtemp = FIST_MAGIC_QINT + inval;
00145   // Note that on both low-endian (x86) and big-endian (m68k) we have
00146   // to shift by two bytes. So no need for an #ifdef.
00147   long result = CS_LONG_AT_BYTE (dtemp, 2);
00148   return result < 0 ? (result >> 1) + 1 : result;
00149 }
00150 #endif
00151 
00156 #define FIST_MAGIC_QROUND (((65536.0 * 65536.0 * 16.0) + (65536.0 * 0.5)) * 65536.0)
00157 
00161 static inline long QRound (double inval)
00162 {
00163   double dtemp = FIST_MAGIC_QROUND + inval;
00164   return CS_LONG_AT_BYTE (dtemp, CS_LOWER_WORD_BYTE) - 0x80000000;
00165 }
00166 
00171 #define FIST_MAGIC_QINT8 (((65536.0 * 16.0) + 0.5) * 65536.0 * 256.0)
00172 
00174 inline long QInt8 (float inval)
00175 {
00176   double dtemp = FIST_MAGIC_QINT8 + inval;
00177   return CS_LONG_AT_BYTE (dtemp, CS_LOWER_WORD_BYTE) - 0x80000000;
00178 }
00179 
00184 #define FIST_MAGIC_QINT16 (((65536.0 * 16.0) + 0.5) * 65536.0)
00185 
00187 inline long QInt16 (float inval)
00188 {
00189   double dtemp = FIST_MAGIC_QINT16 + inval;
00190   return CS_LONG_AT_BYTE (dtemp, CS_LOWER_WORD_BYTE) - 0x80000000;
00191 }
00192 
00197 #define FIST_MAGIC_QINT24 (((65536.0 * 16.0) + 0.5) * 256.0)
00198 
00200 inline long QInt24 (float inval)
00201 {
00202   double dtemp = FIST_MAGIC_QINT24 + inval;
00203   return CS_LONG_AT_BYTE (dtemp, CS_LOWER_WORD_BYTE) - 0x80000000;
00204 }
00205 
00208 #else /* not CS_IEEE_DOUBLE_FORMAT */
00209 
00210 #define QRound(x) (int ((x) + ((x < 0) ? -0.5 : +0.5)))
00211 #define QInt(x)   (int (x))
00212 #define QInt8(x)  (int ((x)*256.))
00213 #define QInt16(x) (int ((x)*65536.))
00214 #define QInt24(x) (int ((x)*16777216.))
00215 
00216 #endif /* CS_IEEE_DOUBLE_FORMAT */
00217 
00218 #endif // __CS_QINT_H__

Generated for Crystal Space by doxygen 1.2.14