Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

501 lines
16 KiB

  1. //===-- llvm/Support/MathExtras.h - Useful math functions -------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file contains some functions that are useful for math stuff.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_SUPPORT_MATHEXTRAS_H
  14. #define LLVM_SUPPORT_MATHEXTRAS_H
  15. #include "llvm/Support/SwapByteOrder.h"
  16. #ifdef _MSC_VER
  17. # include <intrin.h>
  18. #endif
  19. namespace llvm {
  20. // NOTE: The following support functions use the _32/_64 extensions instead of
  21. // type overloading so that signed and unsigned integers can be used without
  22. // ambiguity.
  23. /// Hi_32 - This function returns the high 32 bits of a 64 bit value.
  24. inline uint32_t Hi_32(uint64_t Value) {
  25. return static_cast<uint32_t>(Value >> 32);
  26. }
  27. /// Lo_32 - This function returns the low 32 bits of a 64 bit value.
  28. inline uint32_t Lo_32(uint64_t Value) {
  29. return static_cast<uint32_t>(Value);
  30. }
  31. /// isInt - Checks if an integer fits into the given bit width.
  32. template<unsigned N>
  33. inline bool isInt(int64_t x) {
  34. return N >= 64 || (-(INT64_C(1)<<(N-1)) <= x && x < (INT64_C(1)<<(N-1)));
  35. }
  36. // Template specializations to get better code for common cases.
  37. template<>
  38. inline bool isInt<8>(int64_t x) {
  39. return static_cast<int8_t>(x) == x;
  40. }
  41. template<>
  42. inline bool isInt<16>(int64_t x) {
  43. return static_cast<int16_t>(x) == x;
  44. }
  45. template<>
  46. inline bool isInt<32>(int64_t x) {
  47. return static_cast<int32_t>(x) == x;
  48. }
  49. /// isShiftedInt<N,S> - Checks if a signed integer is an N bit number shifted
  50. /// left by S.
  51. template<unsigned N, unsigned S>
  52. inline bool isShiftedInt(int64_t x) {
  53. return isInt<N+S>(x) && (x % (1<<S) == 0);
  54. }
  55. /// isUInt - Checks if an unsigned integer fits into the given bit width.
  56. template<unsigned N>
  57. inline bool isUInt(uint64_t x) {
  58. return N >= 64 || x < (UINT64_C(1)<<(N));
  59. }
  60. // Template specializations to get better code for common cases.
  61. template<>
  62. inline bool isUInt<8>(uint64_t x) {
  63. return static_cast<uint8_t>(x) == x;
  64. }
  65. template<>
  66. inline bool isUInt<16>(uint64_t x) {
  67. return static_cast<uint16_t>(x) == x;
  68. }
  69. template<>
  70. inline bool isUInt<32>(uint64_t x) {
  71. return static_cast<uint32_t>(x) == x;
  72. }
  73. /// isShiftedUInt<N,S> - Checks if a unsigned integer is an N bit number shifted
  74. /// left by S.
  75. template<unsigned N, unsigned S>
  76. inline bool isShiftedUInt(uint64_t x) {
  77. return isUInt<N+S>(x) && (x % (1<<S) == 0);
  78. }
  79. /// isUIntN - Checks if an unsigned integer fits into the given (dynamic)
  80. /// bit width.
  81. inline bool isUIntN(unsigned N, uint64_t x) {
  82. return x == (x & (~0ULL >> (64 - N)));
  83. }
  84. /// isIntN - Checks if an signed integer fits into the given (dynamic)
  85. /// bit width.
  86. inline bool isIntN(unsigned N, int64_t x) {
  87. return N >= 64 || (-(INT64_C(1)<<(N-1)) <= x && x < (INT64_C(1)<<(N-1)));
  88. }
  89. /// isMask_32 - This function returns true if the argument is a sequence of ones
  90. /// starting at the least significant bit with the remainder zero (32 bit
  91. /// version). Ex. isMask_32(0x0000FFFFU) == true.
  92. inline bool isMask_32(uint32_t Value) {
  93. return Value && ((Value + 1) & Value) == 0;
  94. }
  95. /// isMask_64 - This function returns true if the argument is a sequence of ones
  96. /// starting at the least significant bit with the remainder zero (64 bit
  97. /// version).
  98. inline bool isMask_64(uint64_t Value) {
  99. return Value && ((Value + 1) & Value) == 0;
  100. }
  101. /// isShiftedMask_32 - This function returns true if the argument contains a
  102. /// sequence of ones with the remainder zero (32 bit version.)
  103. /// Ex. isShiftedMask_32(0x0000FF00U) == true.
  104. inline bool isShiftedMask_32(uint32_t Value) {
  105. return isMask_32((Value - 1) | Value);
  106. }
  107. /// isShiftedMask_64 - This function returns true if the argument contains a
  108. /// sequence of ones with the remainder zero (64 bit version.)
  109. inline bool isShiftedMask_64(uint64_t Value) {
  110. return isMask_64((Value - 1) | Value);
  111. }
  112. /// isPowerOf2_32 - This function returns true if the argument is a power of
  113. /// two > 0. Ex. isPowerOf2_32(0x00100000U) == true (32 bit edition.)
  114. inline bool isPowerOf2_32(uint32_t Value) {
  115. return Value && !(Value & (Value - 1));
  116. }
  117. /// isPowerOf2_64 - This function returns true if the argument is a power of two
  118. /// > 0 (64 bit edition.)
  119. inline bool isPowerOf2_64(uint64_t Value) {
  120. return Value && !(Value & (Value - int64_t(1L)));
  121. }
  122. /// ByteSwap_16 - This function returns a byte-swapped representation of the
  123. /// 16-bit argument, Value.
  124. inline uint16_t ByteSwap_16(uint16_t Value) {
  125. return sys::SwapByteOrder_16(Value);
  126. }
  127. /// ByteSwap_32 - This function returns a byte-swapped representation of the
  128. /// 32-bit argument, Value.
  129. inline uint32_t ByteSwap_32(uint32_t Value) {
  130. return sys::SwapByteOrder_32(Value);
  131. }
  132. /// ByteSwap_64 - This function returns a byte-swapped representation of the
  133. /// 64-bit argument, Value.
  134. inline uint64_t ByteSwap_64(uint64_t Value) {
  135. return sys::SwapByteOrder_64(Value);
  136. }
  137. /// CountLeadingZeros_32 - this function performs the platform optimal form of
  138. /// counting the number of zeros from the most significant bit to the first one
  139. /// bit. Ex. CountLeadingZeros_32(0x00F000FF) == 8.
  140. /// Returns 32 if the word is zero.
  141. inline unsigned CountLeadingZeros_32(uint32_t Value) {
  142. unsigned Count; // result
  143. #if __GNUC__ >= 4
  144. // PowerPC is defined for __builtin_clz(0)
  145. #if !defined(__ppc__) && !defined(__ppc64__)
  146. if (!Value) return 32;
  147. #endif
  148. Count = __builtin_clz(Value);
  149. #else
  150. if (!Value) return 32;
  151. Count = 0;
  152. // bisection method for count leading zeros
  153. for (unsigned Shift = 32 >> 1; Shift; Shift >>= 1) {
  154. uint32_t Tmp = Value >> Shift;
  155. if (Tmp) {
  156. Value = Tmp;
  157. } else {
  158. Count |= Shift;
  159. }
  160. }
  161. #endif
  162. return Count;
  163. }
  164. /// CountLeadingOnes_32 - this function performs the operation of
  165. /// counting the number of ones from the most significant bit to the first zero
  166. /// bit. Ex. CountLeadingOnes_32(0xFF0FFF00) == 8.
  167. /// Returns 32 if the word is all ones.
  168. inline unsigned CountLeadingOnes_32(uint32_t Value) {
  169. return CountLeadingZeros_32(~Value);
  170. }
  171. /// CountLeadingZeros_64 - This function performs the platform optimal form
  172. /// of counting the number of zeros from the most significant bit to the first
  173. /// one bit (64 bit edition.)
  174. /// Returns 64 if the word is zero.
  175. inline unsigned CountLeadingZeros_64(uint64_t Value) {
  176. unsigned Count; // result
  177. #if __GNUC__ >= 4
  178. // PowerPC is defined for __builtin_clzll(0)
  179. #if !defined(__ppc__) && !defined(__ppc64__)
  180. if (!Value) return 64;
  181. #endif
  182. Count = __builtin_clzll(Value);
  183. #else
  184. if (sizeof(long) == sizeof(int64_t)) {
  185. if (!Value) return 64;
  186. Count = 0;
  187. // bisection method for count leading zeros
  188. for (unsigned Shift = 64 >> 1; Shift; Shift >>= 1) {
  189. uint64_t Tmp = Value >> Shift;
  190. if (Tmp) {
  191. Value = Tmp;
  192. } else {
  193. Count |= Shift;
  194. }
  195. }
  196. } else {
  197. // get hi portion
  198. uint32_t Hi = Hi_32(Value);
  199. // if some bits in hi portion
  200. if (Hi) {
  201. // leading zeros in hi portion plus all bits in lo portion
  202. Count = CountLeadingZeros_32(Hi);
  203. } else {
  204. // get lo portion
  205. uint32_t Lo = Lo_32(Value);
  206. // same as 32 bit value
  207. Count = CountLeadingZeros_32(Lo)+32;
  208. }
  209. }
  210. #endif
  211. return Count;
  212. }
  213. /// CountLeadingOnes_64 - This function performs the operation
  214. /// of counting the number of ones from the most significant bit to the first
  215. /// zero bit (64 bit edition.)
  216. /// Returns 64 if the word is all ones.
  217. inline unsigned CountLeadingOnes_64(uint64_t Value) {
  218. return CountLeadingZeros_64(~Value);
  219. }
  220. /// CountTrailingZeros_32 - this function performs the platform optimal form of
  221. /// counting the number of zeros from the least significant bit to the first one
  222. /// bit. Ex. CountTrailingZeros_32(0xFF00FF00) == 8.
  223. /// Returns 32 if the word is zero.
  224. inline unsigned CountTrailingZeros_32(uint32_t Value) {
  225. #if __GNUC__ >= 4
  226. return Value ? __builtin_ctz(Value) : 32;
  227. #else
  228. static const unsigned Mod37BitPosition[] = {
  229. 32, 0, 1, 26, 2, 23, 27, 0, 3, 16, 24, 30, 28, 11, 0, 13,
  230. 4, 7, 17, 0, 25, 22, 31, 15, 29, 10, 12, 6, 0, 21, 14, 9,
  231. 5, 20, 8, 19, 18
  232. };
  233. // Replace "-Value" by "1+~Value" in the following commented code to avoid
  234. // MSVC warning C4146
  235. // return Mod37BitPosition[(-Value & Value) % 37];
  236. return Mod37BitPosition[((1 + ~Value) & Value) % 37];
  237. #endif
  238. }
  239. /// CountTrailingOnes_32 - this function performs the operation of
  240. /// counting the number of ones from the least significant bit to the first zero
  241. /// bit. Ex. CountTrailingOnes_32(0x00FF00FF) == 8.
  242. /// Returns 32 if the word is all ones.
  243. inline unsigned CountTrailingOnes_32(uint32_t Value) {
  244. return CountTrailingZeros_32(~Value);
  245. }
  246. /// CountTrailingZeros_64 - This function performs the platform optimal form
  247. /// of counting the number of zeros from the least significant bit to the first
  248. /// one bit (64 bit edition.)
  249. /// Returns 64 if the word is zero.
  250. inline unsigned CountTrailingZeros_64(uint64_t Value) {
  251. #if __GNUC__ >= 4
  252. return Value ? __builtin_ctzll(Value) : 64;
  253. #else
  254. static const unsigned Mod67Position[] = {
  255. 64, 0, 1, 39, 2, 15, 40, 23, 3, 12, 16, 59, 41, 19, 24, 54,
  256. 4, 64, 13, 10, 17, 62, 60, 28, 42, 30, 20, 51, 25, 44, 55,
  257. 47, 5, 32, 65, 38, 14, 22, 11, 58, 18, 53, 63, 9, 61, 27,
  258. 29, 50, 43, 46, 31, 37, 21, 57, 52, 8, 26, 49, 45, 36, 56,
  259. 7, 48, 35, 6, 34, 33, 0
  260. };
  261. // Replace "-Value" by "1+~Value" in the following commented code to avoid
  262. // MSVC warning C4146
  263. // return Mod67Position[(-Value & Value) % 67];
  264. return Mod67Position[((1 + ~Value) & Value) % 67];
  265. #endif
  266. }
  267. /// CountTrailingOnes_64 - This function performs the operation
  268. /// of counting the number of ones from the least significant bit to the first
  269. /// zero bit (64 bit edition.)
  270. /// Returns 64 if the word is all ones.
  271. inline unsigned CountTrailingOnes_64(uint64_t Value) {
  272. return CountTrailingZeros_64(~Value);
  273. }
  274. /// CountPopulation_32 - this function counts the number of set bits in a value.
  275. /// Ex. CountPopulation(0xF000F000) = 8
  276. /// Returns 0 if the word is zero.
  277. inline unsigned CountPopulation_32(uint32_t Value) {
  278. #if __GNUC__ >= 4
  279. return __builtin_popcount(Value);
  280. #else
  281. uint32_t v = Value - ((Value >> 1) & 0x55555555);
  282. v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
  283. return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
  284. #endif
  285. }
  286. /// CountPopulation_64 - this function counts the number of set bits in a value,
  287. /// (64 bit edition.)
  288. inline unsigned CountPopulation_64(uint64_t Value) {
  289. #if __GNUC__ >= 4
  290. return __builtin_popcountll(Value);
  291. #else
  292. uint64_t v = Value - ((Value >> 1) & 0x5555555555555555ULL);
  293. v = (v & 0x3333333333333333ULL) + ((v >> 2) & 0x3333333333333333ULL);
  294. v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
  295. return unsigned((uint64_t)(v * 0x0101010101010101ULL) >> 56);
  296. #endif
  297. }
  298. /// Log2_32 - This function returns the floor log base 2 of the specified value,
  299. /// -1 if the value is zero. (32 bit edition.)
  300. /// Ex. Log2_32(32) == 5, Log2_32(1) == 0, Log2_32(0) == -1, Log2_32(6) == 2
  301. inline unsigned Log2_32(uint32_t Value) {
  302. return 31 - CountLeadingZeros_32(Value);
  303. }
  304. /// Log2_64 - This function returns the floor log base 2 of the specified value,
  305. /// -1 if the value is zero. (64 bit edition.)
  306. inline unsigned Log2_64(uint64_t Value) {
  307. return 63 - CountLeadingZeros_64(Value);
  308. }
  309. /// Log2_32_Ceil - This function returns the ceil log base 2 of the specified
  310. /// value, 32 if the value is zero. (32 bit edition).
  311. /// Ex. Log2_32_Ceil(32) == 5, Log2_32_Ceil(1) == 0, Log2_32_Ceil(6) == 3
  312. inline unsigned Log2_32_Ceil(uint32_t Value) {
  313. return 32-CountLeadingZeros_32(Value-1);
  314. }
  315. /// Log2_64_Ceil - This function returns the ceil log base 2 of the specified
  316. /// value, 64 if the value is zero. (64 bit edition.)
  317. inline unsigned Log2_64_Ceil(uint64_t Value) {
  318. return 64-CountLeadingZeros_64(Value-1);
  319. }
  320. /// GreatestCommonDivisor64 - Return the greatest common divisor of the two
  321. /// values using Euclid's algorithm.
  322. inline uint64_t GreatestCommonDivisor64(uint64_t A, uint64_t B) {
  323. while (B) {
  324. uint64_t T = B;
  325. B = A % B;
  326. A = T;
  327. }
  328. return A;
  329. }
  330. /// BitsToDouble - This function takes a 64-bit integer and returns the bit
  331. /// equivalent double.
  332. inline double BitsToDouble(uint64_t Bits) {
  333. union {
  334. uint64_t L;
  335. double D;
  336. } T;
  337. T.L = Bits;
  338. return T.D;
  339. }
  340. /// BitsToFloat - This function takes a 32-bit integer and returns the bit
  341. /// equivalent float.
  342. inline float BitsToFloat(uint32_t Bits) {
  343. union {
  344. uint32_t I;
  345. float F;
  346. } T;
  347. T.I = Bits;
  348. return T.F;
  349. }
  350. /// DoubleToBits - This function takes a double and returns the bit
  351. /// equivalent 64-bit integer. Note that copying doubles around
  352. /// changes the bits of NaNs on some hosts, notably x86, so this
  353. /// routine cannot be used if these bits are needed.
  354. inline uint64_t DoubleToBits(double Double) {
  355. union {
  356. uint64_t L;
  357. double D;
  358. } T;
  359. T.D = Double;
  360. return T.L;
  361. }
  362. /// FloatToBits - This function takes a float and returns the bit
  363. /// equivalent 32-bit integer. Note that copying floats around
  364. /// changes the bits of NaNs on some hosts, notably x86, so this
  365. /// routine cannot be used if these bits are needed.
  366. inline uint32_t FloatToBits(float Float) {
  367. union {
  368. uint32_t I;
  369. float F;
  370. } T;
  371. T.F = Float;
  372. return T.I;
  373. }
  374. /// Platform-independent wrappers for the C99 isnan() function.
  375. int IsNAN(float f);
  376. int IsNAN(double d);
  377. /// Platform-independent wrappers for the C99 isinf() function.
  378. int IsInf(float f);
  379. int IsInf(double d);
  380. /// MinAlign - A and B are either alignments or offsets. Return the minimum
  381. /// alignment that may be assumed after adding the two together.
  382. inline uint64_t MinAlign(uint64_t A, uint64_t B) {
  383. // The largest power of 2 that divides both A and B.
  384. //
  385. // Replace "-Value" by "1+~Value" in the following commented code to avoid
  386. // MSVC warning C4146
  387. // return (A | B) & -(A | B);
  388. return (A | B) & (1 + ~(A | B));
  389. }
  390. /// NextPowerOf2 - Returns the next power of two (in 64-bits)
  391. /// that is strictly greater than A. Returns zero on overflow.
  392. inline uint64_t NextPowerOf2(uint64_t A) {
  393. A |= (A >> 1);
  394. A |= (A >> 2);
  395. A |= (A >> 4);
  396. A |= (A >> 8);
  397. A |= (A >> 16);
  398. A |= (A >> 32);
  399. return A + 1;
  400. }
  401. /// Returns the next integer (mod 2**64) that is greater than or equal to
  402. /// \p Value and is a multiple of \p Align. \p Align must be non-zero.
  403. ///
  404. /// Examples:
  405. /// \code
  406. /// RoundUpToAlignment(5, 8) = 8
  407. /// RoundUpToAlignment(17, 8) = 24
  408. /// RoundUpToAlignment(~0LL, 8) = 0
  409. /// \endcode
  410. inline uint64_t RoundUpToAlignment(uint64_t Value, uint64_t Align) {
  411. return ((Value + Align - 1) / Align) * Align;
  412. }
  413. /// Returns the offset to the next integer (mod 2**64) that is greater than
  414. /// or equal to \p Value and is a multiple of \p Align. \p Align must be
  415. /// non-zero.
  416. inline uint64_t OffsetToAlignment(uint64_t Value, uint64_t Align) {
  417. return RoundUpToAlignment(Value, Align) - Value;
  418. }
  419. /// abs64 - absolute value of a 64-bit int. Not all environments support
  420. /// "abs" on whatever their name for the 64-bit int type is. The absolute
  421. /// value of the largest negative number is undefined, as with "abs".
  422. inline int64_t abs64(int64_t x) {
  423. return (x < 0) ? -x : x;
  424. }
  425. /// SignExtend32 - Sign extend B-bit number x to 32-bit int.
  426. /// Usage int32_t r = SignExtend32<5>(x);
  427. template <unsigned B> inline int32_t SignExtend32(uint32_t x) {
  428. return int32_t(x << (32 - B)) >> (32 - B);
  429. }
  430. /// \brief Sign extend number in the bottom B bits of X to a 32-bit int.
  431. /// Requires 0 < B <= 32.
  432. inline int32_t SignExtend32(uint32_t X, unsigned B) {
  433. return int32_t(X << (32 - B)) >> (32 - B);
  434. }
  435. /// SignExtend64 - Sign extend B-bit number x to 64-bit int.
  436. /// Usage int64_t r = SignExtend64<5>(x);
  437. template <unsigned B> inline int64_t SignExtend64(uint64_t x) {
  438. return int64_t(x << (64 - B)) >> (64 - B);
  439. }
  440. /// \brief Sign extend number in the bottom B bits of X to a 64-bit int.
  441. /// Requires 0 < B <= 64.
  442. inline int64_t SignExtend64(uint64_t X, unsigned B) {
  443. return int64_t(X << (64 - B)) >> (64 - B);
  444. }
  445. } // End llvm namespace
  446. #endif