Skip to content

Commit 6464292

Browse files
authored
refactor: standardize div_ceil (apache#1999)
1 parent dfadd2d commit 6464292

5 files changed

Lines changed: 14 additions & 36 deletions

File tree

native/core/src/common/bit.rs

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,6 @@ where
174174
memcpy(&source.as_bytes()[..num_bytes], target)
175175
}
176176

177-
/// Returns the ceil of value/divisor
178-
#[inline]
179-
pub fn ceil(value: usize, divisor: usize) -> usize {
180-
value / divisor + ((value % divisor != 0) as usize)
181-
}
182-
183177
/// Returns ceil(log2(x))
184178
#[inline]
185179
pub fn log2(mut x: u64) -> u32 {
@@ -354,7 +348,7 @@ impl BitWriter {
354348
/// Flushes the internal buffered bits and the align the buffer to the next byte.
355349
#[inline]
356350
pub fn flush(&mut self) {
357-
let num_bytes = ceil(self.bit_offset, 8);
351+
let num_bytes = self.bit_offset.div_ceil(8);
358352
debug_assert!(self.byte_offset + num_bytes <= self.max_bytes);
359353
memcpy_value(
360354
&self.buffered_values,
@@ -401,7 +395,7 @@ impl BitWriter {
401395

402396
#[inline]
403397
pub fn bytes_written(&self) -> usize {
404-
self.byte_offset - self.start + ceil(self.bit_offset, 8)
398+
self.byte_offset - self.start + self.bit_offset.div_ceil(8)
405399
}
406400

407401
#[inline]
@@ -598,7 +592,7 @@ impl BitReader {
598592
/// Gets the current byte offset
599593
#[inline]
600594
pub fn get_byte_offset(&self) -> usize {
601-
self.byte_offset + ceil(self.bit_offset, 8)
595+
self.byte_offset + self.bit_offset.div_ceil(8)
602596
}
603597

604598
/// Reads a value of type `T` and of size `num_bits`.
@@ -724,7 +718,7 @@ impl BitReader {
724718
v >>= 8 - offset_r;
725719

726720
// Read the rest of the bytes
727-
((offset_i + 1)..(offset_i + ceil(n + offset_r, 8))).for_each(|i| {
721+
((offset_i + 1)..(offset_i + usize::div_ceil(n + offset_r, 8))).for_each(|i| {
728722
dst[i] |= v as u8;
729723
v >>= 8;
730724
});
@@ -907,7 +901,7 @@ impl BitReader {
907901
debug_assert!(8 >= size_of::<T>());
908902
debug_assert!(num_bytes <= size_of::<T>());
909903

910-
let bytes_read = ceil(self.bit_offset, 8);
904+
let bytes_read = self.bit_offset.div_ceil(8);
911905
if unlikely(self.byte_offset + bytes_read + num_bytes > self.total_bytes) {
912906
return None;
913907
}
@@ -1052,21 +1046,6 @@ mod tests {
10521046
assert_eq!(read_u32(&buffer), read_num_bytes!(u32, 4, &buffer),);
10531047
}
10541048

1055-
#[test]
1056-
fn test_ceil() {
1057-
assert_eq!(ceil(0, 1), 0);
1058-
assert_eq!(ceil(1, 1), 1);
1059-
assert_eq!(ceil(1, 2), 1);
1060-
assert_eq!(ceil(1, 8), 1);
1061-
assert_eq!(ceil(7, 8), 1);
1062-
assert_eq!(ceil(8, 8), 1);
1063-
assert_eq!(ceil(9, 8), 2);
1064-
assert_eq!(ceil(9, 9), 1);
1065-
assert_eq!(ceil(10000000000, 10), 1000000000);
1066-
assert_eq!(ceil(10, 10000000000), 1);
1067-
assert_eq!(ceil(10000000000, 1000000000), 10);
1068-
}
1069-
10701049
#[test]
10711050
fn test_bit_reader_get_byte_offset() {
10721051
let buffer = vec![255; 10];
@@ -1316,7 +1295,7 @@ mod tests {
13161295

13171296
fn test_put_value_rand_numbers(total: usize, num_bits: usize) {
13181297
assert!(num_bits < 64);
1319-
let num_bytes = ceil(num_bits, 8);
1298+
let num_bytes = num_bits.div_ceil(8);
13201299
let mut writer = BitWriter::new(num_bytes * total);
13211300
let values: Vec<u64> = random_numbers::<u64>(total)
13221301
.iter()
@@ -1454,7 +1433,7 @@ mod tests {
14541433
T: FromBytes + Default + Clone + Debug + Eq,
14551434
{
14561435
assert!(num_bits <= 32);
1457-
let num_bytes = ceil(num_bits, 8);
1436+
let num_bytes = num_bits.div_ceil(8);
14581437
let mut writer = BitWriter::new(num_bytes * total);
14591438

14601439
let values: Vec<u32> = random_numbers::<u32>(total)
@@ -1487,7 +1466,7 @@ mod tests {
14871466
const SIZE: &[usize] = &[1, 31, 32, 33, 128, 129];
14881467
for total in SIZE {
14891468
for num_bits in 1..33 {
1490-
let num_bytes = ceil(num_bits, 8);
1469+
let num_bytes = usize::div_ceil(num_bits, 8);
14911470
let mut writer = BitWriter::new(num_bytes * total);
14921471

14931472
let values: Vec<u32> = random_numbers::<u32>(*total)
@@ -1533,7 +1512,7 @@ mod tests {
15331512
assert_eq!(total % 2, 0);
15341513

15351514
let aligned_value_byte_width = std::mem::size_of::<T>();
1536-
let value_byte_width = ceil(num_bits, 8);
1515+
let value_byte_width = num_bits.div_ceil(8);
15371516
let mut writer =
15381517
BitWriter::new((total / 2) * (aligned_value_byte_width + value_byte_width));
15391518
let values: Vec<u32> = random_numbers::<u32>(total / 2)

native/core/src/execution/util/spark_bit_array.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
use crate::common::bit;
1918
use arrow::datatypes::ToByteSlice;
2019
use std::iter::zip;
2120

@@ -103,7 +102,7 @@ impl SparkBitArray {
103102
}
104103

105104
pub fn num_words(num_bits: i32) -> i32 {
106-
bit::ceil(num_bits as usize, 64) as i32
105+
usize::div_ceil(num_bits as usize, 64) as i32
107106
}
108107

109108
#[cfg(test)]

native/core/src/parquet/mutable_vector.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl ParquetMutableVector {
6969
arrow_type: ArrowDataType,
7070
bit_width: usize,
7171
) -> Self {
72-
let validity_len = bit::ceil(capacity, 8);
72+
let validity_len = capacity.div_ceil(8);
7373
let validity_buffer = CometBuffer::new(validity_len);
7474

7575
let mut value_capacity = capacity;
@@ -78,7 +78,7 @@ impl ParquetMutableVector {
7878
value_capacity += 1;
7979
}
8080
// Make sure the capacity is positive
81-
let len = bit::ceil(value_capacity * bit_width, 8);
81+
let len = usize::div_ceil(value_capacity * bit_width, 8);
8282
let mut value_buffer = CometBuffer::new(len);
8383

8484
let mut children = Vec::new();

native/core/src/parquet/read/levels.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ impl LevelDecoder {
218218
Mode::RLE => {
219219
// RLE
220220
self.current_count = (indicator_value >> 1) as usize;
221-
let value_width = bit::ceil(self.bit_width as usize, 8);
221+
let value_width = (self.bit_width as usize).div_ceil(8);
222222
self.current_value = bit_reader
223223
.get_aligned::<i32>(value_width)
224224
.expect("current value should be set");

native/core/src/parquet/read/values.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -962,7 +962,7 @@ impl DictDecoder {
962962
self.bit_packed_left = ((indicator_value >> 1) * 8) as usize;
963963
} else {
964964
self.rle_left = (indicator_value >> 1) as usize;
965-
let value_width = bit::ceil(self.bit_width, 8);
965+
let value_width = self.bit_width.div_ceil(8);
966966
self.current_value = self.bit_reader.get_aligned::<u32>(value_width).unwrap();
967967
}
968968
} else {

0 commit comments

Comments
 (0)