xoreos  0.0.5
maths.h
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #ifndef COMMON_MATHS_H
26 #define COMMON_MATHS_H
27 
28 #include <cmath>
29 #include <cfloat>
30 
31 #include "src/common/system.h"
32 #include "src/common/types.h"
33 
34 #ifndef M_SQRT1_2
35  #define M_SQRT1_2 0.70710678118654752440 /* 1/sqrt(2) */
36 #endif
37 
38 #ifndef M_PI
39  #define M_PI 3.14159265358979323846
40 #endif
41 
42 #ifndef FLT_MIN
43  #define FLT_MIN 1E-37
44 #endif
45 
46 #ifndef FLT_MAX
47  #define FLT_MAX 1E+37
48 #endif
49 
50 #ifndef INT32_MIN
51  #define INT32_MIN ((int32) 0x80000000)
52 #endif
53 
54 #ifndef INT32_MAX
55  #define INT32_MAX ((int32) 0x7FFFFFFF)
56 #endif
57 
58 namespace Common {
59 
61 struct Complex {
62  float re, im;
63 };
64 
65 
66 #if defined(__GNUC__) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))
67 static inline int intLog2(uint32 v) {
68  // This is a slightly optimized implementation of log2 for natural numbers
69  // targeting gcc. It also saves some binary size over our fallback
70  // implementation, since it does not need any table.
71  if (v == 0)
72  return -1;
73  else
74  // This is really "sizeof(unsigned int) * CHAR_BIT - 1" but using 8
75  // instead of CHAR_BIT is sane enough and it saves us from including
76  // limits.h
77  return (sizeof(unsigned int) * 8 - 1) - __builtin_clz(v);
78 }
79 #else
80 // See http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogLookup
81 
82 extern const int8 intLog2Table256[256];
83 static inline int intLog2(uint32 v) {
84  register uint32 t, tt;
85 
86  if ((tt = v >> 16))
87  return (t = tt >> 8) ? 24 + intLog2Table256[t] : 16 + intLog2Table256[tt];
88  else
89  return (t = v >> 8) ? 8 + intLog2Table256[t] : intLog2Table256[v];
90 }
91 #endif
92 
93 static inline float rad2deg(float rad) {
94  return rad * 180.0f / M_PI;
95 }
96 
97 static inline float deg2rad(float deg) {
98  return deg * M_PI / 180.0f;
99 }
100 
101 } // End of namespace Common
102 
103 #endif // COMMON_MATHS_H
A complex number.
Definition: maths.h:61
static int intLog2(uint32 v)
Definition: maths.h:83
Definition: 2dafile.h:39
float im
Definition: maths.h:62
#define M_PI
Definition: maths.h:39
float re
Definition: maths.h:62
const int8 intLog2Table256[256]
Definition: maths.cpp:31
Low-level type definitions to handle fixed width types portably.
int8_t int8
Definition: types.h:199
uint32_t uint32
Definition: types.h:204
Low-level detection of architecture/system properties.
static float rad2deg(float rad)
Definition: maths.h:93
static float deg2rad(float deg)
Definition: maths.h:97