muldi3.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* muldi3.c extracted from gcc-2.7.2.3/libgcc2.c and
  2. gcc-2.7.2.3/longlong.h which is: */
  3. /* Copyright (C) 1989, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
  4. This file is part of GNU CC.
  5. GNU CC is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. GNU CC is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU CC; see the file COPYING. If not, write to
  15. the Free Software Foundation, 59 Temple Place - Suite 330,
  16. Boston, MA 02111-1307, USA. */
  17. #if defined(__mc68020__) || defined(__mc68030__) || \
  18. defined(__mc68040__) || defined(__mc68060__) || defined(__mcpu32__)
  19. #define umul_ppmm(w1, w0, u, v) \
  20. __asm__ ("mulu%.l %3,%1:%0" \
  21. : "=d" ((USItype)(w0)), \
  22. "=d" ((USItype)(w1)) \
  23. : "%0" ((USItype)(u)), \
  24. "dmi" ((USItype)(v)))
  25. #else
  26. #define SI_TYPE_SIZE 32
  27. #define __BITS4 (SI_TYPE_SIZE / 4)
  28. #define __ll_B (1L << (SI_TYPE_SIZE / 2))
  29. #define __ll_lowpart(t) ((USItype) (t) % __ll_B)
  30. #define __ll_highpart(t) ((USItype) (t) / __ll_B)
  31. #define umul_ppmm(w1, w0, u, v) \
  32. do { \
  33. USItype __x0, __x1, __x2, __x3; \
  34. USItype __ul, __vl, __uh, __vh; \
  35. \
  36. __ul = __ll_lowpart (u); \
  37. __uh = __ll_highpart (u); \
  38. __vl = __ll_lowpart (v); \
  39. __vh = __ll_highpart (v); \
  40. \
  41. __x0 = (USItype) __ul * __vl; \
  42. __x1 = (USItype) __ul * __vh; \
  43. __x2 = (USItype) __uh * __vl; \
  44. __x3 = (USItype) __uh * __vh; \
  45. \
  46. __x1 += __ll_highpart (__x0);/* this can't give carry */ \
  47. __x1 += __x2; /* but this indeed can */ \
  48. if (__x1 < __x2) /* did we get it? */ \
  49. __x3 += __ll_B; /* yes, add it in the proper pos. */ \
  50. \
  51. (w1) = __x3 + __ll_highpart (__x1); \
  52. (w0) = __ll_lowpart (__x1) * __ll_B + __ll_lowpart (__x0); \
  53. } while (0)
  54. #endif
  55. #define __umulsidi3(u, v) \
  56. ({DIunion __w; \
  57. umul_ppmm (__w.s.high, __w.s.low, u, v); \
  58. __w.ll; })
  59. typedef int SItype __attribute__ ((mode (SI)));
  60. typedef unsigned int USItype __attribute__ ((mode (SI)));
  61. typedef int DItype __attribute__ ((mode (DI)));
  62. typedef int word_type __attribute__ ((mode (__word__)));
  63. struct DIstruct {SItype high, low;};
  64. typedef union
  65. {
  66. struct DIstruct s;
  67. DItype ll;
  68. } DIunion;
  69. DItype
  70. __muldi3 (DItype u, DItype v)
  71. {
  72. DIunion w;
  73. DIunion uu, vv;
  74. uu.ll = u,
  75. vv.ll = v;
  76. w.ll = __umulsidi3 (uu.s.low, vv.s.low);
  77. w.s.high += ((USItype) uu.s.low * (USItype) vv.s.high
  78. + (USItype) uu.s.high * (USItype) vv.s.low);
  79. return w.ll;
  80. }