/src/ogre/OgreMain/include/OgreBitwise.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | ----------------------------------------------------------------------------- |
3 | | This source file is part of OGRE |
4 | | (Object-oriented Graphics Rendering Engine) |
5 | | For the latest info, see http://www.ogre3d.org/ |
6 | | |
7 | | Copyright (c) 2000-2014 Torus Knot Software Ltd |
8 | | |
9 | | Permission is hereby granted, free of charge, to any person obtaining a copy |
10 | | of this software and associated documentation files (the "Software"), to deal |
11 | | in the Software without restriction, including without limitation the rights |
12 | | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
13 | | copies of the Software, and to permit persons to whom the Software is |
14 | | furnished to do so, subject to the following conditions: |
15 | | |
16 | | The above copyright notice and this permission notice shall be included in |
17 | | all copies or substantial portions of the Software. |
18 | | |
19 | | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20 | | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21 | | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
22 | | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23 | | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24 | | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
25 | | THE SOFTWARE. |
26 | | ----------------------------------------------------------------------------- |
27 | | */ |
28 | | #ifndef _Bitwise_H__ |
29 | | #define _Bitwise_H__ |
30 | | |
31 | | #include "OgrePrerequisites.h" |
32 | | |
33 | | #ifdef bswap16 |
34 | | #undef bswap16 |
35 | | #undef bswap32 |
36 | | #undef bswap64 |
37 | | #endif |
38 | | |
39 | | #ifndef __has_builtin |
40 | | // Compatibility with non-clang compilers |
41 | | #define __has_builtin(x) 0 |
42 | | #endif |
43 | | |
44 | | namespace Ogre { |
45 | | /** \addtogroup Core |
46 | | * @{ |
47 | | */ |
48 | | /** \addtogroup Math |
49 | | * @{ |
50 | | */ |
51 | | |
52 | | /** Class for manipulating bit patterns. |
53 | | */ |
54 | | class Bitwise { |
55 | | public: |
56 | | /** Returns value with reversed bytes order. |
57 | | */ |
58 | | static OGRE_FORCE_INLINE uint16 bswap16(uint16 arg) |
59 | 0 | { |
60 | 0 | #if OGRE_COMPILER == OGRE_COMPILER_MSVC && OGRE_COMP_VER >= 1310 |
61 | 0 | return _byteswap_ushort(arg); |
62 | 0 | #elif (OGRE_COMPILER == OGRE_COMPILER_CLANG && __has_builtin(__builtin_bswap16)) || (OGRE_COMPILER == OGRE_COMPILER_GNUC && OGRE_COMP_VER >= 480) |
63 | 0 | return __builtin_bswap16(arg); |
64 | 0 | #else |
65 | 0 | return ((arg << 8) & 0xFF00) | ((arg >> 8) & 0x00FF); |
66 | 0 | #endif |
67 | 0 | } |
68 | | /** Returns value with reversed bytes order. |
69 | | */ |
70 | | static OGRE_FORCE_INLINE uint32 bswap32(uint32 arg) |
71 | 0 | { |
72 | 0 | #if OGRE_COMPILER == OGRE_COMPILER_MSVC && OGRE_COMP_VER >= 1310 |
73 | 0 | return _byteswap_ulong(arg); |
74 | 0 | #elif (OGRE_COMPILER == OGRE_COMPILER_CLANG && __has_builtin(__builtin_bswap32)) || (OGRE_COMPILER == OGRE_COMPILER_GNUC && OGRE_COMP_VER >= 430) |
75 | 0 | return __builtin_bswap32(arg); |
76 | 0 | #else |
77 | 0 | return ((arg & 0x000000FF) << 24) | ((arg & 0x0000FF00) << 8) | ((arg >> 8) & 0x0000FF00) | ((arg >> 24) & 0x000000FF); |
78 | 0 | #endif |
79 | 0 | } |
80 | | /** Returns value with reversed bytes order. |
81 | | */ |
82 | | static OGRE_FORCE_INLINE uint64 bswap64(uint64 arg) |
83 | 0 | { |
84 | 0 | #if OGRE_COMPILER == OGRE_COMPILER_MSVC && OGRE_COMP_VER >= 1310 |
85 | 0 | return _byteswap_uint64(arg); |
86 | 0 | #elif (OGRE_COMPILER == OGRE_COMPILER_CLANG && __has_builtin(__builtin_bswap64)) || (OGRE_COMPILER == OGRE_COMPILER_GNUC && OGRE_COMP_VER >= 430) |
87 | 0 | return __builtin_bswap64(arg); |
88 | 0 | #else |
89 | 0 | union { |
90 | 0 | uint64 sv; |
91 | 0 | uint32 ul[2]; |
92 | 0 | } tmp, result; |
93 | 0 | tmp.sv = arg; |
94 | 0 | result.ul[0] = bswap32(tmp.ul[1]); |
95 | 0 | result.ul[1] = bswap32(tmp.ul[0]); |
96 | 0 | return result.sv; |
97 | 0 | #endif |
98 | 0 | } |
99 | | |
100 | | /** Reverses byte order of buffer. Use bswap16/32/64 instead if possible. |
101 | | */ |
102 | | static inline void bswapBuffer(void * pData, size_t size) |
103 | 0 | { |
104 | 0 | char swapByte; |
105 | 0 | for(char *p0 = (char*)pData, *p1 = p0 + size - 1; p0 < p1; ++p0, --p1) |
106 | 0 | { |
107 | 0 | swapByte = *p0; |
108 | 0 | *p0 = *p1; |
109 | 0 | *p1 = swapByte; |
110 | 0 | } |
111 | 0 | } |
112 | | /** Reverses byte order of chunks in buffer, where 'size' is size of one chunk. |
113 | | */ |
114 | | static inline void bswapChunks(void * pData, size_t size, size_t count) |
115 | 0 | { |
116 | 0 | for(size_t c = 0; c < count; ++c) |
117 | 0 | { |
118 | 0 | char swapByte; |
119 | 0 | for(char *p0 = (char*)pData + c * size, *p1 = p0 + size - 1; p0 < p1; ++p0, --p1) |
120 | 0 | { |
121 | 0 | swapByte = *p0; |
122 | 0 | *p0 = *p1; |
123 | 0 | *p1 = swapByte; |
124 | 0 | } |
125 | 0 | } |
126 | 0 | } |
127 | | |
128 | | /** Returns the most significant bit set in a value. |
129 | | */ |
130 | | static OGRE_FORCE_INLINE unsigned int mostSignificantBitSet(unsigned int value) |
131 | 0 | { |
132 | 0 | // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F |
133 | 0 | static const unsigned char msb[16] = { 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4 }; |
134 | 0 |
|
135 | 0 | unsigned int result = 0; |
136 | 0 | if(value & 0xFFFF0000) { result += 16;value >>= 16; } |
137 | 0 | if(value & 0x0000FF00) { result += 8; value >>= 8; } |
138 | 0 | if(value & 0x000000F0) { result += 4; value >>= 4; } |
139 | 0 | result += msb[value]; |
140 | 0 | return result-1; |
141 | 0 | } |
142 | | /** Returns the closest power-of-two number greater or equal to value. |
143 | | @note 0 and 1 are powers of two, so |
144 | | firstPO2From(0)==0 and firstPO2From(1)==1. |
145 | | */ |
146 | | static OGRE_FORCE_INLINE uint32 firstPO2From(uint32 n) |
147 | 0 | { |
148 | 0 | --n; |
149 | 0 | n |= n >> 16; |
150 | 0 | n |= n >> 8; |
151 | 0 | n |= n >> 4; |
152 | 0 | n |= n >> 2; |
153 | 0 | n |= n >> 1; |
154 | 0 | ++n; |
155 | 0 | return n; |
156 | 0 | } |
157 | | /** Determines whether the number is power-of-two or not. |
158 | | @note 0 and 1 are treat as power of two. |
159 | | */ |
160 | | template<typename T> |
161 | | static OGRE_FORCE_INLINE bool isPO2(T n) |
162 | 0 | { |
163 | 0 | return (n & (n-1)) == 0; |
164 | 0 | } |
165 | | /** Returns the number of bits a pattern must be shifted right by to |
166 | | remove right-hand zeros. |
167 | | */ |
168 | | template<typename T> |
169 | | static OGRE_FORCE_INLINE unsigned int getBitShift(T mask) |
170 | | { |
171 | | if (mask == 0) |
172 | | return 0; |
173 | | |
174 | | unsigned int result = 0; |
175 | | while ((mask & 1) == 0) { |
176 | | ++result; |
177 | | mask >>= 1; |
178 | | } |
179 | | return result; |
180 | | } |
181 | | |
182 | | /** Takes a value with a given src bit mask, and produces another |
183 | | value with a desired bit mask. |
184 | | |
185 | | This routine is useful for colour conversion. |
186 | | */ |
187 | | template<typename SrcT, typename DestT> |
188 | | static inline DestT convertBitPattern(SrcT srcValue, SrcT srcBitMask, DestT destBitMask) |
189 | | { |
190 | | // Mask off irrelevant source value bits (if any) |
191 | | srcValue = srcValue & srcBitMask; |
192 | | |
193 | | // Shift source down to bottom of DWORD |
194 | | const unsigned int srcBitShift = getBitShift(srcBitMask); |
195 | | srcValue >>= srcBitShift; |
196 | | |
197 | | // Get max value possible in source from srcMask |
198 | | const SrcT srcMax = srcBitMask >> srcBitShift; |
199 | | |
200 | | // Get max available in dest |
201 | | const unsigned int destBitShift = getBitShift(destBitMask); |
202 | | const DestT destMax = destBitMask >> destBitShift; |
203 | | |
204 | | // Scale source value into destination, and shift back |
205 | | DestT destValue = (srcValue * destMax) / srcMax; |
206 | | return (destValue << destBitShift); |
207 | | } |
208 | | |
209 | | /** |
210 | | * Convert N bit colour channel value to P bits. It fills P bits with the |
211 | | * bit pattern repeated. (this is /((1<<n)-1) in fixed point) |
212 | | */ |
213 | | static inline unsigned int fixedToFixed(uint32 value, unsigned int n, unsigned int p) |
214 | 0 | { |
215 | 0 | if(n > p) |
216 | 0 | { |
217 | 0 | // Less bits required than available; this is easy |
218 | 0 | value >>= n-p; |
219 | 0 | } |
220 | 0 | else if(n < p) |
221 | 0 | { |
222 | 0 | // More bits required than are there, do the fill |
223 | 0 | // Use old fashioned division, probably better than a loop |
224 | 0 | if(value == 0) |
225 | 0 | value = 0; |
226 | 0 | else if(value == (static_cast<unsigned int>(1)<<n)-1) |
227 | 0 | value = (1<<p)-1; |
228 | 0 | else value = value*(1<<p)/((1<<n)-1); |
229 | 0 | } |
230 | 0 | return value; |
231 | 0 | } |
232 | | |
233 | | /** |
234 | | * Convert floating point colour channel value between 0.0 and 1.0 (otherwise clamped) |
235 | | * to integer of a certain number of bits. Works for any value of bits between 0 and 31. |
236 | | */ |
237 | | static inline unsigned int floatToFixed(const float value, const unsigned int bits) |
238 | 0 | { |
239 | 0 | if(value <= 0.0f) return 0; |
240 | 0 | else if (value >= 1.0f) return (1<<bits)-1; |
241 | 0 | else return (unsigned int)(value * float(1<<bits)); |
242 | 0 | } |
243 | | |
244 | | /** |
245 | | * Fixed point to float |
246 | | */ |
247 | | static inline float fixedToFloat(unsigned value, unsigned int bits) |
248 | 0 | { |
249 | 0 | return bits ? (float)value/(float)((1<<bits)-1) : 0.0f; |
250 | 0 | } |
251 | | |
252 | | /** |
253 | | * Write a n*8 bits integer value to memory in native endian. |
254 | | */ |
255 | | static inline void intWrite(void *dest, const int n, const unsigned int value) |
256 | 0 | { |
257 | 0 | switch(n) { |
258 | 0 | case 1: |
259 | 0 | ((uint8*)dest)[0] = (uint8)value; |
260 | 0 | break; |
261 | 0 | case 2: |
262 | 0 | ((uint16*)dest)[0] = (uint16)value; |
263 | 0 | break; |
264 | 0 | case 3: |
265 | 0 | #if OGRE_ENDIAN == OGRE_ENDIAN_BIG |
266 | 0 | ((uint8*)dest)[0] = (uint8)((value >> 16) & 0xFF); |
267 | 0 | ((uint8*)dest)[1] = (uint8)((value >> 8) & 0xFF); |
268 | 0 | ((uint8*)dest)[2] = (uint8)(value & 0xFF); |
269 | 0 | #else |
270 | 0 | ((uint8*)dest)[2] = (uint8)((value >> 16) & 0xFF); |
271 | 0 | ((uint8*)dest)[1] = (uint8)((value >> 8) & 0xFF); |
272 | 0 | ((uint8*)dest)[0] = (uint8)(value & 0xFF); |
273 | 0 | #endif |
274 | 0 | break; |
275 | 0 | case 4: |
276 | 0 | ((uint32*)dest)[0] = (uint32)value; |
277 | 0 | break; |
278 | 0 | } |
279 | 0 | } |
280 | | /** |
281 | | * Read a n*8 bits integer value to memory in native endian. |
282 | | */ |
283 | 0 | static inline unsigned int intRead(const void *src, int n) { |
284 | 0 | switch(n) { |
285 | 0 | case 1: |
286 | 0 | return ((const uint8*)src)[0]; |
287 | 0 | case 2: |
288 | 0 | return ((const uint16*)src)[0]; |
289 | 0 | case 3: |
290 | 0 | #if OGRE_ENDIAN == OGRE_ENDIAN_BIG |
291 | 0 | return ((uint32)((const uint8*)src)[0]<<16)| |
292 | 0 | ((uint32)((const uint8*)src)[1]<<8)| |
293 | 0 | ((uint32)((const uint8*)src)[2]); |
294 | 0 | #else |
295 | 0 | return ((uint32)((const uint8*)src)[0])| |
296 | 0 | ((uint32)((const uint8*)src)[1]<<8)| |
297 | 0 | ((uint32)((const uint8*)src)[2]<<16); |
298 | 0 | #endif |
299 | 0 | case 4: |
300 | 0 | return ((const uint32*)src)[0]; |
301 | 0 | } |
302 | 0 | return 0; // ? |
303 | 0 | } |
304 | | |
305 | | /** Convert a float32 to a float16 (NV_half_float) |
306 | | Courtesy of meshoptimizer |
307 | | */ |
308 | | static inline uint16 floatToHalf(float i) |
309 | 0 | { |
310 | 0 | union { float f; uint32 i; } v; |
311 | 0 | v.f = i; |
312 | 0 | return floatToHalfI(v.i); |
313 | 0 | } |
314 | | /** Converts float in uint32 format to a a half in uint16 format |
315 | | */ |
316 | | static inline uint16 floatToHalfI(uint32 ui) |
317 | 0 | { |
318 | 0 | int s = (ui >> 16) & 0x8000; |
319 | 0 | int em = ui & 0x7fffffff; |
320 | 0 |
|
321 | 0 | // bias exponent and round to nearest; 112 is relative exponent bias (127-15) |
322 | 0 | int h = (em - (112 << 23) + (1 << 12)) >> 13; |
323 | 0 |
|
324 | 0 | // underflow: flush to zero; 113 encodes exponent -14 |
325 | 0 | h = (em < (113 << 23)) ? 0 : h; |
326 | 0 |
|
327 | 0 | // overflow: infinity; 143 encodes exponent 16 |
328 | 0 | h = (em >= (143 << 23)) ? 0x7c00 : h; |
329 | 0 |
|
330 | 0 | // NaN; note that we convert all types of NaN to qNaN |
331 | 0 | h = (em > (255 << 23)) ? 0x7e00 : h; |
332 | 0 |
|
333 | 0 | return (unsigned short)(s | h); |
334 | 0 | } |
335 | | |
336 | | /** |
337 | | * Convert a float16 (NV_half_float) to a float32 |
338 | | * Courtesy of meshoptimizer |
339 | | */ |
340 | | static inline float halfToFloat(uint16 y) |
341 | 0 | { |
342 | 0 | union { float f; uint32 i; } v; |
343 | 0 | v.i = halfToFloatI(y); |
344 | 0 | return v.f; |
345 | 0 | } |
346 | | /** Converts a half in uint16 format to a float |
347 | | in uint32 format |
348 | | */ |
349 | | static inline uint32 halfToFloatI(uint16 h) |
350 | 0 | { |
351 | 0 | unsigned int s = unsigned(h & 0x8000) << 16; |
352 | 0 | int em = h & 0x7fff; |
353 | 0 |
|
354 | 0 | // bias exponent and pad mantissa with 0; 112 is relative exponent bias (127-15) |
355 | 0 | int r = (em + (112 << 10)) << 13; |
356 | 0 |
|
357 | 0 | // denormal: flush to zero |
358 | 0 | r = (em < (1 << 10)) ? 0 : r; |
359 | 0 |
|
360 | 0 | // infinity/NaN; note that we preserve NaN payload as a byproduct of unifying inf/nan cases |
361 | 0 | // 112 is an exponent bias fixup; since we already applied it once, applying it twice converts 31 to 255 |
362 | 0 | r += (em >= (31 << 10)) ? (112 << 23) : 0; |
363 | 0 |
|
364 | 0 | return s | r; |
365 | 0 | } |
366 | | |
367 | | |
368 | | }; |
369 | | /** @} */ |
370 | | /** @} */ |
371 | | |
372 | | } |
373 | | |
374 | | #endif |