div.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /* -*-comment-start: "//";comment-end:""-*-
  2. * GNU Mes --- Maxwell Equations of Software
  3. * Copyright © 2016,2017,2018,2019,2020 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  4. * Copyright © 2019 Danny Milosavljevic <dannym@scratchpost.org>
  5. * Copyright © 2020 Nathalie Kopaczewski <natkopa@gmail.com>
  6. *
  7. * This file is part of GNU Mes.
  8. *
  9. * GNU Mes is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or (at
  12. * your option) any later version.
  13. *
  14. * GNU Mes is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. #include <mes/lib.h>
  23. #include <stdint.h>
  24. #include <limits.h>
  25. #include <signal.h>
  26. struct ldiv_t
  27. {
  28. long quot;
  29. long rem;
  30. };
  31. int __raise (int);
  32. #if __TINYC__
  33. #define __raise(x) -1
  34. #endif
  35. void
  36. __mesabi_div0 (void)
  37. {
  38. if (__raise (SIGFPE) < 0) /* could not raise SIGFPE */
  39. {
  40. /* Fail in any way possible */
  41. unsigned char *x = (unsigned char *) 0;
  42. *x = 2;
  43. }
  44. }
  45. #define ULONG_HIGHBITMASK LONG_MIN
  46. #define ULONG_BITCOUNT (sizeof (unsigned long)*8)
  47. /** Compute the logarithm of base 2 of D. The result is rounded down.
  48. That is equal to the highest-index set bit in D.
  49. The idea is to shift D to the right in order to find the index i of the first most-significant digit > 0.
  50. The computation is done by bisection, for speed.
  51. Recurse:
  52. Two halves are determined of the remaining slice.
  53. The first half checked is the higher-significant half.
  54. If that higher-significant half is not zero, recurse on that one.
  55. Otherwise, recurse on the lower-significant half.
  56. Precondition: D > 0 */
  57. static unsigned int
  58. __mesabi_log2i (unsigned long D)
  59. {
  60. unsigned int n = ULONG_BITCOUNT;
  61. unsigned int i = 0U;
  62. unsigned long D1;
  63. while (n >= 2U)
  64. { /* while still two halves possible */
  65. n >>= 1U;
  66. /* D1: higher-significant half of D */
  67. D1 = D >> n;
  68. if (D1 > 0UL)
  69. {
  70. /* We know that the resulting index has to be in the higher-significant half.
  71. In that case, lower-significant half of D is superfluous for determination of i,
  72. therefore scroll and continue with higher-significant half. */
  73. D = D1;
  74. i += n;
  75. }
  76. }
  77. return i;
  78. }
  79. #if 0
  80. static void
  81. test_log2i (void)
  82. {
  83. assert (log2i (1) == 0);
  84. assert (log2i (1) == 0);
  85. assert (log2i (2) == 1);
  86. assert (log2i (3) == 1);
  87. assert (log2i (4) == 2);
  88. assert (log2i (5) == 2);
  89. assert (log2i (6) == 2);
  90. assert (log2i (7) == 2);
  91. assert (log2i (8) == 3);
  92. assert (log2i (9) == 3);
  93. assert (log2i (10) == 3);
  94. assert (log2i (11) == 3);
  95. assert (log2i (12) == 3);
  96. assert (log2i (13) == 3);
  97. assert (log2i (71) == 6);
  98. assert (log2i (72) == 6);
  99. assert (log2i (73) == 6);
  100. assert (log2i (74) == 6);
  101. assert (log2i (75) == 6);
  102. assert (log2i (99) == 6);
  103. assert (log2i (2147483648) == 31);
  104. assert (log2i (3221225471) == 31);
  105. assert (log2i (4294967294) == 31);
  106. assert (log2i (4294967295) == 31);
  107. }
  108. #endif
  109. /** Perform unsigned integer division of N by D; store the remainder
  110. into *REMAINDER; return the quotient.
  111. This is currently implemented as long division.
  112. R is the remainder. R >= 0. R starts at N.
  113. QUOTIENT is built up bit by bit starting at the most significant bit [*].
  114. Values D', starting at D << ULONG_BITCOUNT [*], going down to 1,
  115. divided by 2 each time, are iterated over, doing: If R >= D',
  116. subtract D' from R, and append new LSB 1 to the QUOTIENT.
  117. Otherwise, subtract 0 from R (implicit), and append new LSB 0 to the
  118. QUOTIENT (0 is the implicit default).
  119. [*] As a special consideration for C throwing away bits when
  120. left-shifting, D' starts at the highest value that will not lose
  121. bits in this way instead. (ULONG_BITCOUNT - log2i(D) - 1) is
  122. the number of leading zeroes in D in binary radix.
  123. Precondition: D > 0 */
  124. static unsigned long
  125. __mesabi_uldiv1 (unsigned long N, unsigned long D, unsigned long *remainder)
  126. {
  127. // Note: __mesabi_log2i(D) < ULONG_BITCOUNT
  128. unsigned int j = ULONG_BITCOUNT - __mesabi_log2i (D); /* Note: Or j = __mesabi_log2i(N) + 1 - __mesabi_log2i(D) */
  129. // Note: assert(j - 1 == __builtin_clzl(D)); on GCC
  130. unsigned long quotient = 0UL;
  131. unsigned long R = N;
  132. for (D <<= (j - 1); j > 0U; --j, D >>= 1U)
  133. {
  134. quotient <<= 1U;
  135. if (R >= D)
  136. {
  137. R -= D;
  138. quotient |= 1UL;
  139. }
  140. }
  141. *remainder = R;
  142. return quotient;
  143. }
  144. #if 0
  145. static void
  146. assert_uldiv (unsigned long N, unsigned long D, unsigned long expected_quotient,
  147. unsigned long expected_remainder)
  148. {
  149. unsigned long remainder;
  150. unsigned long quotient;
  151. quotient = uldiv (N, D, &remainder);
  152. printf ("%lu/%lu = %lu;%lu\n", N, D, quotient, remainder);
  153. assert (quotient == expected_quotient);
  154. assert (remainder == expected_remainder);
  155. }
  156. static void
  157. test_uldiv (void)
  158. {
  159. //assert_uldiv(0, 0, 0, 0);
  160. assert_uldiv (0, 1, 0, 0);
  161. assert_uldiv (1, 1, 1, 0);
  162. assert_uldiv (72, 5, 14, 2);
  163. assert_uldiv (0xffffffff, 1, 0xffffffff, 0);
  164. assert_uldiv (0xffffffff, 2, 0x7fffffff, 1);
  165. }
  166. #endif
  167. /* Compare gcc: __udivmoddi4 */
  168. unsigned long
  169. __mesabi_uldiv (unsigned long a, unsigned long b, unsigned long *remainder)
  170. {
  171. unsigned long tmp;
  172. if (!remainder)
  173. remainder = &tmp;
  174. *remainder = 0;
  175. switch (b)
  176. {
  177. case 64UL:
  178. *remainder = a & 63UL;
  179. return a >> 6UL;
  180. case 32UL:
  181. *remainder = a & 31UL;
  182. return a >> 5UL;
  183. case 16UL:
  184. *remainder = a & 15UL;
  185. return a >> 4UL;
  186. case 8UL:
  187. *remainder = a & 7UL;
  188. return a >> 3UL;
  189. case 4UL:
  190. *remainder = a & 3UL;
  191. return a >> 2UL;
  192. case 2UL:
  193. *remainder = a & 1UL;
  194. return a >> 1UL;
  195. case 1UL:
  196. *remainder = 0;
  197. return a;
  198. case 0UL:
  199. __mesabi_div0 ();
  200. return 0UL;
  201. default:
  202. return __mesabi_uldiv1 (a, b, remainder);
  203. }
  204. }
  205. /* Note: Rounds towards zero.
  206. Maintainer: Be careful to satisfy quot * b + rem == a.
  207. That means that rem can be negative. */
  208. void
  209. __mesabi_ldiv (long a, long b, struct ldiv_t *result)
  210. {
  211. int negate_result = (a < 0) ^ (b < 0);
  212. if (b == LONG_MIN)
  213. __mesabi_div0 ();
  214. if (a != LONG_MIN)
  215. {
  216. int negative_a = (a < 0);
  217. if (negative_a)
  218. a = -a;
  219. if (b < 0)
  220. b = -b;
  221. result->quot = __mesabi_uldiv (a, b, &result->rem);
  222. if (negate_result)
  223. result->quot = -result->quot;
  224. if (negative_a)
  225. result->rem = -result->rem;
  226. }
  227. else
  228. {
  229. result->rem = 0;
  230. if (b < 0)
  231. b = -b;
  232. if (b == 1)
  233. {
  234. result->quot = a;
  235. /* Since result->quot is already negative, don't negate it again. */
  236. negate_result = !negate_result;
  237. }
  238. else if (b == 0)
  239. __mesabi_div0 ();
  240. else
  241. {
  242. long x;
  243. for (x = 0; a <= -b; a += b)
  244. ++x;
  245. result->rem = a; /* negative */
  246. result->quot = x;
  247. }
  248. if (negate_result)
  249. result->quot = -result->quot;
  250. }
  251. }
  252. long
  253. __mesabi_imod (long a, long b)
  254. {
  255. struct ldiv_t result;
  256. __mesabi_ldiv (a, b, &result);
  257. return result.rem;
  258. }
  259. long
  260. __mesabi_idiv (long a, long b)
  261. {
  262. struct ldiv_t result;
  263. __mesabi_ldiv (a, b, &result);
  264. return result.quot;
  265. }
  266. unsigned long
  267. __mesabi_umod (unsigned long a, unsigned long b)
  268. {
  269. unsigned long result;
  270. __mesabi_uldiv (a, b, &result);
  271. return result;
  272. }
  273. unsigned long
  274. __mesabi_udiv (unsigned long a, unsigned long b)
  275. {
  276. unsigned long result;
  277. return __mesabi_uldiv (a, b, &result);
  278. }