divdi3.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /* 64-bit multiplication and division
  2. Copyright (C) 1989, 1992-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <endian.h>
  16. #include <stdlib.h>
  17. #include <bits/wordsize.h>
  18. #include <stdbit.h>
  19. #if __WORDSIZE != 32
  20. #error This is for 32-bit targets only
  21. #endif
  22. #define Wtype SItype
  23. #define HWtype SItype
  24. #define DWtype DItype
  25. #define UHWtype USItype
  26. #define UDWtype UDItype
  27. #include <gmp.h>
  28. #include <stdlib/gmp-impl.h>
  29. #if __BYTE_ORDER == __BIG_ENDIAN
  30. struct DWstruct { Wtype high, low;};
  31. #elif __BYTE_ORDER == __LITTLE_ENDIAN
  32. struct DWstruct { Wtype low, high;};
  33. #else
  34. #error Unhandled endianity
  35. #endif
  36. typedef union { struct DWstruct s; DWtype ll; } DWunion;
  37. /* Prototypes of exported functions. */
  38. extern DWtype __divdi3 (DWtype u, DWtype v);
  39. extern DWtype __moddi3 (DWtype u, DWtype v);
  40. extern UDWtype __udivdi3 (UDWtype u, UDWtype v);
  41. extern UDWtype __umoddi3 (UDWtype u, UDWtype v);
  42. static UDWtype
  43. __udivmoddi4 (UDWtype n, UDWtype d, UDWtype *rp)
  44. {
  45. DWunion ww;
  46. DWunion nn, dd;
  47. DWunion rr;
  48. UWtype d0, d1, n0, n1, n2;
  49. UWtype q0, q1;
  50. UWtype b, bm;
  51. nn.ll = n;
  52. dd.ll = d;
  53. d0 = dd.s.low;
  54. d1 = dd.s.high;
  55. n0 = nn.s.low;
  56. n1 = nn.s.high;
  57. #if !UDIV_NEEDS_NORMALIZATION
  58. if (d1 == 0)
  59. {
  60. if (d0 > n1)
  61. {
  62. /* 0q = nn / 0D */
  63. udiv_qrnnd (q0, n0, n1, n0, d0);
  64. q1 = 0;
  65. /* Remainder in n0. */
  66. }
  67. else
  68. {
  69. /* qq = NN / 0d */
  70. if (d0 == 0)
  71. d0 = 1 / d0; /* Divide intentionally by zero. */
  72. udiv_qrnnd (q1, n1, 0, n1, d0);
  73. udiv_qrnnd (q0, n0, n1, n0, d0);
  74. /* Remainder in n0. */
  75. }
  76. if (rp != 0)
  77. {
  78. rr.s.low = n0;
  79. rr.s.high = 0;
  80. *rp = rr.ll;
  81. }
  82. }
  83. #else /* UDIV_NEEDS_NORMALIZATION */
  84. if (d1 == 0)
  85. {
  86. if (d0 > n1)
  87. {
  88. /* 0q = nn / 0D */
  89. bm = stdc_leading_zeros (d0);
  90. if (bm != 0)
  91. {
  92. /* Normalize, i.e. make the most significant bit of the
  93. denominator set. */
  94. d0 = d0 << bm;
  95. n1 = (n1 << bm) | (n0 >> (W_TYPE_SIZE - bm));
  96. n0 = n0 << bm;
  97. }
  98. udiv_qrnnd (q0, n0, n1, n0, d0);
  99. q1 = 0;
  100. /* Remainder in n0 >> bm. */
  101. }
  102. else
  103. {
  104. /* qq = NN / 0d */
  105. if (d0 == 0)
  106. d0 = 1 / d0; /* Divide intentionally by zero. */
  107. bm = stdc_leading_zeros (d0);
  108. if (bm == 0)
  109. {
  110. /* From (n1 >= d0) /\ (the most significant bit of d0 is set),
  111. conclude (the most significant bit of n1 is set) /\ (the
  112. leading quotient digit q1 = 1).
  113. This special case is necessary, not an optimization.
  114. (Shifts counts of W_TYPE_SIZE are undefined.) */
  115. n1 -= d0;
  116. q1 = 1;
  117. }
  118. else
  119. {
  120. /* Normalize. */
  121. b = W_TYPE_SIZE - bm;
  122. d0 = d0 << bm;
  123. n2 = n1 >> b;
  124. n1 = (n1 << bm) | (n0 >> b);
  125. n0 = n0 << bm;
  126. udiv_qrnnd (q1, n1, n2, n1, d0);
  127. }
  128. /* n1 != d0... */
  129. udiv_qrnnd (q0, n0, n1, n0, d0);
  130. /* Remainder in n0 >> bm. */
  131. }
  132. if (rp != 0)
  133. {
  134. rr.s.low = n0 >> bm;
  135. rr.s.high = 0;
  136. *rp = rr.ll;
  137. }
  138. }
  139. #endif /* UDIV_NEEDS_NORMALIZATION */
  140. else
  141. {
  142. if (d1 > n1)
  143. {
  144. /* 00 = nn / DD */
  145. q0 = 0;
  146. q1 = 0;
  147. /* Remainder in n1n0. */
  148. if (rp != 0)
  149. {
  150. rr.s.low = n0;
  151. rr.s.high = n1;
  152. *rp = rr.ll;
  153. }
  154. }
  155. else
  156. {
  157. /* 0q = NN / dd */
  158. bm = stdc_leading_zeros (d1);
  159. if (bm == 0)
  160. {
  161. /* From (n1 >= d1) /\ (the most significant bit of d1 is set),
  162. conclude (the most significant bit of n1 is set) /\ (the
  163. quotient digit q0 = 0 or 1).
  164. This special case is necessary, not an optimization. */
  165. /* The condition on the next line takes advantage of that
  166. n1 >= d1 (true due to program flow). */
  167. if (n1 > d1 || n0 >= d0)
  168. {
  169. q0 = 1;
  170. sub_ddmmss (n1, n0, n1, n0, d1, d0);
  171. }
  172. else
  173. q0 = 0;
  174. q1 = 0;
  175. if (rp != 0)
  176. {
  177. rr.s.low = n0;
  178. rr.s.high = n1;
  179. *rp = rr.ll;
  180. }
  181. }
  182. else
  183. {
  184. UWtype m1, m0;
  185. /* Normalize. */
  186. b = W_TYPE_SIZE - bm;
  187. d1 = (d1 << bm) | (d0 >> b);
  188. d0 = d0 << bm;
  189. n2 = n1 >> b;
  190. n1 = (n1 << bm) | (n0 >> b);
  191. n0 = n0 << bm;
  192. udiv_qrnnd (q0, n1, n2, n1, d1);
  193. umul_ppmm (m1, m0, q0, d0);
  194. if (m1 > n1 || (m1 == n1 && m0 > n0))
  195. {
  196. q0--;
  197. sub_ddmmss (m1, m0, m1, m0, d1, d0);
  198. }
  199. q1 = 0;
  200. /* Remainder in (n1n0 - m1m0) >> bm. */
  201. if (rp != 0)
  202. {
  203. sub_ddmmss (n1, n0, n1, n0, m1, m0);
  204. rr.s.low = (n1 << b) | (n0 >> bm);
  205. rr.s.high = n1 >> bm;
  206. *rp = rr.ll;
  207. }
  208. }
  209. }
  210. }
  211. ww.s.low = q0;
  212. ww.s.high = q1;
  213. return ww.ll;
  214. }
  215. DWtype
  216. __divdi3 (DWtype u, DWtype v)
  217. {
  218. Wtype c = 0;
  219. DWtype w;
  220. if (u < 0)
  221. {
  222. c = ~c;
  223. u = -u;
  224. }
  225. if (v < 0)
  226. {
  227. c = ~c;
  228. v = -v;
  229. }
  230. w = __udivmoddi4 (u, v, NULL);
  231. if (c)
  232. w = -w;
  233. return w;
  234. }
  235. strong_alias (__divdi3, __divdi3_internal)
  236. DWtype
  237. __moddi3 (DWtype u, DWtype v)
  238. {
  239. Wtype c = 0;
  240. DWtype w;
  241. if (u < 0)
  242. {
  243. c = ~c;
  244. u = -u;
  245. }
  246. if (v < 0)
  247. v = -v;
  248. __udivmoddi4 (u, v, (UDWtype *) &w);
  249. if (c)
  250. w = -w;
  251. return w;
  252. }
  253. strong_alias (__moddi3, __moddi3_internal)
  254. UDWtype
  255. __udivdi3 (UDWtype u, UDWtype v)
  256. {
  257. return __udivmoddi4 (u, v, NULL);
  258. }
  259. strong_alias (__udivdi3, __udivdi3_internal)
  260. UDWtype
  261. __umoddi3 (UDWtype u, UDWtype v)
  262. {
  263. UDWtype w;
  264. __udivmoddi4 (u, v, &w);
  265. return w;
  266. }
  267. strong_alias (__umoddi3, __umoddi3_internal)
  268. /* We declare these with compat_symbol so that they are not visible at
  269. link time. Programs must use the functions from libgcc. */
  270. #ifdef SHARED
  271. # include <shlib-compat.h>
  272. compat_symbol (libc, __divdi3, __divdi3, GLIBC_2_0);
  273. compat_symbol (libc, __moddi3, __moddi3, GLIBC_2_0);
  274. compat_symbol (libc, __udivdi3, __udivdi3, GLIBC_2_0);
  275. compat_symbol (libc, __umoddi3, __umoddi3, GLIBC_2_0);
  276. #endif