lib2-divmod.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* Copyright (C) 2012-2022 Free Software Foundation, Inc.
  2. Contributed by Altera and Mentor Graphics, Inc.
  3. This file is free software; you can redistribute it and/or modify it
  4. under the terms of the GNU General Public License as published by the
  5. Free Software Foundation; either version 3, or (at your option) any
  6. later version.
  7. This file is distributed in the hope that it will be useful, but
  8. WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. General Public License for more details.
  11. Under Section 7 of GPL version 3, you are granted additional
  12. permissions described in the GCC Runtime Library Exception, version
  13. 3.1, as published by the Free Software Foundation.
  14. You should have received a copy of the GNU General Public License and
  15. a copy of the GCC Runtime Library Exception along with this program;
  16. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  17. <http://www.gnu.org/licenses/>. */
  18. #include "lib2-gcn.h"
  19. /* 32-bit SI divide and modulo as used in gcn. */
  20. static USItype
  21. udivmodsi4 (USItype num, USItype den, word_type modwanted)
  22. {
  23. USItype bit = 1;
  24. USItype res = 0;
  25. while (den < num && bit && !(den & (1L<<31)))
  26. {
  27. den <<=1;
  28. bit <<=1;
  29. }
  30. while (bit)
  31. {
  32. if (num >= den)
  33. {
  34. num -= den;
  35. res |= bit;
  36. }
  37. bit >>=1;
  38. den >>=1;
  39. }
  40. if (modwanted)
  41. return num;
  42. return res;
  43. }
  44. SItype
  45. __divsi3 (SItype a, SItype b)
  46. {
  47. word_type neg = 0;
  48. SItype res;
  49. if (a < 0)
  50. {
  51. a = -a;
  52. neg = !neg;
  53. }
  54. if (b < 0)
  55. {
  56. b = -b;
  57. neg = !neg;
  58. }
  59. res = udivmodsi4 (a, b, 0);
  60. if (neg)
  61. res = -res;
  62. return res;
  63. }
  64. SItype
  65. __modsi3 (SItype a, SItype b)
  66. {
  67. word_type neg = 0;
  68. SItype res;
  69. if (a < 0)
  70. {
  71. a = -a;
  72. neg = 1;
  73. }
  74. if (b < 0)
  75. b = -b;
  76. res = udivmodsi4 (a, b, 1);
  77. if (neg)
  78. res = -res;
  79. return res;
  80. }
  81. USItype
  82. __udivsi3 (USItype a, USItype b)
  83. {
  84. return udivmodsi4 (a, b, 0);
  85. }
  86. USItype
  87. __umodsi3 (USItype a, USItype b)
  88. {
  89. return udivmodsi4 (a, b, 1);
  90. }