LLVM 20.0.0git
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 LLVM_SUPPORT_SWAPBYTEORDER_H
15#define LLVM_SUPPORT_SWAPBYTEORDER_H
16
18#include "llvm/ADT/bit.h"
19#include <cstdint>
20#include <type_traits>
21
22namespace llvm {
23
24namespace sys {
25
26constexpr bool IsBigEndianHost =
28
30
31inline unsigned char getSwappedBytes(unsigned char C) { return llvm::byteswap(C); }
32inline signed char getSwappedBytes( signed char C) { return llvm::byteswap(C); }
33inline char getSwappedBytes( char C) { return llvm::byteswap(C); }
34
35inline unsigned short getSwappedBytes(unsigned short C) { return llvm::byteswap(C); }
36inline signed short getSwappedBytes( signed short C) { return llvm::byteswap(C); }
37
38inline unsigned int getSwappedBytes(unsigned int C) { return llvm::byteswap(C); }
39inline signed int getSwappedBytes( signed int C) { return llvm::byteswap(C); }
40
41inline unsigned long getSwappedBytes(unsigned long C) { return llvm::byteswap(C); }
42inline signed long getSwappedBytes( signed long C) { return llvm::byteswap(C); }
43
44inline unsigned long long getSwappedBytes(unsigned long long C) { return llvm::byteswap(C); }
45inline signed long long getSwappedBytes( signed long long C) { return llvm::byteswap(C); }
46
47inline float getSwappedBytes(float C) {
48 return llvm::bit_cast<float>(llvm::byteswap(llvm::bit_cast<uint32_t>(C)));
49}
50
51inline double getSwappedBytes(double C) {
52 return llvm::bit_cast<double>(llvm::byteswap(llvm::bit_cast<uint64_t>(C)));
53}
54
55template <typename T>
56inline std::enable_if_t<std::is_enum_v<T>, T> getSwappedBytes(T C) {
57 return static_cast<T>(llvm::byteswap(llvm::to_underlying(C)));
58}
59
60template<typename T>
61inline void swapByteOrder(T &Value) {
63}
64
65} // end namespace sys
66} // end namespace llvm
67
68#endif
This file contains library features backported from future STL versions.
This file implements the C++20 <bit> header.
LLVM Value Representation.
Definition: Value.h:74
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
static const bool IsLittleEndianHost
Definition: SwapByteOrder.h:29
unsigned char getSwappedBytes(unsigned char C)
Definition: SwapByteOrder.h:31
constexpr bool IsBigEndianHost
Definition: SwapByteOrder.h:26
void swapByteOrder(T &Value)
Definition: SwapByteOrder.h:61
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
constexpr T byteswap(T V) noexcept
Reverses the bytes in the given integer value V.
Definition: bit.h:101
constexpr std::underlying_type_t< Enum > to_underlying(Enum E)
Returns underlying integer value of an enum.