12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- // header defining supported endian types
- #ifndef endian_h
- #define endian_h
- // all dependencies
- #include "basic_types.h"
- #include "mem_space.h"
- // enum with supported endianness
- typedef enum
- {
- OWL_ENDIAN_UNKNOWN, // no endian (not supported)
- OWL_ENDIAN_BIG, // big endian mode
- OWL_ENDIAN_LITTLE, // little endian mode
- } owl_endian;
- // initialized to that for now, it is mandatory
- // to execute the CHECK_SYS_ENDIAN() funtion
- // for all the other functions in this library
- // to execute correctly
- owl_endian OWL_SYS_ENDIAN = OWL_ENDIAN_UNKNOWN;
- typedef enum
- {
- OWL_TYPE_ENDIAN = OWL_TYPE_MEM_SPACE_ERR + 1,
- } owl_type_endian_h;
- // functions
- void CHECK_SYS_ENDIAN(void);
- // CHECK_SYS_ENDIAN() function
- // function to check the system current endianness.
- // Can only consider BIG and LITTLE endian systems as of now
- void CHECK_SYS_ENDIAN(void)
- {
- // no chance mate
- if (sizeof(owl_umax) < 4) /* I need at least a 4 byte integer type */ {
- printf("ENDIAN_ERR: MAX INTEGER TYPE SIZE UNDER 4 BYTES.\n");
- exit(1);
- }
- // check endianness
- owl_umax test = 0x00112233;
- owl_byte * ptr = (void *) &test;
- if (ptr[sizeof(owl_umax) - 1] == 0x33
- && ptr[sizeof(owl_umax) - 2] == 0x22
- && ptr[sizeof(owl_umax) - 3] == 0x11
- && ptr[sizeof(owl_umax) - 4] == 0x00)
- OWL_SYS_ENDIAN = OWL_ENDIAN_BIG;
- else if (ptr[0] == 0x33
- && ptr[1] == 0x22
- && ptr[2] == 0x11
- && ptr[3] == 0x00)
- OWL_SYS_ENDIAN = OWL_ENDIAN_LITTLE;
- else /* wont allow program execution on an unknown endian system */ {
- printf("ENDIAN_ERR: UNKNOWN SYSTEM ENDIAN.\n");
- exit(1);
- }
- }
- #endif // endian_h
|