le3d - LightEngine 3D
A straightforward C++ 3D software engine for real-time graphics
global.h
Go to the documentation of this file.
1 
33 #ifndef LE_GLOBAL_H
34 #define LE_GLOBAL_H
35 
36 /*****************************************************************************/
38  #include <stdint.h>
39  #include <stdlib.h>
40  #include <stddef.h>
41  typedef unsigned long long LeHandle;
44  #define cmmax(a,b) ((a)>(b)?(a):(b))
45  #define cmmin(a,b) ((a)<(b)?(a):(b))
46  #define cmabs(a) ((a)<0.0f?(-(a)):(a))
47  #define cmbound(v, vmin, vmax) ((v)>(vmax)?(vmax):((v)<(vmin)?(vmin):(v)))
48  #define cmsgn(a) ((a)<0.0f?-1.0f:1.0f)
49  #define cmthr(a, t) ((a)<(-t)?-1.0f:((a)>(t)?1.0f:0.0f))
50  #define cmmod(a, m) (((a) % (m) + (m)) % (m))
51  #define cmmodf(a, m) (fmodf((fmodf((a), (m)) + (m)), (m)))
53  #define randf() (((float) rand() / (float) (RAND_MAX >> 1)) - 1.0f)
54  #define d2r (const float) (M_PI / 180.0f)
55  #define r2d (const float) (180.0f / M_PI)
57 /*****************************************************************************/
58 
59  namespace LeGlobal {
60  void toLower(char * txt);
61  void toUpper(char * txt);
63  void getFileExtention(char * ext, const int extSize, const char * path);
64  void getFileName(char * name, const int nameSize, const char * path);
65  void getFileDirectory(char * dir, int dirSize, const char * path);
67  int log2i32(int n);
68  };
69 
70 /*****************************************************************************/
72  #ifdef __MINGW32__
73  #include <string.h>
74  #include <malloc.h>
75  #define _strdup strdup
76  #elif defined(__GNUC__)
77  #include <string.h>
78  #define _strdup strdup
79  #if defined(__APPLE__)
80  extern "C" void * _aligned_malloc(size_t size, size_t alignment);
81  #else
82  #ifndef _aligned_malloc
83  #define _aligned_malloc(s, a) aligned_alloc(a, s)
84  #endif
85  #endif
86  #ifndef _aligned_free
87  #define _aligned_free free
88  #endif
89  #elif defined(_MSC_VER)
90  #include <malloc.h>
91  #ifndef alloca
92  #define alloca(s) _malloca(s)
93  #endif
94  #endif
95 
96 /*****************************************************************************/
98  #if LE_USE_SIMD == 1 && LE_USE_SSE2 == 1
99  void * operator new(size_t size) {
100  return _aligned_malloc(size, 16);
101  }
102  void * operator new[](size_t size) {
103  return _aligned_malloc(size, 16);
104  }
105  void operator delete(void * ptr) {
106  _aligned_free(ptr);
107  }
108  void operator delete[](void * ptr) {
109  _aligned_free(ptr);
110  }
111  #endif
112 
113 /*****************************************************************************/
115 #ifdef _MSC_VER
116  #include <intrin.h>
117  extern "C" int __builtin_ffs(int x);
118 
119 #elif defined (__WATCOMC__)
120  extern "C" int __builtin_ffs(int x);
121 
122 // Watcom C++ is so outdated that it does not
123 // come with floating point version of math.h
124 // functions
125  extern "C" float copysignf(float x, float y);
126 
127  #define sinf(n) ((float)std::sin(n))
128  #define asinf(n) ((float)std::asin(n))
129  #define cosf(n) ((float)std::cos(n))
130  #define acosf(n) ((float)std::acos(n))
131  #define tanf(n) ((float)std::tan(n))
132  #define atanf(n) ((float)std::atan(n))
133  #define floorf(n) ((float)std::floor(n))
134  #define ceilf(n) ((float)std::ceil(n))
135  #define sqrtf(n) ((float)std::sqrt(n))
136  #define fabsf(n) ((float)std::fabs(n))
137  #define atan2f(n,m) ((float)std::atan2(n,m))
138 
139  #define M_PI 3.14159265358979323846
140 
141 #elif defined (AMIGA)
142  extern "C" float copysignf(float x, float y);
143 #endif
144 
145 /*****************************************************************************/
147 #define FROM_LEU16(x) (x = ((uint8_t *) &(x))[0] + (((uint8_t *) &(x))[1] << 8))
148 #define FROM_LES16(x) FROM_LEU16(x)
149 #define FROM_LEU32(x) (x = ((uint8_t *) &x)[0] + (((uint8_t *) &x)[1] << 8) + (((uint8_t *) &x)[2] << 16) + (((uint8_t *) &x)[3] << 24))
150 #define FROM_LES32(x) FROM_LEU32(x)
151 
152 #define TO_LEU16(x) (\
153  ((uint8_t *) &(x))[0] = ((uint16_t) (x)) & 0xFF, \
154  ((uint8_t *) &(x))[1] = ((uint16_t) (x)) >> 8 \
155  )
156 
157 #define TO_LES16(x) (\
158  ((uint8_t *) &(x))[0] = ((int16_t) (x)) & 0xFF, \
159  ((uint8_t *) &(x))[1] = ((int16_t) (x)) >> 8 \
160  )
161 
162 #define TO_LEU32(x) (\
163  ((uint8_t *) &(x))[0] = ((uint32_t) (x)) & 0xFF, \
164  ((uint8_t *) &(x))[1] = ((uint32_t) (x)) >> 8, \
165  ((uint8_t *) &(x))[2] = ((uint32_t) (x)) >> 16, \
166  ((uint8_t *) &(x))[3] = ((uint32_t) (x)) >> 24 \
167  )
168 
169 #define TO_LES32(x) (\
170  ((uint8_t *) &(x))[0] = ((int32_t) (x)) & 0xFF, \
171  ((uint8_t *) &(x))[1] = ((int32_t) (x)) >> 8, \
172  ((uint8_t *) &(x))[2] = ((int32_t) (x)) >> 16, \
173  ((uint8_t *) &(x))[3] = ((int32_t) (x)) >> 24 \
174  )
175 #endif // LE_GLOBAL_H
int log2i32(int n)
Definition: global.cpp:106
void toUpper(char *txt)
Definition: global.cpp:50
void toLower(char *txt)
Definition: global.cpp:40
void getFileExtention(char *ext, const int extSize, const char *path)
Definition: global.cpp:61
unsigned long long LeHandle
Definition: global.h:41
void getFileDirectory(char *dir, int dirSize, const char *path)
Definition: global.cpp:90
Definition: global.h:59
void getFileName(char *name, const int nameSize, const char *path)
Definition: global.cpp:75