-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc.h
181 lines (145 loc) · 5.22 KB
/
misc.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// -------------------------------------------------------------------------
// File: misc.h
// Desc: miscellaneous useful stuff
//
// Author: Tomas Akenine-Möller
// History: March, 2000 (started)
// August, 2001 (removed "float" stuff)
// -------------------------------------------------------------------------
#ifndef MISC_H
#define MISC_H
#include <assert.h>
// makes the asserts stand out more
#define ASSERT(a) assert(a)
typedef unsigned char uint8;
typedef unsigned short uint16;
typedef unsigned int uint32;
typedef char int8;
typedef short int16;
typedef int int32;
typedef _int64 int64;
typedef char sint8;
typedef short sint16;
typedef int sint32;
const float fEPSILON=1.0e-5f;
typedef float ZBufferType;
inline int ceilInt(int a, int val) {
return (a+val-1) - (a+val-1) % val;
}
inline void fSwap(float &a, float &b)
{
float tmp;
tmp=a;
a=b;
b=tmp;
}
inline void dSwap(double &a, double &b)
{
double tmp;
tmp=a;
a=b;
b=tmp;
}
inline uint8 uClamp(int value, uint8 low,uint8 high)
{
if(value<low) return low;
if(value>high) return high;
return uint8(value);
}
inline int iClamp(int value, int low,int high)
{
if(value<low) return low;
if(value>high) return high;
return value;
}
inline float fClamp(float value, float low, float high)
{
if(value<low) return low;
if(value>high) return high;
return value;
}
inline bool isPowerOfTwo(uint32 number) { return (number&(number-1))==0;}
typedef unsigned char ubyte;
inline ubyte cropToByte(float color)
{
if(color<0) return 0;
else if(color>1.0) return 255;
else return (ubyte)(color*255);
}
// convert from degrees to radians constant
#ifndef TORAD
#define TORAD 0.017453293
#endif
#ifndef MAX2
#define MAX2(a,b) ((a)>(b) ? (a) : (b))
#endif
#ifndef MAX3
#define MAX3(a,b,c) MAX2(MAX2(a,b),c)
#endif
#ifndef MIN2
#define MIN2(a,b) ((a)<(b) ? (a) : (b))
#endif
#ifndef MIN3
#define MIN3(a,b,c) MIN2(MIN2(a,b),c)
#endif
#ifndef FABS
#define FABS(a) ((a)<0 ? -(a) : (a))
#endif
#ifndef _MAXFLOAT
#define _MAXFLOAT
#define MAXFLOAT ((float)3.40282346638528860e+38)
#endif /* _MAXFLOAT */
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#ifndef NULL
#define NULL 0
#endif
// find the min and max of three floats
inline void fFindMinMax(float x0,float x1, float x2, float &min, float &max)
{
min=MIN3(x0,x1,x2);
max=MAX3(x0,x1,x2);
}
// need to ask Timo, if I can use these. Ulf could use them in Illuminate Labs' code, so this should be fine...
inline int intChop (const float& f)
{
int32 a = *reinterpret_cast<const int32*>(&f); // take bit pattern of float into a register
int32 sign = (a>>31); // sign = 0xFFFFFFFF if original value is negative, 0 if positive
int32 mantissa = (a&((1<<23)-1))|(1<<23); // extract mantissa and add the hidden bit
int32 exponent = ((a&0x7fffffff)>>23)-127; // extract the exponent
int32 r = ((uint32)(mantissa)<<8)>>(31-exponent); // ((1<<exponent)*mantissa)>>24 -- (we know that mantissa > (1<<24))
return ((r ^ (sign)) - sign ) &~ (exponent>>31); // add original sign. If exponent was negative, make return value 0.
}
inline int intFloor (const float& f)
{
int32 a = *reinterpret_cast<const int32*>(&f); // take bit pattern of float into a register
int32 sign = (a>>31); // sign = 0xFFFFFFFF if original value is negative, 0 if positive
a&=0x7fffffff; // we don't need the sign any more
int32 exponent = (a>>23)-127; // extract the exponent
int32 expsign = ~(exponent>>31); // 0xFFFFFFFF if exponent is positive, 0 otherwise
int32 imask = ( (1<<(31-(exponent))))-1; // mask for true integer values
int32 mantissa = (a&((1<<23)-1)); // extract mantissa (without the hidden bit)
int32 r = ((uint32)(mantissa|(1<<23))<<8)>>(31-exponent); // ((1<<exponent)*(mantissa|hidden bit))>>24 -- (we know that mantissa > (1<<24))
r = ((r & expsign) ^ (sign)) + ((!((mantissa<<8)&imask)&(expsign^((a-1)>>31)))&sign); // if (fabs(value)<1.0) value = 0; copy sign; if (value < 0 && value==(int)(value)) value++;
return r;
}
inline int intCeil (const float& f)
{
int32 a = *reinterpret_cast<const int32*>(&f) ^ 0x80000000; // take bit pattern of float into a register
int32 sign = (a>>31); // sign = 0xFFFFFFFF if original value is negative, 0 if positive
a&=0x7fffffff; // we don't need the sign any more
int32 exponent = (a>>23)-127; // extract the exponent
int32 expsign = ~(exponent>>31); // 0xFFFFFFFF if exponent is positive, 0 otherwise
int32 imask = ( (1<<(31-(exponent))))-1; // mask for true integer values
int32 mantissa = (a&((1<<23)-1)); // extract mantissa (without the hidden bit)
int32 r = ((uint32)(mantissa|(1<<23))<<8)>>(31-exponent); // ((1<<exponent)*(mantissa|hidden bit))>>24 -- (we know that mantissa > (1<<24))
r = ((r & expsign) ^ (sign)) + ((!((mantissa<<8)&imask)&(expsign^((a-1)>>31)))&sign); // if (fabs(value)<1.0) value = 0; copy sign; if (value < 0 && value==(int)(value)) value++;
return -r;
}
inline int32 floatToFixed(int fractionalBits,float v)
{
ASSERT(fractionalBits>=0);
return intFloor((1<<fractionalBits)*v+0.5f);
}
#endif