]> git.vpit.fr Git - perl/modules/Scalar-Vec-Util.git/blobdiff - bitvect.h
Update VPIT::TestHelpers to 15e8aee3
[perl/modules/Scalar-Vec-Util.git] / bitvect.h
index a90ada96b4a1c5acae52b3f13d9d57519da9c0ca..207279f39afcf375074af5010042e37506be8f75 100644 (file)
--- a/bitvect.h
+++ b/bitvect.h
@@ -5,17 +5,17 @@
 
 /* ... Generic macros ...................................................... */
 
-#define INLINE_DECLARE(P) STATIC P
+#define INLINE_DECLARE(P) static P
 #define INLINE_DEFINE
 
 #ifndef BV_UNIT
 # define BV_UNIT unsigned char
 #endif
 
-#define BV_SIZE(I) ((((I) % CHAR_BIT) != 0) + ((I) / CHAR_BIT))
-
 #define BITS(T) (CHAR_BIT * sizeof(T))
 
+#define BV_SIZE(I) (((((I) % BITS(BV_UNIT)) != 0) + ((I) / BITS(BV_UNIT))) * sizeof(BV_UNIT))
+
 /* 0 <= I <  CHAR_BIT * sizeof(T) */
 #define BV_MASK_LOWER(T, I)  (~((~((T) 0)) << (I)))
 /* 0 <  I <= CHAR_BIT * sizeof(T) */
@@ -230,9 +230,6 @@ INLINE_DECLARE(void bv_copy(void *t_, size_t ts, const void *f_, size_t fs, size
  T ins, mask, *t = (T *) t_;
  const T *f = (const T *) f_, *end;
 
- if (!l)
-  return;
-
  t  += ts / BITS(T);
  ts %= BITS(T);
 
@@ -295,7 +292,7 @@ INLINE_DECLARE(void bv_move(void *bv_, size_t ts, size_t fs, size_t l))
  T ins, tmp, mask, *bv = (T *) bv_, *t, *f;
  const T *begin, *end;
 
- if (!l || ts == fs)
+ if (ts == fs)
   return;
 
  to = ts % BITS(T);
@@ -335,6 +332,48 @@ INLINE_DECLARE(void bv_move(void *bv_, size_t ts, size_t fs, size_t l))
 #endif /* INLINE_DEFINE */
 #undef T
 
+/* ... Test if zero ........................................................ */
+
+#define T BV_UNIT
+INLINE_DECLARE(int bv_zero(const void *bv_, size_t s, size_t l))
+#ifdef INLINE_DEFINE
+{
+ size_t o;
+ T mask;
+ const T *bv = (const T *) bv_, *end;
+
+ bv += s / BITS(T);
+ o   = s % BITS(T);
+
+ mask = BV_MASK_HIGHER(T, BITS(T) - o);
+ if (o + l <= BITS(T)) {
+  if (o + l < BITS(T))
+   mask &= BV_MASK_LOWER(T, o + l);
+  if (*bv & mask)
+   return 0;
+ } else {
+  if (*bv & mask)
+   return 0;
+  ++bv;
+  l  -= (BITS(T) - o);
+  end = bv + l / BITS(T);
+  for (; bv < end; ++bv) {
+   if (*bv)
+    return 0;
+  }
+  o = l % BITS(T);
+  if (o) {
+   mask = BV_MASK_LOWER(T, o);
+   if (*bv & mask)
+    return 0;
+  }
+ }
+
+ return 1;
+}
+#endif /* INLINE_DEFINE */
+#undef T
+
 /* ... Compare ............................................................. */
 
 #define BV_EQ(T, B1, B2) \
@@ -503,11 +542,8 @@ INLINE_DECLARE(void bv_fill(void *bv_, size_t s, size_t l, unsigned int f))
  size_t o, k;
  T mask, *bv = (T *) bv_;
 
- if (!l)
-  return;
-
  if (f)
-  f = ~0;
+  f = ~0u;
 
  bv += s / BITS(T);
  o   = s % BITS(T);