srfi-60.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /* srfi-60.c --- Integers as Bits
  2. Copyright 2005-2006,2008,2010,2014,2018
  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 "list.h"
  24. #include "numbers.h"
  25. #include "pairs.h"
  26. #include "version.h"
  27. #include "srfi-60.h"
  28. SCM_DEFINE (scm_srfi60_log2_binary_factors, "log2-binary-factors", 1, 0, 0,
  29. (SCM n),
  30. "Return a count of how many factors of 2 are present in @var{n}.\n"
  31. "This is also the bit index of the lowest 1 bit in @var{n}. If\n"
  32. "@var{n} is 0, the return is @math{-1}.\n"
  33. "\n"
  34. "@example\n"
  35. "(log2-binary-factors 6) @result{} 1\n"
  36. "(log2-binary-factors -8) @result{} 3\n"
  37. "@end example")
  38. #define FUNC_NAME s_scm_srfi60_log2_binary_factors
  39. {
  40. SCM ret = SCM_EOL;
  41. if (SCM_I_INUMP (n))
  42. {
  43. long nn = SCM_I_INUM (n);
  44. if (nn == 0)
  45. return SCM_I_MAKINUM (-1);
  46. nn = nn ^ (nn-1); /* 1 bits for each low 0 and lowest 1 */
  47. return scm_logcount (SCM_I_MAKINUM (nn >> 1));
  48. }
  49. else if (SCM_BIGP (n))
  50. {
  51. /* no need for scm_remember_upto_here_1 here, mpz_scan1 doesn't do
  52. anything that could result in a gc */
  53. return SCM_I_MAKINUM (mpz_scan1 (SCM_I_BIG_MPZ (n), 0L));
  54. }
  55. else
  56. SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
  57. return ret;
  58. }
  59. #undef FUNC_NAME
  60. SCM_DEFINE (scm_srfi60_copy_bit, "copy-bit", 3, 0, 0,
  61. (SCM index, SCM n, SCM newbit),
  62. "Return @var{n} with the bit at @var{index} set according to\n"
  63. "@var{newbit}. @var{newbit} should be @code{#t} to set the bit\n"
  64. "to 1, or @code{#f} to set it to 0. Bits other than at\n"
  65. "@var{index} are unchanged in the return.\n"
  66. "\n"
  67. "@example\n"
  68. "(copy-bit 1 #b0101 #t) @result{} 7\n"
  69. "@end example")
  70. #define FUNC_NAME s_scm_srfi60_copy_bit
  71. {
  72. SCM r;
  73. unsigned long ii;
  74. int bb;
  75. ii = scm_to_ulong (index);
  76. bb = scm_to_bool (newbit);
  77. if (SCM_I_INUMP (n))
  78. {
  79. long nn = SCM_I_INUM (n);
  80. /* can't set high bit ii==SCM_LONG_BIT-1, that would change the sign,
  81. which is not what's wanted */
  82. if (ii < SCM_LONG_BIT-1)
  83. {
  84. nn &= ~(1L << ii); /* zap bit at index */
  85. nn |= ((long) bb << ii); /* insert desired bit */
  86. return scm_from_long (nn);
  87. }
  88. else
  89. {
  90. /* bits at ii==SCM_LONG_BIT-1 and above are all copies of the sign
  91. bit, if this is already the desired "bit" value then no need to
  92. make a new bignum value */
  93. if (bb == (nn < 0))
  94. return n;
  95. r = scm_i_long2big (nn);
  96. goto big;
  97. }
  98. }
  99. else if (SCM_BIGP (n))
  100. {
  101. /* if the bit is already what's wanted then no need to make a new
  102. bignum */
  103. if (bb == mpz_tstbit (SCM_I_BIG_MPZ (n), ii))
  104. return n;
  105. r = scm_i_clonebig (n, 1);
  106. big:
  107. if (bb)
  108. mpz_setbit (SCM_I_BIG_MPZ (r), ii);
  109. else
  110. mpz_clrbit (SCM_I_BIG_MPZ (r), ii);
  111. /* changing a high bit might put the result into range of a fixnum */
  112. return scm_i_normbig (r);
  113. }
  114. else
  115. SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
  116. }
  117. #undef FUNC_NAME
  118. SCM_DEFINE (scm_srfi60_rotate_bit_field, "rotate-bit-field", 4, 0, 0,
  119. (SCM n, SCM count, SCM start, SCM end),
  120. "Return @var{n} with the bit field from @var{start} (inclusive)\n"
  121. "to @var{end} (exclusive) rotated upwards by @var{count} bits.\n"
  122. "\n"
  123. "@var{count} can be positive or negative, and it can be more\n"
  124. "than the field width (it'll be reduced modulo the width).\n"
  125. "\n"
  126. "@example\n"
  127. "(rotate-bit-field #b0110 2 1 4) @result{} #b1010\n"
  128. "@end example")
  129. #define FUNC_NAME s_scm_srfi60_rotate_bit_field
  130. {
  131. unsigned long ss = scm_to_ulong (start);
  132. unsigned long ee = scm_to_ulong (end);
  133. unsigned long ww, cc;
  134. SCM_ASSERT_RANGE (3, end, (ee >= ss));
  135. ww = ee - ss;
  136. /* we must avoid division by zero, and a field whose width is 0 or 1
  137. will be left unchanged anyway, so in that case we set cc to 0. */
  138. if (ww <= 1)
  139. cc = 0;
  140. else
  141. cc = scm_to_ulong (scm_modulo (count, scm_difference (end, start)));
  142. if (SCM_I_INUMP (n))
  143. {
  144. long nn = SCM_I_INUM (n);
  145. if (ee <= SCM_LONG_BIT-1)
  146. {
  147. /* Everything fits within a long. To avoid undefined behavior
  148. when shifting negative numbers, we do all operations using
  149. unsigned values, and then convert to signed at the end. */
  150. unsigned long unn = nn;
  151. unsigned long below = unn & ((1UL << ss) - 1); /* below start */
  152. unsigned long above = unn & ~((1UL << ee) - 1); /* above end */
  153. unsigned long fmask = ((1UL << ww) - 1) << ss; /* field mask */
  154. unsigned long ff = unn & fmask; /* field */
  155. unsigned long uresult = (above
  156. | ((ff << cc) & fmask)
  157. | ((ff >> (ww-cc)) & fmask)
  158. | below);
  159. long result;
  160. if (uresult > LONG_MAX)
  161. /* The high bit is set in uresult, so the result is
  162. negative. We have to handle the conversion to signed
  163. integer carefully, to avoid undefined behavior. First we
  164. compute ~uresult, equivalent to (ULONG_MAX - uresult),
  165. which will be between 0 and LONG_MAX (inclusive): exactly
  166. the set of numbers that can be represented as both signed
  167. and unsigned longs and thus convertible between them. We
  168. cast that difference to a signed long and then substract
  169. it from -1. */
  170. result = -1 - (long) ~uresult;
  171. else
  172. result = (long) uresult;
  173. return scm_from_long (result);
  174. }
  175. else
  176. {
  177. /* if there's no movement, avoid creating a bignum. */
  178. if (cc == 0)
  179. return n;
  180. n = scm_i_long2big (nn);
  181. goto big;
  182. }
  183. }
  184. else if (SCM_BIGP (n))
  185. {
  186. mpz_t tmp;
  187. SCM r;
  188. /* if there's no movement, avoid creating a new bignum. */
  189. if (cc == 0)
  190. return n;
  191. big:
  192. r = scm_i_ulong2big (0);
  193. mpz_init (tmp);
  194. /* portion above end */
  195. mpz_fdiv_q_2exp (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (n), ee);
  196. mpz_mul_2exp (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (r), ee);
  197. /* field high part, width-count bits from start go to start+count */
  198. mpz_fdiv_q_2exp (tmp, SCM_I_BIG_MPZ (n), ss);
  199. mpz_fdiv_r_2exp (tmp, tmp, ww - cc);
  200. mpz_mul_2exp (tmp, tmp, ss + cc);
  201. mpz_ior (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (r), tmp);
  202. /* field low part, count bits from end-count go to start */
  203. mpz_fdiv_q_2exp (tmp, SCM_I_BIG_MPZ (n), ee - cc);
  204. mpz_fdiv_r_2exp (tmp, tmp, cc);
  205. mpz_mul_2exp (tmp, tmp, ss);
  206. mpz_ior (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (r), tmp);
  207. /* portion below start */
  208. mpz_fdiv_r_2exp (tmp, SCM_I_BIG_MPZ (n), ss);
  209. mpz_ior (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (r), tmp);
  210. mpz_clear (tmp);
  211. /* bits moved around might leave us in range of an inum */
  212. return scm_i_normbig (r);
  213. }
  214. else
  215. SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
  216. }
  217. #undef FUNC_NAME
  218. SCM_DEFINE (scm_srfi60_reverse_bit_field, "reverse-bit-field", 3, 0, 0,
  219. (SCM n, SCM start, SCM end),
  220. "Return @var{n} with the bits between @var{start} (inclusive) to\n"
  221. "@var{end} (exclusive) reversed.\n"
  222. "\n"
  223. "@example\n"
  224. "(reverse-bit-field #b101001 2 4) @result{} #b100101\n"
  225. "@end example")
  226. #define FUNC_NAME s_scm_srfi60_reverse_bit_field
  227. {
  228. long ss = scm_to_long (start);
  229. long ee = scm_to_long (end);
  230. long swaps = (ee - ss) / 2; /* number of swaps */
  231. SCM b;
  232. if (SCM_I_INUMP (n))
  233. {
  234. long nn = SCM_I_INUM (n);
  235. if (ee <= SCM_LONG_BIT-1)
  236. {
  237. /* all within a long */
  238. long smask = 1L << ss;
  239. long emask = 1L << (ee-1);
  240. for ( ; swaps > 0; swaps--)
  241. {
  242. long sbit = nn & smask;
  243. long ebit = nn & emask;
  244. nn ^= sbit ^ (ebit ? smask : 0) /* zap sbit, put ebit value */
  245. ^ ebit ^ (sbit ? emask : 0); /* zap ebit, put sbit value */
  246. smask <<= 1;
  247. emask >>= 1;
  248. }
  249. return scm_from_long (nn);
  250. }
  251. else
  252. {
  253. /* avoid creating a new bignum if reversing only 0 or 1 bits */
  254. if (ee - ss <= 1)
  255. return n;
  256. b = scm_i_long2big (nn);
  257. goto big;
  258. }
  259. }
  260. else if (SCM_BIGP (n))
  261. {
  262. /* avoid creating a new bignum if reversing only 0 or 1 bits */
  263. if (ee - ss <= 1)
  264. return n;
  265. b = scm_i_clonebig (n, 1);
  266. big:
  267. ee--;
  268. for ( ; swaps > 0; swaps--)
  269. {
  270. int sbit = mpz_tstbit (SCM_I_BIG_MPZ (b), ss);
  271. int ebit = mpz_tstbit (SCM_I_BIG_MPZ (b), ee);
  272. if (sbit ^ ebit)
  273. {
  274. /* the two bits are different, flip them */
  275. if (sbit)
  276. {
  277. mpz_clrbit (SCM_I_BIG_MPZ (b), ss);
  278. mpz_setbit (SCM_I_BIG_MPZ (b), ee);
  279. }
  280. else
  281. {
  282. mpz_setbit (SCM_I_BIG_MPZ (b), ss);
  283. mpz_clrbit (SCM_I_BIG_MPZ (b), ee);
  284. }
  285. }
  286. ss++;
  287. ee--;
  288. }
  289. /* swapping zero bits into the high might make us fit a fixnum */
  290. return scm_i_normbig (b);
  291. }
  292. else
  293. SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
  294. }
  295. #undef FUNC_NAME
  296. SCM_DEFINE (scm_srfi60_integer_to_list, "integer->list", 1, 1, 0,
  297. (SCM n, SCM len),
  298. "Return bits from @var{n} in the form of a list of @code{#t} for\n"
  299. "1 and @code{#f} for 0. The least significant @var{len} bits\n"
  300. "are returned, and the first list element is the most\n"
  301. "significant of those bits. If @var{len} is not given, the\n"
  302. "default is @code{(integer-length @var{n})} (@pxref{Bitwise\n"
  303. "Operations}).\n"
  304. "\n"
  305. "@example\n"
  306. "(integer->list 6) @result{} (#t #t #f)\n"
  307. "(integer->list 1 4) @result{} (#f #f #f #t)\n"
  308. "@end example")
  309. #define FUNC_NAME s_scm_srfi60_integer_to_list
  310. {
  311. SCM ret = SCM_EOL;
  312. unsigned long ll, i;
  313. if (SCM_UNBNDP (len))
  314. len = scm_integer_length (n);
  315. ll = scm_to_ulong (len);
  316. if (SCM_I_INUMP (n))
  317. {
  318. long nn = SCM_I_INUM (n);
  319. for (i = 0; i < ll; i++)
  320. {
  321. unsigned long shift =
  322. (i < ((unsigned long) SCM_LONG_BIT-1))
  323. ? i : ((unsigned long) SCM_LONG_BIT-1);
  324. int bit = (nn >> shift) & 1;
  325. ret = scm_cons (scm_from_bool (bit), ret);
  326. }
  327. }
  328. else if (SCM_BIGP (n))
  329. {
  330. for (i = 0; i < ll; i++)
  331. ret = scm_cons (scm_from_bool (mpz_tstbit (SCM_I_BIG_MPZ (n), i)),
  332. ret);
  333. scm_remember_upto_here_1 (n);
  334. }
  335. else
  336. SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
  337. return ret;
  338. }
  339. #undef FUNC_NAME
  340. SCM_DEFINE (scm_srfi60_list_to_integer, "list->integer", 1, 0, 0,
  341. (SCM lst),
  342. "Return an integer formed bitwise from the given @var{lst} list\n"
  343. "of booleans. Each boolean is @code{#t} for a 1 and @code{#f}\n"
  344. "for a 0. The first element becomes the most significant bit in\n"
  345. "the return.\n"
  346. "\n"
  347. "@example\n"
  348. "(list->integer '(#t #f #t #f)) @result{} 10\n"
  349. "@end example")
  350. #define FUNC_NAME s_scm_srfi60_list_to_integer
  351. {
  352. long len;
  353. /* strip high zero bits from lst; after this the length tells us whether
  354. an inum or bignum is required */
  355. while (scm_is_pair (lst) && scm_is_false (SCM_CAR (lst)))
  356. lst = SCM_CDR (lst);
  357. SCM_VALIDATE_LIST_COPYLEN (SCM_ARG1, lst, len);
  358. if (len <= SCM_I_FIXNUM_BIT - 1)
  359. {
  360. /* fits an inum (a positive inum) */
  361. long n = 0;
  362. while (scm_is_pair (lst))
  363. {
  364. n <<= 1;
  365. if (! scm_is_false (SCM_CAR (lst)))
  366. n++;
  367. lst = SCM_CDR (lst);
  368. }
  369. return SCM_I_MAKINUM (n);
  370. }
  371. else
  372. {
  373. /* need a bignum */
  374. SCM n = scm_i_ulong2big (0);
  375. while (scm_is_pair (lst))
  376. {
  377. len--;
  378. if (! scm_is_false (SCM_CAR (lst)))
  379. mpz_setbit (SCM_I_BIG_MPZ (n), len);
  380. lst = SCM_CDR (lst);
  381. }
  382. return n;
  383. }
  384. }
  385. #undef FUNC_NAME
  386. /* note: don't put "scm_srfi60_list_to_integer" arg on its own line, a
  387. newline breaks the snarfer */
  388. SCM_REGISTER_PROC (s_srfi60_booleans_to_integer, "booleans->integer", 0, 0, 1, scm_srfi60_list_to_integer);
  389. void
  390. scm_register_srfi_60 (void)
  391. {
  392. scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
  393. "scm_init_srfi_60",
  394. (scm_t_extension_init_func)scm_init_srfi_60, NULL);
  395. }
  396. void
  397. scm_init_srfi_60 (void)
  398. {
  399. #ifndef SCM_MAGIC_SNARFER
  400. #include "srfi-60.x"
  401. #endif
  402. }