muldi3.c 1.9 KB

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