tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
byte_order.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_NET_BYTE_ORDER_HPP
4#define TETL_NET_BYTE_ORDER_HPP
5
6#include <etl/version.hpp>
7
8#include <etl/cstdint.hpp>
9
10namespace etl::experimental::net {
11template <typename T>
12constexpr auto ntoh(T) -> T = delete;
13
14constexpr auto ntoh(char v) noexcept -> char { return v; }
15
16constexpr auto ntoh(uint8_t v) noexcept -> uint8_t { return v; }
17
18constexpr auto ntoh(int8_t v) noexcept -> int8_t { return v; }
19
20constexpr auto ntoh(uint16_t v) noexcept -> uint16_t { return uint16_t(v << uint16_t(8)) | uint16_t(v >> uint16_t(8)); }
21
22constexpr auto ntoh(uint32_t v) noexcept -> uint32_t
23{
24 auto const a = v << 24;
25 auto const b = (v & 0x0000FF00) << 8;
26 auto const c = (v & 0x00FF0000) >> 8;
27 auto const d = v >> 24;
28
29 return a | b | c | d;
30}
31
32template <typename T>
33constexpr auto hton(T) -> T = delete;
34
35constexpr auto hton(char v) noexcept -> char { return v; }
36
37constexpr auto hton(int8_t v) noexcept -> int8_t { return v; }
38
39constexpr auto hton(uint8_t v) noexcept -> uint8_t { return v; }
40
41constexpr auto hton(uint16_t v) noexcept -> uint16_t { return ntoh(v); }
42
43constexpr auto hton(uint32_t v) noexcept -> uint32_t { return ntoh(v); }
44
45} // namespace etl::experimental::net
46
47#endif // TETL_NET_BYTE_ORDER_HPP
Definition buffer.hpp:14
constexpr auto ntoh(T) -> T=delete
constexpr auto hton(T) -> T=delete
TETL_BUILTIN_UINT16 uint16_t
Unsigned integer type with width of exactly 16 bits.
Definition uint_t.hpp:14
TETL_BUILTIN_INT8 int8_t
Signed integer type with width of exactly 8 bits.
Definition int_t.hpp:11
TETL_BUILTIN_UINT8 uint8_t
Unsigned integer type with width of exactly 8 bits.
Definition uint_t.hpp:11
TETL_BUILTIN_UINT32 uint32_t
Unsigned integer type with width of exactly 32 bits.
Definition uint_t.hpp:17