CmBitwise.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #ifndef _Bitwise_H__
  25. #define _Bitwise_H__
  26. #include "CmPrerequisitesUtil.h"
  27. namespace CamelotFramework {
  28. /** \addtogroup Core
  29. * @{
  30. */
  31. /** \addtogroup Math
  32. * @{
  33. */
  34. /** Class for manipulating bit patterns.
  35. */
  36. class Bitwise {
  37. public:
  38. /** Returns the most significant bit set in a value.
  39. */
  40. static FORCEINLINE unsigned int mostSignificantBitSet(unsigned int value)
  41. {
  42. unsigned int result = 0;
  43. while (value != 0) {
  44. ++result;
  45. value >>= 1;
  46. }
  47. return result-1;
  48. }
  49. /** Returns the closest power-of-two number greater or equal to value.
  50. @note 0 and 1 are powers of two, so
  51. firstPO2From(0)==0 and firstPO2From(1)==1.
  52. */
  53. static FORCEINLINE UINT32 firstPO2From(UINT32 n)
  54. {
  55. --n;
  56. n |= n >> 16;
  57. n |= n >> 8;
  58. n |= n >> 4;
  59. n |= n >> 2;
  60. n |= n >> 1;
  61. ++n;
  62. return n;
  63. }
  64. /** Determines whether the number is power-of-two or not.
  65. @note 0 and 1 are tread as power of two.
  66. */
  67. template<typename T>
  68. static FORCEINLINE bool isPO2(T n)
  69. {
  70. return (n & (n-1)) == 0;
  71. }
  72. /** Returns the number of bits a pattern must be shifted right by to
  73. remove right-hand zeros.
  74. */
  75. template<typename T>
  76. static FORCEINLINE unsigned int getBitShift(T mask)
  77. {
  78. if (mask == 0)
  79. return 0;
  80. unsigned int result = 0;
  81. while ((mask & 1) == 0) {
  82. ++result;
  83. mask >>= 1;
  84. }
  85. return result;
  86. }
  87. /** Takes a value with a given src bit mask, and produces another
  88. value with a desired bit mask.
  89. @remarks
  90. This routine is useful for colour conversion.
  91. */
  92. template<typename SrcT, typename DestT>
  93. static inline DestT convertBitPattern(SrcT srcValue, SrcT srcBitMask, DestT destBitMask)
  94. {
  95. // Mask off irrelevant source value bits (if any)
  96. srcValue = srcValue & srcBitMask;
  97. // Shift source down to bottom of DWORD
  98. const unsigned int srcBitShift = getBitShift(srcBitMask);
  99. srcValue >>= srcBitShift;
  100. // Get max value possible in source from srcMask
  101. const SrcT srcMax = srcBitMask >> srcBitShift;
  102. // Get max available in dest
  103. const unsigned int destBitShift = getBitShift(destBitMask);
  104. const DestT destMax = destBitMask >> destBitShift;
  105. // Scale source value into destination, and shift back
  106. DestT destValue = (srcValue * destMax) / srcMax;
  107. return (destValue << destBitShift);
  108. }
  109. /**
  110. * Convert N bit colour channel value to P bits. It fills P bits with the
  111. * bit pattern repeated. (this is /((1<<n)-1) in fixed point)
  112. */
  113. static inline unsigned int fixedToFixed(UINT32 value, unsigned int n, unsigned int p)
  114. {
  115. if(n > p)
  116. {
  117. // Less bits required than available; this is easy
  118. value >>= n-p;
  119. }
  120. else if(n < p)
  121. {
  122. // More bits required than are there, do the fill
  123. // Use old fashioned division, probably better than a loop
  124. if(value == 0)
  125. value = 0;
  126. else if(value == (static_cast<unsigned int>(1)<<n)-1)
  127. value = (1<<p)-1;
  128. else value = value*(1<<p)/((1<<n)-1);
  129. }
  130. return value;
  131. }
  132. /**
  133. * Convert floating point colour channel value between 0.0 and 1.0 (otherwise clamped)
  134. * to integer of a certain number of bits. Works for any value of bits between 0 and 31.
  135. */
  136. static inline unsigned int floatToFixed(const float value, const unsigned int bits)
  137. {
  138. if(value <= 0.0f) return 0;
  139. else if (value >= 1.0f) return (1<<bits)-1;
  140. else return (unsigned int)(value * (1<<bits));
  141. }
  142. /**
  143. * Fixed point to float
  144. */
  145. static inline float fixedToFloat(unsigned value, unsigned int bits)
  146. {
  147. return (float)value/(float)((1<<bits)-1);
  148. }
  149. /**
  150. * Write a n*8 bits integer value to memory in native endian.
  151. */
  152. static inline void intWrite(void *dest, const int n, const unsigned int value)
  153. {
  154. switch(n) {
  155. case 1:
  156. ((UINT8*)dest)[0] = (UINT8)value;
  157. break;
  158. case 2:
  159. ((UINT16*)dest)[0] = (UINT16)value;
  160. break;
  161. case 3:
  162. #if CM_ENDIAN == CM_ENDIAN_BIG
  163. ((UINT8*)dest)[0] = (UINT8)((value >> 16) & 0xFF);
  164. ((UINT8*)dest)[1] = (UINT8)((value >> 8) & 0xFF);
  165. ((UINT8*)dest)[2] = (UINT8)(value & 0xFF);
  166. #else
  167. ((UINT8*)dest)[2] = (UINT8)((value >> 16) & 0xFF);
  168. ((UINT8*)dest)[1] = (UINT8)((value >> 8) & 0xFF);
  169. ((UINT8*)dest)[0] = (UINT8)(value & 0xFF);
  170. #endif
  171. break;
  172. case 4:
  173. ((UINT32*)dest)[0] = (UINT32)value;
  174. break;
  175. }
  176. }
  177. /**
  178. * Read a n*8 bits integer value to memory in native endian.
  179. */
  180. static inline unsigned int intRead(const void *src, int n) {
  181. switch(n) {
  182. case 1:
  183. return ((UINT8*)src)[0];
  184. case 2:
  185. return ((UINT16*)src)[0];
  186. case 3:
  187. #if CM_ENDIAN == CM_ENDIAN_BIG
  188. return ((UINT32)((UINT8*)src)[0]<<16)|
  189. ((UINT32)((UINT8*)src)[1]<<8)|
  190. ((UINT32)((UINT8*)src)[2]);
  191. #else
  192. return ((UINT32)((UINT8*)src)[0])|
  193. ((UINT32)((UINT8*)src)[1]<<8)|
  194. ((UINT32)((UINT8*)src)[2]<<16);
  195. #endif
  196. case 4:
  197. return ((UINT32*)src)[0];
  198. }
  199. return 0; // ?
  200. }
  201. /** Convert a float32 to a float16 (NV_half_float)
  202. Courtesy of OpenEXR
  203. */
  204. static inline UINT16 floatToHalf(float i)
  205. {
  206. union { float f; UINT32 i; } v;
  207. v.f = i;
  208. return floatToHalfI(v.i);
  209. }
  210. /** Converts float in UINT32 format to a a half in UINT16 format
  211. */
  212. static inline UINT16 floatToHalfI(UINT32 i)
  213. {
  214. register int s = (i >> 16) & 0x00008000;
  215. register int e = ((i >> 23) & 0x000000ff) - (127 - 15);
  216. register int m = i & 0x007fffff;
  217. if (e <= 0)
  218. {
  219. if (e < -10)
  220. {
  221. return 0;
  222. }
  223. m = (m | 0x00800000) >> (1 - e);
  224. return static_cast<UINT16>(s | (m >> 13));
  225. }
  226. else if (e == 0xff - (127 - 15))
  227. {
  228. if (m == 0) // Inf
  229. {
  230. return static_cast<UINT16>(s | 0x7c00);
  231. }
  232. else // NAN
  233. {
  234. m >>= 13;
  235. return static_cast<UINT16>(s | 0x7c00 | m | (m == 0));
  236. }
  237. }
  238. else
  239. {
  240. if (e > 30) // Overflow
  241. {
  242. return static_cast<UINT16>(s | 0x7c00);
  243. }
  244. return static_cast<UINT16>(s | (e << 10) | (m >> 13));
  245. }
  246. }
  247. /**
  248. * Convert a float16 (NV_half_float) to a float32
  249. * Courtesy of OpenEXR
  250. */
  251. static inline float halfToFloat(UINT16 y)
  252. {
  253. union { float f; UINT32 i; } v;
  254. v.i = halfToFloatI(y);
  255. return v.f;
  256. }
  257. /** Converts a half in UINT16 format to a float
  258. in UINT32 format
  259. */
  260. static inline UINT32 halfToFloatI(UINT16 y)
  261. {
  262. register int s = (y >> 15) & 0x00000001;
  263. register int e = (y >> 10) & 0x0000001f;
  264. register int m = y & 0x000003ff;
  265. if (e == 0)
  266. {
  267. if (m == 0) // Plus or minus zero
  268. {
  269. return s << 31;
  270. }
  271. else // Denormalized number -- renormalize it
  272. {
  273. while (!(m & 0x00000400))
  274. {
  275. m <<= 1;
  276. e -= 1;
  277. }
  278. e += 1;
  279. m &= ~0x00000400;
  280. }
  281. }
  282. else if (e == 31)
  283. {
  284. if (m == 0) // Inf
  285. {
  286. return (s << 31) | 0x7f800000;
  287. }
  288. else // NaN
  289. {
  290. return (s << 31) | 0x7f800000 | (m << 13);
  291. }
  292. }
  293. e = e + (127 - 15);
  294. m = m << 13;
  295. return (s << 31) | (e << 23) | m;
  296. }
  297. };
  298. /** @} */
  299. /** @} */
  300. }
  301. #endif