shithub: femtolisp

Download patch

ref: b0833420dc549b790af34f2bcf83d4340fc951e5
parent: e4cc50bddd8707dc8feffc623eda092a86e03d8e
author: Sigrid Solveig Haflínudóttir <[email protected]>
date: Tue Nov 12 22:52:01 EST 2024

bitvector_*: turn into defines

--- a/bitvector.c
+++ b/bitvector.c
@@ -1,34 +1,3 @@
-/*
-  bit vector primitives
-
-  todo:
-  * reverse
-  * nreverse
- (- rotate left/right)
-  * shl_to
-  * not
-  - shr_row, shl_row
-
-  These routines are the back end supporting bit matrices. Many operations
-  on bit matrices are slow (such as accessing or setting a single element!)
-  but certain operations are privileged and lend themselves to extremely
-  efficient implementation due to the bit-vector nature of machine integers.
-  These are:
-  done:
-	&  |  $  ~  copy  reverse  fill  sum  prod
-  todo:
-	shift  trans  rowswap
-  would be nice:
-	channel  interleave
-
-  Important note:
-  Out-of-place functions always assume dest and source have the same amount
-  of space available.
-
-  shr_to, shl_to, not_to, and reverse_to assume source and dest don't overlap
-  and_to, or_to, and xor_to allow overlap.
-*/
-
 #include "llt.h"
 
 uint32_t *
@@ -44,31 +13,4 @@
 		memset(&p[osz/sizeof(uint32_t)], 0, sz-osz);
 	}
 	return p;
-}
-
-uint32_t *
-bitvector_new(uint64_t n, int initzero)
-{
-	return bitvector_resize(nil, 0, n, initzero);
-}
-
-size_t
-bitvector_nwords(uint64_t nbits)
-{
-	return (nbits+31)>>5;
-}
-
-void
-bitvector_set(uint32_t *b, uint64_t n, uint32_t c)
-{
-	if(c)
-		b[n>>5] |= 1U<<(n&31);
-	else
-		b[n>>5] &= ~(1U<<(n&31));
-}
-
-uint32_t
-bitvector_get(uint32_t *b, uint64_t n)
-{
-	return b[n>>5] & (1U<<(n&31));
 }
--- a/bitvector.h
+++ b/bitvector.h
@@ -1,5 +1,11 @@
-uint32_t *bitvector_new(uint64_t n, int initzero);
 uint32_t *bitvector_resize(uint32_t *b, uint64_t oldsz, uint64_t newsz, int initzero);
-size_t bitvector_nwords(uint64_t nbits);
-void bitvector_set(uint32_t *b, uint64_t n, uint32_t c);
-uint32_t bitvector_get(uint32_t *b, uint64_t n);
+
+#define bitvector_new(n, initzero) bitvector_resize(nil, 0, (n), (initzero))
+#define bitvector_nwords(nbits) (((uint64_t)(nbits)+31)>>5)
+#define bitvector_get(b, n) (b[(n)>>5] & (1U<<((n)&31)))
+#define bitvector_set(b, n) do{ \
+		b[(n)>>5] |= 1U<<((n)&31); \
+	}while(0)
+#define bitvector_reset(b, n) do{ \
+		b[(n)>>5] &= ~(1U<<((n)&31)); \
+	}while(0)
--- a/flisp.h
+++ b/flisp.h
@@ -106,8 +106,8 @@
 #define cons_reserve(n) tagptr(alloc_words((n)*2), TAG_CONS)
 #define cons_index(c)  (((cons_t*)ptr(c))-((cons_t*)FL(fromspace)))
 #define ismarked(c)     bitvector_get(FL(consflags), cons_index(c))
-#define mark_cons(c)   bitvector_set(FL(consflags), cons_index(c), 1)
-#define unmark_cons(c) bitvector_set(FL(consflags), cons_index(c), 0)
+#define mark_cons(c)   bitvector_set(FL(consflags), cons_index(c))
+#define unmark_cons(c) bitvector_reset(FL(consflags), cons_index(c))
 
 #define isforwarded(v) (((value_t*)ptr(v))[0] == TAG_FWD)
 #define forwardloc(v) (((value_t*)ptr(v))[1])