muldi3.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * U-boot - muldi3.c contains routines for mult and div
  3. *
  4. * Copyright (c) 2005-2007 Analog Devices Inc.
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
  22. * MA 02110-1301 USA
  23. */
  24. /* Generic function got from GNU gcc package, libgcc2.c */
  25. #ifndef SI_TYPE_SIZE
  26. #define SI_TYPE_SIZE 32
  27. #endif
  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 BITS_PER_UNIT 8
  32. #if !defined (umul_ppmm)
  33. #define umul_ppmm(w1, w0, u, v) \
  34. do { \
  35. USItype __x0, __x1, __x2, __x3; \
  36. USItype __ul, __vl, __uh, __vh; \
  37. \
  38. __ul = __ll_lowpart (u); \
  39. __uh = __ll_highpart (u); \
  40. __vl = __ll_lowpart (v); \
  41. __vh = __ll_highpart (v); \
  42. \
  43. __x0 = (USItype) __ul * __vl; \
  44. __x1 = (USItype) __ul * __vh; \
  45. __x2 = (USItype) __uh * __vl; \
  46. __x3 = (USItype) __uh * __vh; \
  47. \
  48. __x1 += __ll_highpart (__x0);/* this can't give carry */ \
  49. __x1 += __x2; /* but this indeed can */ \
  50. if (__x1 < __x2) /* did we get it? */ \
  51. __x3 += __ll_B; /* yes, add it in the proper pos. */ \
  52. \
  53. (w1) = __x3 + __ll_highpart (__x1); \
  54. (w0) = __ll_lowpart (__x1) * __ll_B + __ll_lowpart (__x0); \
  55. } while (0)
  56. #endif
  57. #if !defined (__umulsidi3)
  58. #define __umulsidi3(u, v) \
  59. ({DIunion __w; \
  60. umul_ppmm (__w.s.high, __w.s.low, u, v); \
  61. __w.ll; })
  62. #endif
  63. typedef unsigned int USItype __attribute__ ((mode(SI)));
  64. typedef int SItype __attribute__ ((mode(SI)));
  65. typedef int DItype __attribute__ ((mode(DI)));
  66. typedef int word_type __attribute__ ((mode(__word__)));
  67. struct DIstruct {
  68. SItype low, high;
  69. };
  70. typedef union {
  71. struct DIstruct s;
  72. DItype ll;
  73. } DIunion;
  74. DItype __muldi3(DItype u, DItype v)
  75. {
  76. DIunion w;
  77. DIunion uu, vv;
  78. uu.ll = u, vv.ll = v;
  79. /* panic("kernel panic for __muldi3"); */
  80. w.ll = __umulsidi3(uu.s.low, vv.s.low);
  81. w.s.high += ((USItype) uu.s.low * (USItype) vv.s.high
  82. + (USItype) uu.s.high * (USItype) vv.s.low);
  83. return w.ll;
  84. }