bitvectors.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. /* Copyright 1995-1998,2000-2006,2009-2014,2018,2020
  2. Free Software Foundation, Inc.
  3. This file is part of Guile.
  4. Guile is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Guile is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  11. License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with Guile. If not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifdef HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #include <string.h>
  19. #include "array-handle.h"
  20. #include "arrays.h"
  21. #include "boolean.h"
  22. #include "deprecation.h"
  23. #include "generalized-vectors.h"
  24. #include "gsubr.h"
  25. #include "list.h"
  26. #include "numbers.h"
  27. #include "pairs.h"
  28. #include "ports.h"
  29. #include "srfi-4.h"
  30. #include "bitvectors.h"
  31. #define SCM_F_BITVECTOR_IMMUTABLE (0x80)
  32. /* To do in Guile 3.1.x:
  33. - Allocate bits inline with bitvector, starting from &SCM_CELL_WORD_2.
  34. - Use uintptr_t for bitvector component instead of uint32_t.
  35. - Remove deprecated support for bitvector-ref et al on arrays.
  36. - Replace primitives that operator on bitvectors but don't have
  37. bitvector- prefix.
  38. - Add Scheme compiler support for bitvector primitives. */
  39. #define IS_BITVECTOR(obj) SCM_HAS_TYP7 ((obj), scm_tc7_bitvector)
  40. #define IS_MUTABLE_BITVECTOR(x) \
  41. (SCM_NIMP (x) && \
  42. ((SCM_CELL_TYPE (x) & (0x7f | SCM_F_BITVECTOR_IMMUTABLE)) \
  43. == scm_tc7_bitvector))
  44. #define BITVECTOR_LENGTH(obj) ((size_t)SCM_CELL_WORD_1(obj))
  45. #define BITVECTOR_BITS(obj) ((uint32_t *)SCM_CELL_WORD_2(obj))
  46. #define VALIDATE_BITVECTOR(_pos, _obj) \
  47. SCM_ASSERT_TYPE (IS_BITVECTOR (_obj), (_obj), (_pos), FUNC_NAME, \
  48. "bitvector")
  49. #define VALIDATE_MUTABLE_BITVECTOR(_pos, _obj) \
  50. SCM_ASSERT_TYPE (IS_MUTABLE_BITVECTOR (_obj), (_obj), (_pos), \
  51. FUNC_NAME, "mutable bitvector")
  52. uint32_t *
  53. scm_i_bitvector_bits (SCM vec)
  54. {
  55. if (!IS_BITVECTOR (vec))
  56. abort ();
  57. return BITVECTOR_BITS (vec);
  58. }
  59. int
  60. scm_i_is_mutable_bitvector (SCM vec)
  61. {
  62. return IS_MUTABLE_BITVECTOR (vec);
  63. }
  64. int
  65. scm_i_print_bitvector (SCM vec, SCM port, scm_print_state *pstate)
  66. {
  67. size_t bit_len = BITVECTOR_LENGTH (vec);
  68. size_t word_len = (bit_len+31)/32;
  69. uint32_t *bits = BITVECTOR_BITS (vec);
  70. size_t i, j;
  71. scm_puts ("#*", port);
  72. for (i = 0; i < word_len; i++, bit_len -= 32)
  73. {
  74. uint32_t mask = 1;
  75. for (j = 0; j < 32 && j < bit_len; j++, mask <<= 1)
  76. scm_putc ((bits[i] & mask)? '1' : '0', port);
  77. }
  78. return 1;
  79. }
  80. SCM
  81. scm_i_bitvector_equal_p (SCM vec1, SCM vec2)
  82. {
  83. size_t bit_len = BITVECTOR_LENGTH (vec1);
  84. size_t word_len = (bit_len + 31) / 32;
  85. uint32_t last_mask = ((uint32_t)-1) >> (32*word_len - bit_len);
  86. uint32_t *bits1 = BITVECTOR_BITS (vec1);
  87. uint32_t *bits2 = BITVECTOR_BITS (vec2);
  88. /* compare lengths */
  89. if (BITVECTOR_LENGTH (vec2) != bit_len)
  90. return SCM_BOOL_F;
  91. /* avoid underflow in word_len-1 below. */
  92. if (bit_len == 0)
  93. return SCM_BOOL_T;
  94. /* compare full words */
  95. if (memcmp (bits1, bits2, sizeof (uint32_t) * (word_len-1)))
  96. return SCM_BOOL_F;
  97. /* compare partial last words */
  98. if ((bits1[word_len-1] & last_mask) != (bits2[word_len-1] & last_mask))
  99. return SCM_BOOL_F;
  100. return SCM_BOOL_T;
  101. }
  102. int
  103. scm_is_bitvector (SCM vec)
  104. {
  105. return IS_BITVECTOR (vec);
  106. }
  107. SCM_DEFINE_STATIC (bitvector_p, "bitvector?", 1, 0, 0,
  108. (SCM obj),
  109. "Return @code{#t} when @var{obj} is a bitvector, else\n"
  110. "return @code{#f}.")
  111. #define FUNC_NAME s_bitvector_p
  112. {
  113. return scm_from_bool (scm_is_bitvector (obj));
  114. }
  115. #undef FUNC_NAME
  116. SCM
  117. scm_c_make_bitvector (size_t len, SCM fill)
  118. {
  119. size_t word_len = (len + 31) / 32;
  120. uint32_t *bits;
  121. SCM res;
  122. bits = scm_gc_malloc_pointerless (sizeof (uint32_t) * word_len,
  123. "bitvector");
  124. res = scm_double_cell (scm_tc7_bitvector, len, (scm_t_bits)bits, 0);
  125. if (SCM_UNBNDP (fill) || !scm_is_true (fill))
  126. scm_c_bitvector_clear_all_bits_x (res);
  127. else
  128. scm_c_bitvector_set_all_bits_x (res);
  129. return res;
  130. }
  131. SCM_DEFINE_STATIC (make_bitvector, "make-bitvector", 1, 1, 0,
  132. (SCM len, SCM fill),
  133. "Create a new bitvector of length @var{len} and\n"
  134. "optionally initialize all elements to @var{fill}.")
  135. #define FUNC_NAME s_make_bitvector
  136. {
  137. return scm_c_make_bitvector (scm_to_size_t (len), fill);
  138. }
  139. #undef FUNC_NAME
  140. SCM_DEFINE_STATIC (bitvector, "bitvector", 0, 0, 1,
  141. (SCM bits),
  142. "Create a new bitvector with the arguments as elements.")
  143. #define FUNC_NAME s_bitvector
  144. {
  145. return scm_list_to_bitvector (bits);
  146. }
  147. #undef FUNC_NAME
  148. size_t
  149. scm_c_bitvector_length (SCM vec)
  150. {
  151. if (!IS_BITVECTOR (vec))
  152. scm_wrong_type_arg_msg (NULL, 0, vec, "bitvector");
  153. return BITVECTOR_LENGTH (vec);
  154. }
  155. SCM_DEFINE_STATIC (bitvector_length, "bitvector-length", 1, 0, 0,
  156. (SCM vec),
  157. "Return the length of the bitvector @var{vec}.")
  158. #define FUNC_NAME s_bitvector_length
  159. {
  160. return scm_from_size_t (scm_c_bitvector_length (vec));
  161. }
  162. #undef FUNC_NAME
  163. const uint32_t *
  164. scm_array_handle_bit_elements (scm_t_array_handle *h)
  165. {
  166. if (h->element_type != SCM_ARRAY_ELEMENT_TYPE_BIT)
  167. scm_wrong_type_arg_msg (NULL, 0, h->array, "bit array");
  168. return ((const uint32_t *) h->elements) + h->base/32;
  169. }
  170. uint32_t *
  171. scm_array_handle_bit_writable_elements (scm_t_array_handle *h)
  172. {
  173. if (h->writable_elements != h->elements)
  174. scm_wrong_type_arg_msg (NULL, 0, h->array, "mutable bit array");
  175. return (uint32_t *) scm_array_handle_bit_elements (h);
  176. }
  177. size_t
  178. scm_array_handle_bit_elements_offset (scm_t_array_handle *h)
  179. {
  180. return h->base % 32;
  181. }
  182. const uint32_t *
  183. scm_bitvector_elements (SCM vec,
  184. scm_t_array_handle *h,
  185. size_t *offp,
  186. size_t *lenp,
  187. ssize_t *incp)
  188. {
  189. scm_array_get_handle (vec, h);
  190. if (1 != scm_array_handle_rank (h))
  191. {
  192. scm_array_handle_release (h);
  193. scm_wrong_type_arg_msg (NULL, 0, vec, "rank 1 bit array");
  194. }
  195. if (offp)
  196. {
  197. scm_t_array_dim *dim = scm_array_handle_dims (h);
  198. *offp = scm_array_handle_bit_elements_offset (h);
  199. *lenp = dim->ubnd - dim->lbnd + 1;
  200. *incp = dim->inc;
  201. }
  202. return scm_array_handle_bit_elements (h);
  203. }
  204. uint32_t *
  205. scm_bitvector_writable_elements (SCM vec,
  206. scm_t_array_handle *h,
  207. size_t *offp,
  208. size_t *lenp,
  209. ssize_t *incp)
  210. {
  211. const uint32_t *ret = scm_bitvector_elements (vec, h, offp, lenp, incp);
  212. if (h->writable_elements != h->elements)
  213. scm_wrong_type_arg_msg (NULL, 0, h->array, "mutable bit array");
  214. return (uint32_t *) ret;
  215. }
  216. int
  217. scm_c_bitvector_bit_is_set (SCM vec, size_t idx)
  218. #define FUNC_NAME "bitvector-bit-set?"
  219. {
  220. VALIDATE_BITVECTOR (1, vec);
  221. if (idx >= BITVECTOR_LENGTH (vec))
  222. SCM_OUT_OF_RANGE (2, scm_from_size_t (idx));
  223. const uint32_t *bits = BITVECTOR_BITS (vec);
  224. return (bits[idx/32] & (1L << (idx%32))) ? 1 : 0;
  225. }
  226. #undef FUNC_NAME
  227. int
  228. scm_c_bitvector_bit_is_clear (SCM vec, size_t idx)
  229. {
  230. return !scm_c_bitvector_bit_is_set (vec, idx);
  231. }
  232. SCM_DEFINE_STATIC (scm_bitvector_bit_set_p, "bitvector-bit-set?", 2, 0, 0,
  233. (SCM vec, SCM idx),
  234. "Return @code{#t} if the bit at index @var{idx} of the \n"
  235. "bitvector @var{vec} is set, or @code{#f} otherwise.")
  236. #define FUNC_NAME s_scm_bitvector_bit_set_p
  237. {
  238. return scm_from_bool (scm_c_bitvector_bit_is_set (vec, scm_to_size_t (idx)));
  239. }
  240. #undef FUNC_NAME
  241. SCM_DEFINE_STATIC (scm_bitvector_bit_clear_p, "bitvector-bit-clear?", 2, 0, 0,
  242. (SCM vec, SCM idx),
  243. "Return @code{#t} if the bit at index @var{idx} of the \n"
  244. "bitvector @var{vec} is clear (unset), or @code{#f} otherwise.")
  245. #define FUNC_NAME s_scm_bitvector_bit_clear_p
  246. {
  247. return scm_from_bool
  248. (scm_c_bitvector_bit_is_clear (vec, scm_to_size_t (idx)));
  249. }
  250. #undef FUNC_NAME
  251. void
  252. scm_c_bitvector_set_bit_x (SCM vec, size_t idx)
  253. #define FUNC_NAME "bitvector-set-bit!"
  254. {
  255. VALIDATE_MUTABLE_BITVECTOR (1, vec);
  256. if (idx >= BITVECTOR_LENGTH (vec))
  257. SCM_OUT_OF_RANGE (2, scm_from_size_t (idx));
  258. uint32_t *bits = BITVECTOR_BITS (vec);
  259. uint32_t mask = 1L << (idx%32);
  260. bits[idx/32] |= mask;
  261. }
  262. #undef FUNC_NAME
  263. void
  264. scm_c_bitvector_clear_bit_x (SCM vec, size_t idx)
  265. #define FUNC_NAME "bitvector-clear-bit!"
  266. {
  267. VALIDATE_MUTABLE_BITVECTOR (1, vec);
  268. if (idx >= BITVECTOR_LENGTH (vec))
  269. SCM_OUT_OF_RANGE (2, scm_from_size_t (idx));
  270. uint32_t *bits = BITVECTOR_BITS (vec);
  271. uint32_t mask = 1L << (idx%32);
  272. bits[idx/32] &= ~mask;
  273. }
  274. #undef FUNC_NAME
  275. SCM_DEFINE_STATIC (scm_bitvector_set_bit_x, "bitvector-set-bit!", 2, 0, 0,
  276. (SCM vec, SCM idx),
  277. "Set the element at index @var{idx} of the bitvector\n"
  278. "@var{vec}.")
  279. #define FUNC_NAME s_scm_bitvector_set_bit_x
  280. {
  281. scm_c_bitvector_set_bit_x (vec, scm_to_size_t (idx));
  282. return SCM_UNSPECIFIED;
  283. }
  284. #undef FUNC_NAME
  285. SCM_DEFINE_STATIC (scm_bitvector_clear_bit_x, "bitvector-clear-bit!", 2, 0, 0,
  286. (SCM vec, SCM idx),
  287. "Clear the element at index @var{idx} of the bitvector\n"
  288. "@var{vec}.")
  289. #define FUNC_NAME s_scm_bitvector_set_bit_x
  290. {
  291. scm_c_bitvector_clear_bit_x (vec, scm_to_size_t (idx));
  292. return SCM_UNSPECIFIED;
  293. }
  294. #undef FUNC_NAME
  295. void
  296. scm_c_bitvector_set_all_bits_x (SCM bv)
  297. #define FUNC_NAME "bitvector-set-all-bits!"
  298. {
  299. VALIDATE_MUTABLE_BITVECTOR (1, bv);
  300. size_t len = BITVECTOR_LENGTH (bv);
  301. if (len > 0)
  302. {
  303. uint32_t *bits = BITVECTOR_BITS (bv);
  304. size_t word_len = (len + 31) / 32;
  305. uint32_t last_mask = ((uint32_t)-1) >> (32*word_len - len);
  306. memset (bits, 0xFF, sizeof(uint32_t)*(word_len-1));
  307. bits[word_len-1] |= last_mask;
  308. }
  309. }
  310. #undef FUNC_NAME
  311. void
  312. scm_c_bitvector_clear_all_bits_x (SCM bv)
  313. #define FUNC_NAME "bitvector-clear-all-bits!"
  314. {
  315. VALIDATE_MUTABLE_BITVECTOR (1, bv);
  316. size_t len = BITVECTOR_LENGTH (bv);
  317. if (len > 0)
  318. {
  319. uint32_t *bits = BITVECTOR_BITS (bv);
  320. size_t word_len = (len + 31) / 32;
  321. uint32_t last_mask = ((uint32_t)-1) >> (32*word_len - len);
  322. memset (bits, 0x00, sizeof(uint32_t)*(word_len-1));
  323. bits[word_len-1] &= ~last_mask;
  324. }
  325. }
  326. #undef FUNC_NAME
  327. SCM_DEFINE_STATIC (scm_bitvector_set_all_bits_x,
  328. "bitvector-set-all-bits!", 1, 0, 0, (SCM vec),
  329. "Set all elements of the bitvector @var{vec}.")
  330. {
  331. scm_c_bitvector_set_all_bits_x (vec);
  332. return SCM_UNSPECIFIED;
  333. }
  334. SCM_DEFINE_STATIC (scm_bitvector_clear_all_bits_x,
  335. "bitvector-clear-all-bits!", 1, 0, 0, (SCM vec),
  336. "Clear all elements of the bitvector @var{vec}.")
  337. {
  338. scm_c_bitvector_clear_all_bits_x (vec);
  339. return SCM_UNSPECIFIED;
  340. }
  341. SCM_DEFINE (scm_list_to_bitvector, "list->bitvector", 1, 0, 0,
  342. (SCM list),
  343. "Return a new bitvector initialized with the elements\n"
  344. "of @var{list}.")
  345. #define FUNC_NAME s_scm_list_to_bitvector
  346. {
  347. size_t bit_len = scm_to_size_t (scm_length (list));
  348. SCM vec = scm_c_make_bitvector (bit_len, SCM_UNDEFINED);
  349. size_t word_len = (bit_len+31)/32;
  350. uint32_t *bits = BITVECTOR_BITS (vec);
  351. size_t i, j;
  352. for (i = 0; i < word_len && scm_is_pair (list); i++, bit_len -= 32)
  353. {
  354. uint32_t mask = 1;
  355. bits[i] = 0;
  356. for (j = 0; j < 32 && j < bit_len;
  357. j++, mask <<= 1, list = SCM_CDR (list))
  358. if (scm_is_true (SCM_CAR (list)))
  359. bits[i] |= mask;
  360. }
  361. return vec;
  362. }
  363. #undef FUNC_NAME
  364. SCM_DEFINE (scm_bitvector_to_list, "bitvector->list", 1, 0, 0,
  365. (SCM vec),
  366. "Return a new list initialized with the elements\n"
  367. "of the bitvector @var{vec}.")
  368. #define FUNC_NAME s_scm_bitvector_to_list
  369. {
  370. SCM res = SCM_EOL;
  371. if (IS_BITVECTOR (vec))
  372. {
  373. const uint32_t *bits = BITVECTOR_BITS (vec);
  374. size_t len = BITVECTOR_LENGTH (vec);
  375. size_t word_len = (len + 31) / 32;
  376. for (size_t i = 0; i < word_len; i++, len -= 32)
  377. {
  378. uint32_t mask = 1;
  379. for (size_t j = 0; j < 32 && j < len; j++, mask <<= 1)
  380. res = scm_cons ((bits[i] & mask)? SCM_BOOL_T : SCM_BOOL_F, res);
  381. }
  382. }
  383. else
  384. {
  385. scm_t_array_handle handle;
  386. size_t off, len;
  387. ssize_t inc;
  388. scm_bitvector_elements (vec, &handle, &off, &len, &inc);
  389. scm_c_issue_deprecation_warning
  390. ("Using bitvector->list on arrays is deprecated. "
  391. "Use array->list instead.");
  392. for (size_t i = 0; i < len; i++)
  393. res = scm_cons (scm_array_handle_ref (&handle, i*inc), res);
  394. scm_array_handle_release (&handle);
  395. }
  396. return scm_reverse_x (res, SCM_EOL);
  397. }
  398. #undef FUNC_NAME
  399. /* From mmix-arith.w by Knuth.
  400. Here's a fun way to count the number of bits in a tetrabyte.
  401. [This classical trick is called the ``Gillies--Miller method for
  402. sideways addition'' in {\sl The Preparation of Programs for an
  403. Electronic Digital Computer\/} by Wilkes, Wheeler, and Gill, second
  404. edition (Reading, Mass.:\ Addison--Wesley, 1957), 191--193. Some of
  405. the tricks used here were suggested by Balbir Singh, Peter
  406. Rossmanith, and Stefan Schwoon.]
  407. */
  408. static size_t
  409. count_ones (uint32_t x)
  410. {
  411. x=x-((x>>1)&0x55555555);
  412. x=(x&0x33333333)+((x>>2)&0x33333333);
  413. x=(x+(x>>4))&0x0f0f0f0f;
  414. x=x+(x>>8);
  415. return (x+(x>>16)) & 0xff;
  416. }
  417. size_t
  418. scm_c_bitvector_count (SCM bitvector)
  419. #define FUNC_NAME "bitvector-count"
  420. {
  421. VALIDATE_BITVECTOR (1, bitvector);
  422. size_t len = BITVECTOR_LENGTH (bitvector);
  423. if (len == 0)
  424. return 0;
  425. const uint32_t *bits = BITVECTOR_BITS (bitvector);
  426. size_t count = 0;
  427. size_t word_len = (len + 31) / 32;
  428. size_t i;
  429. for (i = 0; i < word_len-1; i++)
  430. count += count_ones (bits[i]);
  431. uint32_t last_mask = ((uint32_t)-1) >> (32*word_len - len);
  432. count += count_ones (bits[i] & last_mask);
  433. return count;
  434. }
  435. #undef FUNC_NAME
  436. SCM_DEFINE_STATIC (scm_bitvector_count, "bitvector-count", 1, 0, 0,
  437. (SCM bitvector),
  438. "Return the number of set bits in @var{bitvector}.")
  439. #define FUNC_NAME s_scm_bitvector_count
  440. {
  441. return scm_from_size_t (scm_c_bitvector_count (bitvector));
  442. }
  443. #undef FUNC_NAME
  444. /* returns 32 for x == 0.
  445. */
  446. static size_t
  447. find_first_one (uint32_t x)
  448. {
  449. size_t pos = 0;
  450. /* do a binary search in x. */
  451. if ((x & 0xFFFF) == 0)
  452. x >>= 16, pos += 16;
  453. if ((x & 0xFF) == 0)
  454. x >>= 8, pos += 8;
  455. if ((x & 0xF) == 0)
  456. x >>= 4, pos += 4;
  457. if ((x & 0x3) == 0)
  458. x >>= 2, pos += 2;
  459. if ((x & 0x1) == 0)
  460. pos += 1;
  461. return pos;
  462. }
  463. SCM_DEFINE (scm_bitvector_position, "bitvector-position", 2, 1, 0,
  464. (SCM v, SCM bit, SCM start),
  465. "Return the index of the first occurrence of @var{bit} in bit\n"
  466. "vector @var{v}, starting from @var{start} (or zero if not given)\n."
  467. "If there is no @var{bit} entry between @var{start} and the end of\n"
  468. "@var{v}, then return @code{#f}. For example,\n"
  469. "\n"
  470. "@example\n"
  471. "(bitvector-position #*000101 #t) @result{} 3\n"
  472. "(bitvector-position #*0001111 #f 3) @result{} #f\n"
  473. "@end example")
  474. #define FUNC_NAME s_scm_bitvector_position
  475. {
  476. VALIDATE_BITVECTOR (1, v);
  477. size_t len = BITVECTOR_LENGTH (v);
  478. int c_bit = scm_to_bool (bit);
  479. size_t first_bit =
  480. SCM_UNBNDP (start) ? 0 : scm_to_unsigned_integer (start, 0, len);
  481. if (first_bit == len)
  482. return SCM_BOOL_F;
  483. const uint32_t *bits = BITVECTOR_BITS (v);
  484. size_t word_len = (len + 31) / 32;
  485. uint32_t last_mask = ((uint32_t)-1) >> (32*word_len - len);
  486. size_t first_word = first_bit / 32;
  487. uint32_t first_mask =
  488. ((uint32_t)-1) << (first_bit - 32*first_word);
  489. for (size_t i = first_word; i < word_len; i++)
  490. {
  491. uint32_t w = c_bit ? bits[i] : ~bits[i];
  492. if (i == first_word)
  493. w &= first_mask;
  494. if (i == word_len-1)
  495. w &= last_mask;
  496. if (w)
  497. return scm_from_size_t (32*i + find_first_one (w));
  498. }
  499. return SCM_BOOL_F;
  500. }
  501. #undef FUNC_NAME
  502. void
  503. scm_c_bitvector_set_bits_x (SCM v, SCM bits)
  504. #define FUNC_NAME "bitvector-set-bits!"
  505. {
  506. VALIDATE_MUTABLE_BITVECTOR (1, v);
  507. VALIDATE_BITVECTOR (2, bits);
  508. size_t v_len = BITVECTOR_LENGTH (v);
  509. uint32_t *v_bits = BITVECTOR_BITS (v);
  510. size_t kv_len = BITVECTOR_LENGTH (bits);
  511. const uint32_t *kv_bits = BITVECTOR_BITS (bits);
  512. if (v_len < kv_len)
  513. scm_misc_error (NULL,
  514. "selection bitvector longer than target bitvector",
  515. SCM_EOL);
  516. if (kv_len > 0)
  517. {
  518. size_t word_len = (kv_len + 31) / 32;
  519. uint32_t last_mask = ((uint32_t)-1) >> (32*word_len - kv_len);
  520. size_t i;
  521. for (i = 0; i < word_len-1; i++)
  522. v_bits[i] |= kv_bits[i];
  523. v_bits[i] |= kv_bits[i] & last_mask;
  524. }
  525. }
  526. #undef FUNC_NAME
  527. void
  528. scm_c_bitvector_clear_bits_x (SCM v, SCM bits)
  529. #define FUNC_NAME "bitvector-clear-bits!"
  530. {
  531. VALIDATE_MUTABLE_BITVECTOR (1, v);
  532. VALIDATE_BITVECTOR (2, bits);
  533. size_t v_len = BITVECTOR_LENGTH (v);
  534. uint32_t *v_bits = BITVECTOR_BITS (v);
  535. size_t kv_len = BITVECTOR_LENGTH (bits);
  536. const uint32_t *kv_bits = BITVECTOR_BITS (bits);
  537. if (v_len < kv_len)
  538. scm_misc_error (NULL,
  539. "selection bitvector longer than target bitvector",
  540. SCM_EOL);
  541. if (kv_len > 0)
  542. {
  543. size_t word_len = (kv_len + 31) / 32;
  544. uint32_t last_mask = ((uint32_t)-1) >> (32*word_len - kv_len);
  545. size_t i;
  546. for (i = 0; i < word_len-1; i++)
  547. v_bits[i] &= ~kv_bits[i];
  548. v_bits[i] &= ~(kv_bits[i] & last_mask);
  549. }
  550. }
  551. #undef FUNC_NAME
  552. SCM_DEFINE_STATIC (scm_bitvector_set_bits_x, "bitvector-set-bits!", 2, 0, 0,
  553. (SCM v, SCM bits),
  554. "Update the bitvector @var{v} in place by performing a\n"
  555. "logical OR of its bits with those of @var{bits}.\n"
  556. "For example:\n"
  557. "\n"
  558. "@example\n"
  559. "(define bv (bitvector-copy #*11000010))\n"
  560. "(bitvector-set-bits! bv #*10010001)\n"
  561. "bv\n"
  562. "@result{} #*11010011\n"
  563. "@end example")
  564. #define FUNC_NAME s_scm_bitvector_set_bits_x
  565. {
  566. scm_c_bitvector_set_bits_x (v, bits);
  567. return SCM_UNSPECIFIED;
  568. }
  569. #undef FUNC_NAME
  570. SCM_DEFINE_STATIC (scm_bitvector_clear_bits_x, "bitvector-clear-bits!", 2, 0, 0,
  571. (SCM v, SCM bits),
  572. "Update the bitvector @var{v} in place by performing a\n"
  573. "logical AND of its bits with the complement of those of\n"
  574. "@var{bits}. For example:\n"
  575. "\n"
  576. "@example\n"
  577. "(define bv (bitvector-copy #*11000010))\n"
  578. "(bitvector-clear-bits! bv #*10010001)\n"
  579. "bv\n"
  580. "@result{} #*01000010\n"
  581. "@end example")
  582. #define FUNC_NAME s_scm_bitvector_clear_bits_x
  583. {
  584. scm_c_bitvector_clear_bits_x (v, bits);
  585. return SCM_UNSPECIFIED;
  586. }
  587. #undef FUNC_NAME
  588. SCM_DEFINE (scm_bitvector_copy, "bitvector-copy", 1, 2, 0,
  589. (SCM bv, SCM start, SCM end),
  590. "Returns a freshly allocated bitvector containing the elements\n"
  591. "of bitvector @var{bv} between @var{start} and @var{end}.\n\n"
  592. "@var{start} defaults to 0 and @var{end} defaults to the\n"
  593. "length of @var{bv}.")
  594. #define FUNC_NAME s_scm_bitvector_copy
  595. {
  596. VALIDATE_BITVECTOR (1, bv);
  597. /* cf scm_vector_copy */
  598. size_t cstart = 0, cend = BITVECTOR_LENGTH (bv);
  599. if (!SCM_UNBNDP (start))
  600. {
  601. cstart = scm_to_size_t (start);
  602. SCM_ASSERT_RANGE (SCM_ARG2, start, cstart<=cend);
  603. if (!SCM_UNBNDP (end))
  604. {
  605. size_t e = scm_to_size_t (end);
  606. SCM_ASSERT_RANGE (SCM_ARG3, end, e>=cstart && e<=cend);
  607. cend = e;
  608. }
  609. }
  610. size_t len = cend-cstart;
  611. SCM result = scm_c_make_bitvector (len, SCM_BOOL_F);
  612. const uint32_t *kv_bits = BITVECTOR_BITS (bv);
  613. uint32_t *v_bits = BITVECTOR_BITS (result);
  614. if (len > 0)
  615. {
  616. size_t wlen = (len + 31u) / 32u;
  617. size_t wshift = cstart / 32u;
  618. size_t bshift = cstart % 32u;
  619. if (0 == bshift)
  620. memcpy (v_bits, kv_bits + wshift, wlen*sizeof(uint32_t));
  621. else
  622. for (size_t i = 0; i < wlen; ++i)
  623. v_bits[i] = (kv_bits[i + wshift] >> bshift) | (kv_bits[i + wshift + 1] << (32-bshift));
  624. }
  625. return result;
  626. }
  627. #undef FUNC_NAME
  628. size_t
  629. scm_c_bitvector_count_bits (SCM bv, SCM bits)
  630. #define FUNC_NAME "bitvector-count-bits"
  631. {
  632. VALIDATE_BITVECTOR (1, bv);
  633. VALIDATE_BITVECTOR (2, bits);
  634. size_t v_len = BITVECTOR_LENGTH (bv);
  635. const uint32_t *v_bits = BITVECTOR_BITS (bv);
  636. size_t kv_len = BITVECTOR_LENGTH (bits);
  637. const uint32_t *kv_bits = BITVECTOR_BITS (bits);
  638. if (v_len < kv_len)
  639. SCM_MISC_ERROR ("selection bitvector longer than target bitvector",
  640. SCM_EOL);
  641. size_t i, word_len = (kv_len + 31) / 32;
  642. uint32_t last_mask = ((uint32_t)-1) >> (32*word_len - kv_len);
  643. size_t count = 0;
  644. for (i = 0; i < word_len-1; i++)
  645. count += count_ones (v_bits[i] & kv_bits[i]);
  646. count += count_ones (v_bits[i] & kv_bits[i] & last_mask);
  647. return count;
  648. }
  649. #undef FUNC_NAME
  650. SCM_DEFINE_STATIC (scm_bitvector_count_bits, "bitvector-count-bits", 2, 0, 0,
  651. (SCM v, SCM kv),
  652. "Return a count of how many entries in bit vector @var{v}\n"
  653. "are set, with @var{kv} selecting the entries to consider.\n"
  654. "\n"
  655. "For example,\n"
  656. "\n"
  657. "@example\n"
  658. "(bitvector-count-bits #*01110111 #*11001101) @result{} 3\n"
  659. "@end example")
  660. #define FUNC_NAME s_scm_bitvector_count_bits
  661. {
  662. return scm_from_size_t (scm_c_bitvector_count_bits (v, kv));
  663. }
  664. #undef FUNC_NAME
  665. void
  666. scm_c_bitvector_flip_all_bits_x (SCM v)
  667. #define FUNC_NAME "bitvector-flip-all-bits!"
  668. {
  669. VALIDATE_MUTABLE_BITVECTOR (1, v);
  670. size_t len = BITVECTOR_LENGTH (v);
  671. uint32_t *bits = BITVECTOR_BITS (v);
  672. size_t word_len = (len + 31) / 32;
  673. uint32_t last_mask = ((uint32_t)-1) >> (32*word_len - len);
  674. size_t i;
  675. for (i = 0; i < word_len-1; i++)
  676. bits[i] = ~bits[i];
  677. bits[i] = bits[i] ^ last_mask;
  678. }
  679. #undef FUNC_NAME
  680. SCM_DEFINE_STATIC (scm_bitvector_flip_all_bits_x,
  681. "bitvector-flip-all-bits!", 1, 0, 0, (SCM v),
  682. "Modify the bit vector @var{v} in place by setting all\n"
  683. "clear bits and clearing all set bits.")
  684. #define FUNC_NAME s_scm_bitvector_flip_all_bits_x
  685. {
  686. scm_c_bitvector_flip_all_bits_x (v);
  687. return SCM_UNSPECIFIED;
  688. }
  689. #undef FUNC_NAME
  690. SCM_VECTOR_IMPLEMENTATION (SCM_ARRAY_ELEMENT_TYPE_BIT, make_bitvector)
  691. void
  692. scm_init_bitvectors ()
  693. {
  694. #include "bitvectors.x"
  695. }