muldi3.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include <linux/module.h>
  2. #include "libgcc.h"
  3. #define DWtype long long
  4. #define UWtype unsigned long
  5. #define UHWtype unsigned short
  6. #define W_TYPE_SIZE 32
  7. #define __ll_B ((UWtype) 1 << (W_TYPE_SIZE / 2))
  8. #define __ll_lowpart(t) ((UWtype) (t) & (__ll_B - 1))
  9. #define __ll_highpart(t) ((UWtype) (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. UWtype __x0, __x1, __x2, __x3; \
  15. UHWtype __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 = (UWtype) __ul * __vl; \
  23. __x1 = (UWtype) __ul * __vh; \
  24. __x2 = (UWtype) __uh * __vl; \
  25. __x3 = (UWtype) __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. DWtype __muldi3(DWtype u, DWtype 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 += ((UWtype) uu.s.low * (UWtype) vv.s.high
  49. + (UWtype) uu.s.high * (UWtype) vv.s.low);
  50. return w.ll;
  51. }
  52. EXPORT_SYMBOL(__muldi3);