Legs.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. Copyright (c) 2009-2010 Christopher A. Taylor. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. * Redistributions of source code must retain the above copyright notice,
  6. this list of conditions and the following disclaimer.
  7. * Redistributions in binary form must reproduce the above copyright notice,
  8. this list of conditions and the following disclaimer in the documentation
  9. and/or other materials provided with the distribution.
  10. * Neither the name of LibCat nor the names of its contributors may be used
  11. to endorse or promote products derived from this software without
  12. specific prior written permission.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  14. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  17. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  23. POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef CAT_LEGS_HPP
  26. #define CAT_LEGS_HPP
  27. #include <cat/Platform.hpp>
  28. namespace cat {
  29. #if defined(CAT_WORD_64)
  30. #define CAT_LEG_BITS 64
  31. #define CAT_USED_BITS(x) BSR64(x) /* does not work if x = 0 */
  32. #define CAT_TRAILING_ZEROES(x) BSF64(x) /* does not work if x = 0 */
  33. typedef u64 Leg;
  34. typedef s64 LegSigned;
  35. #if !defined(CAT_COMPILER_MSVC)
  36. typedef u128 LegPair;
  37. typedef s128 LegPairSigned;
  38. # define CAT_LEG_PAIRMUL(A, B) ((LegPair)A * B)
  39. #else
  40. # define CAT_NO_LEGPAIR
  41. #endif
  42. // If x86-64 and MSVC
  43. #if defined(CAT_ISA_X86) && defined(CAT_COMPILER_MSVC)
  44. #define CAT_USE_LEGS_ASM64 /* use 64-bit assembly code inner loops */
  45. #endif
  46. #elif defined(CAT_WORD_32)
  47. #define CAT_LEG_BITS 32
  48. #define CAT_USED_BITS(x) BSR32(x) /* does not work if x = 0 */
  49. #define CAT_TRAILING_ZEROES(x) BSF32(x) /* does not work if x = 0 */
  50. typedef u32 Leg;
  51. typedef s32 LegSigned;
  52. typedef u64 LegPair;
  53. typedef s64 LegPairSigned;
  54. #if defined(CAT_COMPILER_MSVC)
  55. # define CAT_LEG_PAIRMUL(A, B) __emulu(A, B) /* slightly faster in ICC */
  56. #else
  57. # define CAT_LEG_PAIRMUL(A, B) ((LegPair)A * B)
  58. #endif
  59. #endif // CAT_WORD_32
  60. // Largest value that can be taken on by a Leg
  61. const Leg CAT_LEG_LARGEST = ~(Leg)0;
  62. // MSB of a leg
  63. const Leg CAT_LEG_MSB = (Leg)1 << (CAT_LEG_BITS - 1);
  64. #if defined(CAT_NO_LEGPAIR)
  65. ////
  66. // Platforms that do not have LegPair (assumes 64-bit MSVC)
  67. ////
  68. // p(hi:lo) = A * B
  69. #define CAT_LEG_MUL(A, B, p_hi, p_lo) \
  70. { \
  71. p_lo = _umul128(A, B, &p_hi); \
  72. }
  73. // p(hi:lo) = A * B + C
  74. #define CAT_LEG_MULADD(A, B, C, p_hi, p_lo) \
  75. { \
  76. u64 _C0 = C; \
  77. p_lo = _umul128(A, B, &p_hi); \
  78. p_hi += ((p_lo += _C0) < _C0); \
  79. }
  80. // p(hi:lo) = A * B + C + D
  81. #define CAT_LEG_MULADD2(A, B, C, D, p_hi, p_lo) \
  82. { \
  83. u64 _C0 = C, _D0 = D; \
  84. p_lo = _umul128(A, B, &p_hi); \
  85. p_hi += ((p_lo += _C0) < _C0); \
  86. p_hi += ((p_lo += _D0) < _D0); \
  87. }
  88. // p(C2:C1:C0) = A * B + (C1:C0)
  89. #define CAT_LEG_COMBA2(A, B, C0, C1, C2) \
  90. { \
  91. u64 _p_hi, _p_lo; \
  92. _p_lo = _umul128(A, B, &_p_hi); \
  93. _p_hi += ((C0 += _p_lo) < _p_lo); \
  94. C2 = ((C1 += _p_hi) < _p_hi); \
  95. }
  96. // p(C2:C1:C0) = A * B + (C2:C1:C0)
  97. #define CAT_LEG_COMBA3(A, B, C0, C1, C2) \
  98. { \
  99. u64 _p_hi, _p_lo; \
  100. _p_lo = _umul128(A, B, &_p_hi); \
  101. _p_hi += ((C0 += _p_lo) < _p_lo); \
  102. C2 += ((C1 += _p_hi) < _p_hi); \
  103. }
  104. #else // has LegPair
  105. ////
  106. // Platforms that have LegPair (assumes 32-bit MSVC or any-bit GCC)
  107. ////
  108. // p(hi:lo) = A * B
  109. #define CAT_LEG_MUL(A, B, p_hi, p_lo) \
  110. { \
  111. LegPair _mt = CAT_LEG_PAIRMUL(A, B); \
  112. (p_lo) = (Leg)_mt; \
  113. (p_hi) = (Leg)(_mt >> CAT_LEG_BITS); \
  114. }
  115. // p(hi:lo) = A * B + C
  116. #define CAT_LEG_MULADD(A, B, C, p_hi, p_lo) \
  117. { \
  118. LegPair _mt = CAT_LEG_PAIRMUL(A, B) + (Leg)(C); \
  119. (p_lo) = (Leg)_mt; \
  120. (p_hi) = (Leg)(_mt >> CAT_LEG_BITS); \
  121. }
  122. // p(hi:lo) = A * B + C + D
  123. #define CAT_LEG_MULADD2(A, B, C, D, p_hi, p_lo) \
  124. { \
  125. LegPair _mt = CAT_LEG_PAIRMUL(A, B) + (Leg)(C) + (Leg)(D); \
  126. (p_lo) = (Leg)_mt; \
  127. (p_hi) = (Leg)(_mt >> CAT_LEG_BITS); \
  128. }
  129. // p(C2:C1:C0) = A * B + (C1:C0)
  130. #define CAT_LEG_COMBA2(A, B, C0, C1, C2) \
  131. { \
  132. LegPair _cp = CAT_LEG_PAIRMUL(A, B) + (C0); \
  133. (C0) = (Leg)_cp; \
  134. _cp = (_cp >> CAT_LEG_BITS) + (C1); \
  135. (C1) = (Leg)_cp; \
  136. (C2) = (Leg)(_cp >> CAT_LEG_BITS); \
  137. }
  138. // p(C2:C1:C0) = A * B + (C2:C1:C0)
  139. #define CAT_LEG_COMBA3(A, B, C0, C1, C2) \
  140. { \
  141. LegPair _cp = CAT_LEG_PAIRMUL(A, B) + (C0); \
  142. (C0) = (Leg)_cp; \
  143. _cp = (_cp >> CAT_LEG_BITS) + (C1); \
  144. (C1) = (Leg)_cp; \
  145. (C2) += (Leg)(_cp >> CAT_LEG_BITS); \
  146. }
  147. // Q(hi:lo) = A(hi:lo) / B
  148. #define CAT_LEG_DIV(A_hi, A_lo, B, Q_hi, Q_lo) \
  149. { \
  150. LegPair _Ax = ((LegPair)(A_hi) << CAT_LEG_BITS) | (Leg)(A_lo); \
  151. LegPair _qt = (LegPair)(_Ax / (B)); \
  152. (Q_hi) = (Leg)(_qt >> CAT_LEG_BITS); \
  153. (Q_lo) = (Leg)_qt; \
  154. }
  155. #endif // CAT_NO_LEGPAIR
  156. } // namespace cat
  157. #endif // CAT_LEGS_HPP
粤ICP备19079148号