BsBitwise.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. #pragma once
  2. #include "BsPrerequisitesUtil.h"
  3. namespace BansheeEngine
  4. {
  5. /**
  6. * @brief Class for manipulating bit patterns.
  7. */
  8. class Bitwise
  9. {
  10. public:
  11. /**
  12. * @brief Returns the most significant bit set in a value.
  13. */
  14. static FORCEINLINE unsigned int mostSignificantBitSet(unsigned int value)
  15. {
  16. unsigned int result = 0;
  17. while (value != 0) {
  18. ++result;
  19. value >>= 1;
  20. }
  21. return result-1;
  22. }
  23. /**
  24. * @brief Returns the closest power-of-two number greater or equal to value.
  25. */
  26. static FORCEINLINE UINT32 firstPO2From(UINT32 n)
  27. {
  28. --n;
  29. n |= n >> 16;
  30. n |= n >> 8;
  31. n |= n >> 4;
  32. n |= n >> 2;
  33. n |= n >> 1;
  34. ++n;
  35. return n;
  36. }
  37. /**
  38. * @brief Determines whether the number is power-of-two or not.
  39. */
  40. template<typename T>
  41. static FORCEINLINE bool isPO2(T n)
  42. {
  43. return (n & (n-1)) == 0;
  44. }
  45. /**
  46. * @brief Returns the number of bits a pattern must be shifted right by to
  47. * remove right-hand zeros.
  48. */
  49. template<typename T>
  50. static FORCEINLINE unsigned int getBitShift(T mask)
  51. {
  52. if (mask == 0)
  53. return 0;
  54. unsigned int result = 0;
  55. while ((mask & 1) == 0) {
  56. ++result;
  57. mask >>= 1;
  58. }
  59. return result;
  60. }
  61. /**
  62. * @brief Takes a value with a given src bit mask, and produces another
  63. * value with a desired bit mask.
  64. */
  65. template<typename SrcT, typename DestT>
  66. static inline DestT convertBitPattern(SrcT srcValue, SrcT srcBitMask, DestT destBitMask)
  67. {
  68. // Mask off irrelevant source value bits (if any)
  69. srcValue = srcValue & srcBitMask;
  70. // Shift source down to bottom of DWORD
  71. const unsigned int srcBitShift = getBitShift(srcBitMask);
  72. srcValue >>= srcBitShift;
  73. // Get max value possible in source from srcMask
  74. const SrcT srcMax = srcBitMask >> srcBitShift;
  75. // Get max available in dest
  76. const unsigned int destBitShift = getBitShift(destBitMask);
  77. const DestT destMax = destBitMask >> destBitShift;
  78. // Scale source value into destination, and shift back
  79. DestT destValue = (srcValue * destMax) / srcMax;
  80. return (destValue << destBitShift);
  81. }
  82. /**
  83. * @brief Convert N bit colour channel value to P bits. It fills P bits with the
  84. * bit pattern repeated. (this is /((1<<n)-1) in fixed point).
  85. */
  86. static inline unsigned int fixedToFixed(UINT32 value, unsigned int n, unsigned int p)
  87. {
  88. if(n > p)
  89. {
  90. // Less bits required than available; this is easy
  91. value >>= n-p;
  92. }
  93. else if(n < p)
  94. {
  95. // More bits required than are there, do the fill
  96. // Use old fashioned division, probably better than a loop
  97. if(value == 0)
  98. value = 0;
  99. else if(value == (static_cast<unsigned int>(1)<<n)-1)
  100. value = (1<<p)-1;
  101. else value = value*(1<<p)/((1<<n)-1);
  102. }
  103. return value;
  104. }
  105. /**
  106. * @brief Convert floating point color channel value between 0.0 and 1.0 (otherwise clamped)
  107. * to integer of a certain number of bits. Works for any value of bits between 0 and 31.
  108. */
  109. static inline unsigned int floatToFixed(const float value, const unsigned int bits)
  110. {
  111. if(value <= 0.0f) return 0;
  112. else if (value >= 1.0f) return (1<<bits)-1;
  113. else return (unsigned int)(value * (1<<bits));
  114. }
  115. /**
  116. * @brief Fixed point to float.
  117. */
  118. static inline float fixedToFloat(unsigned value, unsigned int bits)
  119. {
  120. return (float)value/(float)((1<<bits)-1);
  121. }
  122. /**
  123. * @brief Write a n*8 bits integer value to memory in native endian.
  124. */
  125. static inline void intWrite(void *dest, const int n, const unsigned int value)
  126. {
  127. switch(n) {
  128. case 1:
  129. ((UINT8*)dest)[0] = (UINT8)value;
  130. break;
  131. case 2:
  132. ((UINT16*)dest)[0] = (UINT16)value;
  133. break;
  134. case 3:
  135. #if BS_ENDIAN == BS_ENDIAN_BIG
  136. ((UINT8*)dest)[0] = (UINT8)((value >> 16) & 0xFF);
  137. ((UINT8*)dest)[1] = (UINT8)((value >> 8) & 0xFF);
  138. ((UINT8*)dest)[2] = (UINT8)(value & 0xFF);
  139. #else
  140. ((UINT8*)dest)[2] = (UINT8)((value >> 16) & 0xFF);
  141. ((UINT8*)dest)[1] = (UINT8)((value >> 8) & 0xFF);
  142. ((UINT8*)dest)[0] = (UINT8)(value & 0xFF);
  143. #endif
  144. break;
  145. case 4:
  146. ((UINT32*)dest)[0] = (UINT32)value;
  147. break;
  148. }
  149. }
  150. /**
  151. * @brief Read a n*8 bits integer value to memory in native endian.
  152. */
  153. static inline unsigned int intRead(const void *src, int n) {
  154. switch(n) {
  155. case 1:
  156. return ((UINT8*)src)[0];
  157. case 2:
  158. return ((UINT16*)src)[0];
  159. case 3:
  160. #if BS_ENDIAN == BS_ENDIAN_BIG
  161. return ((UINT32)((UINT8*)src)[0]<<16)|
  162. ((UINT32)((UINT8*)src)[1]<<8)|
  163. ((UINT32)((UINT8*)src)[2]);
  164. #else
  165. return ((UINT32)((UINT8*)src)[0])|
  166. ((UINT32)((UINT8*)src)[1]<<8)|
  167. ((UINT32)((UINT8*)src)[2]<<16);
  168. #endif
  169. case 4:
  170. return ((UINT32*)src)[0];
  171. }
  172. return 0; // ?
  173. }
  174. /**
  175. * @brief Convert a float32 to a float16 (NV_half_float).
  176. */
  177. static inline UINT16 floatToHalf(float i)
  178. {
  179. union { float f; UINT32 i; } v;
  180. v.f = i;
  181. return floatToHalfI(v.i);
  182. }
  183. /**
  184. * @brief Converts float in UINT32 format to a a half in UINT16 format.
  185. */
  186. static inline UINT16 floatToHalfI(UINT32 i)
  187. {
  188. register int s = (i >> 16) & 0x00008000;
  189. register int e = ((i >> 23) & 0x000000ff) - (127 - 15);
  190. register int m = i & 0x007fffff;
  191. if (e <= 0)
  192. {
  193. if (e < -10)
  194. {
  195. return 0;
  196. }
  197. m = (m | 0x00800000) >> (1 - e);
  198. return static_cast<UINT16>(s | (m >> 13));
  199. }
  200. else if (e == 0xff - (127 - 15))
  201. {
  202. if (m == 0) // Inf
  203. {
  204. return static_cast<UINT16>(s | 0x7c00);
  205. }
  206. else // NAN
  207. {
  208. m >>= 13;
  209. return static_cast<UINT16>(s | 0x7c00 | m | (m == 0));
  210. }
  211. }
  212. else
  213. {
  214. if (e > 30) // Overflow
  215. {
  216. return static_cast<UINT16>(s | 0x7c00);
  217. }
  218. return static_cast<UINT16>(s | (e << 10) | (m >> 13));
  219. }
  220. }
  221. /**
  222. * @brief Convert a float16 (NV_half_float) to a float32.
  223. */
  224. static inline float halfToFloat(UINT16 y)
  225. {
  226. union { float f; UINT32 i; } v;
  227. v.i = halfToFloatI(y);
  228. return v.f;
  229. }
  230. /**
  231. * @brief Converts a half in UINT16 format to a float
  232. * in UINT32 format.
  233. */
  234. static inline UINT32 halfToFloatI(UINT16 y)
  235. {
  236. register int s = (y >> 15) & 0x00000001;
  237. register int e = (y >> 10) & 0x0000001f;
  238. register int m = y & 0x000003ff;
  239. if (e == 0)
  240. {
  241. if (m == 0) // Plus or minus zero
  242. {
  243. return s << 31;
  244. }
  245. else // Denormalized number -- renormalize it
  246. {
  247. while (!(m & 0x00000400))
  248. {
  249. m <<= 1;
  250. e -= 1;
  251. }
  252. e += 1;
  253. m &= ~0x00000400;
  254. }
  255. }
  256. else if (e == 31)
  257. {
  258. if (m == 0) // Inf
  259. {
  260. return (s << 31) | 0x7f800000;
  261. }
  262. else // NaN
  263. {
  264. return (s << 31) | 0x7f800000 | (m << 13);
  265. }
  266. }
  267. e = e + (127 - 15);
  268. m = m << 13;
  269. return (s << 31) | (e << 23) | m;
  270. }
  271. };
  272. }