Files
perfect_dark/src/include/platform.h
T
fgsfds da94644c38 port: make it link and load
nothing happens after loading though
2023-07-29 23:21:35 +02:00

76 lines
1.6 KiB
C

#ifndef _IN_PLATFORM_H
#define _IN_PLATFORM_H
// detect OS
#if defined(_WIN32)
#define PLATFORM_WIN32 1
#else
// assume POSIX compatible
#define PLATFORM_POSIX 1
#if defined(__linux__)
#define PLATFORM_LINUX 1
#else // TODO
#error "Unknown platform."
#endif
#endif
// detect arch
#if defined(__x86_64__) || defined(_M_X64)
#define PLATFORM_X86_64 1
#define PLATFORM_64BIT 1
#elif defined(__i386__) || defined(_X86_) || defined(_M_IX86)
#define PLATFORM_X86 1
#else // TODO
#error "Unknown CPU arch."
#endif
// detect endianness
#if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__)
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#define PLATFORM_BIG_ENDIAN 1
#else
#define PLATFORM_LITTLE_ENDIAN 1
#endif
#elif __BIG_ENDIAN__
#define PLATFORM_BIG_ENDIAN 1
#else
#error "Could not determine endianness."
#endif
// byteswap macros
#ifdef __GNUC__
#define PD_BSWAP16(x) __builtin_bswap16(x)
#define PD_BSWAP32(x) __builtin_bswap32(x)
#define PD_BSWAP64(x) __builtin_bswap64(x)
#else
#error "Implement PD_BSWAP macros for your compiler."
#endif
#ifdef PLATFORM_BIG_ENDIAN
#define PD_BE16(x) (x)
#define PD_BE32(x) (x)
#define PD_BE64(x) (x)
#define PD_LE16(x) PD_BSWAP16(x)
#define PD_LE32(x) PD_BSWAP32(x)
#define PD_LE64(x) PD_BSWAP64(x)
#else
#define PD_BE16(x) PD_BSWAP16(x)
#define PD_BE32(x) PD_BSWAP32(x)
#define PD_BE64(x) PD_BSWAP64(x)
#define PD_LE16(x) (x)
#define PD_LE32(x) (x)
#define PD_LE64(x) (x)
#endif
// TODO: use uintptr_t or something
#ifdef PLATFORM_64BIT
#define PD_BEPTR(x) PD_BE64(x)
#define PD_LEPTR(x) PD_LE64(x)
#else
#define PD_BEPTR(x) PD_BE32(x)
#define PD_LEPTR(x) PD_LE32(x)
#endif
#endif