1 | // Licensed to the Apache Software Foundation (ASF) under one |
2 | // or more contributor license agreements. See the NOTICE file |
3 | // distributed with this work for additional information |
4 | // regarding copyright ownership. The ASF licenses this file |
5 | // to you under the Apache License, Version 2.0 (the |
6 | // "License"); you may not use this file except in compliance |
7 | // with the License. You may obtain a copy of the License at |
8 | // |
9 | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | // |
11 | // Unless required by applicable law or agreed to in writing, |
12 | // software distributed under the License is distributed on an |
13 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | // KIND, either express or implied. See the License for the |
15 | // specific language governing permissions and limitations |
16 | // under the License. |
17 | |
18 | #ifndef ARROW_UTIL_INT_UTIL_H |
19 | #define ARROW_UTIL_INT_UTIL_H |
20 | |
21 | #include <cstdint> |
22 | #include <type_traits> |
23 | |
24 | #include "arrow/util/visibility.h" |
25 | |
26 | namespace arrow { |
27 | namespace internal { |
28 | |
29 | ARROW_EXPORT |
30 | uint8_t DetectUIntWidth(const uint64_t* values, int64_t length, uint8_t min_width = 1); |
31 | |
32 | ARROW_EXPORT |
33 | uint8_t DetectUIntWidth(const uint64_t* values, const uint8_t* valid_bytes, |
34 | int64_t length, uint8_t min_width = 1); |
35 | |
36 | ARROW_EXPORT |
37 | uint8_t DetectIntWidth(const int64_t* values, int64_t length, uint8_t min_width = 1); |
38 | |
39 | ARROW_EXPORT |
40 | uint8_t DetectIntWidth(const int64_t* values, const uint8_t* valid_bytes, int64_t length, |
41 | uint8_t min_width = 1); |
42 | |
43 | ARROW_EXPORT |
44 | void DowncastInts(const int64_t* source, int8_t* dest, int64_t length); |
45 | |
46 | ARROW_EXPORT |
47 | void DowncastInts(const int64_t* source, int16_t* dest, int64_t length); |
48 | |
49 | ARROW_EXPORT |
50 | void DowncastInts(const int64_t* source, int32_t* dest, int64_t length); |
51 | |
52 | ARROW_EXPORT |
53 | void DowncastInts(const int64_t* source, int64_t* dest, int64_t length); |
54 | |
55 | ARROW_EXPORT |
56 | void DowncastUInts(const uint64_t* source, uint8_t* dest, int64_t length); |
57 | |
58 | ARROW_EXPORT |
59 | void DowncastUInts(const uint64_t* source, uint16_t* dest, int64_t length); |
60 | |
61 | ARROW_EXPORT |
62 | void DowncastUInts(const uint64_t* source, uint32_t* dest, int64_t length); |
63 | |
64 | ARROW_EXPORT |
65 | void DowncastUInts(const uint64_t* source, uint64_t* dest, int64_t length); |
66 | |
67 | template <typename InputInt, typename OutputInt> |
68 | ARROW_EXPORT void TransposeInts(const InputInt* source, OutputInt* dest, int64_t length, |
69 | const int32_t* transpose_map); |
70 | |
71 | /// Signed addition with well-defined behaviour on overflow (as unsigned) |
72 | template <typename SignedInt> |
73 | SignedInt SafeSignedAdd(SignedInt u, SignedInt v) { |
74 | using UnsignedInt = typename std::make_unsigned<SignedInt>::type; |
75 | return static_cast<SignedInt>(static_cast<UnsignedInt>(u) + |
76 | static_cast<UnsignedInt>(v)); |
77 | } |
78 | |
79 | /// Signed left shift with well-defined behaviour on negative numbers or overflow |
80 | template <typename SignedInt, typename Shift> |
81 | SignedInt SafeLeftShift(SignedInt u, Shift shift) { |
82 | using UnsignedInt = typename std::make_unsigned<SignedInt>::type; |
83 | return static_cast<SignedInt>(static_cast<UnsignedInt>(u) << shift); |
84 | } |
85 | |
86 | } // namespace internal |
87 | } // namespace arrow |
88 | |
89 | #endif // ARROW_UTIL_INT_UTIL_H |
90 | |