OpenTTD Source 20250205-master-gfd85ab1e2c
endian_func.hpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
10#ifndef ENDIAN_FUNC_HPP
11#define ENDIAN_FUNC_HPP
12
13#include "bitmath_func.hpp"
14
15static constexpr uint16_t FROM_BE16(uint16_t x)
16{
17 if constexpr (std::endian::native == std::endian::big) return x;
18 return std::byteswap(x);
19}
20
21static constexpr uint32_t FROM_BE32(uint32_t x)
22{
23 if constexpr (std::endian::native == std::endian::big) return x;
24 return std::byteswap(x);
25}
26
27static constexpr uint16_t TO_BE16(uint16_t x)
28{
29 if constexpr (std::endian::native == std::endian::big) return x;
30 return std::byteswap(x);
31}
32
33static constexpr uint32_t TO_BE32(uint32_t x)
34{
35 if constexpr (std::endian::native == std::endian::big) return x;
36 return std::byteswap(x);
37}
38
39static constexpr uint16_t FROM_LE16(uint16_t x)
40{
41 if constexpr (std::endian::native == std::endian::little) return x;
42 return std::byteswap(x);
43}
44
45static constexpr uint32_t FROM_LE32(uint32_t x)
46{
47 if constexpr (std::endian::native == std::endian::little) return x;
48 return std::byteswap(x);
49}
50
51static constexpr uint16_t TO_LE16(uint16_t x)
52{
53 if constexpr (std::endian::native == std::endian::little) return x;
54 return std::byteswap(x);
55}
56
57static constexpr uint32_t TO_LE32(uint32_t x)
58{
59 if constexpr (std::endian::native == std::endian::little) return x;
60 return std::byteswap(x);
61}
62
63#endif /* ENDIAN_FUNC_HPP */
Functions related to bit mathematics.
constexpr enable_if_t< is_integral_v< T >, T > byteswap(T x) noexcept
Custom implementation of std::byteswap; remove once we build with C++23.