49 unsigned int result = 0;
77 return (n & (n-1)) == 0;
88 unsigned int result = 0;
89 while ((mask & 1) == 0) {
101 template<
typename SrcT,
typename DestT>
105 srcValue = srcValue & srcBitMask;
108 const unsigned int srcBitShift =
getBitShift(srcBitMask);
109 srcValue >>= srcBitShift;
112 const SrcT srcMax = srcBitMask >> srcBitShift;
115 const unsigned int destBitShift =
getBitShift(destBitMask);
116 const DestT destMax = destBitMask >> destBitShift;
119 DestT destValue = (srcValue * destMax) / srcMax;
120 return (destValue << destBitShift);
140 else if(value == (static_cast<unsigned int>(1)<<n)-1)
142 else value = value*(1<<p)/((1<<n)-1);
151 static inline unsigned int floatToFixed(
const float value,
const unsigned int bits)
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));
163 return (
float)value/(float)((1<<bits)-1);
169 static inline void intWrite(
void *dest,
const int n,
const unsigned int value)
179 #if OGRE_ENDIAN == OGRE_ENDIAN_BIG
180 ((
uint8*)dest)[0] = (
uint8)((value >> 16) & 0xFF);
181 ((
uint8*)dest)[1] = (
uint8)((value >> 8) & 0xFF);
184 ((
uint8*)dest)[2] = (
uint8)((value >> 16) & 0xFF);
185 ((
uint8*)dest)[1] = (
uint8)((value >> 8) & 0xFF);
197 static inline unsigned int intRead(
const void *src,
int n) {
200 return ((
const uint8*)src)[0];
202 return ((
const uint16*)src)[0];
204 #if OGRE_ENDIAN == OGRE_ENDIAN_BIG
214 return ((
const uint32*)src)[0];
224 union {
float f;
uint32 i; } v;
232 register int s = (i >> 16) & 0x00008000;
233 register int e = ((i >> 23) & 0x000000ff) - (127 - 15);
234 register int m = i & 0x007fffff;
242 m = (m | 0x00800000) >> (1 - e);
244 return static_cast<uint16>(s | (m >> 13));
246 else if (e == 0xff - (127 - 15))
250 return static_cast<uint16>(s | 0x7c00);
255 return static_cast<uint16>(s | 0x7c00 | m | (m == 0));
262 return static_cast<uint16>(s | 0x7c00);
265 return static_cast<uint16>(s | (e << 10) | (m >> 13));
275 union {
float f;
uint32 i; } v;
284 register int s = (y >> 15) & 0x00000001;
285 register int e = (y >> 10) & 0x0000001f;
286 register int m = y & 0x000003ff;
296 while (!(m & 0x00000400))
310 return (s << 31) | 0x7f800000;
314 return (s << 31) | 0x7f800000 | (m << 13);
321 return (s << 31) | (e << 23) | m;
static unsigned int fixedToFixed(uint32 value, unsigned int n, unsigned int p)
Convert N bit colour channel value to P bits.
static float fixedToFloat(unsigned value, unsigned int bits)
Fixed point to float.
static uint32 halfToFloatI(uint16 y)
Converts a half in uint16 format to a float in uint32 format.
static float halfToFloat(uint16 y)
Convert a float16 (NV_half_float) to a float32 Courtesy of OpenEXR.
static FORCEINLINE bool isPO2(T n)
Determines whether the number is power-of-two or not.
Class for manipulating bit patterns.
static FORCEINLINE uint32 firstPO2From(uint32 n)
Returns the closest power-of-two number greater or equal to value.
static FORCEINLINE unsigned int mostSignificantBitSet(unsigned int value)
Returns the most significant bit set in a value.
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...
static uint16 floatToHalfI(uint32 i)
Converts float in uint32 format to a a half in uint16 format.
static unsigned int intRead(const void *src, int n)
Read a n*8 bits integer value to memory in native endian.
static void intWrite(void *dest, const int n, const unsigned int value)
Write a n*8 bits integer value to memory in native endian.
static FORCEINLINE unsigned int getBitShift(T mask)
Returns the number of bits a pattern must be shifted right by to remove right-hand zeros...
static uint16 floatToHalf(float i)
Convert a float32 to a float16 (NV_half_float) Courtesy of OpenEXR.
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...