types.h

Engine/source/platform/types.h

More...

Classes:

class

"Null" type used by templates

Public Defines

define
__EQUAL_CONST_F() (0.000001)

Constant float epsilon used for F32 comparisons.

define
_Offset_Normal(x, cls) (()(( char *)&(((cls *)1)->x)-( char *)1))
define
_Offset_Variant_1(x, cls) ((int)(&((cls *)1)->x) - 1)
define
_Offset_Variant_2(x, cls) offsetof(cls, x)
define
BIT(x) (1 << (x))
define
DeclareTemplatizedMinMax(type)   inline type getMin(type , type ) { return  >  ?  : ; } \
  inline type getMax(type , type ) { return  >  ?  : ; }
define
define
TORQUE_UNUSED(var) ()sizeof(var)

Public Typedefs

float
F32 

Compiler independent 32-bit float.

double
F64 

Compiler independent 64-bit float.

MEM_ADDRESS 

Integral type matching the host's memory address width.

signed short
S16 

Compiler independent Signed 16-bit short.

signed int
S32 

Compiler independent Signed 32-bit integer.

signed char
S8 

Compiler independent Signed Char.

const char *
StringTableEntry 
unsigned short
U16 

Compiler independent Unsigned 16-bit short.

unsigned int
U32 

Compiler independent Unsigned 32-bit integer.

unsigned char
U8 

Compiler independent Unsigned Char.

unsigned short
UTF16 

Compiler independent 16 bit Unicode encoded character.

unsigned int
UTF32 

Compiler independent 32 bit Unicode encoded character.

char
UTF8 

Compiler independent 8 bit Unicode encoded character.

Public Variables

Constant Max Limit F32.

Constant Min Limit F32.

Constant float 2 / PI.

Constant float 2*PI.

Constant float 0.5.

Constant float 1/2 * PI.

Constant float 0.5 / PI.

Constant float 1 / PI.

Constant float 1.0.

Constant float PI.

Constant float sqrt(2)

Constant float sqrt(0.5)

Constant float 0.0.

Constant Max Limit S16.

Constant Min Limit S16.

Constant Max Limit S32.

Constant Min Limit S32.

Constant Max Limit S8.

Constant Min Limit S8.

Constant Max Limit U16.

Constant Max Limit U32.

Constant Max Limit U8.

Public Functions

getBinLog2(U32 value)

Determines the binary logarithm of the input value rounded down to the nearest power of 2.

Determines the binary logarithm of the next greater power of two of the input number.

Determines the next greater power of two from the value. If the value is a power of two, it is returned.

bool

Determines if number is a power of two.

Detailed Description

Public Defines

__EQUAL_CONST_F() (0.000001)

Constant float epsilon used for F32 comparisons.

_Offset_Normal(x, cls) (()(( char *)&(((cls *)1)->x)-( char *)1))
_Offset_Variant_1(x, cls) ((int)(&((cls *)1)->x) - 1)
_Offset_Variant_2(x, cls) offsetof(cls, x)
BIT(x) (1 << (x))
DeclareTemplatizedMinMax(type)   inline type getMin(type , type ) { return  >  ?  : ; } \
  inline type getMax(type , type ) { return  >  ?  : ; }
STDCALL() 
TORQUE_UNUSED(var) ()sizeof(var)

Public Typedefs

typedef float F32 

Compiler independent 32-bit float.

typedef double F64 

Compiler independent 64-bit float.

typedef U32 MEM_ADDRESS 

Integral type matching the host's memory address width.

typedef signed short S16 

Compiler independent Signed 16-bit short.

typedef signed int S32 

Compiler independent Signed 32-bit integer.

typedef signed char S8 

Compiler independent Signed Char.

typedef const char * StringTableEntry 
typedef unsigned short U16 

Compiler independent Unsigned 16-bit short.

typedef unsigned int U32 

Compiler independent Unsigned 32-bit integer.

