write a portable version of endian.h, to replace the system's endian.h.

utilising GNU C standard library for some of the functionality, or GNU C
extensions.
This commit is contained in:
2025-08-29 11:46:02 +02:00
parent 8b952fb86f
commit 8a5afd6915
5 changed files with 52 additions and 9 deletions

View File

@@ -1,9 +1,45 @@
/* Copyright (c) 2025 Quinn /* Copyright (c) 2025 Quinn
* Licensed under the MIT Licence. See LICENSE for details */ * Licensed under the MIT Licence. See LICENSE for details */
#pragma once
#if __has_include(<endian.h>) #ifndef PORTABLE_ENDIAN_H
#include <endian.h> #define PORTABLE_ENDIAN_H 1
#if defined(__GNUC__)
/* test for the byteswap header */
#if __has_include(<byteswap.h>)
#include <byteswap.h>
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define le16toh(x) (x)
#define le32toh(x) (x)
#define le64toh(x) (x)
#define htole16(x) (x)
#define htole32(x) (x)
#define htole64(x) (x)
#define be16toh(x) __bswap_16(x)
#define be32toh(x) __bswap_32(x)
#define be64toh(x) __bswap_64(x)
#define htobe16(x) __bswap_16(x)
#define htobe32(x) __bswap_32(x)
#define htobe64(x) __bswap_64(x)
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#define le16toh(x) __bswap_16(x)
#define le32toh(x) __bswap_32(x)
#define le64toh(x) __bswap_64(x)
#define htole16(x) __bswap_16(x)
#define htole32(x) __bswap_32(x)
#define htole64(x) __bswap_64(x)
#define be16toh(x) (x)
#define be32toh(x) (x)
#define be64toh(x) (x)
#define htobe16(x) (x)
#define htobe32(x) (x)
#define htobe64(x) (x)
#else
#error machine architecture unsupported! Expected either big-endian or little-endian, make sure to use a compiler which defines __BYTE_ORDER__ (like clang or gcc)
#endif /* byte order */
/* otherwise, utilise the compiler built-ins */
#else #else
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define le16toh(x) __uint16_identity(x) #define le16toh(x) __uint16_identity(x)
@@ -33,5 +69,12 @@
#define htobe64(x) __uint64_identity(x) #define htobe64(x) __uint64_identity(x)
#else #else
#error machine architecture unsupported! Expected either big-endian or little-endian, make sure to use a compiler which defines __BYTE_ORDER__ (like clang or gcc) #error machine architecture unsupported! Expected either big-endian or little-endian, make sure to use a compiler which defines __BYTE_ORDER__ (like clang or gcc)
#endif #endif /* byte order */
#endif
#endif /* has byteswap.h */
#else
#error GNU C is unavailable
#endif /* __GNUC__ */
#endif /* PORTABLE_ENDIAN_H */

View File

@@ -51,7 +51,7 @@ PROF = rel
CFLAGS += -DNDEBUG -O2 CFLAGS += -DNDEBUG -O2
endif endif
CFLAGS += $(shell pkg-config --cflags glfw3 libarchive) -Ilib/glad/include CFLAGS += $(shell pkg-config --cflags glfw3 libarchive) -Iinclude -Ilib/glad/include
LDFLAGS += $(shell pkg-config --libs glfw3 libarchive) -lm LDFLAGS += $(shell pkg-config --libs glfw3 libarchive) -lm
# get source files # get source files

View File

@@ -3,12 +3,12 @@
#include "mcx.h" #include "mcx.h"
#include <assert.h> #include <assert.h>
#include <endian.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#include "../util/compat/endian.h"
#include "../util/intdef.h" #include "../util/intdef.h"
#define TABLE 0x2000 // table byte size #define TABLE 0x2000 // table byte size

View File

@@ -3,11 +3,11 @@
#include "nbt.h" #include "nbt.h"
#include <assert.h> #include <assert.h>
#include <endian.h>
#include <stddef.h> #include <stddef.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "../util/compat/endian.h"
#include "../util/intdef.h" #include "../util/intdef.h"
#define MAX_DEPTH 512 #define MAX_DEPTH 512

View File

@@ -3,11 +3,11 @@
#pragma once #pragma once
#include <assert.h> #include <assert.h>
#include <endian.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include "../util/atrb.h" #include "../util/atrb.h"
#include "../util/compat/endian.h"
#include "../util/intdef.h" #include "../util/intdef.h"
/* NBT (named binary tag) is a tree data structure. Tags have a numeric type ID, name and a payload. /* NBT (named binary tag) is a tree data structure. Tags have a numeric type ID, name and a payload.