xoreos  0.0.5
util.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 /* Based on ScummVM (<http://scummvm.org>) code, which is released
26  * under the terms of version 2 or later of the GNU General Public
27  * License.
28  *
29  * The original copyright note in ScummVM reads as follows:
30  *
31  * ScummVM is the legal property of its developers, whose names
32  * are too numerous to list here. Please refer to the COPYRIGHT
33  * file distributed with this source distribution.
34  *
35  * This program is free software; you can redistribute it and/or
36  * modify it under the terms of the GNU General Public License
37  * as published by the Free Software Foundation; either version 2
38  * of the License, or (at your option) any later version.
39  *
40  * This program is distributed in the hope that it will be useful,
41  * but WITHOUT ANY WARRANTY; without even the implied warranty of
42  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43  * GNU General Public License for more details.
44  *
45  * You should have received a copy of the GNU General Public License
46  * along with this program; if not, write to the Free Software
47  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
48  */
49 
50 #ifndef COMMON_UTIL_H
51 #define COMMON_UTIL_H
52 
53 #include "src/common/endianness.h"
54 
55 #include <cmath>
56 
57 #ifdef MIN
58  #undef MIN
59 #endif
60 
61 #ifdef MAX
62  #undef MAX
63 #endif
64 
65 #ifdef ARRAYSIZE
66  #undef ARRAYSIZE
67 #endif
68 
69 template<typename T> inline T ABS (T x) { return (x>=0) ? x : -x; }
70 template<typename T> inline T MIN (T a, T b) { return (a<b) ? a : b; }
71 template<typename T> inline T MAX (T a, T b) { return (a>b) ? a : b; }
72 template<typename T> inline T CLIP (T v, T amin, T amax)
73  { if (v < amin) return amin; else if (v > amax) return amax; else return v; }
74 
78 template<typename T> inline void SWAP(T &a, T &b) { T tmp = a; a = b; b = tmp; }
79 
81 template<typename T> inline bool ISPOWER2(T x) { return x && !(x & (x - 1)); }
82 
84 static inline uint32 NEXTPOWER2(uint32 x) {
85  if (x == 0)
86  return 1;
87 
88  x--;
89 
90  x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16;
91 
92  return x + 1;
93 }
94 
96 static inline uint64 NEXTPOWER2(uint64 x) {
97  if (x == 0)
98  return 1;
99 
100  x--;
101 
102  x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; x |= x >> 32;
103 
104  return x + 1;
105 }
106 
107 #ifdef HAVE_FABS
108 template<> inline double ABS(double x) { return fabs(x); }
109 #endif
110 #ifdef HAVE_FABSF
111 template<> inline float ABS(float x) { return fabsf(x); }
112 #endif
113 
114 #ifdef HAVE_FMIN
115 template<> inline double MIN(double a, double b) { return fmin(a, b); }
116 #endif
117 #ifdef HAVE_FMINF
118 template<> inline float MIN(float a, float b) { return fminf(a, b); }
119 #endif
120 
121 #ifdef HAVE_FMAX
122 template<> inline double MAX(double a, double b) { return fmax(a, b); }
123 #endif
124 #ifdef HAVE_FMAXF
125 template<> inline float MAX(float a, float b) { return fmaxf(a, b); }
126 #endif
127 
131 #define ARRAYSIZE(x) (sizeof(x) / sizeof(x[0]))
132 
140 void warning(const char *s, ...) GCC_PRINTF(1, 2);
147 void status(const char *s, ...) GCC_PRINTF(1, 2);
154 void info(const char *s, ...) GCC_PRINTF(1, 2);
155 
166 void NORETURN_PRE error(const char *s, ...) GCC_PRINTF(1, 2) NORETURN_POST;
167 
174 float convertIEEEFloat(uint32 data);
181 double convertIEEEDouble(uint64 data);
182 
189 uint32 convertIEEEFloat(float value);
196 uint64 convertIEEEDouble(double value);
197 
206 double readNintendoFixedPoint(uint32 value, bool sign, uint8 iBits, uint8 fBits);
207 
209 float readIEEEFloat16(uint16 value);
210 
211 #endif // COMMON_UTIL_H
T ABS(T x)
Definition: util.h:69
void void status(const char *s,...) GCC_PRINTF(1
Print a status message to both stderr and the global log file (if a global log file has been opened)...
uint64_t uint64
Definition: types.h:206
uint8_t uint8
Definition: types.h:200
Low-level macros and functions to handle different endianness portably.
float convertIEEEFloat(uint32 data)
Convert a uint32 holding the bit pattern of a 32-bit IEEE 754 single precision floating point value i...
Definition: util.cpp:116
void void void void NORETURN_PRE NORETURN_POST
Definition: util.h:166
void warning(const char *s,...) GCC_PRINTF(1
Print a warning message to both stderr and the global log file (if a global log file has been opened)...
static uint32 NEXTPOWER2(uint32 x)
Round up to the next power of 2.
Definition: util.h:84
bool ISPOWER2(T x)
Is this integer value a power of 2?
Definition: util.h:81
uint16_t uint16
Definition: types.h:202
T MIN(T a, T b)
Definition: util.h:70
void void void void NORETURN_PRE error(const char *s,...) GCC_PRINTF(1
Print an error message to both stderr and the global log file (if a global log file has been opened)...
double convertIEEEDouble(uint64 data)
Convert a uint64 holding the bit pattern of a 64-bit IEEE 754 double precision floating point value i...
Definition: util.cpp:143
double readNintendoFixedPoint(uint32 value, bool sign, uint8 iBits, uint8 fBits)
Read a fixed-point value, in a format used by the Nintendo DS.
Definition: util.cpp:159
uint32_t uint32
Definition: types.h:204
#define GCC_PRINTF(x, y)
Definition: system.h:161
#define NORETURN_PRE
Definition: noreturn.h:35
T CLIP(T v, T amin, T amax)
Definition: util.h:72
T MAX(T a, T b)
Definition: util.h:71
float readIEEEFloat16(uint16 value)
Read a half-precision 16-bit IEEE float, converting it into a 32-bit iEEE float.
Definition: util.cpp:198
void SWAP(T &a, T &b)
Template method which swaps the values of its two parameters.
Definition: util.h:78
void void void info(const char *s,...) GCC_PRINTF(1
Print an info message to both stdout and the global log file (if a global log file has been opened)...