typedef unsigned char U8 

Compiler independent Unsigned Char.

typedef unsigned short UTF16 

Compiler independent 16 bit Unicode encoded character.

typedef unsigned int UTF32 

Compiler independent 32 bit Unicode encoded character.

typedef char UTF8 

Compiler independent 8 bit Unicode encoded character.

Public Variables

const F32 F32_MAX 

Constant Max Limit F32.

const F32 F32_MIN 

Constant Min Limit F32.

const F32 Float_2InversePi 

Constant float 2 / PI.

const F32 Float_2Pi 

Constant float 2*PI.

const F32 Float_Half 

Constant float 0.5.

const F32 Float_HalfPi 

Constant float 1/2 * PI.

const F32 Float_Inf 
const F32 Float_Inverse2Pi 

Constant float 0.5 / PI.

const F32 Float_InversePi 

Constant float 1 / PI.

const F32 Float_One 

Constant float 1.0.

const F32 Float_Pi 

Constant float PI.

const F32 Float_Sqrt2 

Constant float sqrt(2)

const F32 Float_SqrtHalf 

Constant float sqrt(0.5)

const F32 Float_Zero 

Constant float 0.0.

const S16 S16_MAX 

Constant Max Limit S16.

const S16 S16_MIN 

Constant Min Limit S16.

const S32 S32_MAX 

Constant Max Limit S32.

const S32 S32_MIN 

Constant Min Limit S32.

const S8 S8_MAX 

Constant Max Limit S8.

const S8 S8_MIN 

Constant Min Limit S8.

const U16 U16_MAX 

Constant Max Limit U16.

const U32 U32_MAX 

Constant Max Limit U32.

const U8 U8_MAX 

Constant Max Limit U8.

Public Functions

getBinLog2(U32 value)

Determines the binary logarithm of the input value rounded down to the nearest power of 2.

getNextBinLog2(U32 number)

Determines the binary logarithm of the next greater power of two of the input number.

getNextPow2(U32 value)

Determines the next greater power of two from the value. If the value is a power of two, it is returned.

isPow2(const U32 num)

Determines if number is a power of two.

