math64.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #ifndef _LINUX_MATH64_H
  2. #define _LINUX_MATH64_H
  3. #include <linux/types.h>
  4. #include <asm/div64.h>
  5. #if BITS_PER_LONG == 64
  6. #define div64_long(x, y) div64_s64((x), (y))
  7. #define div64_ul(x, y) div64_u64((x), (y))
  8. /**
  9. * div_u64_rem - unsigned 64bit divide with 32bit divisor with remainder
  10. *
  11. * This is commonly provided by 32bit archs to provide an optimized 64bit
  12. * divide.
  13. */
  14. static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder)
  15. {
  16. *remainder = dividend % divisor;
  17. return dividend / divisor;
  18. }
  19. /**
  20. * div_s64_rem - signed 64bit divide with 32bit divisor with remainder
  21. */
  22. static inline s64 div_s64_rem(s64 dividend, s32 divisor, s32 *remainder)
  23. {
  24. *remainder = dividend % divisor;
  25. return dividend / divisor;
  26. }
  27. /**
  28. * div64_u64_rem - unsigned 64bit divide with 64bit divisor and remainder
  29. */
  30. static inline u64 div64_u64_rem(u64 dividend, u64 divisor, u64 *remainder)
  31. {
  32. *remainder = dividend % divisor;
  33. return dividend / divisor;
  34. }
  35. /**
  36. * div64_u64 - unsigned 64bit divide with 64bit divisor
  37. */
  38. static inline u64 div64_u64(u64 dividend, u64 divisor)
  39. {
  40. return dividend / divisor;
  41. }
  42. /**
  43. * div64_s64 - signed 64bit divide with 64bit divisor
  44. */
  45. static inline s64 div64_s64(s64 dividend, s64 divisor)
  46. {
  47. return dividend / divisor;
  48. }
  49. #elif BITS_PER_LONG == 32
  50. #define div64_long(x, y) div_s64((x), (y))
  51. #define div64_ul(x, y) div_u64((x), (y))
  52. #ifndef div_u64_rem
  53. static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder)
  54. {
  55. *remainder = do_div(dividend, divisor);
  56. return dividend;
  57. }
  58. #endif
  59. #ifndef div_s64_rem
  60. extern s64 div_s64_rem(s64 dividend, s32 divisor, s32 *remainder);
  61. #endif
  62. #ifndef div64_u64_rem
  63. extern u64 div64_u64_rem(u64 dividend, u64 divisor, u64 *remainder);
  64. #endif
  65. #ifndef div64_u64
  66. extern u64 div64_u64(u64 dividend, u64 divisor);
  67. #endif
  68. #ifndef div64_s64
  69. extern s64 div64_s64(s64 dividend, s64 divisor);
  70. #endif
  71. #endif /* BITS_PER_LONG */
  72. /**
  73. * div_u64 - unsigned 64bit divide with 32bit divisor
  74. *
  75. * This is the most common 64bit divide and should be used if possible,
  76. * as many 32bit archs can optimize this variant better than a full 64bit
  77. * divide.
  78. */
  79. #ifndef div_u64
  80. static inline u64 div_u64(u64 dividend, u32 divisor)
  81. {
  82. u32 remainder;
  83. return div_u64_rem(dividend, divisor, &remainder);
  84. }
  85. #endif
  86. /**
  87. * div_s64 - signed 64bit divide with 32bit divisor
  88. */
  89. #ifndef div_s64
  90. static inline s64 div_s64(s64 dividend, s32 divisor)
  91. {
  92. s32 remainder;
  93. return div_s64_rem(dividend, divisor, &remainder);
  94. }
  95. #endif
  96. u32 iter_div_u64_rem(u64 dividend, u32 divisor, u64 *remainder);
  97. static __always_inline u32
  98. __iter_div_u64_rem(u64 dividend, u32 divisor, u64 *remainder)
  99. {
  100. u32 ret = 0;
  101. while (dividend >= divisor) {
  102. /* The following asm() prevents the compiler from
  103. optimising this loop into a modulo operation. */
  104. asm("" : "+rm"(dividend));
  105. dividend -= divisor;
  106. ret++;
  107. }
  108. *remainder = dividend;
  109. return ret;
  110. }
  111. #if defined(CONFIG_ARCH_SUPPORTS_INT128) && defined(__SIZEOF_INT128__)
  112. #ifndef mul_u64_u32_shr
  113. static inline u64 mul_u64_u32_shr(u64 a, u32 mul, unsigned int shift)
  114. {
  115. return (u64)(((unsigned __int128)a * mul) >> shift);
  116. }
  117. #endif /* mul_u64_u32_shr */
  118. #ifndef mul_u64_u64_shr
  119. static inline u64 mul_u64_u64_shr(u64 a, u64 mul, unsigned int shift)
  120. {
  121. return (u64)(((unsigned __int128)a * mul) >> shift);
  122. }
  123. #endif /* mul_u64_u64_shr */
  124. #else
  125. #ifndef mul_u64_u32_shr
  126. static inline u64 mul_u64_u32_shr(u64 a, u32 mul, unsigned int shift)
  127. {
  128. u32 ah, al;
  129. u64 ret;
  130. al = a;
  131. ah = a >> 32;
  132. ret = ((u64)al * mul) >> shift;
  133. if (ah)
  134. ret += ((u64)ah * mul) << (32 - shift);
  135. return ret;
  136. }
  137. #endif /* mul_u64_u32_shr */
  138. #ifndef mul_u64_u64_shr
  139. static inline u64 mul_u64_u64_shr(u64 a, u64 b, unsigned int shift)
  140. {
  141. union {
  142. u64 ll;
  143. struct {
  144. #ifdef __BIG_ENDIAN
  145. u32 high, low;
  146. #else
  147. u32 low, high;
  148. #endif
  149. } l;
  150. } rl, rm, rn, rh, a0, b0;
  151. u64 c;
  152. a0.ll = a;
  153. b0.ll = b;
  154. rl.ll = (u64)a0.l.low * b0.l.low;
  155. rm.ll = (u64)a0.l.low * b0.l.high;
  156. rn.ll = (u64)a0.l.high * b0.l.low;
  157. rh.ll = (u64)a0.l.high * b0.l.high;
  158. /*
  159. * Each of these lines computes a 64-bit intermediate result into "c",
  160. * starting at bits 32-95. The low 32-bits go into the result of the
  161. * multiplication, the high 32-bits are carried into the next step.
  162. */
  163. rl.l.high = c = (u64)rl.l.high + rm.l.low + rn.l.low;
  164. rh.l.low = c = (c >> 32) + rm.l.high + rn.l.high + rh.l.low;
  165. rh.l.high = (c >> 32) + rh.l.high;
  166. /*
  167. * The 128-bit result of the multiplication is in rl.ll and rh.ll,
  168. * shift it right and throw away the high part of the result.
  169. */
  170. if (shift == 0)
  171. return rl.ll;
  172. if (shift < 64)
  173. return (rl.ll >> shift) | (rh.ll << (64 - shift));
  174. return rh.ll >> (shift & 63);
  175. }
  176. #endif /* mul_u64_u64_shr */
  177. #endif
  178. #ifndef mul_u64_u32_div
  179. static inline u64 mul_u64_u32_div(u64 a, u32 mul, u32 divisor)
  180. {
  181. union {
  182. u64 ll;
  183. struct {
  184. #ifdef __BIG_ENDIAN
  185. u32 high, low;
  186. #else
  187. u32 low, high;
  188. #endif
  189. } l;
  190. } u, rl, rh;
  191. u.ll = a;
  192. rl.ll = (u64)u.l.low * mul;
  193. rh.ll = (u64)u.l.high * mul + rl.l.high;
  194. /* Bits 32-63 of the result will be in rh.l.low. */
  195. rl.l.high = do_div(rh.ll, divisor);
  196. /* Bits 0-31 of the result will be in rl.l.low. */
  197. do_div(rl.ll, divisor);
  198. rl.l.high = rh.l.low;
  199. return rl.ll;
  200. }
  201. #endif /* mul_u64_u32_div */
  202. #endif /* _LINUX_MATH64_H */