OGRE  2.0
Object-Oriented Graphics Rendering Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
OgreBitwise.h
Go to the documentation of this file.
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 namespace Ogre {
43  class Bitwise {
44  public:
47  static FORCEINLINE unsigned int mostSignificantBitSet(unsigned int value)
48  {
49  unsigned int result = 0;
50  while (value != 0) {
51  ++result;
52  value >>= 1;
53  }
54  return result-1;
55  }
61  {
62  --n;
63  n |= n >> 16;
64  n |= n >> 8;
65  n |= n >> 4;
66  n |= n >> 2;
67  n |= n >> 1;
68  ++n;
69  return n;
70  }
74  template<typename T>
75  static FORCEINLINE bool isPO2(T n)
76  {
77  return (n & (n-1)) == 0;
78  }
82  template<typename T>
83  static FORCEINLINE unsigned int getBitShift(T mask)
84  {
85  if (mask == 0)
86  return 0;
87 
88  unsigned int result = 0;
89  while ((mask & 1) == 0) {
90  ++result;
91  mask >>= 1;
92  }
93  return result;
94  }
95 
101  template<typename SrcT, typename DestT>
102  static inline DestT convertBitPattern(SrcT srcValue, SrcT srcBitMask, DestT destBitMask)
103  {
104  // Mask off irrelevant source value bits (if any)
105  srcValue = srcValue & srcBitMask;
106 
107  // Shift source down to bottom of DWORD
108  const unsigned int srcBitShift = getBitShift(srcBitMask);
109  srcValue >>= srcBitShift;
110 
111  // Get max value possible in source from srcMask
112  const SrcT srcMax = srcBitMask >> srcBitShift;
113 
114  // Get max available in dest
115  const unsigned int destBitShift = getBitShift(destBitMask);
116  const DestT destMax = destBitMask >> destBitShift;
117 
118  // Scale source value into destination, and shift back
119  DestT destValue = (srcValue * destMax) / srcMax;
120  return (destValue << destBitShift);
121  }
122 
127  static inline unsigned int fixedToFixed(uint32 value, unsigned int n, unsigned int p)
128  {
129  if(n > p)
130  {
131  // Less bits required than available; this is easy
132  value >>= n-p;
133  }
134  else if(n < p)
135  {
136  // More bits required than are there, do the fill
137  // Use old fashioned division, probably better than a loop
138  if(value == 0)
139  value = 0;
140  else if(value == (static_cast<unsigned int>(1)<<n)-1)
141  value = (1<<p)-1;
142  else value = value*(1<<p)/((1<<n)-1);
143  }
144  return value;
145  }
146 
151  static inline unsigned int floatToFixed(const float value, const unsigned int bits)
152  {
153  if(value <= 0.0f) return 0;
154  else if (value >= 1.0f) return (1<<bits)-1;
155  else return (unsigned int)(value * (1<<bits));
156  }
157 
161  static inline float fixedToFloat(unsigned value, unsigned int bits)
162  {
163  return (float)value/(float)((1<<bits)-1);
164  }
165 
169  static inline void intWrite(void *dest, const int n, const unsigned int value)
170  {
171  switch(n) {
172  case 1:
173  ((uint8*)dest)[0] = (uint8)value;
174  break;
175  case 2:
176  ((uint16*)dest)[0] = (uint16)value;
177  break;
178  case 3:
179 #if OGRE_ENDIAN == OGRE_ENDIAN_BIG
180  ((uint8*)dest)[0] = (uint8)((value >> 16) & 0xFF);
181  ((uint8*)dest)[1] = (uint8)((value >> 8) & 0xFF);
182  ((uint8*)dest)[2] = (uint8)(value & 0xFF);
183 #else
184  ((uint8*)dest)[2] = (uint8)((value >> 16) & 0xFF);
185  ((uint8*)dest)[1] = (uint8)((value >> 8) & 0xFF);
186  ((uint8*)dest)[0] = (uint8)(value & 0xFF);
187 #endif
188  break;
189  case 4:
190  ((uint32*)dest)[0] = (uint32)value;
191  break;
192  }
193  }
197  static inline unsigned int intRead(const void *src, int n) {
198  switch(n) {
199  case 1:
200  return ((const uint8*)src)[0];
201  case 2:
202  return ((const uint16*)src)[0];
203  case 3:
204 #if OGRE_ENDIAN == OGRE_ENDIAN_BIG
205  return ((uint32)((const uint8*)src)[0]<<16)|
206  ((uint32)((const uint8*)src)[1]<<8)|
207  ((uint32)((const uint8*)src)[2]);
208 #else
209  return ((uint32)((const uint8*)src)[0])|
210  ((uint32)((const uint8*)src)[1]<<8)|
211  ((uint32)((const uint8*)src)[2]<<16);
212 #endif
213  case 4:
214  return ((const uint32*)src)[0];
215  }
216  return 0; // ?
217  }
218 
222  static inline uint16 floatToHalf(float i)
223  {
224  union { float f; uint32 i; } v;
225  v.f = i;
226  return floatToHalfI(v.i);
227  }
230  static inline uint16 floatToHalfI(uint32 i)
231  {
232  register int s = (i >> 16) & 0x00008000;
233  register int e = ((i >> 23) & 0x000000ff) - (127 - 15);
234  register int m = i & 0x007fffff;
235 
236  if (e <= 0)
237  {
238  if (e < -10)
239  {
240  return 0;
241  }
242  m = (m | 0x00800000) >> (1 - e);
243 
244  return static_cast<uint16>(s | (m >> 13));
245  }
246  else if (e == 0xff - (127 - 15))
247  {
248  if (m == 0) // Inf
249  {
250  return static_cast<uint16>(s | 0x7c00);
251  }
252  else // NAN
253  {
254  m >>= 13;
255  return static_cast<uint16>(s | 0x7c00 | m | (m == 0));
256  }
257  }
258  else
259  {
260  if (e > 30) // Overflow
261  {
262  return static_cast<uint16>(s | 0x7c00);
263  }
264 
265  return static_cast<uint16>(s | (e << 10) | (m >> 13));
266  }
267  }
268 
273  static inline float halfToFloat(uint16 y)
274  {
275  union { float f; uint32 i; } v;
276  v.i = halfToFloatI(y);
277  return v.f;
278  }
282  static inline uint32 halfToFloatI(uint16 y)
283  {
284  register int s = (y >> 15) & 0x00000001;
285  register int e = (y >> 10) & 0x0000001f;
286  register int m = y & 0x000003ff;
287 
288  if (e == 0)
289  {
290  if (m == 0) // Plus or minus zero
291  {
292  return s << 31;
293  }
294  else // Denormalized number -- renormalize it
295  {
296  while (!(m & 0x00000400))
297  {
298  m <<= 1;
299  e -= 1;
300  }
301 
302  e += 1;
303  m &= ~0x00000400;
304  }
305  }
306  else if (e == 31)
307  {
308  if (m == 0) // Inf
309  {
310  return (s << 31) | 0x7f800000;
311  }
312  else // NaN
313  {
314  return (s << 31) | 0x7f800000 | (m << 13);
315  }
316  }
317 
318  e = e + (127 - 15);
319  m = m << 13;
320 
321  return (s << 31) | (e << 23) | m;
322  }
323 
324 
325  };
329 }
330 
331 #endif
static unsigned int fixedToFixed(uint32 value, unsigned int n, unsigned int p)
Convert N bit colour channel value to P bits.
Definition: OgreBitwise.h:127
unsigned char uint8
Definition: OgrePlatform.h:422
static float fixedToFloat(unsigned value, unsigned int bits)
Fixed point to float.
Definition: OgreBitwise.h:161
unsigned int uint32
Definition: OgrePlatform.h:420
static uint32 halfToFloatI(uint16 y)
Converts a half in uint16 format to a float in uint32 format.
Definition: OgreBitwise.h:282
static float halfToFloat(uint16 y)
Convert a float16 (NV_half_float) to a float32 Courtesy of OpenEXR.
Definition: OgreBitwise.h:273
static FORCEINLINE bool isPO2(T n)
Determines whether the number is power-of-two or not.
Definition: OgreBitwise.h:75
Class for manipulating bit patterns.
Definition: OgreBitwise.h:43
static FORCEINLINE uint32 firstPO2From(uint32 n)
Returns the closest power-of-two number greater or equal to value.
Definition: OgreBitwise.h:60
static FORCEINLINE unsigned int mostSignificantBitSet(unsigned int value)
Returns the most significant bit set in a value.
Definition: OgreBitwise.h:47
static unsigned int floatToFixed(const float value, const unsigned int bits)
Convert floating point colour channel value between 0.0 and 1.0 (otherwise clamped) to integer of a c...
Definition: OgreBitwise.h:151
static uint16 floatToHalfI(uint32 i)
Converts float in uint32 format to a a half in uint16 format.
Definition: OgreBitwise.h:230
static unsigned int intRead(const void *src, int n)
Read a n*8 bits integer value to memory in native endian.
Definition: OgreBitwise.h:197
static void intWrite(void *dest, const int n, const unsigned int value)
Write a n*8 bits integer value to memory in native endian.
Definition: OgreBitwise.h:169
static FORCEINLINE unsigned int getBitShift(T mask)
Returns the number of bits a pattern must be shifted right by to remove right-hand zeros...
Definition: OgreBitwise.h:83
static uint16 floatToHalf(float i)
Convert a float32 to a float16 (NV_half_float) Courtesy of OpenEXR.
Definition: OgreBitwise.h:222
#define FORCEINLINE
Definition: OgrePlatform.h:104
unsigned short uint16
Definition: OgrePlatform.h:421
static DestT convertBitPattern(SrcT srcValue, SrcT srcBitMask, DestT destBitMask)
Takes a value with a given src bit mask, and produces another value with a desired bit mask...
Definition: OgreBitwise.h:102