Factor out routine for octet size calculation.

Create a dedicated static function which given a bit size returns a byte
size.


Project: http://git-wip-us.apache.org/repos/asf/lucy/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/efd6ac7b
Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/efd6ac7b
Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/efd6ac7b

Branch: refs/heads/master
Commit: efd6ac7bf73ffa4b99bfe493bb3ef1ef5c5e8ef6
Parents: 0728569
Author: Marvin Humphrey <[email protected]>
Authored: Wed Apr 6 17:41:12 2016 -0700
Committer: Marvin Humphrey <[email protected]>
Committed: Tue Apr 12 21:38:55 2016 -0700

----------------------------------------------------------------------
 core/Lucy/Object/BitVector.c | 37 ++++++++++++++++++++++---------------
 1 file changed, 22 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy/blob/efd6ac7b/core/Lucy/Object/BitVector.c
----------------------------------------------------------------------
diff --git a/core/Lucy/Object/BitVector.c b/core/Lucy/Object/BitVector.c
index b680553..6ae926f 100644
--- a/core/Lucy/Object/BitVector.c
+++ b/core/Lucy/Object/BitVector.c
@@ -46,6 +46,13 @@ static const uint32_t BYTE_COUNTS[256] = {
     4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
 };
 
+static CFISH_INLINE size_t
+SI_octet_size(size_t bit_size) {
+    if (bit_size > SIZE_MAX - 8) {
+        return SIZE_MAX / 8;
+    }
+    return (bit_size + 7) / 8;
+}
 
 BitVector*
 BitVec_new(size_t capacity) {
@@ -56,7 +63,7 @@ BitVec_new(size_t capacity) {
 BitVector*
 BitVec_init(BitVector *self, size_t capacity) {
     BitVectorIVARS *const ivars = BitVec_IVARS(self);
-    const size_t byte_size = (capacity + 7) / 8;
+    const size_t byte_size = SI_octet_size(capacity);
 
     // Derive.
     ivars->bits = capacity
@@ -80,7 +87,7 @@ BitVector*
 BitVec_Clone_IMP(BitVector *self) {
     BitVectorIVARS *const ivars = BitVec_IVARS(self);
     BitVector *other = BitVec_new(ivars->cap);
-    size_t byte_size = (ivars->cap + 7) / 8;
+    size_t byte_size = SI_octet_size(ivars->cap);
     BitVectorIVARS *const ovars = BitVec_IVARS(other);
 
     // Forbid inheritance.
@@ -109,8 +116,8 @@ BitVec_Mimic_IMP(BitVector *self, Obj *other) {
     CERTIFY(other, BITVECTOR);
     BitVectorIVARS *const ivars = BitVec_IVARS(self);
     BitVectorIVARS *const ovars = BitVec_IVARS((BitVector*)other);
-    const size_t my_byte_size = (ivars->cap + 7) / 8;
-    const size_t other_byte_size = (ovars->cap + 7) / 8;
+    const size_t my_byte_size = SI_octet_size(ivars->cap);
+    const size_t other_byte_size = SI_octet_size(ovars->cap);
     if (my_byte_size > other_byte_size) {
         size_t space = my_byte_size - other_byte_size;
         memset(ivars->bits + other_byte_size, 0, space);
@@ -125,8 +132,8 @@ void
 BitVec_Grow_IMP(BitVector *self, size_t capacity) {
     BitVectorIVARS *const ivars = BitVec_IVARS(self);
     if (capacity > ivars->cap) {
-        const size_t old_byte_cap  = (ivars->cap + 7) / 8;
-        const size_t new_byte_cap  = (capacity   + 7) / 8;
+        const size_t old_byte_cap  = SI_octet_size(ivars->cap);
+        const size_t new_byte_cap  = SI_octet_size(capacity);
         const size_t num_new_bytes = new_byte_cap - old_byte_cap;
 
         ivars->bits = (uint8_t*)REALLOCATE(ivars->bits, new_byte_cap);
@@ -157,7 +164,7 @@ BitVec_Clear_IMP(BitVector *self, size_t tick) {
 void
 BitVec_Clear_All_IMP(BitVector *self) {
     BitVectorIVARS *const ivars = BitVec_IVARS(self);
-    const size_t byte_size = (ivars->cap + 7) / 8;
+    const size_t byte_size = SI_octet_size(ivars->cap);
     memset(ivars->bits, 0, byte_size);
 }
 
@@ -182,7 +189,7 @@ S_first_bit_in_nonzero_byte(uint8_t num) {
 int32_t
 BitVec_Next_Hit_IMP(BitVector *self, size_t tick) {
     BitVectorIVARS *const ivars = BitVec_IVARS(self);
-    size_t byte_size = (ivars->cap + 7) / 8;
+    size_t byte_size = SI_octet_size(ivars->cap);
     uint8_t *const limit = ivars->bits + byte_size;
     uint8_t *ptr = ivars->bits + (tick >> 3);
 
@@ -222,7 +229,7 @@ BitVec_And_IMP(BitVector *self, const BitVector *other) {
     const size_t min_cap = ivars->cap < ovars->cap
                            ? ivars->cap
                            : ovars->cap;
-    const size_t byte_size = (min_cap + 7) / 8;
+    const size_t byte_size = SI_octet_size(min_cap);
     uint8_t *const limit = bits_a + byte_size;
 
     // Intersection.
@@ -233,7 +240,7 @@ BitVec_And_IMP(BitVector *self, const BitVector *other) {
 
     // Set all remaining to zero.
     if (ivars->cap > min_cap) {
-        const size_t self_byte_size = (ivars->cap + 7) / 8;
+        const size_t self_byte_size = SI_octet_size(ivars->cap);
         memset(bits_a, 0, self_byte_size - byte_size);
     }
 }
@@ -271,7 +278,7 @@ S_do_or_or_xor(BitVector *self, const BitVector *other, int 
operation) {
     if (max_cap > ivars->cap) { BitVec_Grow(self, max_cap); }
     bits_a        = ivars->bits;
     bits_b        = ovars->bits;
-    byte_size     = (min_cap + 7) / 8;
+    byte_size     = SI_octet_size(min_cap);
     limit         = ivars->bits + (size_t)byte_size;
 
     // Perform union of common bits.
@@ -293,7 +300,7 @@ S_do_or_or_xor(BitVector *self, const BitVector *other, int 
operation) {
 
     // Copy remaining bits if other is bigger than self.
     if (ovars->cap > min_cap) {
-        const size_t other_byte_size = (ovars->cap + 7) / 8;
+        const size_t other_byte_size = SI_octet_size(ovars->cap);
         const size_t bytes_to_copy = other_byte_size - byte_size;
         memcpy(bits_a, bits_b, bytes_to_copy);
     }
@@ -308,7 +315,7 @@ BitVec_And_Not_IMP(BitVector *self, const BitVector *other) 
{
     const size_t min_cap = ivars->cap < ovars->cap
                            ? ivars->cap
                            : ovars->cap;
-    const size_t byte_size = (min_cap + 7) / 8;
+    const size_t byte_size = SI_octet_size(min_cap);
     uint8_t *const limit = bits_a + byte_size;
 
     // Clear bits set in other.
@@ -375,7 +382,7 @@ size_t
 BitVec_Count_IMP(BitVector *self) {
     BitVectorIVARS *const ivars = BitVec_IVARS(self);
     size_t count = 0;
-    const size_t byte_size = (ivars->cap + 7) / 8;
+    const size_t byte_size = SI_octet_size(ivars->cap);
     uint8_t *ptr = ivars->bits;
     uint8_t *const limit = ptr + byte_size;
 
@@ -393,7 +400,7 @@ BitVec_To_Array_IMP(BitVector *self) {
     size_t          num_left  = count;
     const size_t    capacity  = ivars->cap;
     uint32_t *const array     = (uint32_t*)CALLOCATE(count, sizeof(uint32_t));
-    const size_t    byte_size = (ivars->cap + 7) / 8;
+    const size_t    byte_size = SI_octet_size(ivars->cap);
     uint8_t *const  bits      = ivars->bits;
     uint8_t *const  limit     = bits + byte_size;
     uint32_t        num       = 0;

Reply via email to