srfi-60.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /* srfi-60.c --- Integers as Bits
  2. Copyright 2005-2006,2008,2010,2014,2018,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. unsigned long ii;
  64. int bb;
  65. ii = scm_to_ulong (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. unsigned long ss = scm_to_ulong (start);
  96. unsigned long ee = scm_to_ulong (end);
  97. unsigned long 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_ulong (scm_modulo (count, scm_difference (end, start)));
  106. mpz_t zn;
  107. if (SCM_I_INUMP (n))
  108. {
  109. long nn = SCM_I_INUM (n);
  110. if (ee <= SCM_LONG_BIT-1)
  111. {
  112. /* Everything fits within a long. To avoid undefined behavior
  113. when shifting negative numbers, we do all operations using
  114. unsigned values, and then convert to signed at the end. */
  115. unsigned long unn = nn;
  116. unsigned long below = unn & ((1UL << ss) - 1); /* below start */
  117. unsigned long above = unn & ~((1UL << ee) - 1); /* above end */
  118. unsigned long fmask = ((1UL << ww) - 1) << ss; /* field mask */
  119. unsigned long ff = unn & fmask; /* field */
  120. unsigned long uresult = (above
  121. | ((ff << cc) & fmask)
  122. | ((ff >> (ww-cc)) & fmask)
  123. | below);
  124. long result;
  125. if (uresult > LONG_MAX)
  126. /* The high bit is set in uresult, so the result is
  127. negative. We have to handle the conversion to signed
  128. integer carefully, to avoid undefined behavior. First we
  129. compute ~uresult, equivalent to (ULONG_MAX - uresult),
  130. which will be between 0 and LONG_MAX (inclusive): exactly
  131. the set of numbers that can be represented as both signed
  132. and unsigned longs and thus convertible between them. We
  133. cast that difference to a signed long and then substract
  134. it from -1. */
  135. result = -1 - (long) ~uresult;
  136. else
  137. result = (long) uresult;
  138. return scm_from_long (result);
  139. }
  140. else
  141. {
  142. /* if there's no movement, avoid creating a bignum. */
  143. if (cc == 0)
  144. return n;
  145. mpz_init_set_si (zn, nn);
  146. }
  147. }
  148. else if (SCM_BIGP (n))
  149. {
  150. /* if there's no movement, avoid creating a new bignum. */
  151. if (cc == 0)
  152. return n;
  153. scm_integer_init_set_mpz_z (scm_bignum (n), zn);
  154. }
  155. else
  156. SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
  157. mpz_t tmp, r;
  158. mpz_init (tmp);
  159. mpz_init_set_si (r, 0);
  160. /* portion above end */
  161. mpz_fdiv_q_2exp (r, zn, ee);
  162. mpz_mul_2exp (r, r, ee);
  163. /* field high part, width-count bits from start go to start+count */
  164. mpz_fdiv_q_2exp (tmp, zn, ss);
  165. mpz_fdiv_r_2exp (tmp, tmp, ww - cc);
  166. mpz_mul_2exp (tmp, tmp, ss + cc);
  167. mpz_ior (r, r, tmp);
  168. /* field low part, count bits from end-count go to start */
  169. mpz_fdiv_q_2exp (tmp, zn, ee - cc);
  170. mpz_fdiv_r_2exp (tmp, tmp, cc);
  171. mpz_mul_2exp (tmp, tmp, ss);
  172. mpz_ior (r, r, tmp);
  173. /* portion below start */
  174. mpz_fdiv_r_2exp (tmp, zn, ss);
  175. mpz_ior (r, r, tmp);
  176. mpz_clear (zn);
  177. mpz_clear (tmp);
  178. /* bits moved around might leave us in range of an inum */
  179. SCM ret = scm_from_mpz (r);
  180. mpz_clear (r);
  181. return ret;
  182. }
  183. #undef FUNC_NAME
  184. SCM_DEFINE (scm_srfi60_reverse_bit_field, "reverse-bit-field", 3, 0, 0,
  185. (SCM n, SCM start, SCM end),
  186. "Return @var{n} with the bits between @var{start} (inclusive) to\n"
  187. "@var{end} (exclusive) reversed.\n"
  188. "\n"
  189. "@example\n"
  190. "(reverse-bit-field #b101001 2 4) @result{} #b100101\n"
  191. "@end example")
  192. #define FUNC_NAME s_scm_srfi60_reverse_bit_field
  193. {
  194. long ss = scm_to_long (start);
  195. long ee = scm_to_long (end);
  196. long swaps = (ee - ss) / 2; /* number of swaps */
  197. mpz_t b;
  198. if (SCM_I_INUMP (n))
  199. {
  200. long nn = SCM_I_INUM (n);
  201. if (ee <= SCM_LONG_BIT-1)
  202. {
  203. /* all within a long */
  204. long smask = 1L << ss;
  205. long emask = 1L << (ee-1);
  206. for ( ; swaps > 0; swaps--)
  207. {
  208. long sbit = nn & smask;
  209. long ebit = nn & emask;
  210. nn ^= sbit ^ (ebit ? smask : 0) /* zap sbit, put ebit value */
  211. ^ ebit ^ (sbit ? emask : 0); /* zap ebit, put sbit value */
  212. smask <<= 1;
  213. emask >>= 1;
  214. }
  215. return scm_from_long (nn);
  216. }
  217. else
  218. {
  219. /* avoid creating a new bignum if reversing only 0 or 1 bits */
  220. if (ee - ss <= 1)
  221. return n;
  222. mpz_init_set_si (b, nn);
  223. }
  224. }
  225. else if (SCM_BIGP (n))
  226. {
  227. /* avoid creating a new bignum if reversing only 0 or 1 bits */
  228. if (ee - ss <= 1)
  229. return n;
  230. scm_integer_init_set_mpz_z (scm_bignum (n), b);
  231. }
  232. else
  233. SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
  234. ee--;
  235. for ( ; swaps > 0; swaps--)
  236. {
  237. int sbit = mpz_tstbit (b, ss);
  238. int ebit = mpz_tstbit (b, ee);
  239. if (sbit ^ ebit)
  240. {
  241. /* the two bits are different, flip them */
  242. if (sbit)
  243. {
  244. mpz_clrbit (b, ss);
  245. mpz_setbit (b, ee);
  246. }
  247. else
  248. {
  249. mpz_setbit (b, ss);
  250. mpz_clrbit (b, ee);
  251. }
  252. }
  253. ss++;
  254. ee--;
  255. }
  256. SCM ret = scm_integer_from_mpz (b);
  257. mpz_clear (b);
  258. return ret;
  259. }
  260. #undef FUNC_NAME
  261. SCM_DEFINE (scm_srfi60_integer_to_list, "integer->list", 1, 1, 0,
  262. (SCM n, SCM len),
  263. "Return bits from @var{n} in the form of a list of @code{#t} for\n"
  264. "1 and @code{#f} for 0. The least significant @var{len} bits\n"
  265. "are returned, and the first list element is the most\n"
  266. "significant of those bits. If @var{len} is not given, the\n"
  267. "default is @code{(integer-length @var{n})} (@pxref{Bitwise\n"
  268. "Operations}).\n"
  269. "\n"
  270. "@example\n"
  271. "(integer->list 6) @result{} (#t #t #f)\n"
  272. "(integer->list 1 4) @result{} (#f #f #f #t)\n"
  273. "@end example")
  274. #define FUNC_NAME s_scm_srfi60_integer_to_list
  275. {
  276. SCM ret = SCM_EOL;
  277. unsigned long ll;
  278. if (SCM_UNBNDP (len))
  279. len = scm_integer_length (n);
  280. ll = scm_to_ulong (len);
  281. if (SCM_I_INUMP (n))
  282. {
  283. scm_t_inum nn = SCM_I_INUM (n);
  284. for (unsigned long i = 0; i < ll; i++)
  285. ret = scm_cons (scm_from_bool (scm_integer_logbit_ui (i, nn)), ret);
  286. }
  287. else if (SCM_BIGP (n))
  288. {
  289. struct scm_bignum *nn = scm_bignum (n);
  290. for (unsigned long i = 0; i < ll; i++)
  291. ret = scm_cons (scm_from_bool (scm_integer_logbit_uz (i, nn)), ret);
  292. }
  293. else
  294. SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
  295. return ret;
  296. }
  297. #undef FUNC_NAME
  298. SCM_DEFINE (scm_srfi60_list_to_integer, "list->integer", 1, 0, 0,
  299. (SCM lst),
  300. "Return an integer formed bitwise from the given @var{lst} list\n"
  301. "of booleans. Each boolean is @code{#t} for a 1 and @code{#f}\n"
  302. "for a 0. The first element becomes the most significant bit in\n"
  303. "the return.\n"
  304. "\n"
  305. "@example\n"
  306. "(list->integer '(#t #f #t #f)) @result{} 10\n"
  307. "@end example")
  308. #define FUNC_NAME s_scm_srfi60_list_to_integer
  309. {
  310. long len;
  311. /* strip high zero bits from lst; after this the length tells us whether
  312. an inum or bignum is required */
  313. while (scm_is_pair (lst) && scm_is_false (SCM_CAR (lst)))
  314. lst = SCM_CDR (lst);
  315. SCM_VALIDATE_LIST_COPYLEN (SCM_ARG1, lst, len);
  316. if (len <= SCM_I_FIXNUM_BIT - 1)
  317. {
  318. /* fits an inum (a positive inum) */
  319. long n = 0;
  320. while (scm_is_pair (lst))
  321. {
  322. n <<= 1;
  323. if (! scm_is_false (SCM_CAR (lst)))
  324. n++;
  325. lst = SCM_CDR (lst);
  326. }
  327. return SCM_I_MAKINUM (n);
  328. }
  329. else
  330. {
  331. mpz_t z;
  332. mpz_init (z);
  333. while (scm_is_pair (lst))
  334. {
  335. len--;
  336. if (! scm_is_false (SCM_CAR (lst)))
  337. mpz_setbit (z, len);
  338. lst = SCM_CDR (lst);
  339. }
  340. SCM ret = scm_from_mpz (z);
  341. mpz_clear (z);
  342. return ret;
  343. }
  344. }
  345. #undef FUNC_NAME
  346. /* note: don't put "scm_srfi60_list_to_integer" arg on its own line, a
  347. newline breaks the snarfer */
  348. SCM_REGISTER_PROC (s_srfi60_booleans_to_integer, "booleans->integer", 0, 0, 1, scm_srfi60_list_to_integer);
  349. void
  350. scm_register_srfi_60 (void)
  351. {
  352. scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
  353. "scm_init_srfi_60",
  354. (scm_t_extension_init_func)scm_init_srfi_60, NULL);
  355. }
  356. void
  357. scm_init_srfi_60 (void)
  358. {
  359. #ifndef SCM_MAGIC_SNARFER
  360. #include "srfi-60.x"
  361. #endif
  362. }