WPILibC++ 2024.3.2
SwapByteOrder.h
Go to the documentation of this file.
1//===- SwapByteOrder.h - Generic and optimized byte swaps -------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file declares generic and optimized functions to swap the byte order of
10// an integral type.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef WPIUTIL_WPI_SWAPBYTEORDER_H
15#define WPIUTIL_WPI_SWAPBYTEORDER_H
16
17#include "wpi/bit.h"
18#include <cstddef>
19#include <cstdint>
20#include <type_traits>
21
22#if defined(__linux__) || defined(__GNU__) || defined(__HAIKU__) || \
23 defined(__Fuchsia__) || defined(__EMSCRIPTEN__)
24#include <endian.h>
25#elif defined(_AIX)
26#include <sys/machine.h>
27#elif defined(__sun)
28/* Solaris provides _BIG_ENDIAN/_LITTLE_ENDIAN selector in sys/types.h */
29#include <sys/types.h>
30#define BIG_ENDIAN 4321
31#define LITTLE_ENDIAN 1234
32#if defined(_BIG_ENDIAN)
33#define BYTE_ORDER BIG_ENDIAN
34#else
35#define BYTE_ORDER LITTLE_ENDIAN
36#endif
37#elif defined(__MVS__)
38#define BIG_ENDIAN 4321
39#define LITTLE_ENDIAN 1234
40#define BYTE_ORDER BIG_ENDIAN
41#else
42#if !defined(BYTE_ORDER) && !defined(_WIN32)
43#include <machine/endian.h>
44#endif
45#endif
46
47namespace wpi {
48
49namespace sys {
50
51#if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN
52constexpr bool IsBigEndianHost = true;
53#else
54constexpr bool IsBigEndianHost = false;
55#endif
56
58
59inline unsigned char getSwappedBytes(unsigned char C) { return wpi::byteswap(C); }
60inline signed char getSwappedBytes( signed char C) { return wpi::byteswap(C); }
61inline char getSwappedBytes( char C) { return wpi::byteswap(C); }
62
63inline unsigned short getSwappedBytes(unsigned short C) { return wpi::byteswap(C); }
64inline signed short getSwappedBytes( signed short C) { return wpi::byteswap(C); }
65
66inline unsigned int getSwappedBytes(unsigned int C) { return wpi::byteswap(C); }
67inline signed int getSwappedBytes( signed int C) { return wpi::byteswap(C); }
68
69inline unsigned long getSwappedBytes(unsigned long C) { return wpi::byteswap(C); }
70inline signed long getSwappedBytes( signed long C) { return wpi::byteswap(C); }
71
72inline unsigned long long getSwappedBytes(unsigned long long C) { return wpi::byteswap(C); }
73inline signed long long getSwappedBytes( signed long long C) { return wpi::byteswap(C); }
74
75inline float getSwappedBytes(float C) {
76 union {
77 uint32_t i;
78 float f;
79 } in, out;
80 in.f = C;
81 out.i = wpi::byteswap(in.i);
82 return out.f;
83}
84
85inline double getSwappedBytes(double C) {
86 union {
87 uint64_t i;
88 double d;
89 } in, out;
90 in.d = C;
91 out.i = wpi::byteswap(in.i);
92 return out.d;
93}
94
95template <typename T>
96inline std::enable_if_t<std::is_enum_v<T>, T> getSwappedBytes(T C) {
97 return static_cast<T>(
98 wpi::byteswap(static_cast<std::underlying_type_t<T>>(C)));
99}
100
101template<typename T>
102inline void swapByteOrder(T &Value) {
103 Value = getSwappedBytes(Value);
104}
105
106} // end namespace sys
107} // end namespace wpi
108
109#endif
This file implements the C++20 <bit> header.
constexpr auto in(type t, int set) -> bool
Definition: core.h:611
void swapByteOrder(T &Value)
Definition: SwapByteOrder.h:102
constexpr bool IsBigEndianHost
Definition: SwapByteOrder.h:54
unsigned char getSwappedBytes(unsigned char C)
Definition: SwapByteOrder.h:59
static const bool IsLittleEndianHost
Definition: SwapByteOrder.h:57
Definition: ntcore_cpp.h:26
constexpr T byteswap(T V) noexcept
Reverses the bytes in the given integer value V.
Definition: bit.h:53