mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-04-30 11:50:28 +02:00
Replace bit-count with bitvector-count
The old name was wonky and had bad argument order. * NEWS: Add entry. * doc/ref/api-data.texi (Bit Vectors): Update. * libguile/bitvectors.h: * libguile/bitvectors.c (VALIDATE_BITVECTOR): New helper. (scm_bitvector_count): New function. * libguile/deprecated.h: * libguile/deprecated.c (scm_bit_count): Deprecate. * module/ice-9/sandbox.scm (bitvector-bindings): Replace bit-count with bitvector-count. * module/srfi/srfi-60.scm: No need to #:replace bit-count. * module/system/vm/frame.scm (available-bindings): Use bitvector-count. * test-suite/tests/bitvectors.test ("bitvector-count"): Add test.
This commit is contained in:
parent
24a34074ef
commit
cae74359de
10 changed files with 88 additions and 49 deletions
11
NEWS
11
NEWS
|
@ -7,8 +7,19 @@ Please send Guile bug reports to bug-guile@gnu.org.
|
||||||
|
|
||||||
Changes in 3.0.3 (since 3.0.2)
|
Changes in 3.0.3 (since 3.0.2)
|
||||||
|
|
||||||
|
* New interfaces and functionality
|
||||||
|
|
||||||
|
** New bitvector-count procedure
|
||||||
|
|
||||||
|
This replaces the wonky "bit-count" procedure. See "Bit Vectors" in the
|
||||||
|
manual, for more.
|
||||||
|
|
||||||
* New deprecations
|
* New deprecations
|
||||||
|
|
||||||
|
** bit-count deprecated
|
||||||
|
|
||||||
|
Use bitvector-count instead. See "Bit Vectors" in the manual.
|
||||||
|
|
||||||
** Passing a u32vector to 'bit-set*!' and 'bit-count*' deprecated
|
** Passing a u32vector to 'bit-set*!' and 'bit-count*' deprecated
|
||||||
|
|
||||||
These functions had an interface that allowed the second bit-selection
|
These functions had an interface that allowed the second bit-selection
|
||||||
|
|
|
@ -6613,13 +6613,12 @@ Return a new list initialized with the elements
|
||||||
of the bitvector @var{vec}.
|
of the bitvector @var{vec}.
|
||||||
@end deffn
|
@end deffn
|
||||||
|
|
||||||
@deffn {Scheme Procedure} bit-count bool bitvector
|
@deffn {Scheme Procedure} bitvector-count bitvector
|
||||||
@deffnx {C Function} scm_bit_count (bool, bitvector)
|
@deffnx {C Function} scm_bitvector_count (bitvector)
|
||||||
Return a count of how many entries in @var{bitvector} are equal to
|
Return a count of how many entries in @var{bitvector} are set.
|
||||||
@var{bool}. For example,
|
|
||||||
|
|
||||||
@example
|
@example
|
||||||
(bit-count #f #*000111000) @result{} 6
|
(bitvector-count #*000111000) @result{} 3
|
||||||
@end example
|
@end example
|
||||||
@end deffn
|
@end deffn
|
||||||
|
|
||||||
|
|
|
@ -58,6 +58,10 @@
|
||||||
#define BITVECTOR_LENGTH(obj) ((size_t)SCM_CELL_WORD_1(obj))
|
#define BITVECTOR_LENGTH(obj) ((size_t)SCM_CELL_WORD_1(obj))
|
||||||
#define BITVECTOR_BITS(obj) ((uint32_t *)SCM_CELL_WORD_2(obj))
|
#define BITVECTOR_BITS(obj) ((uint32_t *)SCM_CELL_WORD_2(obj))
|
||||||
|
|
||||||
|
#define VALIDATE_BITVECTOR(_pos, _obj) \
|
||||||
|
SCM_ASSERT_TYPE (IS_BITVECTOR (_obj), (_obj), (_pos), FUNC_NAME, \
|
||||||
|
"bitvector")
|
||||||
|
|
||||||
uint32_t *
|
uint32_t *
|
||||||
scm_i_bitvector_bits (SCM vec)
|
scm_i_bitvector_bits (SCM vec)
|
||||||
{
|
{
|
||||||
|
@ -479,51 +483,30 @@ count_ones (uint32_t x)
|
||||||
return (x+(x>>16)) & 0xff;
|
return (x+(x>>16)) & 0xff;
|
||||||
}
|
}
|
||||||
|
|
||||||
SCM_DEFINE (scm_bit_count, "bit-count", 2, 0, 0,
|
SCM_DEFINE (scm_bitvector_count, "bitvector-count", 1, 0, 0,
|
||||||
(SCM b, SCM bitvector),
|
(SCM bitvector),
|
||||||
"Return the number of occurrences of the boolean @var{b} in\n"
|
"Return the number of set bits in @var{bitvector}.")
|
||||||
"@var{bitvector}.")
|
#define FUNC_NAME s_scm_bitvector_count
|
||||||
#define FUNC_NAME s_scm_bit_count
|
|
||||||
{
|
{
|
||||||
int bit = scm_to_bool (b);
|
VALIDATE_BITVECTOR (1, bitvector);
|
||||||
size_t count = 0, len;
|
|
||||||
|
|
||||||
if (IS_BITVECTOR (bitvector))
|
size_t len = BITVECTOR_LENGTH (bitvector);
|
||||||
{
|
|
||||||
len = BITVECTOR_LENGTH (bitvector);
|
if (len == 0)
|
||||||
|
return SCM_INUM0;
|
||||||
|
|
||||||
if (len > 0)
|
|
||||||
{
|
|
||||||
const uint32_t *bits = BITVECTOR_BITS (bitvector);
|
const uint32_t *bits = BITVECTOR_BITS (bitvector);
|
||||||
size_t word_len = (len + 31) / 32;
|
size_t count = 0;
|
||||||
uint32_t last_mask = ((uint32_t)-1) >> (32*word_len - len);
|
|
||||||
|
|
||||||
|
size_t word_len = (len + 31) / 32;
|
||||||
size_t i;
|
size_t i;
|
||||||
for (i = 0; i < word_len-1; i++)
|
for (i = 0; i < word_len-1; i++)
|
||||||
count += count_ones (bits[i]);
|
count += count_ones (bits[i]);
|
||||||
|
|
||||||
|
uint32_t last_mask = ((uint32_t)-1) >> (32*word_len - len);
|
||||||
count += count_ones (bits[i] & last_mask);
|
count += count_ones (bits[i] & last_mask);
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
scm_t_array_handle handle;
|
|
||||||
size_t off;
|
|
||||||
ssize_t inc;
|
|
||||||
|
|
||||||
scm_bitvector_elements (bitvector, &handle, &off, &len, &inc);
|
return scm_from_size_t (count);
|
||||||
|
|
||||||
scm_c_issue_deprecation_warning
|
|
||||||
("Using bit-count on arrays is deprecated. "
|
|
||||||
"Use array->list instead.");
|
|
||||||
|
|
||||||
for (size_t i = 0; i < len; i++)
|
|
||||||
if (scm_is_true (scm_array_handle_ref (&handle, i*inc)))
|
|
||||||
count++;
|
|
||||||
|
|
||||||
scm_array_handle_release (&handle);
|
|
||||||
}
|
|
||||||
|
|
||||||
return scm_from_size_t (bit ? count : len-count);
|
|
||||||
}
|
}
|
||||||
#undef FUNC_NAME
|
#undef FUNC_NAME
|
||||||
|
|
||||||
|
|
|
@ -42,7 +42,8 @@ SCM_API SCM scm_list_to_bitvector (SCM list);
|
||||||
SCM_API SCM scm_bitvector_to_list (SCM vec);
|
SCM_API SCM scm_bitvector_to_list (SCM vec);
|
||||||
SCM_API SCM scm_bitvector_fill_x (SCM vec, SCM val);
|
SCM_API SCM scm_bitvector_fill_x (SCM vec, SCM val);
|
||||||
|
|
||||||
SCM_API SCM scm_bit_count (SCM item, SCM seq);
|
SCM_API SCM scm_bitvector_count (SCM v);
|
||||||
|
|
||||||
SCM_API SCM scm_bit_position (SCM item, SCM v, SCM k);
|
SCM_API SCM scm_bit_position (SCM item, SCM v, SCM k);
|
||||||
SCM_API SCM scm_bit_set_star_x (SCM v, SCM kv, SCM obj);
|
SCM_API SCM scm_bit_set_star_x (SCM v, SCM kv, SCM obj);
|
||||||
SCM_API SCM scm_bit_count_star (SCM v, SCM kv, SCM obj);
|
SCM_API SCM scm_bit_count_star (SCM v, SCM kv, SCM obj);
|
||||||
|
|
|
@ -27,9 +27,11 @@
|
||||||
|
|
||||||
#define SCM_BUILDING_DEPRECATED_CODE
|
#define SCM_BUILDING_DEPRECATED_CODE
|
||||||
|
|
||||||
|
#include "boolean.h"
|
||||||
#include "bitvectors.h"
|
#include "bitvectors.h"
|
||||||
#include "deprecation.h"
|
#include "deprecation.h"
|
||||||
#include "gc.h"
|
#include "gc.h"
|
||||||
|
#include "gsubr.h"
|
||||||
#include "strings.h"
|
#include "strings.h"
|
||||||
|
|
||||||
#include "deprecated.h"
|
#include "deprecated.h"
|
||||||
|
@ -85,6 +87,43 @@ scm_find_executable (const char *name)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
SCM_DEFINE (scm_bit_count, "bit-count", 2, 0, 0,
|
||||||
|
(SCM b, SCM bitvector),
|
||||||
|
"Return the number of occurrences of the boolean @var{b} in\n"
|
||||||
|
"@var{bitvector}.")
|
||||||
|
#define FUNC_NAME s_scm_bit_count
|
||||||
|
{
|
||||||
|
int bit = scm_to_bool (b);
|
||||||
|
size_t count = 0, len;
|
||||||
|
|
||||||
|
scm_c_issue_deprecation_warning
|
||||||
|
("bit-count is deprecated. Use bitvector-count, or a loop over array-ref "
|
||||||
|
"if array support is needed.");
|
||||||
|
|
||||||
|
if (scm_is_true (scm_bitvector_p (bitvector)))
|
||||||
|
{
|
||||||
|
len = scm_to_size_t (scm_bitvector_length (bitvector));
|
||||||
|
count = scm_to_size_t (scm_bitvector_count (bitvector));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
scm_t_array_handle handle;
|
||||||
|
size_t off;
|
||||||
|
ssize_t inc;
|
||||||
|
|
||||||
|
scm_bitvector_elements (bitvector, &handle, &off, &len, &inc);
|
||||||
|
|
||||||
|
for (size_t i = 0; i < len; i++)
|
||||||
|
if (scm_is_true (scm_array_handle_ref (&handle, i*inc)))
|
||||||
|
count++;
|
||||||
|
|
||||||
|
scm_array_handle_release (&handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
return scm_from_size_t (bit ? count : len-count);
|
||||||
|
}
|
||||||
|
#undef FUNC_NAME
|
||||||
|
|
||||||
SCM
|
SCM
|
||||||
scm_istr2bve (SCM str)
|
scm_istr2bve (SCM str)
|
||||||
{
|
{
|
||||||
|
|
|
@ -115,6 +115,7 @@ typedef struct scm_thread scm_i_thread SCM_DEPRECATED_TYPE;
|
||||||
|
|
||||||
SCM_DEPRECATED char* scm_find_executable (const char *name);
|
SCM_DEPRECATED char* scm_find_executable (const char *name);
|
||||||
|
|
||||||
|
SCM_DEPRECATED SCM scm_bit_count (SCM item, SCM seq);
|
||||||
SCM_DEPRECATED SCM scm_istr2bve (SCM str);
|
SCM_DEPRECATED SCM scm_istr2bve (SCM str);
|
||||||
|
|
||||||
void scm_i_init_deprecated (void);
|
void scm_i_init_deprecated (void);
|
||||||
|
|
|
@ -1075,7 +1075,7 @@ allocation limit is exceeded, an exception will be thrown to the
|
||||||
|
|
||||||
(define bitvector-bindings
|
(define bitvector-bindings
|
||||||
'(((guile)
|
'(((guile)
|
||||||
bit-count
|
bitvector-count
|
||||||
bit-count*
|
bit-count*
|
||||||
bit-extract
|
bit-extract
|
||||||
bit-position
|
bit-position
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
;;; srfi-60.scm --- Integers as Bits
|
;;; srfi-60.scm --- Integers as Bits
|
||||||
|
|
||||||
;; Copyright (C) 2005, 2006, 2010 Free Software Foundation, Inc.
|
;; Copyright (C) 2005, 2006, 2010, 2020 Free Software Foundation, Inc.
|
||||||
;;
|
;;
|
||||||
;; This library is free software; you can redistribute it and/or
|
;; This library is free software; you can redistribute it and/or
|
||||||
;; modify it under the terms of the GNU Lesser General Public
|
;; modify it under the terms of the GNU Lesser General Public
|
||||||
|
@ -26,6 +26,7 @@
|
||||||
log2-binary-factors first-set-bit
|
log2-binary-factors first-set-bit
|
||||||
bit-set?
|
bit-set?
|
||||||
copy-bit
|
copy-bit
|
||||||
|
bit-count
|
||||||
bit-field
|
bit-field
|
||||||
copy-bit-field
|
copy-bit-field
|
||||||
arithmetic-shift
|
arithmetic-shift
|
||||||
|
@ -34,7 +35,6 @@
|
||||||
integer->list
|
integer->list
|
||||||
list->integer
|
list->integer
|
||||||
booleans->integer)
|
booleans->integer)
|
||||||
#:replace (bit-count)
|
|
||||||
#:re-export (logand
|
#:re-export (logand
|
||||||
logior
|
logior
|
||||||
logxor
|
logxor
|
||||||
|
|
|
@ -243,7 +243,7 @@
|
||||||
(out (vector-ref outv n))
|
(out (vector-ref outv n))
|
||||||
(kill (vector-ref killv n))
|
(kill (vector-ref killv n))
|
||||||
(gen (vector-ref genv n)))
|
(gen (vector-ref genv n)))
|
||||||
(let ((out-count (or changed? (bit-count #t out))))
|
(let ((out-count (or changed? (bitvector-count out))))
|
||||||
(bitvector-fill! in (not (zero? n)))
|
(bitvector-fill! in (not (zero? n)))
|
||||||
(let lp ((preds (vector-ref preds n)))
|
(let lp ((preds (vector-ref preds n)))
|
||||||
(match preds
|
(match preds
|
||||||
|
@ -258,7 +258,7 @@
|
||||||
(bitvector-set! out def #t))
|
(bitvector-set! out def #t))
|
||||||
gen)
|
gen)
|
||||||
(lp (1+ n) first?
|
(lp (1+ n) first?
|
||||||
(or changed? (not (eqv? out-count (bit-count #t out))))))))
|
(or changed? (not (eqv? out-count (bitvector-count out))))))))
|
||||||
((or changed? first?)
|
((or changed? first?)
|
||||||
(lp 0 #f #f))))
|
(lp 0 #f #f))))
|
||||||
|
|
||||||
|
|
|
@ -71,5 +71,10 @@
|
||||||
(bit-set*! v #*101 #f)
|
(bit-set*! v #*101 #f)
|
||||||
(equal? v #*0100))))
|
(equal? v #*0100))))
|
||||||
|
|
||||||
|
(with-test-prefix "bitvector-count"
|
||||||
|
(pass-if-equal 6 (bitvector-count #*01110111))
|
||||||
|
(pass-if-equal 2 (let ((bv #*01110111))
|
||||||
|
(- (bitvector-length bv) (bitvector-count bv)))))
|
||||||
|
|
||||||
(with-test-prefix "bit-count*"
|
(with-test-prefix "bit-count*"
|
||||||
(pass-if-equal 3 (bit-count* #*01110111 #*11001101 #t)))
|
(pass-if-equal 3 (bit-count* #*01110111 #*11001101 #t)))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue