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
26namespace arrow {
27namespace internal {
28
29ARROW_EXPORT
30uint8_t DetectUIntWidth(const uint64_t* values, int64_t length, uint8_t min_width = 1);
31
32ARROW_EXPORT
33uint8_t DetectUIntWidth(const uint64_t* values, const uint8_t* valid_bytes,
34 int64_t length, uint8_t min_width = 1);
35
36ARROW_EXPORT
37uint8_t DetectIntWidth(const int64_t* values, int64_t length, uint8_t min_width = 1);
38
39ARROW_EXPORT
40uint8_t DetectIntWidth(const int64_t* values, const uint8_t* valid_bytes, int64_t length,
41 uint8_t min_width = 1);
42
43ARROW_EXPORT
44void DowncastInts(const int64_t* source, int8_t* dest, int64_t length);
45
46ARROW_EXPORT
47void DowncastInts(const int64_t* source, int16_t* dest, int64_t length);
48
49ARROW_EXPORT
50void DowncastInts(const int64_t* source, int32_t* dest, int64_t length);
51
52ARROW_EXPORT
53void DowncastInts(const int64_t* source, int64_t* dest, int64_t length);
54
55ARROW_EXPORT
56void DowncastUInts(const uint64_t* source, uint8_t* dest, int64_t length);
57
58ARROW_EXPORT
59void DowncastUInts(const uint64_t* source, uint16_t* dest, int64_t length);
60
61ARROW_EXPORT
62void DowncastUInts(const uint64_t* source, uint32_t* dest, int64_t length);
63
64ARROW_EXPORT
65void DowncastUInts(const uint64_t* source, uint64_t* dest, int64_t length);
66
67template <typename InputInt, typename OutputInt>
68ARROW_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)
72template <typename SignedInt>
73SignedInt 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
80template <typename SignedInt, typename Shift>
81SignedInt 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