From 4cfcf6bae975dba823d2f2a8ea1a7b173a638054 Mon Sep 17 00:00:00 2001 From: jakcron Date: Sat, 17 Mar 2018 19:59:53 +0800 Subject: [PATCH] Split fnd/types.h into separate files --- lib/libfnd/include/fnd/BitMath.h | 14 ++++++++ lib/libfnd/include/fnd/Endian.h | 56 ++++++++++++++++++++++++++++++++ lib/libfnd/include/fnd/types.h | 50 +++------------------------- 3 files changed, 74 insertions(+), 46 deletions(-) create mode 100644 lib/libfnd/include/fnd/BitMath.h create mode 100644 lib/libfnd/include/fnd/Endian.h diff --git a/lib/libfnd/include/fnd/BitMath.h b/lib/libfnd/include/fnd/BitMath.h new file mode 100644 index 0000000..368dd9a --- /dev/null +++ b/lib/libfnd/include/fnd/BitMath.h @@ -0,0 +1,14 @@ +/* +BitMath.h +(c) 2018 Jakcron + +This is a 0x40 byte header to prepend to raw EXEFS .code binaries that provide enough data to be equivalent to an ELF. +*/ +#pragma once + +// to be deprecated +#define BIT(n) (1ULL << (n)) + +// Bit math macros +#define _BIT(n) BIT(n) +#define _HAS_BIT(val, bit) ((val) & _BIT(bit) != 0) diff --git a/lib/libfnd/include/fnd/Endian.h b/lib/libfnd/include/fnd/Endian.h new file mode 100644 index 0000000..ee90f40 --- /dev/null +++ b/lib/libfnd/include/fnd/Endian.h @@ -0,0 +1,56 @@ +#pragma once +#include + +static inline uint16_t __local_bswap16(uint16_t x) { + return ((x << 8) & 0xff00) | ((x >> 8) & 0x00ff); +} + + +static inline uint32_t __local_bswap32(uint32_t x) { + return ((x << 24) & 0xff000000 ) | + ((x << 8) & 0x00ff0000 ) | + ((x >> 8) & 0x0000ff00 ) | + ((x >> 24) & 0x000000ff ); +} + +static inline uint64_t __local_bswap64(uint64_t x) +{ + return (uint64_t)__local_bswap32(x>>32) | + ((uint64_t)__local_bswap32(x&0xFFFFFFFF) << 32); +} + +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ +static inline uint64_t be_dword(uint64_t a) { return __local_bswap64(a); } +static inline uint32_t be_word(uint32_t a) { return __local_bswap32(a); } +static inline uint16_t be_hword(uint16_t a) { return __local_bswap16(a); } +static inline uint64_t le_dword(uint64_t a) { return a; } +static inline uint32_t le_word(uint32_t a) { return a; } +static inline uint16_t le_hword(uint16_t a) { return a; } +#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ +static inline uint64_t be_dword(uint64_t a) { return a; } +static inline uint32_t be_word(uint32_t a) { return a; } +static inline uint16_t be_hword(uint16_t a) { return a; } +static inline uint64_t le_dword(uint64_t a) { return __local_bswap64(a); } +static inline uint32_t le_word(uint32_t a) { return __local_bswap32(a); } +static inline uint16_t le_hword(uint16_t a) { return __local_bswap16(a); } +#else +#error "What's the endianness of the platform you're targeting?" +#endif + +template +class ISerialiseablePrimative { +public: + inline T get() const { return F(mVar);} + inline void set(T var) { mVar = F(var); } + inline T operator=(T var) { set(var); return get();} + inline T operator*() const { return get(); } +private: + T mVar; +}; + +typedef ISerialiseablePrimative le_uint16_t; +typedef ISerialiseablePrimative be_uint16_t; +typedef ISerialiseablePrimative le_uint32_t; +typedef ISerialiseablePrimative be_uint32_t; +typedef ISerialiseablePrimative le_uint64_t; +typedef ISerialiseablePrimative be_uint64_t; \ No newline at end of file diff --git a/lib/libfnd/include/fnd/types.h b/lib/libfnd/include/fnd/types.h index 3ed1ae8..50961f9 100644 --- a/lib/libfnd/include/fnd/types.h +++ b/lib/libfnd/include/fnd/types.h @@ -1,62 +1,20 @@ #pragma once -#include +#include #include +#include +#include -typedef uint64_t dword_t; -typedef uint32_t word_t; -typedef uint16_t hword_t; -typedef uint8_t byte_t; -typedef int64_t dlong_t; -typedef int32_t long_t; -typedef int16_t short_t; -typedef int8_t char_t; typedef uint64_t u64; typedef uint32_t u32; typedef uint16_t u16; typedef uint8_t u8; - -#define BIT(n) (1ULL << (n)) +typedef uint8_t byte_t; #define MIN(x,y) ((x) <= (y)? (x) : (y)) #define MAX(x,y) ((x) >= (y)? (x) : (y)) -static inline uint16_t __local_bswap16(uint16_t x) { - return ((x << 8) & 0xff00) | ((x >> 8) & 0x00ff); -} - - -static inline uint32_t __local_bswap32(uint32_t x) { - return ((x << 24) & 0xff000000 ) | - ((x << 8) & 0x00ff0000 ) | - ((x >> 8) & 0x0000ff00 ) | - ((x >> 24) & 0x000000ff ); -} - -static inline uint64_t __local_bswap64(uint64_t x) -{ - return (uint64_t)__local_bswap32(x>>32) | - ((uint64_t)__local_bswap32(x&0xFFFFFFFF) << 32); -} - static inline uint64_t align(uint64_t size, uint64_t align) { return (size % align) == 0? size : (size - (size % align) + align); } -#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ -#define be_dword(a) __local_bswap64(a) -#define be_word(a) __local_bswap32(a) -#define be_hword(a) __local_bswap16(a) -#define le_dword(a) (a) -#define le_word(a) (a) -#define le_hword(a) (a) -#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ -#define be_dword(a) (a) -#define be_word(a) (a) -#define be_hword(a) (a) -#define le_dword(a) __local_bswap64(a) -#define le_word(a) __local_bswap32(a) -#define le_hword(a) __local_bswap16(a) -#else -#error "What's the endianness of the platform you're targeting?" -#endif