diff --git a/src/util/compat/endian.h b/include/endian.h similarity index 50% rename from src/util/compat/endian.h rename to include/endian.h index e113efb..bf9e606 100644 --- a/src/util/compat/endian.h +++ b/include/endian.h @@ -1,9 +1,45 @@ /* Copyright (c) 2025 Quinn * Licensed under the MIT Licence. See LICENSE for details */ -#pragma once -#if __has_include() -#include +#ifndef PORTABLE_ENDIAN_H +#define PORTABLE_ENDIAN_H 1 + +#if defined(__GNUC__) + +/* test for the byteswap header */ +#if __has_include() +#include +#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 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ #define le16toh(x) __uint16_identity(x) @@ -33,5 +69,12 @@ #define htobe64(x) __uint64_identity(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 -#endif +#endif /* byte order */ + +#endif /* has byteswap.h */ + +#else +#error GNU C is unavailable +#endif /* __GNUC__ */ + +#endif /* PORTABLE_ENDIAN_H */ diff --git a/makefile b/makefile index 7751663..a437a08 100644 --- a/makefile +++ b/makefile @@ -51,7 +51,7 @@ PROF = rel CFLAGS += -DNDEBUG -O2 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 # get source files diff --git a/src/dat/mcx.c b/src/dat/mcx.c index 242f5af..dfd05f2 100644 --- a/src/dat/mcx.c +++ b/src/dat/mcx.c @@ -3,12 +3,12 @@ #include "mcx.h" #include +#include #include #include #include #include -#include "../util/compat/endian.h" #include "../util/intdef.h" #define TABLE 0x2000 // table byte size diff --git a/src/dat/nbt.c b/src/dat/nbt.c index 6a449e8..d8de46c 100644 --- a/src/dat/nbt.c +++ b/src/dat/nbt.c @@ -3,11 +3,11 @@ #include "nbt.h" #include +#include #include #include #include -#include "../util/compat/endian.h" #include "../util/intdef.h" #define MAX_DEPTH 512 diff --git a/src/dat/nbt.h b/src/dat/nbt.h index d7ab6ed..ddb85af 100644 --- a/src/dat/nbt.h +++ b/src/dat/nbt.h @@ -3,11 +3,11 @@ #pragma once #include +#include #include #include #include "../util/atrb.h" -#include "../util/compat/endian.h" #include "../util/intdef.h" /* NBT (named binary tag) is a tree data structure. Tags have a numeric type ID, name and a payload.