muldi3.c 1.7 KB

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