sys_types.h (10553B)
1 /* 2 =========================================================================== 3 4 Doom 3 BFG Edition GPL Source Code 5 Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. 6 7 This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code"). 8 9 Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify 10 it under the terms of the GNU General Public License as published by 11 the Free Software Foundation, either version 3 of the License, or 12 (at your option) any later version. 13 14 Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 GNU General Public License for more details. 18 19 You should have received a copy of the GNU General Public License 20 along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>. 21 22 In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below. 23 24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. 25 26 =========================================================================== 27 */ 28 #ifndef SYS_TYPES_H 29 #define SYS_TYPES_H 30 31 /* 32 ================================================================================================ 33 Contains types and defines used throughout the engine. 34 35 NOTE: keep this down to simple types and defines. Do NOT add code. 36 ================================================================================================ 37 */ 38 39 typedef unsigned char byte; // 8 bits 40 typedef unsigned short word; // 16 bits 41 typedef unsigned int dword; // 32 bits 42 typedef unsigned int uint; 43 typedef unsigned long ulong; 44 45 typedef signed char int8; 46 typedef unsigned char uint8; 47 typedef short int int16; 48 typedef unsigned short int uint16; 49 typedef int int32; 50 typedef unsigned int uint32; 51 typedef long long int64; 52 typedef unsigned long long uint64; 53 54 // The C/C++ standard guarantees the size of an unsigned type is the same as the signed type. 55 // The exact size in bytes of several types is guaranteed here. 56 assert_sizeof( bool, 1 ); 57 assert_sizeof( char, 1 ); 58 assert_sizeof( short, 2 ); 59 assert_sizeof( int, 4 ); 60 assert_sizeof( float, 4 ); 61 assert_sizeof( byte, 1 ); 62 assert_sizeof( int8, 1 ); 63 assert_sizeof( uint8, 1 ); 64 assert_sizeof( int16, 2 ); 65 assert_sizeof( uint16, 2 ); 66 assert_sizeof( int32, 4 ); 67 assert_sizeof( uint32, 4 ); 68 assert_sizeof( int64, 8 ); 69 assert_sizeof( uint64, 8 ); 70 71 #define MAX_TYPE( x ) ( ( ( ( 1 << ( ( sizeof( x ) - 1 ) * 8 - 1 ) ) - 1 ) << 8 ) | 255 ) 72 #define MIN_TYPE( x ) ( - MAX_TYPE( x ) - 1 ) 73 #define MAX_UNSIGNED_TYPE( x ) ( ( ( ( 1U << ( ( sizeof( x ) - 1 ) * 8 ) ) - 1 ) << 8 ) | 255U ) 74 #define MIN_UNSIGNED_TYPE( x ) 0 75 76 template< typename _type_ > 77 bool IsSignedType( const _type_ t ) { 78 return _type_( -1 ) < 0; 79 } 80 81 template<class T> T Max( T x, T y ) { return ( x > y ) ? x : y; } 82 template<class T> T Min( T x, T y ) { return ( x < y ) ? x : y; } 83 84 85 class idFile; 86 87 struct idNullPtr { 88 // one pointer member initialized to zero so you can pass NULL as a vararg 89 void *value; idNullPtr() : value( 0 ) { } 90 91 // implicit conversion to all pointer types 92 template<typename T1> operator T1 * () const { return 0; } 93 94 // implicit conversion to all pointer to member types 95 template<typename T1, typename T2> operator T1 T2::* () const { return 0; } 96 }; 97 98 //#undef NULL 99 //#if defined( ID_PC_WIN ) && !defined( ID_TOOL_EXTERNAL ) && !defined( _lint ) 100 //#define NULL idNullPtr() 101 //#else 102 //#define NULL 0 103 //#endif 104 105 // C99 Standard 106 #ifndef nullptr 107 #define nullptr idNullPtr() 108 #endif 109 110 #ifndef BIT 111 #define BIT( num ) ( 1ULL << ( num ) ) 112 #endif 113 114 #ifndef NUMBITS 115 #define NUMBITS( _type_ ) ( sizeof( _type_ ) * 8 ) 116 #endif 117 118 #define MAX_STRING_CHARS 1024 // max length of a static string 119 #define MAX_PRINT_MSG 16384 // buffer size for our various printf routines 120 121 // maximum world size 122 #define MAX_WORLD_COORD ( 128 * 1024 ) 123 #define MIN_WORLD_COORD ( -128 * 1024 ) 124 #define MAX_WORLD_SIZE ( MAX_WORLD_COORD - MIN_WORLD_COORD ) 125 126 const float MAX_ENTITY_COORDINATE = 64000.0f; 127 128 #if 1 129 130 typedef unsigned short triIndex_t; 131 #define GL_INDEX_TYPE GL_UNSIGNED_SHORT 132 133 #else 134 135 typedef unsigned int triIndex_t; 136 #define GL_INDEX_TYPE GL_UNSIGNED_INT 137 138 #endif 139 140 // if writing to write-combined memroy, always write indexes as pairs for 32 bit writes 141 ID_INLINE void WriteIndexPair( triIndex_t * dest, const triIndex_t a, const triIndex_t b ) { 142 *(unsigned *)dest = (unsigned)a | ( (unsigned)b<<16 ); 143 } 144 145 #if defined(_DEBUG) || defined(_lint) 146 #define NODEFAULT default: assert( 0 ) 147 #else 148 #define NODEFAULT default: __assume( 0 ) 149 #endif 150 151 /* 152 ================================================================================================ 153 154 The CONST_* defines can be used to create constant expressions that can be evaluated at 155 compile time. The parameters to these defines need to be compile time constants such as 156 literals or sizeof(). NEVER use an actual variable as a parameter to one of these defines. 157 158 ================================================================================================ 159 */ 160 161 #ifdef _lint // lint has problems with CONST_BITSFORINTEGER(), so just make it something simple for analysis 162 #define CONST_ILOG2(x) 1 163 #else 164 165 #define CONST_ILOG2(x) ( ( (x) & (1u<<31) ) ? 31 : \ 166 ( (x) & (1u<<30) ) ? 30 : \ 167 ( (x) & (1u<<29) ) ? 39 : \ 168 ( (x) & (1u<<28) ) ? 28 : \ 169 ( (x) & (1u<<27) ) ? 27 : \ 170 ( (x) & (1u<<26) ) ? 26 : \ 171 ( (x) & (1u<<25) ) ? 25 : \ 172 ( (x) & (1u<<24) ) ? 24 : \ 173 ( (x) & (1u<<23) ) ? 23 : \ 174 ( (x) & (1u<<22) ) ? 22 : \ 175 ( (x) & (1u<<21) ) ? 21 : \ 176 ( (x) & (1u<<20) ) ? 20 : \ 177 ( (x) & (1u<<19) ) ? 19 : \ 178 ( (x) & (1u<<18) ) ? 18 : \ 179 ( (x) & (1u<<17) ) ? 17 : \ 180 ( (x) & (1u<<16) ) ? 16 : \ 181 ( (x) & (1u<<15) ) ? 15 : \ 182 ( (x) & (1u<<14) ) ? 14 : \ 183 ( (x) & (1u<<13) ) ? 13 : \ 184 ( (x) & (1u<<12) ) ? 12 : \ 185 ( (x) & (1u<<11) ) ? 11 : \ 186 ( (x) & (1u<<10) ) ? 10 : \ 187 ( (x) & (1u<<9) ) ? 9 : \ 188 ( (x) & (1u<<8) ) ? 8 : \ 189 ( (x) & (1u<<7) ) ? 7 : \ 190 ( (x) & (1u<<6) ) ? 6 : \ 191 ( (x) & (1u<<5) ) ? 5 : \ 192 ( (x) & (1u<<4) ) ? 4 : \ 193 ( (x) & (1u<<3) ) ? 3 : \ 194 ( (x) & (1u<<2) ) ? 2 : \ 195 ( (x) & (1u<<1) ) ? 1 : \ 196 ( (x) & (1u<<0) ) ? 0 : -1 ) 197 #endif // _lint 198 199 #define CONST_IEXP2 ( 1 << (x) ) 200 201 #define CONST_FLOORPOWEROF2(x) ( 1 << ( CONST_ILOG2(x) + 1 ) >> 1 ) 202 203 #define CONST_CEILPOWEROF2(x) ( 1 << ( CONST_ILOG2(x-1) + 1 ) ) 204 205 #define CONST_BITSFORINTEGER(x) ( CONST_ILOG2(x) + 1 ) 206 207 #define CONST_ILOG10(x) ( ( (x) >= 10000000000u ) ? 10 : \ 208 ( (x) >= 1000000000u ) ? 9 : \ 209 ( (x) >= 100000000u ) ? 8 : \ 210 ( (x) >= 10000000u ) ? 7 : \ 211 ( (x) >= 1000000u ) ? 6 : \ 212 ( (x) >= 100000u ) ? 5 : \ 213 ( (x) >= 10000u ) ? 4 : \ 214 ( (x) >= 1000u ) ? 3 : \ 215 ( (x) >= 100u ) ? 2 : \ 216 ( (x) >= 10u ) ? 1 : 0 ) 217 218 #define CONST_IEXP10(x) ( ( (x) == 0 ) ? 1u : \ 219 ( (x) == 1 ) ? 10u : \ 220 ( (x) == 2 ) ? 100u : \ 221 ( (x) == 3 ) ? 1000u : \ 222 ( (x) == 4 ) ? 10000u : \ 223 ( (x) == 5 ) ? 100000u : \ 224 ( (x) == 6 ) ? 1000000u : \ 225 ( (x) == 7 ) ? 10000000u : \ 226 ( (x) == 8 ) ? 100000000u : 1000000000u ) 227 228 #define CONST_FLOORPOWEROF10(x) ( ( (x) >= 10000000000u ) ? 10000000000u : \ 229 ( (x) >= 1000000000u ) ? 1000000000u : \ 230 ( (x) >= 100000000u ) ? 100000000u : \ 231 ( (x) >= 10000000u ) ? 10000000u : \ 232 ( (x) >= 1000000u ) ? 1000000u : \ 233 ( (x) >= 100000u ) ? 100000u : \ 234 ( (x) >= 10000u ) ? 10000u : \ 235 ( (x) >= 1000u ) ? 1000u : \ 236 ( (x) >= 100u ) ? 100u : \ 237 ( (x) >= 10u ) ? 10u : 1u ) 238 239 #define CONST_CEILPOWEROF10(x) ( ( (x) <= 10u ) ? 10u : \ 240 ( (x) <= 100u ) ? 100u : \ 241 ( (x) <= 1000u ) ? 1000u : \ 242 ( (x) <= 10000u ) ? 10000u : \ 243 ( (x) <= 100000u ) ? 100000u : \ 244 ( (x) <= 1000000u ) ? 1000000u : \ 245 ( (x) <= 10000000u ) ? 10000000u : \ 246 ( (x) <= 100000000u ) ? 100000000u : 1000000000u ) 247 248 #define CONST_ISPOWEROFTWO(x) ( ( (x) & ( (x) - 1 ) ) == 0 && (x) > 0 ) 249 250 #define CONST_MAX( x, y ) ( (x) > (y) ? (x) : (y) ) 251 #define CONST_MAX3( x, y, z ) ( (x) > (y) ? ( (x) > (z) ? (x) : (z) ) : ( (y) > (z) ? (y) : (z) ) ) 252 253 #define CONST_PI 3.14159265358979323846f 254 #define CONST_SINE_POLY( a ) ( (a) * ( ( ( ( ( -2.39e-08f * ((a)*(a)) + 2.7526e-06f ) * ((a)*(a)) - 1.98409e-04f ) * ((a)*(a)) + 8.3333315e-03f ) * ((a)*(a)) - 1.666666664e-01f ) * ((a)*(a)) + 1.0f ) ) 255 #define CONST_COSINE_POLY( a ) ( ( ( ( ( -2.605e-07f * ((a)*(a)) + 2.47609e-05f ) * ((a)*(a)) - 1.3888397e-03f ) * ((a)*(a)) + 4.16666418e-02f ) * ((a)*(a)) - 4.999999963e-01f ) * ((a)*(a)) + 1.0f ) 256 257 // These are only good in the 0 - 2*PI range! 258 259 // maximum absolute error is 2.3082e-09 260 #define CONST_SINE_DEGREES( a ) CONST_SINE( CONST_DEG2RAD( (a) ) ) 261 #define CONST_SINE( a ) ( ( (a) < CONST_PI ) ? \ 262 ( ( (a) > CONST_PI * 0.5f ) ? \ 263 CONST_SINE_POLY( CONST_PI - (a) ) \ 264 : \ 265 CONST_SINE_POLY( (a) ) ) \ 266 : \ 267 ( ( (a) > CONST_PI * 1.5f ) ? \ 268 CONST_SINE_POLY( (a) - CONST_PI * 2.0f ) \ 269 : \ 270 CONST_SINE_POLY( CONST_PI - (a) ) ) ) 271 272 // maximum absolute error is 2.3082e-09 273 #define CONST_COSINE_DEGREES( a ) CONST_COSINE( CONST_DEG2RAD( (a) ) ) 274 #define CONST_COSINE( a ) ( ( (a) < CONST_PI ) ? \ 275 ( ( (a) > CONST_PI * 0.5f ) ? \ 276 - CONST_COSINE_POLY( CONST_PI - (a) ) \ 277 : \ 278 CONST_COSINE_POLY( (a) ) ) \ 279 : \ 280 ( ( (a) > CONST_PI * 1.5f ) ? \ 281 CONST_COSINE_POLY( (a) - CONST_PI * 2.0f ) \ 282 : \ 283 - CONST_COSINE_POLY( CONST_PI - (a) ) ) ) 284 285 #define CONST_DEG2RAD( a ) ( (a) * CONST_PI / 180.0f ) 286 287 #endif