muldi3.c 1.6 KB

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