implement endiannes run-time detection
This commit is contained in:
parent
deec226221
commit
50c17f33d9
6 changed files with 52 additions and 1 deletions
|
|
@ -7,6 +7,7 @@ set(HEADERS
|
|||
list.h
|
||||
array.h
|
||||
bitmap.h
|
||||
endian.h
|
||||
)
|
||||
|
||||
set(SOURCES
|
||||
|
|
@ -14,6 +15,7 @@ set(SOURCES
|
|||
list.c
|
||||
array.c
|
||||
bitmap.c
|
||||
endian.c
|
||||
)
|
||||
|
||||
add_library(cutil STATIC ${SOURCES} ${HEADERS})
|
||||
|
|
|
|||
3
cutil.c
3
cutil.c
|
|
@ -1,4 +1,5 @@
|
|||
#include "cutil.h"
|
||||
#include "endian.h"
|
||||
#include <string.h>
|
||||
|
||||
void* (*cu_malloc)(size_t);
|
||||
|
|
@ -10,6 +11,8 @@ void* (*cu_memmove)(void*,const void*,size_t);
|
|||
|
||||
void cutil_init()
|
||||
{
|
||||
cu_endian_init();
|
||||
|
||||
cu_malloc = CUTIL_MALLOC;
|
||||
cu_realloc = CUTIL_REALLOC;
|
||||
cu_free = CUTIL_FREE;
|
||||
|
|
|
|||
|
|
@ -89,7 +89,7 @@ typedef int64_t iword;
|
|||
typedef uint32_t uword;
|
||||
typedef int32_t iword;
|
||||
#else
|
||||
# warning "define CU_32BIT or CU_64BIT"
|
||||
# warning "unknown bus width for arch " CU_ARCH
|
||||
#endif
|
||||
|
||||
typedef uint32_t uint;
|
||||
|
|
|
|||
27
endian.c
Normal file
27
endian.c
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
#include "endian.h"
|
||||
|
||||
enum cu_endian_e cu_endian;
|
||||
|
||||
static const union {
|
||||
uint8_t bytes[4];
|
||||
uint32_t value;
|
||||
} host_order = {
|
||||
{0, 1, 2, 3}
|
||||
};
|
||||
|
||||
#define ORDER_LITTLE_ENDIAN 0x03020100
|
||||
#define ORDER_BIG_ENDIAN 0x00010203
|
||||
#define ORDER_PDP_ENDIAN 0x01000302
|
||||
#define ORDER_HONEYWELL_ENDIAN 0x02030001
|
||||
|
||||
void cu_endian_init()
|
||||
{
|
||||
if (host_order.value == ORDER_LITTLE_ENDIAN)
|
||||
cu_endian = LittleEndian;
|
||||
else if (host_order.value == ORDER_BIG_ENDIAN)
|
||||
cu_endian = BigEndian;
|
||||
else if (host_order.value == ORDER_PDP_ENDIAN)
|
||||
cu_endian = PDPEndian;
|
||||
else if (host_order.value == ORDER_HONEYWELL_ENDIAN)
|
||||
cu_endian = HoneywellEndian;
|
||||
}
|
||||
17
endian.h
Normal file
17
endian.h
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
#ifndef __ENDIAN_H
|
||||
#define __ENDIAN_H
|
||||
|
||||
#include "cutypes.h"
|
||||
|
||||
enum cu_endian_e {
|
||||
LittleEndian,
|
||||
BigEndian,
|
||||
PDPEndian,
|
||||
HoneywellEndian
|
||||
};
|
||||
|
||||
extern enum cu_endian_e cu_endian;
|
||||
|
||||
void cu_endian_init();
|
||||
|
||||
#endif
|
||||
2
test.c
2
test.c
|
|
@ -5,6 +5,7 @@
|
|||
#include "list.h"
|
||||
#include "array.h"
|
||||
#include "bitmap.h"
|
||||
#include "endian.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
|
|
@ -76,6 +77,7 @@ int main()
|
|||
|
||||
printf("[cutil]\n");
|
||||
printf("cutil_arch\t%u\n", CU_ARCH);
|
||||
printf("cutil_endian\t%u\n", cu_endian);
|
||||
|
||||
cutil_exit();
|
||||
return 0;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue