Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-08-28 08:27:08

0001 // Licensed to the Apache Software Foundation (ASF) under one
0002 // or more contributor license agreements.  See the NOTICE file
0003 // distributed with this work for additional information
0004 // regarding copyright ownership.  The ASF licenses this file
0005 // to you under the Apache License, Version 2.0 (the
0006 // "License"); you may not use this file except in compliance
0007 // with the License.  You may obtain a copy of the License at
0008 //
0009 //   http://www.apache.org/licenses/LICENSE-2.0
0010 //
0011 // Unless required by applicable law or agreed to in writing,
0012 // software distributed under the License is distributed on an
0013 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
0014 // KIND, either express or implied.  See the License for the
0015 // specific language governing permissions and limitations
0016 // under the License.
0017 
0018 #pragma once
0019 
0020 #ifdef _WIN32
0021 #  define ARROW_LITTLE_ENDIAN 1
0022 #else
0023 #  if defined(__APPLE__) || defined(__FreeBSD__)
0024 #    include <machine/endian.h>  // IWYU pragma: keep
0025 #  elif defined(sun) || defined(__sun)
0026 #    include <sys/byteorder.h>  // IWYU pragma: keep
0027 #  else
0028 #    include <endian.h>  // IWYU pragma: keep
0029 #  endif
0030 #
0031 #  ifndef __BYTE_ORDER__
0032 #    error "__BYTE_ORDER__ not defined"
0033 #  endif
0034 #
0035 #  ifndef __ORDER_LITTLE_ENDIAN__
0036 #    error "__ORDER_LITTLE_ENDIAN__ not defined"
0037 #  endif
0038 #
0039 #  if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
0040 #    define ARROW_LITTLE_ENDIAN 1
0041 #  else
0042 #    define ARROW_LITTLE_ENDIAN 0
0043 #  endif
0044 #endif
0045 
0046 #if defined(_MSC_VER)
0047 #  include <intrin.h>  // IWYU pragma: keep
0048 #  define ARROW_BYTE_SWAP64 _byteswap_uint64
0049 #  define ARROW_BYTE_SWAP32 _byteswap_ulong
0050 #else
0051 #  define ARROW_BYTE_SWAP64 __builtin_bswap64
0052 #  define ARROW_BYTE_SWAP32 __builtin_bswap32
0053 #endif
0054 
0055 #include <algorithm>
0056 #include <array>
0057 
0058 #include "arrow/util/type_traits.h"
0059 #include "arrow/util/ubsan.h"
0060 
0061 namespace arrow {
0062 namespace bit_util {
0063 
0064 //
0065 // Byte-swap 16-bit, 32-bit and 64-bit values
0066 //
0067 
0068 // Swap the byte order (i.e. endianness)
0069 static inline int64_t ByteSwap(int64_t value) { return ARROW_BYTE_SWAP64(value); }
0070 static inline uint64_t ByteSwap(uint64_t value) {
0071   return static_cast<uint64_t>(ARROW_BYTE_SWAP64(value));
0072 }
0073 static inline int32_t ByteSwap(int32_t value) { return ARROW_BYTE_SWAP32(value); }
0074 static inline uint32_t ByteSwap(uint32_t value) {
0075   return static_cast<uint32_t>(ARROW_BYTE_SWAP32(value));
0076 }
0077 static inline int16_t ByteSwap(int16_t value) {
0078   constexpr auto m = static_cast<int16_t>(0xff);
0079   return static_cast<int16_t>(((value >> 8) & m) | ((value & m) << 8));
0080 }
0081 static inline uint16_t ByteSwap(uint16_t value) {
0082   return static_cast<uint16_t>(ByteSwap(static_cast<int16_t>(value)));
0083 }
0084 static inline uint8_t ByteSwap(uint8_t value) { return value; }
0085 static inline int8_t ByteSwap(int8_t value) { return value; }
0086 static inline double ByteSwap(double value) {
0087   const uint64_t swapped = ARROW_BYTE_SWAP64(util::SafeCopy<uint64_t>(value));
0088   return util::SafeCopy<double>(swapped);
0089 }
0090 static inline float ByteSwap(float value) {
0091   const uint32_t swapped = ARROW_BYTE_SWAP32(util::SafeCopy<uint32_t>(value));
0092   return util::SafeCopy<float>(swapped);
0093 }
0094 
0095 // Write the swapped bytes into dst. Src and dst cannot overlap.
0096 static inline void ByteSwap(void* dst, const void* src, int len) {
0097   switch (len) {
0098     case 1:
0099       *reinterpret_cast<int8_t*>(dst) = *reinterpret_cast<const int8_t*>(src);
0100       return;
0101     case 2:
0102       *reinterpret_cast<int16_t*>(dst) = ByteSwap(*reinterpret_cast<const int16_t*>(src));
0103       return;
0104     case 4:
0105       *reinterpret_cast<int32_t*>(dst) = ByteSwap(*reinterpret_cast<const int32_t*>(src));
0106       return;
0107     case 8:
0108       *reinterpret_cast<int64_t*>(dst) = ByteSwap(*reinterpret_cast<const int64_t*>(src));
0109       return;
0110     default:
0111       break;
0112   }
0113 
0114   auto d = reinterpret_cast<uint8_t*>(dst);
0115   auto s = reinterpret_cast<const uint8_t*>(src);
0116   for (int i = 0; i < len; ++i) {
0117     d[i] = s[len - i - 1];
0118   }
0119 }
0120 
0121 // Convert to little/big endian format from the machine's native endian format.
0122 #if ARROW_LITTLE_ENDIAN
0123 template <typename T, typename = internal::EnableIfIsOneOf<
0124                           T, int64_t, uint64_t, int32_t, uint32_t, int16_t, uint16_t,
0125                           uint8_t, int8_t, float, double, bool>>
0126 static inline T ToBigEndian(T value) {
0127   return ByteSwap(value);
0128 }
0129 
0130 template <typename T, typename = internal::EnableIfIsOneOf<
0131                           T, int64_t, uint64_t, int32_t, uint32_t, int16_t, uint16_t,
0132                           uint8_t, int8_t, float, double, bool>>
0133 static inline T ToLittleEndian(T value) {
0134   return value;
0135 }
0136 #else
0137 template <typename T, typename = internal::EnableIfIsOneOf<
0138                           T, int64_t, uint64_t, int32_t, uint32_t, int16_t, uint16_t,
0139                           uint8_t, int8_t, float, double, bool>>
0140 static inline T ToBigEndian(T value) {
0141   return value;
0142 }
0143 
0144 template <typename T, typename = internal::EnableIfIsOneOf<
0145                           T, int64_t, uint64_t, int32_t, uint32_t, int16_t, uint16_t,
0146                           uint8_t, int8_t, float, double, bool>>
0147 static inline T ToLittleEndian(T value) {
0148   return ByteSwap(value);
0149 }
0150 #endif
0151 
0152 // Convert from big/little endian format to the machine's native endian format.
0153 #if ARROW_LITTLE_ENDIAN
0154 template <typename T, typename = internal::EnableIfIsOneOf<
0155                           T, int64_t, uint64_t, int32_t, uint32_t, int16_t, uint16_t,
0156                           uint8_t, int8_t, float, double, bool>>
0157 static inline T FromBigEndian(T value) {
0158   return ByteSwap(value);
0159 }
0160 
0161 template <typename T, typename = internal::EnableIfIsOneOf<
0162                           T, int64_t, uint64_t, int32_t, uint32_t, int16_t, uint16_t,
0163                           uint8_t, int8_t, float, double, bool>>
0164 static inline T FromLittleEndian(T value) {
0165   return value;
0166 }
0167 #else
0168 template <typename T, typename = internal::EnableIfIsOneOf<
0169                           T, int64_t, uint64_t, int32_t, uint32_t, int16_t, uint16_t,
0170                           uint8_t, int8_t, float, double, bool>>
0171 static inline T FromBigEndian(T value) {
0172   return value;
0173 }
0174 
0175 template <typename T, typename = internal::EnableIfIsOneOf<
0176                           T, int64_t, uint64_t, int32_t, uint32_t, int16_t, uint16_t,
0177                           uint8_t, int8_t, float, double, bool>>
0178 static inline T FromLittleEndian(T value) {
0179   return ByteSwap(value);
0180 }
0181 #endif
0182 
0183 // Handle endianness in *word* granularity (keep individual array element untouched)
0184 namespace little_endian {
0185 
0186 namespace detail {
0187 
0188 // Read a native endian array as little endian
0189 template <typename T, size_t N>
0190 struct Reader {
0191   const std::array<T, N>& native_array;
0192 
0193   explicit Reader(const std::array<T, N>& native_array) : native_array(native_array) {}
0194 
0195   const T& operator[](size_t i) const {
0196     return native_array[ARROW_LITTLE_ENDIAN ? i : N - 1 - i];
0197   }
0198 };
0199 
0200 // Read/write a native endian array as little endian
0201 template <typename T, size_t N>
0202 struct Writer {
0203   std::array<T, N>* native_array;
0204 
0205   explicit Writer(std::array<T, N>* native_array) : native_array(native_array) {}
0206 
0207   const T& operator[](size_t i) const {
0208     return (*native_array)[ARROW_LITTLE_ENDIAN ? i : N - 1 - i];
0209   }
0210   T& operator[](size_t i) { return (*native_array)[ARROW_LITTLE_ENDIAN ? i : N - 1 - i]; }
0211 };
0212 
0213 }  // namespace detail
0214 
0215 // Construct array reader and try to deduce template augments
0216 template <typename T, size_t N>
0217 static inline detail::Reader<T, N> Make(const std::array<T, N>& native_array) {
0218   return detail::Reader<T, N>(native_array);
0219 }
0220 
0221 // Construct array writer and try to deduce template augments
0222 template <typename T, size_t N>
0223 static inline detail::Writer<T, N> Make(std::array<T, N>* native_array) {
0224   return detail::Writer<T, N>(native_array);
0225 }
0226 
0227 // Convert little endian array to native endian
0228 template <typename T, size_t N>
0229 static inline std::array<T, N> ToNative(std::array<T, N> array) {
0230   if (!ARROW_LITTLE_ENDIAN) {
0231     std::reverse(array.begin(), array.end());
0232   }
0233   return array;
0234 }
0235 
0236 // Convert native endian array to little endian
0237 template <typename T, size_t N>
0238 static inline std::array<T, N> FromNative(std::array<T, N> array) {
0239   return ToNative(array);
0240 }
0241 
0242 }  // namespace little_endian
0243 
0244 }  // namespace bit_util
0245 }  // namespace arrow