lib2-divmod-hi.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. /* 16-bit HI divide and modulo as used in gcn. */
  20. static UHItype
  21. udivmodhi4 (UHItype num, UHItype den, word_type modwanted)
  22. {
  23. UHItype bit = 1;
  24. UHItype res = 0;
  25. while (den < num && bit && !(den & (1L<<15)))
  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. HItype
  45. __divhi3 (HItype a, HItype b)
  46. {
  47. word_type neg = 0;
  48. HItype 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 = udivmodhi4 (a, b, 0);
  60. if (neg)
  61. res = -res;
  62. return res;
  63. }
  64. HItype
  65. __modhi3 (HItype a, HItype b)
  66. {
  67. word_type neg = 0;
  68. HItype res;
  69. if (a < 0)
  70. {
  71. a = -a;
  72. neg = 1;
  73. }
  74. if (b < 0)
  75. b = -b;
  76. res = udivmodhi4 (a, b, 1);
  77. if (neg)
  78. res = -res;
  79. return res;
  80. }
  81. UHItype
  82. __udivhi3 (UHItype a, UHItype b)
  83. {
  84. return udivmodhi4 (a, b, 0);
  85. }
  86. UHItype
  87. __umodhi3 (UHItype a, UHItype b)
  88. {
  89. return udivmodhi4 (a, b, 1);
  90. }