paleolimbot commented on code in PR #276:
URL: https://github.com/apache/arrow-nanoarrow/pull/276#discussion_r1296598699


##########
src/nanoarrow/buffer_inline.h:
##########
@@ -236,6 +248,46 @@ static inline int8_t ArrowBitGet(const uint8_t* bits, 
int64_t i) {
   return (bits[i >> 3] >> (i & 0x07)) & 1;
 }
 
+static inline void ArrowBitmapUnpackInt8Unsafe(const uint8_t* bits, int64_t 
start_offset,
+                                               int64_t length, int8_t* out) {
+  if (length == 0) {
+    return;
+  }
+
+  const int64_t i_begin = start_offset;
+  const int64_t i_end = start_offset + length;
+  const int64_t i_last_valid = i_end - 1;
+
+  const int64_t bytes_begin = i_begin / 8;
+  const int64_t bytes_last_valid = i_last_valid / 8;
+
+  if (bytes_begin == bytes_last_valid) {
+    // count bits within a single byte
+    for (int i = 0; i < length; i++) {
+      out[i] = ArrowBitGet(&bits[bytes_begin], i + i_begin % 8);
+    }
+
+    return;
+  }
+
+  // first byte
+  for (int i = 0; i < 8 - (i_begin % 8); i++) {
+    *out++ = ArrowBitGet(&bits[bytes_begin], i + i_begin % 8);
+  }
+
+  // middle bytes
+  for (int64_t i = bytes_begin + 1; i < bytes_last_valid; i++) {
+    _ArrowBitmapUnpackInt8(&bits[i], out);

Review Comment:
   ```suggestion
       _ArrowBitmapUnpackInt8(bits[i], out);
   ```



##########
src/nanoarrow/buffer_test.cc:
##########
@@ -271,6 +271,65 @@ TEST(BitmapTest, BitmapTestElement) {
   EXPECT_EQ(ArrowBitGet(bitmap, 16 + 7), 0);
 }
 
+TEST(BitmapTest, BitmapTestBitsGet) {
+  uint8_t bitmap[10];
+  int8_t result[sizeof(bitmap) * 8];
+
+  memset(bitmap, 0xff, sizeof(bitmap));
+  ArrowBitsGet(bitmap, 0, sizeof(result), result);
+  for (int i = 0; i < sizeof(result); i++) {
+    EXPECT_EQ(result[i], 1);
+  }
+
+  bitmap[2] = 0xfd;
+  ArrowBitsGet(bitmap, 0, sizeof(result), result);
+  EXPECT_EQ(result[16 + 0], 1);

Review Comment:
   It's perfect! Thanks! Does it make sense to remove this first bit that 
doesn't use the helper? (I am a little confused as to what it's testing that 
the tests below it don't cover). 



##########
src/nanoarrow/buffer_inline.h:
##########
@@ -222,6 +222,18 @@ static inline int64_t _ArrowBytesForBits(int64_t bits) {
   return (bits >> 3) + ((bits & 7) != 0);
 }
 
+static inline void _ArrowBitmapUnpackInt8(const uint8_t* bits, int8_t* out) {
+  const uint8_t word = *bits;

Review Comment:
   ```suggestion
   static inline void _ArrowBitmapUnpackInt8(uint8_t word, int8_t* out) {
   ```
   
   ...may simplify its usage below?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to