divmod.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * This file is part of GNU CC.
  3. *
  4. * GNU CC is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published
  6. * by the Free Software Foundation; either version 2, or (at your
  7. * option) any later version.
  8. *
  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. *
  14. * You should have received a copy of the GNU General Public
  15. * License along with GNU CC; see the file COPYING. If not, write
  16. * to the Free Software Foundation, 59 Temple Place - Suite 330,
  17. * Boston, MA 02111-1307, USA.
  18. */
  19. #include "math.h"
  20. USItype udivmodsi4 (USItype num, USItype den, word_type modwanted)
  21. {
  22. USItype bit = 1;
  23. USItype res = 0;
  24. while (den < num && bit && !(den & (1L << 31))) {
  25. den <<= 1;
  26. bit <<= 1;
  27. }
  28. while (bit) {
  29. if (num >= den) {
  30. num -= den;
  31. res |= bit;
  32. }
  33. bit >>= 1;
  34. den >>= 1;
  35. }
  36. if (modwanted)
  37. return num;
  38. return res;
  39. }
  40. SItype __divsi3 (SItype a, SItype b)
  41. {
  42. word_type neg = 0;
  43. SItype res;
  44. if (a < 0) {
  45. a = -a;
  46. neg = !neg;
  47. }
  48. if (b < 0) {
  49. b = -b;
  50. neg = !neg;
  51. }
  52. res = udivmodsi4 (a, b, 0);
  53. if (neg)
  54. res = -res;
  55. return res;
  56. }
  57. SItype __modsi3 (SItype a, SItype b)
  58. {
  59. word_type neg = 0;
  60. SItype res;
  61. if (a < 0) {
  62. a = -a;
  63. neg = 1;
  64. }
  65. if (b < 0)
  66. b = -b;
  67. res = udivmodsi4 (a, b, 1);
  68. if (neg)
  69. res = -res;
  70. return res;
  71. }
  72. SItype __udivsi3 (SItype a, SItype b)
  73. {
  74. return udivmodsi4 (a, b, 0);
  75. }
  76. SItype __umodsi3 (SItype a, SItype b)
  77. {
  78. return udivmodsi4 (a, b, 1);
  79. }