Skip to content

Commit

Permalink
Split fnd/types.h into separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
jakcron committed Mar 17, 2018
1 parent 74c15bd commit 4cfcf6b
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 46 deletions.
14 changes: 14 additions & 0 deletions 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)
56 changes: 56 additions & 0 deletions lib/libfnd/include/fnd/Endian.h
@@ -0,0 +1,56 @@
#pragma once
#include <cinttypes>

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 T, T (*F)(T)>
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<uint16_t, le_hword> le_uint16_t;
typedef ISerialiseablePrimative<uint16_t, be_hword> be_uint16_t;
typedef ISerialiseablePrimative<uint32_t, le_word> le_uint32_t;
typedef ISerialiseablePrimative<uint32_t, be_word> be_uint32_t;
typedef ISerialiseablePrimative<uint64_t, le_dword> le_uint64_t;
typedef ISerialiseablePrimative<uint64_t, be_dword> be_uint64_t;
50 changes: 4 additions & 46 deletions lib/libfnd/include/fnd/types.h
@@ -1,62 +1,20 @@
#pragma once
#include <cstdint>
#include <cinttypes>
#include <fnd/Exception.h>
#include <fnd/BitMath.h>
#include <fnd/Endian.h>

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

0 comments on commit 4cfcf6b

Please sign in to comment.