65
65
#include "arrow/util/ubsan.h"
66
66
67
67
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
+ }
68
73
"""
69
74
70
75
FOOTER = """
@@ -118,11 +123,12 @@ def print_unpack_0(self) -> None:
118
123
def print_unpack_last (self ) -> None :
119
124
print (self .unpack_signature (self .out_bit_width ))
120
125
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
+ )
124
130
print (" }" )
125
- print (" return in;" )
131
+ print (f " return in + ( { self . out_byte_width } * { self . howmany } ) ;" )
126
132
print ("}" )
127
133
128
134
def print_unpack_k (self , bit : int ) -> None :
@@ -136,24 +142,22 @@ def print_unpack_k(self, bit: int) -> None:
136
142
137
143
for k in range (self .howmanywords (bit ) - 1 ):
138
144
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 } );"
141
147
)
142
- print (f" in += { self .out_byte_width } ;" )
143
148
144
149
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 :
146
152
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 } ));"
148
155
)
149
- print (f" w{ k } = bit_util::FromLittleEndian(w{ k } );" )
150
- print (f" in += { self .out_byte_width // 2 } ;" )
151
156
else :
152
157
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 } );"
155
160
)
156
- print (f" in += { self .out_byte_width } ;" )
157
161
158
162
for j in range (self .howmany ):
159
163
firstword = j * bit // self .out_bit_width
@@ -174,7 +178,14 @@ def print_unpack_k(self, bit: int) -> None:
174
178
f"(w{ firstword + 1 } << { secondshift } )){ maskstr } ;"
175
179
)
176
180
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 } );" )
178
189
print ("}" )
179
190
180
191
def print_all (self ) -> None :
0 commit comments