muldi3.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "libgcc.h"
  3. #define __ll_B ((UWtype) 1 << (W_TYPE_SIZE / 2))
  4. #define __ll_lowpart(t) ((UWtype) (t) & (__ll_B - 1))
  5. #define __ll_highpart(t) ((UWtype) (t) >> (W_TYPE_SIZE / 2))
  6. #define umul_ppmm(w1, w0, u, v) \
  7. do { \
  8. UWtype __x0, __x1, __x2, __x3; \
  9. UHWtype __ul, __vl, __uh, __vh; \
  10. __ul = __ll_lowpart(u); \
  11. __uh = __ll_highpart(u); \
  12. __vl = __ll_lowpart(v); \
  13. __vh = __ll_highpart(v); \
  14. __x0 = (UWtype) __ul * __vl; \
  15. __x1 = (UWtype) __ul * __vh; \
  16. __x2 = (UWtype) __uh * __vl; \
  17. __x3 = (UWtype) __uh * __vh; \
  18. __x1 += __ll_highpart(__x0); \
  19. __x1 += __x2; \
  20. if (__x1 < __x2) \
  21. __x3 += __ll_B; \
  22. (w1) = __x3 + __ll_highpart(__x1); \
  23. (w0) = __ll_lowpart(__x1) * __ll_B + __ll_lowpart(__x0); \
  24. } while (0)
  25. #define __umulsidi3(u, v) ( \
  26. { \
  27. DWunion __w; \
  28. umul_ppmm(__w.s.high, __w.s.low, u, v); \
  29. __w.ll; } \
  30. )
  31. DWtype __muldi3(DWtype u, DWtype v)
  32. {
  33. const DWunion uu = {.ll = u};
  34. const DWunion vv = {.ll = v};
  35. DWunion w = {.ll = __umulsidi3(uu.s.low, vv.s.low)};
  36. w.s.high += ((UWtype) uu.s.low * (UWtype) vv.s.high
  37. + (UWtype) uu.s.high * (UWtype) vv.s.low);
  38. return w.ll;
  39. }