srfi-60.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /* srfi-60.c --- Integers as Bits
  2. Copyright 2005-2006,2008,2010,2014,2018,2021,2022
  3. Free Software Foundation, Inc.
  4. This file is part of Guile.
  5. Guile is free software: you can redistribute it and/or modify it
  6. under the terms of the GNU Lesser General Public License as published
  7. by the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. Guile is distributed in the hope that it will be useful, but WITHOUT
  10. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  12. License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with Guile. If not, see
  15. <https://www.gnu.org/licenses/>. */
  16. #ifdef HAVE_CONFIG_H
  17. # include <config.h>
  18. #endif
  19. #include "boolean.h"
  20. #include "eq.h"
  21. #include "extensions.h"
  22. #include "gsubr.h"
  23. #include "integers.h"
  24. #include "list.h"
  25. #include "numbers.h"
  26. #include "pairs.h"
  27. #include "version.h"
  28. #include "srfi-60.h"
  29. SCM_DEFINE (scm_srfi60_log2_binary_factors, "log2-binary-factors", 1, 0, 0,
  30. (SCM n),
  31. "Return a count of how many factors of 2 are present in @var{n}.\n"
  32. "This is also the bit index of the lowest 1 bit in @var{n}. If\n"
  33. "@var{n} is 0, the return is @math{-1}.\n"
  34. "\n"
  35. "@example\n"
  36. "(log2-binary-factors 6) @result{} 1\n"
  37. "(log2-binary-factors -8) @result{} 3\n"
  38. "@end example")
  39. #define FUNC_NAME s_scm_srfi60_log2_binary_factors
  40. {
  41. SCM ret = SCM_EOL;
  42. if (SCM_I_INUMP (n))
  43. return scm_integer_scan1_i (SCM_I_INUM (n));
  44. else if (SCM_BIGP (n))
  45. return scm_integer_scan1_z (scm_bignum (n));
  46. else
  47. SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
  48. return ret;
  49. }
  50. #undef FUNC_NAME
  51. SCM_DEFINE (scm_srfi60_copy_bit, "copy-bit", 3, 0, 0,
  52. (SCM index, SCM n, SCM newbit),
  53. "Return @var{n} with the bit at @var{index} set according to\n"
  54. "@var{newbit}. @var{newbit} should be @code{#t} to set the bit\n"
  55. "to 1, or @code{#f} to set it to 0. Bits other than at\n"
  56. "@var{index} are unchanged in the return.\n"
  57. "\n"
  58. "@example\n"
  59. "(copy-bit 1 #b0101 #t) @result{} 7\n"
  60. "@end example")
  61. #define FUNC_NAME s_scm_srfi60_copy_bit
  62. {
  63. uintptr_t ii;
  64. int bb;
  65. ii = scm_to_uintptr_t (index);
  66. bb = scm_to_bool (newbit);
  67. if (SCM_I_INUMP (n))
  68. {
  69. if (scm_integer_logbit_ui (ii, SCM_I_INUM (n)) == bb)
  70. return n;
  71. }
  72. else if (SCM_BIGP (n))
  73. {
  74. if (scm_integer_logbit_uz (ii, scm_bignum (n)) == bb)
  75. return n;
  76. }
  77. else
  78. SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
  79. return scm_logxor (n, ii == 0 ? SCM_INUM1 : scm_integer_lsh_iu (1, ii));
  80. }
  81. #undef FUNC_NAME
  82. SCM_DEFINE (scm_srfi60_rotate_bit_field, "rotate-bit-field", 4, 0, 0,
  83. (SCM n, SCM count, SCM start, SCM end),
  84. "Return @var{n} with the bit field from @var{start} (inclusive)\n"
  85. "to @var{end} (exclusive) rotated upwards by @var{count} bits.\n"
  86. "\n"
  87. "@var{count} can be positive or negative, and it can be more\n"
  88. "than the field width (it'll be reduced modulo the width).\n"
  89. "\n"
  90. "@example\n"
  91. "(rotate-bit-field #b0110 2 1 4) @result{} #b1010\n"
  92. "@end example")
  93. #define FUNC_NAME s_scm_srfi60_rotate_bit_field
  94. {
  95. uintptr_t ss = scm_to_uintptr_t (start);
  96. uintptr_t ee = scm_to_uintptr_t (end);
  97. uintptr_t ww, cc;
  98. SCM_ASSERT_RANGE (3, end, (ee >= ss));
  99. ww = ee - ss;
  100. /* we must avoid division by zero, and a field whose width is 0 or 1
  101. will be left unchanged anyway, so in that case we set cc to 0. */
  102. if (ww <= 1)
  103. cc = 0;
  104. else
  105. cc = scm_to_uintptr_t (scm_modulo (count, scm_difference (end, start)));
  106. mpz_t zn;
  107. if (SCM_I_INUMP (n))
  108. {
  109. intptr_t nn = SCM_I_INUM (n);
  110. if (ee <= SCM_INTPTR_T_BIT-1)
  111. {
  112. /* Everything fits within a intptr_t. To avoid undefined
  113. behavior when shifting negative numbers, we do all
  114. operations using unsigned values, and then convert to
  115. signed at the end. */
  116. const uintptr_t UL1 = 1UL;
  117. uintptr_t unn = nn;
  118. uintptr_t below = unn & ((UL1 << ss) - 1); /* below start */
  119. uintptr_t above = unn & ~((UL1 << ee) - 1); /* above end */
  120. uintptr_t fmask = ((UL1 << ww) - 1) << ss; /* field mask */
  121. uintptr_t ff = unn & fmask; /* field */
  122. uintptr_t uresult = (above
  123. | ((ff << cc) & fmask)
  124. | ((ff >> (ww-cc)) & fmask)
  125. | below);
  126. intptr_t result;
  127. if (uresult > INTPTR_MAX)
  128. /* The high bit is set in uresult, so the result is
  129. negative. We have to handle the conversion to signed
  130. integer carefully, to avoid undefined behavior. First we
  131. compute ~uresult, equivalent to (ULONG_MAX - uresult),
  132. which will be between 0 and LONG_MAX (inclusive): exactly
  133. the set of numbers that can be represented as both intptr_t
  134. and uintptr_p and thus convertible between them. We
  135. cast that difference to a signed long and then substract
  136. it from -1. */
  137. result = -1 - (intptr_t) ~uresult;
  138. else
  139. result = (intptr_t) uresult;
  140. return scm_from_intptr_t (result);
  141. }
  142. else
  143. {
  144. /* if there's no movement, avoid creating a bignum. */
  145. if (cc == 0)
  146. return n;
  147. mpz_init_set_si (zn, nn);
  148. }
  149. }
  150. else if (SCM_BIGP (n))
  151. {
  152. /* if there's no movement, avoid creating a new bignum. */
  153. if (cc == 0)
  154. return n;
  155. scm_integer_init_set_mpz_z (scm_bignum (n), zn);
  156. }
  157. else
  158. SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
  159. mpz_t tmp, r;
  160. mpz_init (tmp);
  161. mpz_init_set_si (r, 0);
  162. /* portion above end */
  163. mpz_fdiv_q_2exp (r, zn, ee);
  164. mpz_mul_2exp (r, r, ee);
  165. /* field high part, width-count bits from start go to start+count */
  166. mpz_fdiv_q_2exp (tmp, zn, ss);
  167. mpz_fdiv_r_2exp (tmp, tmp, ww - cc);
  168. mpz_mul_2exp (tmp, tmp, ss + cc);
  169. mpz_ior (r, r, tmp);
  170. /* field low part, count bits from end-count go to start */
  171. mpz_fdiv_q_2exp (tmp, zn, ee - cc);
  172. mpz_fdiv_r_2exp (tmp, tmp, cc);
  173. mpz_mul_2exp (tmp, tmp, ss);
  174. mpz_ior (r, r, tmp);
  175. /* portion below start */
  176. mpz_fdiv_r_2exp (tmp, zn, ss);
  177. mpz_ior (r, r, tmp);
  178. mpz_clear (zn);
  179. mpz_clear (tmp);
  180. /* bits moved around might leave us in range of an inum */
  181. SCM ret = scm_from_mpz (r);
  182. mpz_clear (r);
  183. return ret;
  184. }
  185. #undef FUNC_NAME
  186. SCM_DEFINE (scm_srfi60_reverse_bit_field, "reverse-bit-field", 3, 0, 0,
  187. (SCM n, SCM start, SCM end),
  188. "Return @var{n} with the bits between @var{start} (inclusive) to\n"
  189. "@var{end} (exclusive) reversed.\n"
  190. "\n"
  191. "@example\n"
  192. "(reverse-bit-field #b101001 2 4) @result{} #b100101\n"
  193. "@end example")
  194. #define FUNC_NAME s_scm_srfi60_reverse_bit_field
  195. {
  196. intptr_t ss = scm_to_intptr_t (start);
  197. intptr_t ee = scm_to_intptr_t (end);
  198. intptr_t swaps = (ee - ss) / 2; /* number of swaps */
  199. mpz_t b;
  200. if (SCM_I_INUMP (n))
  201. {
  202. intptr_t nn = SCM_I_INUM (n);
  203. if (ee <= SCM_INTPTR_T_BIT-1)
  204. {
  205. /* all within a intptr_t */
  206. intptr_t L1 = 1L;
  207. intptr_t smask = L1 << ss;
  208. intptr_t emask = L1 << (ee-1);
  209. for ( ; swaps > 0; swaps--)
  210. {
  211. intptr_t sbit = nn & smask;
  212. intptr_t ebit = nn & emask;
  213. nn ^= sbit ^ (ebit ? smask : 0) /* zap sbit, put ebit value */
  214. ^ ebit ^ (sbit ? emask : 0); /* zap ebit, put sbit value */
  215. smask <<= 1;
  216. emask >>= 1;
  217. }
  218. return scm_from_intptr_t (nn);
  219. }
  220. else
  221. {
  222. /* avoid creating a new bignum if reversing only 0 or 1 bits */
  223. if (ee - ss <= 1)
  224. return n;
  225. mpz_init_set_si (b, nn);
  226. }
  227. }
  228. else if (SCM_BIGP (n))
  229. {
  230. /* avoid creating a new bignum if reversing only 0 or 1 bits */
  231. if (ee - ss <= 1)
  232. return n;
  233. scm_integer_init_set_mpz_z (scm_bignum (n), b);
  234. }
  235. else
  236. SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
  237. ee--;
  238. for ( ; swaps > 0; swaps--)
  239. {
  240. int sbit = mpz_tstbit (b, ss);
  241. int ebit = mpz_tstbit (b, ee);
  242. if (sbit ^ ebit)
  243. {
  244. /* the two bits are different, flip them */
  245. if (sbit)
  246. {
  247. mpz_clrbit (b, ss);
  248. mpz_setbit (b, ee);
  249. }
  250. else
  251. {
  252. mpz_setbit (b, ss);
  253. mpz_clrbit (b, ee);
  254. }
  255. }
  256. ss++;
  257. ee--;
  258. }
  259. SCM ret = scm_integer_from_mpz (b);
  260. mpz_clear (b);
  261. return ret;
  262. }
  263. #undef FUNC_NAME
  264. SCM_DEFINE (scm_srfi60_integer_to_list, "integer->list", 1, 1, 0,
  265. (SCM n, SCM len),
  266. "Return bits from @var{n} in the form of a list of @code{#t} for\n"
  267. "1 and @code{#f} for 0. The least significant @var{len} bits\n"
  268. "are returned, and the first list element is the most\n"
  269. "significant of those bits. If @var{len} is not given, the\n"
  270. "default is @code{(integer-length @var{n})} (@pxref{Bitwise\n"
  271. "Operations}).\n"
  272. "\n"
  273. "@example\n"
  274. "(integer->list 6) @result{} (#t #t #f)\n"
  275. "(integer->list 1 4) @result{} (#f #f #f #t)\n"
  276. "@end example")
  277. #define FUNC_NAME s_scm_srfi60_integer_to_list
  278. {
  279. SCM ret = SCM_EOL;
  280. uintptr_t ll;
  281. if (SCM_UNBNDP (len))
  282. len = scm_integer_length (n);
  283. ll = scm_to_uintptr_t (len);
  284. if (SCM_I_INUMP (n))
  285. {
  286. intptr_t nn = SCM_I_INUM (n);
  287. for (uintptr_t i = 0; i < ll; i++)
  288. ret = scm_cons (scm_from_bool (scm_integer_logbit_ui (i, nn)), ret);
  289. }
  290. else if (SCM_BIGP (n))
  291. {
  292. struct scm_bignum *nn = scm_bignum (n);
  293. for (uintptr_t i = 0; i < ll; i++)
  294. ret = scm_cons (scm_from_bool (scm_integer_logbit_uz (i, nn)), ret);
  295. }
  296. else
  297. SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
  298. return ret;
  299. }
  300. #undef FUNC_NAME
  301. SCM_DEFINE (scm_srfi60_list_to_integer, "list->integer", 1, 0, 0,
  302. (SCM lst),
  303. "Return an integer formed bitwise from the given @var{lst} list\n"
  304. "of booleans. Each boolean is @code{#t} for a 1 and @code{#f}\n"
  305. "for a 0. The first element becomes the most significant bit in\n"
  306. "the return.\n"
  307. "\n"
  308. "@example\n"
  309. "(list->integer '(#t #f #t #f)) @result{} 10\n"
  310. "@end example")
  311. #define FUNC_NAME s_scm_srfi60_list_to_integer
  312. {
  313. intptr_t len;
  314. /* strip high zero bits from lst; after this the length tells us whether
  315. an inum or bignum is required */
  316. while (scm_is_pair (lst) && scm_is_false (SCM_CAR (lst)))
  317. lst = SCM_CDR (lst);
  318. SCM_VALIDATE_LIST_COPYLEN (SCM_ARG1, lst, len);
  319. if (len <= SCM_I_FIXNUM_BIT - 1)
  320. {
  321. /* fits an inum (a positive inum) */
  322. intptr_t n = 0;
  323. while (scm_is_pair (lst))
  324. {
  325. n <<= 1;
  326. if (! scm_is_false (SCM_CAR (lst)))
  327. n++;
  328. lst = SCM_CDR (lst);
  329. }
  330. return SCM_I_MAKINUM (n);
  331. }
  332. else
  333. {
  334. mpz_t z;
  335. mpz_init (z);
  336. while (scm_is_pair (lst))
  337. {
  338. len--;
  339. if (! scm_is_false (SCM_CAR (lst)))
  340. mpz_setbit (z, len);
  341. lst = SCM_CDR (lst);
  342. }
  343. SCM ret = scm_from_mpz (z);
  344. mpz_clear (z);
  345. return ret;
  346. }
  347. }
  348. #undef FUNC_NAME
  349. /* note: don't put "scm_srfi60_list_to_integer" arg on its own line, a
  350. newline breaks the snarfer */
  351. SCM_REGISTER_PROC (s_srfi60_booleans_to_integer, "booleans->integer", 0, 0, 1, scm_srfi60_list_to_integer);
  352. void
  353. scm_register_srfi_60 (void)
  354. {
  355. scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
  356. "scm_init_srfi_60",
  357. (scm_t_extension_init_func)scm_init_srfi_60, NULL);
  358. }
  359. void
  360. scm_init_srfi_60 (void)
  361. {
  362. #ifndef SCM_MAGIC_SNARFER
  363. #include "srfi-60.x"
  364. #endif
  365. }