iceberg-cpp
Loading...
Searching...
No Matches
endian.h
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#pragma once
21
22#include <bit>
23#include <concepts>
24#include <cstdint>
25#include <cstring>
26#include <type_traits>
27
28#include "iceberg/compat/byte_swap.h"
29
32
33namespace iceberg {
34
36template <typename T>
37concept EndianConvertible = std::is_arithmetic_v<T>;
38
41template <EndianConvertible T>
42constexpr T ByteSwap(T value) {
43 if constexpr (sizeof(T) <= 1) {
44 return value;
45 } else if constexpr (std::is_integral_v<T>) {
46 return static_cast<T>(
47 internal::ByteSwapUnsigned(static_cast<std::make_unsigned_t<T>>(value)));
48 } else if constexpr (std::is_floating_point_v<T>) {
49 if constexpr (sizeof(T) == sizeof(uint16_t)) {
50 return std::bit_cast<T>(internal::ByteSwapUnsigned(std::bit_cast<uint16_t>(value)));
51 } else if constexpr (sizeof(T) == sizeof(uint32_t)) {
52 return std::bit_cast<T>(internal::ByteSwapUnsigned(std::bit_cast<uint32_t>(value)));
53 } else if constexpr (sizeof(T) == sizeof(uint64_t)) {
54 return std::bit_cast<T>(internal::ByteSwapUnsigned(std::bit_cast<uint64_t>(value)));
55 } else {
56 static_assert(sizeof(T) == 0,
57 "Unsupported floating-point size for endian conversion.");
58 }
59 }
60}
61
63template <EndianConvertible T>
64constexpr T ToLittleEndian(T value) {
65 if constexpr (std::endian::native == std::endian::little) {
66 return value;
67 } else {
68 return ByteSwap(value);
69 }
70}
71
73template <EndianConvertible T>
74constexpr T FromLittleEndian(T value) {
75 if constexpr (std::endian::native == std::endian::little) {
76 return value;
77 } else {
78 return ByteSwap(value);
79 }
80}
81
83template <EndianConvertible T>
84constexpr T ToBigEndian(T value) {
85 if constexpr (std::endian::native == std::endian::big) {
86 return value;
87 } else {
88 return ByteSwap(value);
89 }
90}
91
93template <EndianConvertible T>
94constexpr T FromBigEndian(T value) {
95 if constexpr (std::endian::native == std::endian::big) {
96 return value;
97 } else {
98 return ByteSwap(value);
99 }
100}
101
104template <EndianConvertible T>
105void WriteLittleEndian(T value, void* output) {
106 auto le = ToLittleEndian(value);
107 std::memcpy(output, &le, sizeof(le));
108}
109
112template <EndianConvertible T>
113T ReadLittleEndian(const void* input) {
114 T value;
115 std::memcpy(&value, input, sizeof(value));
116 return FromLittleEndian(value);
117}
118
121template <EndianConvertible T>
122void WriteBigEndian(T value, void* output) {
123 auto be = ToBigEndian(value);
124 std::memcpy(output, &be, sizeof(be));
125}
126
129template <EndianConvertible T>
130T ReadBigEndian(const void* input) {
131 T value;
132 std::memcpy(&value, input, sizeof(value));
133 return FromBigEndian(value);
134}
135
136} // namespace iceberg
Concept for values that can be converted to/from another endian format.
Definition endian.h:37
Core Apache Iceberg C++ APIs.
Definition arrow_io_util.h:34
constexpr T ToLittleEndian(T value)
Convert a value to little-endian format.
Definition endian.h:64
constexpr T FromLittleEndian(T value)
Convert a value from little-endian format.
Definition endian.h:74
T ReadBigEndian(const void *input)
Read a value in big-endian format from a buffer.
Definition endian.h:130
constexpr T ByteSwap(T value)
Byte-swap a value. For floating-point types, only support 32-bit and 64-bit floats.
Definition endian.h:42
constexpr T ToBigEndian(T value)
Convert a value to big-endian format.
Definition endian.h:84
void WriteLittleEndian(T value, void *output)
Write a value in little-endian format to a buffer.
Definition endian.h:105
T ReadLittleEndian(const void *input)
Read a value in little-endian format from a buffer.
Definition endian.h:113
void WriteBigEndian(T value, void *output)
Write a value in big-endian format to a buffer.
Definition endian.h:122
constexpr T FromBigEndian(T value)
Convert a value from big-endian format.
Definition endian.h:94