ATLAS Offline Software
Loading...
Searching...
No Matches
byteswap.h
Go to the documentation of this file.
1// This file's extension implies that it's C, but it's really -*- C++ -*-.
2/*
3 * Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration.
4 */
11
12
13#ifndef CXXUTILS_BYTESWAP_H
14#define CXXUTILS_BYTESWAP_H
15
16
17#include <version>
18#include <concepts>
19#include <bit>
20#include <array>
21#include <algorithm>
22
23
24namespace CxxUtils {
25
26
27#if __cpp_lib_byteswap
28
29// Use library version if available.
30using std::byteswap;
31
32#else
33
34
41template<std::integral T>
42constexpr T byteswap(T value) noexcept
43{
44 static_assert(std::has_unique_object_representations_v<T>,
45 "T may not have padding bits");
46 auto value_representation = std::bit_cast<std::array<std::byte, sizeof(T)>>(value);
47 std::ranges::reverse(value_representation);
48 return std::bit_cast<T>(value_representation);
49}
50
51
52#endif // not __cpp_lib_byteswap
53
54
55} // namespace CxxUtils
56
57
58#endif // not CXXUTILS_BYTESWAP_H
constexpr T byteswap(T value) noexcept
Reverse the bytes in n.
Definition byteswap.h:42