SeqAn3 3.4.0-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
to_little_endian.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2006-2024 Knut Reinert & Freie Universität Berlin
2// SPDX-FileCopyrightText: 2016-2024 Knut Reinert & MPI für molekulare Genetik
3// SPDX-License-Identifier: BSD-3-Clause
4
10#pragma once
11
12#include <bit>
13#include <concepts>
14
15// Find correct header for byte-order conversion functions.
16#if __has_include(<endian.h>) // unix GLIBC
17# include <endian.h>
18#elif __has_include(<sys/endian.h>) // *BSD
19# include <sys/endian.h>
20#endif // __has_include(endian.h)
21
23
24namespace seqan3::detail
25{
38template <std::integral type>
39constexpr type to_little_endian(type const in) noexcept
40{
41 if constexpr (std::endian::native == std::endian::little)
42 {
43 return in;
44 }
45 else if constexpr (std::endian::native == std::endian::big)
46 {
47 static_assert(sizeof(type) <= 8,
48 "Can only convert the byte encoding for integral numbers with a size of up to 8 bytes.");
49 static_assert(std::has_single_bit(sizeof(type)),
50 "Can only convert the byte encoding for integral numbers whose byte size is a power of two.");
51
52 if constexpr (sizeof(type) == 2)
53 return htole16(in);
54 else if constexpr (sizeof(type) == 4)
55 return htole32(in);
56 else if constexpr (sizeof(type) == 8)
57 return htole64(in);
58 else
59 return in; // single byte.
60 }
61 else
62 {
63 static_assert(std::endian::native == std::endian::little || std::endian::native == std::endian::big,
64 "Expected a little-endian or big-endian platform.");
65 }
66}
67
68} // namespace seqan3::detail
T has_single_bit(T... args)
Provides platform and dependency checks.
Hide me