srfi-60.c 12 KB

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