muldi3.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * U-Boot - muldi3.c contains routines for mult and div
  4. *
  5. */
  6. /* Generic function got from GNU gcc package, libgcc2.c */
  7. #ifndef SI_TYPE_SIZE
  8. #define SI_TYPE_SIZE 32
  9. #endif
  10. #define __ll_B (1L << (SI_TYPE_SIZE / 2))
  11. #define __ll_lowpart(t) ((USItype) (t) % __ll_B)
  12. #define __ll_highpart(t) ((USItype) (t) / __ll_B)
  13. #define BITS_PER_UNIT 8
  14. #if !defined(umul_ppmm)
  15. #define umul_ppmm(w1, w0, u, v) \
  16. do { \
  17. USItype __x0, __x1, __x2, __x3; \
  18. USItype __ul, __vl, __uh, __vh; \
  19. \
  20. __ul = __ll_lowpart(u); \
  21. __uh = __ll_highpart(u); \
  22. __vl = __ll_lowpart(v); \
  23. __vh = __ll_highpart(v); \
  24. \
  25. __x0 = (USItype) __ul * __vl; \
  26. __x1 = (USItype) __ul * __vh; \
  27. __x2 = (USItype) __uh * __vl; \
  28. __x3 = (USItype) __uh * __vh; \
  29. \
  30. __x1 += __ll_highpart(__x0); /* this can't give carry */\
  31. __x1 += __x2; /* but this indeed can */ \
  32. if (__x1 < __x2) /* did we get it? */ \
  33. __x3 += __ll_B; /* yes, add it in the proper pos. */ \
  34. \
  35. (w1) = __x3 + __ll_highpart(__x1); \
  36. (w0) = __ll_lowpart(__x1) * __ll_B + __ll_lowpart(__x0);\
  37. } while (0)
  38. #endif
  39. #if !defined(__umulsidi3)
  40. #define __umulsidi3(u, v) \
  41. ({DIunion __w; \
  42. umul_ppmm(__w.s.high, __w.s.low, u, v); \
  43. __w.ll; })
  44. #endif
  45. typedef unsigned int USItype __attribute__ ((mode(SI)));
  46. typedef int SItype __attribute__ ((mode(SI)));
  47. typedef int DItype __attribute__ ((mode(DI)));
  48. typedef int word_type __attribute__ ((mode(__word__)));
  49. struct DIstruct {
  50. SItype low, high;
  51. };
  52. typedef union {
  53. struct DIstruct s;
  54. DItype ll;
  55. } DIunion;
  56. DItype __muldi3(DItype u, DItype v)
  57. {
  58. DIunion w;
  59. DIunion uu, vv;
  60. uu.ll = u, vv.ll = v;
  61. /* panic("kernel panic for __muldi3"); */
  62. w.ll = __umulsidi3(uu.s.low, vv.s.low);
  63. w.s.high += ((USItype) uu.s.low * (USItype) vv.s.high
  64. + (USItype) uu.s.high * (USItype) vv.s.low);
  65. return w.ll;
  66. }