Zero is not a power of two. So we want take into account that edge case

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#ifndef _TORQUE_TYPES_H_
 25#define _TORQUE_TYPES_H_
 26
 27#if (defined _MSC_VER) && (_MSC_VER <= 1500)
 28#include "platformWin32/stdint.h"
 29#else
 30#include <stdint.h>
 31#endif
 32
 33//////////////////////////////////////////////////////////////////////////////////////////////////////////
 34//-----------------------------------------Basic Types--------------------------------------------------//
 35//////////////////////////////////////////////////////////////////////////////////////////////////////////
 36
 37typedef signed char        S8;      ///< Compiler independent Signed Char
 38typedef unsigned char      U8;      ///< Compiler independent Unsigned Char
 39
 40typedef signed short       S16;     ///< Compiler independent Signed 16-bit short
 41typedef unsigned short     U16;     ///< Compiler independent Unsigned 16-bit short
 42
 43typedef signed int         S32;     ///< Compiler independent Signed 32-bit integer
 44typedef unsigned int       U32;     ///< Compiler independent Unsigned 32-bit integer
 45
 46typedef float              F32;     ///< Compiler independent 32-bit float
 47typedef double             F64;     ///< Compiler independent 64-bit float
 48
 49struct EmptyType {};                ///< "Null" type used by templates
 50
 51#define TORQUE_UNUSED(var) (void)sizeof(var)
 52
 53//////////////////////////////////////////////////////////////////////////////////////////////////////////
 54//----------------------------------------String Types--------------------------------------------------//
 55//////////////////////////////////////////////////////////////////////////////////////////////////////////
 56
 57typedef char           UTF8;        ///< Compiler independent 8  bit Unicode encoded character
 58
 59#if defined(_MSC_VER) && defined(__clang__)
 60// Clang's MSVC compatibility mode doesn't currently support /Zc:wchar_t-,
 61// which we rely on to avoid type conversion errors when calling system
 62// APIs when UTF16 is defined as unsigned short.  So, just define UTF16
 63// as wchar_t instead since it's always a 2 byte unsigned on windows anyway.
 64typedef wchar_t        UTF16;
 65#else
 66typedef unsigned short UTF16;       ///< Compiler independent 16 bit Unicode encoded character
 67#endif
 68
 69typedef unsigned int   UTF32;       ///< Compiler independent 32 bit Unicode encoded character
 70
 71typedef const char* StringTableEntry;
 72
 73//////////////////////////////////////////////////////////////////////////////////////////////////////////
 74//-------------------------------------- Type constants-------------------------------------------------//
 75//////////////////////////////////////////////////////////////////////////////////////////////////////////
 76#define __EQUAL_CONST_F F32(0.000001)                                  ///< Constant float epsilon used for F32 comparisons
 77
 78extern const F32 Float_Inf;
 79static const F32 Float_One  = F32(1.0);                           ///< Constant float 1.0
 80static const F32 Float_Half = F32(0.5);                           ///< Constant float 0.5
 81static const F32 Float_Zero = F32(0.0);                           ///< Constant float 0.0
 82static const F32 Float_Pi   = F32(3.14159265358979323846);        ///< Constant float PI
 83static const F32 Float_2Pi  = F32(2.0 * 3.14159265358979323846);  ///< Constant float 2*PI
 84static const F32 Float_InversePi = F32(1.0 / 3.14159265358979323846); ///< Constant float 1 / PI
 85static const F32 Float_HalfPi = F32(0.5 * 3.14159265358979323846);    ///< Constant float 1/2 * PI
 86static const F32 Float_2InversePi = F32(2.0 / 3.14159265358979323846);///< Constant float 2 / PI
 87static const F32 Float_Inverse2Pi = F32(0.5 / 3.14159265358979323846);///< Constant float 0.5 / PI
 88
 89static const F32 Float_Sqrt2 = F32(1.41421356237309504880f);          ///< Constant float sqrt(2)
 90static const F32 Float_SqrtHalf = F32(0.7071067811865475244008443f);  ///< Constant float sqrt(0.5)
 91
 92static const S8  S8_MIN  = S8(-128);                              ///< Constant Min Limit S8
 93static const S8  S8_MAX  = S8(127);                               ///< Constant Max Limit S8
 94static const U8  U8_MAX  = U8(255);                               ///< Constant Max Limit U8
 95
 96static const S16 S16_MIN = S16(-32768);                           ///< Constant Min Limit S16
 97static const S16 S16_MAX = S16(32767);                            ///< Constant Max Limit S16
 98static const U16 U16_MAX = U16(65535);                            ///< Constant Max Limit U16
 99
