umodsi3.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* Generic unsigned 32 bit modulo implementation.
  2. Copyright (C) 2009-2022 Free Software Foundation, Inc.
  3. Contributed by Embecosm on behalf of Adapteva, Inc.
  4. This file is part of GCC.
  5. This file is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU General Public License as published by the
  7. Free Software Foundation; either version 3, or (at your option) any
  8. later version.
  9. This file is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public 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 union { unsigned int i; float f; } fu;
  21. unsigned int __umodsi3 (unsigned int a, unsigned int b);
  22. unsigned int
  23. __umodsi3 (unsigned int a, unsigned int b)
  24. {
  25. unsigned int d, t, s0, s1, s2, r0, r1;
  26. fu u0, u1, u2, u1b, u2b;
  27. if (b > a)
  28. return a;
  29. /* Compute difference in number of bits in S0. */
  30. u0.i = 0x40000000;
  31. u1b.i = u2b.i = u0.i;
  32. u1.i = a;
  33. u2.i = b;
  34. u1.i = a | u0.i;
  35. t = 0x4b800000 | ((a >> 23) & 0xffff);
  36. if (a >> 23)
  37. {
  38. u1.i = t;
  39. u1b.i = 0x4b800000;
  40. }
  41. u2.i = b | u0.i;
  42. t = 0x4b800000 | ((b >> 23) & 0xffff);
  43. if (b >> 23)
  44. {
  45. u2.i = t;
  46. u2b.i = 0x4b800000;
  47. }
  48. u1.f = u1.f - u1b.f;
  49. u2.f = u2.f - u2b.f;
  50. s1 = u1.i >> 23;
  51. s2 = u2.i >> 23;
  52. s0 = s1 - s2;
  53. #define STEP(n) case n: d = b << n; t = a - d; if (t <= a) a = t;
  54. switch (s0)
  55. {
  56. STEP (31)
  57. STEP (30)
  58. STEP (29)
  59. STEP (28)
  60. STEP (27)
  61. STEP (26)
  62. STEP (25)
  63. STEP (24)
  64. STEP (23)
  65. STEP (22)
  66. STEP (21)
  67. STEP (20)
  68. STEP (19)
  69. STEP (18)
  70. STEP (17)
  71. STEP (16)
  72. STEP (15)
  73. STEP (14)
  74. STEP (13)
  75. STEP (12)
  76. STEP (11)
  77. STEP (10)
  78. STEP (9)
  79. STEP (8)
  80. STEP (7)
  81. STEP (6)
  82. STEP (5)
  83. STEP (4)
  84. STEP (3)
  85. STEP (2)
  86. STEP (1)
  87. STEP (0)
  88. }
  89. return a;
  90. }