lmms
Cross-platform music production software
Loading...
Searching...
No Matches
endian_handling.h
Go to the documentation of this file.
1/*
2 * endian_handling.h - handle endianness
3 *
4 * Copyright (c) 2005-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
5 * Copyright (c) 2026 Dalton Messmer <messmer.dalton/at/gmail.com>
6 *
7 * This file is part of LMMS - https://lmms.io
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public
20 * License along with this program (see COPYING); if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301 USA.
23 *
24 */
25
26#ifndef LMMS_ENDIAN_HANDLING_H
27#define LMMS_ENDIAN_HANDLING_H
28
29#include <bit>
30#include <type_traits>
31
32namespace lmms {
33
34constexpr bool isLittleEndian() noexcept
35{
36 return std::endian::native == std::endian::little;
37}
38
39constexpr bool isBigEndian() noexcept
40{
41 return std::endian::native == std::endian::big;
42}
43
45template<typename Int>
46 requires (sizeof(Int) <= 4 && std::is_integral_v<Int>)
47constexpr Int byteswap(Int i) noexcept
48{
49 if constexpr (sizeof(Int) == 1) { return i; }
50 else if constexpr (sizeof(Int) == 2)
51 {
52 return ((i & 0xFF) << 8) | ((i >> 8) & 0xFF);
53 }
54 else if constexpr (sizeof(Int) == 4)
55 {
56 return ((i & 0xff000000) >> 24)
57 | ((i & 0x00ff0000) >> 8)
58 | ((i & 0x0000ff00) << 8)
59 | ((i & 0x000000ff) << 24);
60 }
61 else { static_assert(sizeof(Int) == 1, "invalid size"); }
62}
63
64template<std::endian e, typename Int>
65constexpr Int byteswapIf(Int i) noexcept
66{
67 if constexpr (std::endian::native == e) { return byteswap(i); }
68 else { return i; }
69}
70
71template<typename Int>
72constexpr Int byteswapIfBE(Int i) noexcept { return byteswapIf<std::endian::big>(i); }
73
74template<typename Int>
75constexpr Int byteswapIfLE(Int i) noexcept { return byteswapIf<std::endian::little>(i); }
76
77} // namespace lmms
78
79#endif // LMMS_ENDIAN_HANDLING_H
Definition RemotePluginBase.cpp:34
constexpr Int byteswapIf(Int i) noexcept
Definition endian_handling.h:65
constexpr bool isLittleEndian() noexcept
Definition endian_handling.h:34
constexpr Int byteswapIfBE(Int i) noexcept
Definition endian_handling.h:72
constexpr Int byteswap(Int i) noexcept
TODO C++23: Use std::byteswap.
Definition endian_handling.h:47
constexpr Int byteswapIfLE(Int i) noexcept
Definition endian_handling.h:75
constexpr bool isBigEndian() noexcept
Definition endian_handling.h:39