rl78-divmod.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* libgcc routines for RL78
  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. UINT_TYPE C3(udivmod,NAME_MODE,4) (UINT_TYPE, UINT_TYPE, word_type);
  21. SINT_TYPE C3(__div,NAME_MODE,3) (SINT_TYPE, SINT_TYPE);
  22. SINT_TYPE C3(__mod,NAME_MODE,3) (SINT_TYPE, SINT_TYPE);
  23. UINT_TYPE C3(__udiv,NAME_MODE,3) (UINT_TYPE, UINT_TYPE);
  24. UINT_TYPE C3(__umod,NAME_MODE,3) (UINT_TYPE, UINT_TYPE);
  25. UINT_TYPE
  26. C3(udivmod,NAME_MODE,4) (UINT_TYPE num, UINT_TYPE den, word_type modwanted)
  27. {
  28. UINT_TYPE bit = 1;
  29. UINT_TYPE res = 0;
  30. while (den < num && bit && !(den & (1L << BITS_MINUS_1)))
  31. {
  32. den <<= 1;
  33. bit <<= 1;
  34. }
  35. while (bit)
  36. {
  37. if (num >= den)
  38. {
  39. num -= den;
  40. res |= bit;
  41. }
  42. bit >>= 1;
  43. den >>= 1;
  44. }
  45. if (modwanted)
  46. return num;
  47. return res;
  48. }
  49. SINT_TYPE
  50. C3(__div,NAME_MODE,3) (SINT_TYPE a, SINT_TYPE b)
  51. {
  52. word_type neg = 0;
  53. SINT_TYPE res;
  54. if (a < 0)
  55. {
  56. a = -a;
  57. neg = !neg;
  58. }
  59. if (b < 0)
  60. {
  61. b = -b;
  62. neg = !neg;
  63. }
  64. res = C3(udivmod,NAME_MODE,4) (a, b, 0);
  65. if (neg)
  66. res = -res;
  67. return res;
  68. }
  69. SINT_TYPE
  70. C3(__mod,NAME_MODE,3) (SINT_TYPE a, SINT_TYPE b)
  71. {
  72. word_type neg = 0;
  73. SINT_TYPE res;
  74. if (a < 0)
  75. {
  76. a = -a;
  77. neg = 1;
  78. }
  79. if (b < 0)
  80. b = -b;
  81. res = C3(udivmod,NAME_MODE,4) (a, b, 1);
  82. if (neg)
  83. res = -res;
  84. return res;
  85. }
  86. UINT_TYPE
  87. C3(__udiv,NAME_MODE,3) (UINT_TYPE a, UINT_TYPE b)
  88. {
  89. return C3(udivmod,NAME_MODE,4) (a, b, 0);
  90. }
  91. UINT_TYPE
  92. C3(__umod,NAME_MODE,3) (UINT_TYPE a, UINT_TYPE b)
  93. {
  94. return C3(udivmod,NAME_MODE,4) (a, b, 1);
  95. }