BsBitwise.h 8.6 KB

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