LibC: Add endian.h

This is part of POSIX issue 2024 and some software depends on it
This commit is contained in:
Bananymous 2025-06-17 13:03:56 +03:00
parent c61ded8a1e
commit a9f58e96d2
3 changed files with 73 additions and 0 deletions

View File

@ -5,6 +5,7 @@ set(LIBC_SOURCES
ctype.cpp
dirent.cpp
dlfcn.cpp
endian.cpp
environ.cpp
errno.cpp
fcntl.cpp

View File

@ -0,0 +1,31 @@
#include <BAN/Endianness.h>
#include <endian.h>
#define BE_TO_H(size) \
uint##size##_t be##size##toh(uint##size##_t x) { return BAN::big_endian_to_host(x); }
BE_TO_H(16)
BE_TO_H(32)
BE_TO_H(64)
#undef BE_TO_H
#define H_TO_BE(size) \
uint##size##_t htobe##size(uint##size##_t x) { return BAN::host_to_big_endian(x); }
H_TO_BE(16)
H_TO_BE(32)
H_TO_BE(64)
#undef H_TO_BE
#define LE_TO_H(size) \
uint##size##_t le##size##toh(uint##size##_t x) { return BAN::little_endian_to_host(x); }
LE_TO_H(16)
LE_TO_H(32)
LE_TO_H(64)
#undef LE_TO_H
#define H_TO_LE(size) \
uint##size##_t htole##size(uint##size##_t x) { return BAN::host_to_little_endian(x); }
H_TO_LE(16)
H_TO_LE(32)
H_TO_LE(64)
#undef H_TO_LE

View File

@ -0,0 +1,41 @@
#ifndef _ENDIAN_H
#define _ENDIAN_H 1
// https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/endian.h.html
#include <sys/cdefs.h>
#include <stdint.h>
#define LITTLE_ENDIAN 0
#define BIG_ENDIAN 1
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define BYTE_ORDER LITTLE_ENDIAN
#endif
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#define BYTE_ORDER BIG_ENDIAN
#endif
__BEGIN_DECLS
uint16_t be16toh(uint16_t);
uint32_t be32toh(uint32_t);
uint64_t be64toh(uint64_t);
uint16_t htobe16(uint16_t);
uint32_t htobe32(uint32_t);
uint64_t htobe64(uint64_t);
uint16_t htole16(uint16_t);
uint32_t htole32(uint32_t);
uint64_t htole64(uint64_t);
uint16_t le16toh(uint16_t);
uint32_t le32toh(uint32_t);
uint64_t le64toh(uint64_t);
__END_DECLS
#endif