Introducing short to data_t and handlers for converting endian
This commit is contained in:
21
lib/base.c
21
lib/base.c
@@ -14,6 +14,12 @@
|
||||
|
||||
#include <string.h>
|
||||
|
||||
short_t short_byteswap(const short_t w)
|
||||
{
|
||||
// TODO: is there a faster way of doing this?
|
||||
return WORD_NTH_BYTE(w, 1) | (WORD_NTH_BYTE(w, 0) << 8);
|
||||
}
|
||||
|
||||
hword_t hword_byteswap(const hword_t w)
|
||||
{
|
||||
// TODO: is there a faster way of doing this?
|
||||
@@ -30,6 +36,15 @@ word_t word_byteswap(const word_t w)
|
||||
WORD_NTH_BYTE(w, 1) << 48 | WORD_NTH_BYTE(w, 0) << 56;
|
||||
}
|
||||
|
||||
short_t convert_bytes_to_short(const byte_t *bytes)
|
||||
{
|
||||
short_t s = 0;
|
||||
memcpy(&s, bytes, SHORT_SIZE);
|
||||
if (!LITTLE_ENDIAN)
|
||||
s = short_byteswap(s);
|
||||
return s;
|
||||
}
|
||||
|
||||
hword_t convert_bytes_to_hword(const byte_t *bytes)
|
||||
{
|
||||
hword_t h = 0;
|
||||
@@ -48,6 +63,12 @@ word_t convert_bytes_to_word(const byte_t *bytes)
|
||||
return h;
|
||||
}
|
||||
|
||||
void convert_short_to_bytes(short_t w, byte_t *bytes)
|
||||
{
|
||||
short_t h = LITTLE_ENDIAN ? w : short_byteswap(w);
|
||||
memcpy(bytes, &h, SHORT_SIZE);
|
||||
}
|
||||
|
||||
void convert_hword_to_bytes(hword_t w, byte_t *bytes)
|
||||
{
|
||||
hword_t h = LITTLE_ENDIAN ? w : hword_byteswap(w);
|
||||
|
||||
49
lib/base.h
49
lib/base.h
@@ -35,22 +35,26 @@
|
||||
/* Ease of use aliases for numeric types */
|
||||
typedef uint8_t u8;
|
||||
typedef int8_t i8;
|
||||
typedef uint16_t u16;
|
||||
typedef int16_t i16;
|
||||
typedef uint32_t u32;
|
||||
typedef int32_t i32;
|
||||
typedef float f32;
|
||||
typedef uint64_t u64;
|
||||
typedef int64_t i64;
|
||||
|
||||
typedef float f32;
|
||||
typedef double f64;
|
||||
|
||||
typedef u8 byte_t;
|
||||
typedef i8 sbyte_t;
|
||||
typedef u16 short_t;
|
||||
typedef i16 sshort_t;
|
||||
typedef u32 hword_t;
|
||||
typedef i32 shword_t;
|
||||
typedef u64 word_t;
|
||||
typedef i64 sword_t;
|
||||
|
||||
/* Macros for the sizes of common base data types. */
|
||||
#define SHORT_SIZE sizeof(short_t)
|
||||
#define HWORD_SIZE sizeof(hword_t)
|
||||
#define WORD_SIZE sizeof(word_t)
|
||||
|
||||
@@ -74,6 +78,8 @@ typedef union
|
||||
{
|
||||
byte_t as_byte;
|
||||
sbyte_t as_sbyte;
|
||||
short_t as_short;
|
||||
sshort_t as_sshort;
|
||||
hword_t as_hword;
|
||||
shword_t as_shword;
|
||||
word_t as_word;
|
||||
@@ -88,12 +94,14 @@ typedef enum
|
||||
{
|
||||
DATA_TYPE_NIL = -1,
|
||||
DATA_TYPE_BYTE,
|
||||
DATA_TYPE_SHORT,
|
||||
DATA_TYPE_HWORD,
|
||||
DATA_TYPE_WORD,
|
||||
} data_type_t;
|
||||
|
||||
/* Some macros for constructing data_t instances quickly. */
|
||||
#define DBYTE(BYTE) ((data_t){.as_byte = (BYTE)})
|
||||
#define DSHORT(SHORT) ((data_t){.as_short = (SHORT)})
|
||||
#define DHWORD(HWORD) ((data_t){.as_hword = (HWORD)})
|
||||
#define DWORD(WORD) ((data_t){.as_word = (WORD)})
|
||||
|
||||
@@ -119,6 +127,13 @@ static const hword_t __i = 0xFFFF0000;
|
||||
*/
|
||||
#define WORD_NTH_BYTE(WORD, N) (((WORD) >> ((N) * 8)) & 0xFF)
|
||||
|
||||
/**
|
||||
@brief Return the Nth short of WORD.
|
||||
|
||||
@details N should range from 0 to 3 as there are 4 shorts in a word
|
||||
*/
|
||||
#define WORD_NTH_SHORT(WORD, N) (((WORD) >> ((N) * 16)) & 0xFFFF)
|
||||
|
||||
/**
|
||||
@brief Return the Nth half word of WORD.
|
||||
|
||||
@@ -127,6 +142,26 @@ static const hword_t __i = 0xFFFF0000;
|
||||
*/
|
||||
#define WORD_NTH_HWORD(WORD, N) (((WORD) >> ((N) * 32)) & 0xFFFFFFFF)
|
||||
|
||||
/**
|
||||
@brief Convert a buffer of bytes to a short
|
||||
|
||||
@details It is assumed that the buffer of bytes are in virtual
|
||||
machine byte code format (little endian) and that they are at least
|
||||
SHORT_SIZE in size.
|
||||
*/
|
||||
short_t convert_bytes_to_short(const byte_t *buffer);
|
||||
|
||||
/**
|
||||
@brief Convert a half word into a VM byte code format bytes (big
|
||||
endian)
|
||||
|
||||
@param s: Short to convert
|
||||
|
||||
@param buffer: Buffer to store into. It is assumed that the buffer
|
||||
has at least SHORT_SIZE space.
|
||||
*/
|
||||
void convert_short_to_bytes(const short_t s, byte_t *buffer);
|
||||
|
||||
/**
|
||||
@brief Convert a buffer of bytes to a half word.
|
||||
|
||||
@@ -167,6 +202,16 @@ word_t convert_bytes_to_word(const byte_t *);
|
||||
*/
|
||||
void convert_word_to_bytes(const word_t w, byte_t *buffer);
|
||||
|
||||
/**
|
||||
@brief Swap the ordering of bytes within an short
|
||||
|
||||
@details The ordering of the bytes in the short are reversed (2
|
||||
bytes in a short).
|
||||
|
||||
@param s: short to swap
|
||||
*/
|
||||
short_t short_byteswap(const short_t s);
|
||||
|
||||
/**
|
||||
@brief Swap the ordering of bytes within an half word
|
||||
|
||||
|
||||
Reference in New Issue
Block a user