Skip to content

Commit 0512219

Browse files
committed
Simplify scalar_unpack increments
1 parent 5ad1254 commit 0512219

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

cpp/src/arrow/util/bpacking_scalar_codegen.py

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@
6565
#include "arrow/util/ubsan.h"
6666
6767
namespace arrow::internal {
68+
69+
template <typename Int>
70+
Int LoadInt(const uint8_t* in) {
71+
return bit_util::FromLittleEndian(util::SafeLoadAs<Int>(in));
72+
}
6873
"""
6974

7075
FOOTER = """
@@ -118,11 +123,12 @@ def print_unpack_0(self) -> None:
118123
def print_unpack_last(self) -> None:
119124
print(self.unpack_signature(self.out_bit_width))
120125
print(f" for(int k = 0; k < {self.howmany}; k += 1) {{")
121-
print(f" auto w = util::SafeLoadAs<{self.unsigned_type}>(in);")
122-
print(" out[k] = bit_util::FromLittleEndian(w);")
123-
print(f" in += {self.out_byte_width};")
126+
print(
127+
f" out[k] = LoadInt<{self.unsigned_type}>("
128+
f"in + (k * {self.out_byte_width}));"
129+
)
124130
print(" }")
125-
print(" return in;")
131+
print(f" return in + ({self.out_byte_width} * {self.howmany});")
126132
print("}")
127133

128134
def print_unpack_k(self, bit: int) -> None:
@@ -136,24 +142,22 @@ def print_unpack_k(self, bit: int) -> None:
136142

137143
for k in range(self.howmanywords(bit) - 1):
138144
print(
139-
f" const auto w{k} = "
140-
f"bit_util::FromLittleEndian(util::SafeLoadAs<{self.unsigned_type}>(in));"
145+
f" const auto w{k} = LoadInt<{self.unsigned_type}>("
146+
f"in + {k} * {self.out_byte_width});"
141147
)
142-
print(f" in += {self.out_byte_width};")
143148

144149
k = self.howmanywords(bit) - 1
145-
if self.smart_halve and bit % 2 == 1:
150+
use_smart_halving = self.smart_halve and bit % 2 == 1
151+
if use_smart_halving:
146152
print(
147-
f" auto w{k} = static_cast<{self.unsigned_type}>(util::SafeLoadAs<{self.unsigned_type_half}>(in));"
153+
f" const auto w{k} = static_cast<{self.unsigned_type}>(LoadInt<{self.unsigned_type_half}>("
154+
f"in + {k} * {self.out_byte_width}));"
148155
)
149-
print(f" w{k} = bit_util::FromLittleEndian(w{k});")
150-
print(f" in += {self.out_byte_width // 2};")
151156
else:
152157
print(
153-
f" const auto w{k} = "
154-
f"bit_util::FromLittleEndian(util::SafeLoadAs<{self.unsigned_type}>(in));"
158+
f" const auto w{k} = LoadInt<{self.unsigned_type}>("
159+
f"in + {k} * {self.out_byte_width});"
155160
)
156-
print(f" in += {self.out_byte_width};")
157161

158162
for j in range(self.howmany):
159163
firstword = j * bit // self.out_bit_width
@@ -174,7 +178,14 @@ def print_unpack_k(self, bit: int) -> None:
174178
f"(w{firstword + 1} << {secondshift})){maskstr};"
175179
)
176180
print("")
177-
print(" return in;")
181+
182+
if use_smart_halving:
183+
print(
184+
f" return in + ({self.howmanywords(bit) - 1} * {self.out_byte_width}"
185+
f" + {self.out_byte_width // 2});"
186+
)
187+
else:
188+
print(f" return in + ({self.howmanywords(bit)} * {self.out_byte_width});")
178189
print("}")
179190

180191
def print_all(self) -> None:

0 commit comments

Comments
 (0)