100static const S32 S32_MIN = S32(-2147483647 - 1);                  ///< Constant Min Limit S32
101static const S32 S32_MAX = S32(2147483647);                       ///< Constant Max Limit S32
102static const U32 U32_MAX = U32(0xffffffff);                       ///< Constant Max Limit U32
103
104static const F32 F32_MIN = F32(1.175494351e-38F);                 ///< Constant Min Limit F32
105static const F32 F32_MAX = F32(3.402823466e+38F);                 ///< Constant Max Limit F32
106
107// define all the variants of Offset that we might use
108#define _Offset_Normal(x, cls) ((dsize_t)((const char *)&(((cls *)1)->x)-(const char *)1))
109#define _Offset_Variant_1(x, cls) ((int)(&((cls *)1)->x) - 1)
110#define _Offset_Variant_2(x, cls) offsetof(cls, x) // also requires #include <stddef.h>
111
112//--------------------------------------
113// Identify the compiler being used
114
115// PC-lint
116#if defined(_lint)
117#  include "platform/types.lint.h"
118// Metrowerks CodeWarrior
119#elif defined(__MWERKS__)
120#  include "platform/types.codewarrior.h"
121// Microsoft Visual C++/Visual.NET
122#elif defined(_MSC_VER)
123#  include "platform/types.visualc.h"
124// GNU GCC
125#elif defined(__GNUC__)
126#  include "platform/types.gcc.h"
127#else
128#  error "Unknown Compiler"
129#endif
130
131/// Integral type matching the host's memory address width.
132#ifdef TORQUE_CPU_X64
133   typedef U64 MEM_ADDRESS;
134#else
135   typedef U32 MEM_ADDRESS;
136#endif
137
138//////////////////////////////////////////////////////////////////////////////////////////////////////////
139//---------------------------------------- GeneralMath Helpers ---------------------------------------- //
140//////////////////////////////////////////////////////////////////////////////////////////////////////////
141
142/// Determines if number is a power of two.
143/// Zero is not a power of two. So we want take into account that edge case
144inline bool isPow2(const U32 num)
145{
146   return (num != 0) && ((num & (num - 1)) == 0);
147}
148
149/// Determines the binary logarithm of the input value rounded down to the nearest power of 2.
150inline U32 getBinLog2(U32 value)
151{
152   F32 floatValue = F32(value);
153   return (*((U32 *) &floatValue) >> 23) - 127;
154}
155
156/// Determines the binary logarithm of the next greater power of two of the input number.
157inline U32 getNextBinLog2(U32 number)
158{
159   return getBinLog2(number) + (isPow2(number) ? 0 : 1);
160}
161
162/// Determines the next greater power of two from the value. If the value is a power of two, it is returned.
163inline U32 getNextPow2(U32 value)
164{
165   return isPow2(value) ? value : (1 << (getBinLog2(value) + 1));
166}
167
168//////////////////////////////////////////////////////////////////////////////////////////////////////////
169//------------------------------------Many versions of min and max--------------------------------------//
170//////////////////////////////////////////////////////////////////////////////////////////////////////////
171
172#define DeclareTemplatizedMinMax(type) \
173  inline type getMin(type a, type b) { return a > b ? b : a; } \
174  inline type getMax(type a, type b) { return a > b ? a : b; }
175
176DeclareTemplatizedMinMax( U32 )
177DeclareTemplatizedMinMax( S32 )
178DeclareTemplatizedMinMax( U16 )
179DeclareTemplatizedMinMax( S16 )
180DeclareTemplatizedMinMax( U8 )
181DeclareTemplatizedMinMax( S8 )
182DeclareTemplatizedMinMax( F32 )
183DeclareTemplatizedMinMax( F64 )
184
185//////////////////////////////////////////////////////////////////////////////////////////////////////////
186//------------------------------------------------FOURCC------------------------------------------------//
187//////////////////////////////////////////////////////////////////////////////////////////////////////////
188
189#if defined(TORQUE_BIG_ENDIAN)
190#define makeFourCCTag(c0,c1,c2,c3) ((U32) ((((U32)((U8)(c0)))<<24) + (((U32)((U8)(c1)))<<16) + (((U32)((U8)(c2)))<<8) + ((((U32)((U8)(c3))))))
191#else
192#ifdef TORQUE_LITTLE_ENDIAN
193#define makeFourCCTag(c3,c2,c1,c0) ((U32) ((((U32)((U8)(c0)))<<24) + (((U32)((U8)(c1)))<<16) + (((U32)((U8)(c2)))<<8) + (((U32)((U8)(c3))))))
194#else
195#error BYTE_ORDER not defined
196#endif
197#endif
198
199#define BIT(x) (1 << (x))                       ///< Returns value with bit x set (2^x)
200
201#if defined(TORQUE_OS_WIN)
202#define STDCALL __stdcall
203#else
204#define STDCALL
205#endif
206
207#endif //_TORQUE_TYPES_H_
208