muldi3.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * muldi3.c extracted from gcc-2.7.2.3/libgcc2.c and
  4. * gcc-2.7.2.3/longlong.h
  5. *
  6. * Copyright (C) 1989, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
  7. */
  8. #define SI_TYPE_SIZE 32
  9. #define __BITS4 (SI_TYPE_SIZE / 4)
  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 umul_ppmm(w1, w0, u, v) \
  14. do { \
  15. USItype __x0, __x1, __x2, __x3; \
  16. USItype __ul, __vl, __uh, __vh; \
  17. \
  18. __ul = __ll_lowpart (u); \
  19. __uh = __ll_highpart (u); \
  20. __vl = __ll_lowpart (v); \
  21. __vh = __ll_highpart (v); \
  22. \
  23. __x0 = (USItype) __ul * __vl; \
  24. __x1 = (USItype) __ul * __vh; \
  25. __x2 = (USItype) __uh * __vl; \
  26. __x3 = (USItype) __uh * __vh; \
  27. \
  28. __x1 += __ll_highpart (__x0);/* this can't give carry */ \
  29. __x1 += __x2; /* but this indeed can */ \
  30. if (__x1 < __x2) /* did we get it? */ \
  31. __x3 += __ll_B; /* yes, add it in the proper pos. */ \
  32. \
  33. (w1) = __x3 + __ll_highpart (__x1); \
  34. (w0) = __ll_lowpart (__x1) * __ll_B + __ll_lowpart (__x0); \
  35. } while (0)
  36. #define __umulsidi3(u, v) \
  37. ({DIunion __w; \
  38. umul_ppmm (__w.s.high, __w.s.low, u, v); \
  39. __w.ll; })
  40. typedef int SItype __attribute__ ((mode (SI)));
  41. typedef unsigned int USItype __attribute__ ((mode (SI)));
  42. typedef int DItype __attribute__ ((mode (DI)));
  43. typedef int word_type __attribute__ ((mode (__word__)));
  44. struct DIstruct {SItype high, low;};
  45. typedef union
  46. {
  47. struct DIstruct s;
  48. DItype ll;
  49. } DIunion;
  50. DItype __muldi3 (DItype u, DItype v)
  51. {
  52. DIunion w;
  53. DIunion uu, vv;
  54. uu.ll = u,
  55. vv.ll = v;
  56. w.ll = __umulsidi3 (uu.s.low, vv.s.low);
  57. w.s.high += ((USItype) uu.s.low * (USItype) vv.s.high
  58. + (USItype) uu.s.high * (USItype) vv.s.low);
  59. return w.ll;
  60. }