endian.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2017 to 2019 David Forsgren Piuva
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would be
  16. // appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not be
  19. // misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. // Get settings from here.
  24. #include "../settings.h"
  25. // Endianness abstraction layer for manipulating byte arrays within unsigned integers
  26. // ENDIAN_POS_ADDR
  27. // Bit-shift in the positive direction of addresses
  28. // Precondition: OFFSET % 8 == 0
  29. // ENDIAN_NEG_ADDR
  30. // Bit-shift in the negative direction of addresses
  31. // Precondition: OFFSET % 8 == 0
  32. // ENDIAN32_BYTE_0, A mask from the byte array {255, 0, 0, 0}
  33. // ENDIAN32_BYTE_1, A mask from the byte array {0, 255, 0, 0}
  34. // ENDIAN32_BYTE_2, A mask from the byte array {0, 0, 255, 0}
  35. // ENDIAN32_BYTE_3, A mask from the byte array {0, 0, 0, 255}
  36. // The DSR_BIG_ENDIAN flag should be given manually as a compiler argument when compiling for big-endian hardware
  37. #ifndef DFPSR_ENDIAN
  38. #define DFPSR_ENDIAN
  39. #include <cstdint>
  40. #ifdef DSR_BIG_ENDIAN
  41. // TODO: Not yet tested on a big-endian machine!
  42. #define ENDIAN_POS_ADDR(VALUE,OFFSET) ((VALUE) >> (OFFSET))
  43. #define ENDIAN_NEG_ADDR(VALUE,OFFSET) ((VALUE) << (OFFSET))
  44. #define ENDIAN32_BYTE_0 0xFF000000u
  45. static_assert(false, "Big-endian mode has not been officially tested!");
  46. #else
  47. #define ENDIAN_POS_ADDR(VALUE,OFFSET) ((VALUE) << (OFFSET))
  48. #define ENDIAN_NEG_ADDR(VALUE,OFFSET) ((VALUE) >> (OFFSET))
  49. #define ENDIAN32_BYTE_0 0x000000FFu
  50. #endif
  51. #define ENDIAN32_BYTE_1 ENDIAN_POS_ADDR(ENDIAN32_BYTE_0, 8)
  52. #define ENDIAN32_BYTE_2 ENDIAN_POS_ADDR(ENDIAN32_BYTE_0, 16)
  53. #define ENDIAN32_BYTE_3 ENDIAN_POS_ADDR(ENDIAN32_BYTE_0, 24)
  54. #endif