muldi3.c 2.0 KB

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