endian.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // header defining supported endian types
  2. #ifndef endian_h
  3. #define endian_h
  4. // all dependencies
  5. #include "basic_types.h"
  6. #include "mem_space.h"
  7. // enum with supported endianness
  8. typedef enum
  9. {
  10. OWL_ENDIAN_UNKNOWN, // no endian (not supported)
  11. OWL_ENDIAN_BIG, // big endian mode
  12. OWL_ENDIAN_LITTLE, // little endian mode
  13. } owl_endian;
  14. // initialized to that for now, it is mandatory
  15. // to execute the CHECK_SYS_ENDIAN() funtion
  16. // for all the other functions in this library
  17. // to execute correctly
  18. owl_endian OWL_SYS_ENDIAN = OWL_ENDIAN_UNKNOWN;
  19. typedef enum
  20. {
  21. OWL_TYPE_ENDIAN = OWL_TYPE_MEM_SPACE_ERR + 1,
  22. } owl_type_endian_h;
  23. // functions
  24. void CHECK_SYS_ENDIAN(void);
  25. // CHECK_SYS_ENDIAN() function
  26. // function to check the system current endianness.
  27. // Can only consider BIG and LITTLE endian systems as of now
  28. void CHECK_SYS_ENDIAN(void)
  29. {
  30. // no chance mate
  31. if (sizeof(owl_umax) < 4) /* I need at least a 4 byte integer type */ {
  32. printf("ENDIAN_ERR: MAX INTEGER TYPE SIZE UNDER 4 BYTES.\n");
  33. exit(1);
  34. }
  35. // check endianness
  36. owl_umax test = 0x00112233;
  37. owl_byte * ptr = (void *) &test;
  38. if (ptr[sizeof(owl_umax) - 1] == 0x33
  39. && ptr[sizeof(owl_umax) - 2] == 0x22
  40. && ptr[sizeof(owl_umax) - 3] == 0x11
  41. && ptr[sizeof(owl_umax) - 4] == 0x00)
  42. OWL_SYS_ENDIAN = OWL_ENDIAN_BIG;
  43. else if (ptr[0] == 0x33
  44. && ptr[1] == 0x22
  45. && ptr[2] == 0x11
  46. && ptr[3] == 0x00)
  47. OWL_SYS_ENDIAN = OWL_ENDIAN_LITTLE;
  48. else /* wont allow program execution on an unknown endian system */ {
  49. printf("ENDIAN_ERR: UNKNOWN SYSTEM ENDIAN.\n");
  50. exit(1);
  51. }
  52. }
  53. #endif // endian_h