lib2mul.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* libgcc routines for MSP430
  2. Copyright (C) 2005-2022 Free Software Foundation, Inc.
  3. Contributed by Red Hat.
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU General Public License as published
  7. by the Free Software Foundation; either version 3, or (at your
  8. option) any later version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT
  10. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  12. License for more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. typedef unsigned int uint32_type __attribute__ ((mode (SI)));
  21. typedef unsigned int uint16_type __attribute__ ((mode (HI)));
  22. typedef unsigned int uint08_type __attribute__ ((mode (QI)));
  23. #define C3B(a,b,c) a##b##c
  24. #define C3(a,b,c) C3B(a,b,c)
  25. #if defined (MUL_NONE) || defined (MUL_16)
  26. /* __muldi3 must be excluded from libgcc.a to prevent multiple-definition
  27. errors for the hwmult configurations that have their own definition.
  28. However, for MUL_NONE and MUL_16, the software version is still required, so
  29. the necessary preprocessed output from libgcc2.c to compile that
  30. software version of __muldi3 is below. */
  31. typedef unsigned int USItype __attribute__ ((mode (SI)));
  32. typedef int DItype __attribute__ ((mode (DI)));
  33. typedef int SItype __attribute__ ((mode (SI)));
  34. struct DWstruct {SItype low, high;};
  35. typedef union
  36. {
  37. struct DWstruct s;
  38. DItype ll;
  39. } DWunion;
  40. DItype __muldi3 (DItype u, DItype v);
  41. DItype
  42. __muldi3 (DItype u, DItype v)
  43. {
  44. const DWunion uu = {.ll = u};
  45. const DWunion vv = {.ll = v};
  46. /* The next block of code is expanded from the following line:
  47. DWunion w = {.ll = __umulsidi3 (uu.s.low, vv.s.low)}; */
  48. DWunion w;
  49. USItype __x0, __x1, __x2, __x3;
  50. USItype __ul, __vl, __uh, __vh;
  51. __ul = ((USItype) (uu.s.low) & (((USItype) 1 << ((4 * 8) / 2)) - 1));
  52. __uh = ((USItype) (uu.s.low) >> ((4 * 8) / 2));
  53. __vl = ((USItype) (vv.s.low) & (((USItype) 1 << ((4 * 8) / 2)) - 1));
  54. __vh = ((USItype) (vv.s.low) >> ((4 * 8) / 2));
  55. __x0 = (USItype) __ul * __vl;
  56. __x1 = (USItype) __ul * __vh;
  57. __x2 = (USItype) __uh * __vl;
  58. __x3 = (USItype) __uh * __vh;
  59. __x1 += ((USItype) (__x0) >> ((4 * 8) / 2));
  60. __x1 += __x2;
  61. if (__x1 < __x2)
  62. __x3 += ((USItype) 1 << ((4 * 8) / 2));
  63. (w.s.high) = __x3 + ((USItype) (__x1) >> ((4 * 8) / 2));
  64. (w.s.low) = ((USItype) (__x1) & (((USItype) 1 << ((4 * 8) / 2)) - 1))
  65. * ((USItype) 1 << ((4 * 8) / 2))
  66. + ((USItype) (__x0) & (((USItype) 1 << ((4 * 8) / 2)) - 1));
  67. w.s.high += ((USItype) uu.s.low * (USItype) vv.s.high
  68. + (USItype) uu.s.high * (USItype) vv.s.low);
  69. return w.ll;
  70. }
  71. #endif
  72. #if defined MUL_NONE
  73. /* The software multiply library needs __mspabi_mpyll. */
  74. #undef UINT_TYPE
  75. #undef BITS_MINUS_1
  76. #undef NAME_MODE
  77. #define UINT_TYPE uint32_type
  78. #define BITS_MINUS_1 31
  79. #define NAME_MODE si
  80. #include "msp430-mul.h"
  81. #elif defined MUL_16
  82. /* The 16-bit multiply library needs a software version of SI->DI widening
  83. multiplication. */
  84. signed long long
  85. __mspabi_mpysll (signed long a, signed long b)
  86. {
  87. return (signed long long) a * (signed long long) b;
  88. }
  89. unsigned long long
  90. __mspabi_mpyull (unsigned long a, unsigned long b)
  91. {
  92. return (unsigned long long) a * (unsigned long long) b;
  93. }
  94. #else
  95. #undef UINT_TYPE
  96. #undef BITS_MINUS_1
  97. #undef NAME_MODE
  98. #define UINT_TYPE uint08_type
  99. #define BITS_MINUS_1 7
  100. #define NAME_MODE qi
  101. #include "msp430-mul.h"
  102. #endif /* MUL_NONE */