muldi3.c 2.6 KB

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