llvm-MathExtras.h 15 KB

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