muldi3.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/export.h>
  3. #include "libgcc.h"
  4. #define W_TYPE_SIZE 32
  5. #define __ll_B ((unsigned long) 1 << (W_TYPE_SIZE / 2))
  6. #define __ll_lowpart(t) ((unsigned long) (t) & (__ll_B - 1))
  7. #define __ll_highpart(t) ((unsigned long) (t) >> (W_TYPE_SIZE / 2))
  8. /* If we still don't have umul_ppmm, define it using plain C. */
  9. #if !defined(umul_ppmm)
  10. #define umul_ppmm(w1, w0, u, v) \
  11. do { \
  12. unsigned long __x0, __x1, __x2, __x3; \
  13. unsigned short __ul, __vl, __uh, __vh; \
  14. \
  15. __ul = __ll_lowpart(u); \
  16. __uh = __ll_highpart(u); \
  17. __vl = __ll_lowpart(v); \
  18. __vh = __ll_highpart(v); \
  19. \
  20. __x0 = (unsigned long) __ul * __vl; \
  21. __x1 = (unsigned long) __ul * __vh; \
  22. __x2 = (unsigned long) __uh * __vl; \
  23. __x3 = (unsigned long) __uh * __vh; \
  24. \
  25. __x1 += __ll_highpart(__x0); /* this can't give carry */\
  26. __x1 += __x2; /* but this indeed can */ \
  27. if (__x1 < __x2) /* did we get it? */ \
  28. __x3 += __ll_B; /* yes, add it in the proper pos */ \
  29. \
  30. (w1) = __x3 + __ll_highpart(__x1); \
  31. (w0) = __ll_lowpart(__x1) * __ll_B + __ll_lowpart(__x0);\
  32. } while (0)
  33. #endif
  34. #if !defined(__umulsidi3)
  35. #define __umulsidi3(u, v) ({ \
  36. DWunion __w; \
  37. umul_ppmm(__w.s.high, __w.s.low, u, v); \
  38. __w.ll; \
  39. })
  40. #endif
  41. long long __muldi3(long long u, long long v)
  42. {
  43. const DWunion uu = {.ll = u};
  44. const DWunion vv = {.ll = v};
  45. DWunion w = {.ll = __umulsidi3(uu.s.low, vv.s.low)};
  46. w.s.high += ((unsigned long) uu.s.low * (unsigned long) vv.s.high
  47. + (unsigned long) uu.s.high * (unsigned long) vv.s.low);
  48. return w.ll;
  49. }
  50. EXPORT_SYMBOL(__muldi3);