| 172 | // |
| 173 | |
| 174 | uint8_t DetectIntWidth(const int64_t* values, int64_t length, uint8_t min_width) { |
| 175 | if (min_width == 8) { |
| 176 | return min_width; |
| 177 | } |
| 178 | uint8_t width = min_width; |
| 179 | |
| 180 | auto p = values; |
| 181 | const auto end = p + length; |
| 182 | // Strategy: to determine whether `x` is between -0x80 and 0x7f, |
| 183 | // we determine whether `x + 0x80` is between 0x00 and 0xff. The |
| 184 | // latter can be done with a simple AND mask with ~0xff and, more |
| 185 | // importantly, can be computed in a single step over multiple ORed |
| 186 | // values (so we can branch once every N items instead of once every item). |
| 187 | // This strategy could probably lend itself to explicit SIMD-ization, |
| 188 | // if more performance is needed. |
| 189 | constexpr uint64_t addend8 = 0x80ULL; |
| 190 | constexpr uint64_t addend16 = 0x8000ULL; |
| 191 | constexpr uint64_t addend32 = 0x80000000ULL; |
| 192 | |
| 193 | auto test_one_item = [&](uint64_t addend, uint64_t test_mask) -> bool { |
| 194 | auto v = *p++; |
| 195 | if (ARROW_PREDICT_FALSE(((v + addend) & test_mask) != 0)) { |
| 196 | --p; |
| 197 | return false; |
| 198 | } else { |
| 199 | return true; |
| 200 | } |
| 201 | }; |
| 202 | |
| 203 | auto test_four_items = [&](uint64_t addend, uint64_t test_mask) -> bool { |
| 204 | auto mask = (p[0] + addend) | (p[1] + addend) | (p[2] + addend) | (p[3] + addend); |
| 205 | p += 4; |
| 206 | if (ARROW_PREDICT_FALSE((mask & test_mask) != 0)) { |
| 207 | p -= 4; |
| 208 | return false; |
| 209 | } else { |
| 210 | return true; |
| 211 | } |
| 212 | }; |
| 213 | |
| 214 | if (width == 1) { |
| 215 | while (p <= end - 4) { |
| 216 | if (!test_four_items(addend8, mask_uint8)) { |
| 217 | width = 2; |
| 218 | goto width2; |
| 219 | } |
| 220 | } |
| 221 | while (p < end) { |
| 222 | if (!test_one_item(addend8, mask_uint8)) { |
| 223 | width = 2; |
| 224 | goto width2; |
| 225 | } |
| 226 | } |
| 227 | return 1; |
| 228 | } |
| 229 | width2: |
| 230 | if (width == 2) { |
| 231 | while (p <= end - 4) { |
no outgoing calls