relibc/header/endian/mod.rs
1//! `endian.h` implementation.
2//!
3//! See <https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/endian.h.html>.
4
5use crate::platform::types::{uint16_t, uint32_t, uint64_t};
6
7/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/be16toh.html>.
8///
9/// Convert a `uint16_t` from big endian representation to host endian
10/// respresentation.
11#[unsafe(no_mangle)]
12pub extern "C" fn be16toh(x: uint16_t) -> uint16_t {
13 uint16_t::from_be(x)
14}
15
16/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/be16toh.html>.
17///
18/// Convert a `uint32_t` from big endian representation to host endian
19/// respresentation.
20#[unsafe(no_mangle)]
21pub extern "C" fn be32toh(x: uint32_t) -> uint32_t {
22 uint32_t::from_be(x)
23}
24
25/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/be16toh.html>.
26///
27/// Convert a `uint64_t` from big endian representation to host endian
28/// respresentation.
29#[unsafe(no_mangle)]
30pub extern "C" fn be64toh(x: uint64_t) -> uint64_t {
31 uint64_t::from_be(x)
32}
33
34/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/be16toh.html>.
35///
36/// Convert a `uint16_t` from host endian representation to big endian
37/// respresentation.
38#[unsafe(no_mangle)]
39pub extern "C" fn htobe16(x: uint16_t) -> uint16_t {
40 x.to_be()
41}
42
43/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/be16toh.html>.
44///
45/// Convert a `uint32_t` from host endian representation to big endian
46/// respresentation.
47#[unsafe(no_mangle)]
48pub extern "C" fn htobe32(x: uint32_t) -> uint32_t {
49 x.to_be()
50}
51
52/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/be16toh.html>.
53///
54/// Convert a `uint64_t` from host endian representation to big endian
55/// respresentation.
56#[unsafe(no_mangle)]
57pub extern "C" fn htobe64(x: uint64_t) -> uint64_t {
58 x.to_be()
59}
60
61/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/be16toh.html>.
62///
63/// Convert a `uint16_t` from host endian representation to little endian
64/// respresentation.
65#[unsafe(no_mangle)]
66pub extern "C" fn htole16(x: uint16_t) -> uint16_t {
67 x.to_le()
68}
69
70/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/be16toh.html>.
71///
72/// Convert a `uint32_t` from host endian representation to little endian
73/// respresentation.
74#[unsafe(no_mangle)]
75pub extern "C" fn htole32(x: uint32_t) -> uint32_t {
76 x.to_le()
77}
78
79/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/be16toh.html>.
80///
81/// Convert a `uint64_t` from host endian representation to little endian
82/// respresentation.
83#[unsafe(no_mangle)]
84pub extern "C" fn htole64(x: uint64_t) -> uint64_t {
85 x.to_le()
86}
87
88/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/be16toh.html>.
89///
90/// Convert a `uint16_t` from little endian representation to host endian
91/// respresentation.
92#[unsafe(no_mangle)]
93pub extern "C" fn le16toh(x: uint16_t) -> uint16_t {
94 uint16_t::from_le(x)
95}
96
97/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/be16toh.html>.
98///
99/// Convert a `uint32_t` from little endian representation to host endian
100/// respresentation.
101#[unsafe(no_mangle)]
102pub extern "C" fn le32toh(x: uint32_t) -> uint32_t {
103 uint32_t::from_le(x)
104}
105
106/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/be16toh.html>.
107///
108/// Convert a `uint64_t` from little endian representation to host endian
109/// respresentation.
110#[unsafe(no_mangle)]
111pub extern "C" fn le64toh(x: uint64_t) -> uint64_t {
112 uint64_t::from_le(x)
113}