bstream.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*************************************************************************************************/
  2. /*!
  3. * \file bstream.h
  4. *
  5. * \brief Byte stream to integer conversion macros.
  6. *
  7. * $Date: 2016-08-31 07:40:56 -0700 (Wed, 31 Aug 2016) $
  8. * $Revision: 8672 $
  9. *
  10. * Copyright (c) 2009 Wicentric, Inc., all rights reserved.
  11. * Wicentric confidential and proprietary.
  12. *
  13. * IMPORTANT. Your use of this file is governed by a Software License Agreement
  14. * ("Agreement") that must be accepted in order to download or otherwise receive a
  15. * copy of this file. You may not use or copy this file for any purpose other than
  16. * as described in the Agreement. If you do not agree to all of the terms of the
  17. * Agreement do not use this file and delete all copies in your possession or control;
  18. * if you do not have a copy of the Agreement, you must contact Wicentric, Inc. prior
  19. * to any use, copying or further distribution of this software.
  20. */
  21. /*************************************************************************************************/
  22. #ifndef BSTREAM_H
  23. #define BSTREAM_H
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. /**************************************************************************************************
  28. Macros
  29. **************************************************************************************************/
  30. /*!
  31. * Macros for converting a little endian byte buffer to integers.
  32. */
  33. #define BYTES_TO_UINT16(n, p) {n = ((uint16_t)(p)[0] + ((uint16_t)(p)[1] << 8));}
  34. #define BYTES_TO_UINT24(n, p) {n = ((uint16_t)(p)[0] + ((uint16_t)(p)[1] << 8) + \
  35. ((uint16_t)(p)[2] << 16));}
  36. #define BYTES_TO_UINT32(n, p) {n = ((uint32_t)(p)[0] + ((uint32_t)(p)[1] << 8) + \
  37. ((uint32_t)(p)[2] << 16) + ((uint32_t)(p)[3] << 24));}
  38. #define BYTES_TO_UINT40(n, p) {n = ((uint64_t)(p)[0] + ((uint64_t)(p)[1] << 8) + \
  39. ((uint64_t)(p)[2] << 16) + ((uint64_t)(p)[3] << 24) + \
  40. ((uint64_t)(p)[4] << 32));}
  41. #define BYTES_TO_UINT64(n, p) {n = ((uint64_t)(p)[0] + ((uint64_t)(p)[1] << 8) + \
  42. ((uint64_t)(p)[2] << 16) + ((uint64_t)(p)[3] << 24) + \
  43. ((uint64_t)(p)[4] << 32) + ((uint64_t)(p)[5] << 40) + \
  44. ((uint64_t)(p)[6] << 48) + ((uint64_t)(p)[7] << 56));}
  45. /*!
  46. * Macros for converting little endian integers to array of bytes
  47. */
  48. #define UINT16_TO_BYTES(n) ((u8_t) (n)), ((u8_t)((n) >> 8))
  49. #define UINT32_TO_BYTES(n) ((u8_t) (n)), ((u8_t)((n) >> 8)), ((u8_t)((n) >> 16)), ((u8_t)((n) >> 24))
  50. /*!
  51. * Macros for converting little endian integers to single bytes
  52. */
  53. #define UINT16_TO_BYTE0(n) ((u8_t) (n))
  54. #define UINT16_TO_BYTE1(n) ((u8_t) ((n) >> 8))
  55. #define UINT32_TO_BYTE0(n) ((u8_t) (n))
  56. #define UINT32_TO_BYTE1(n) ((u8_t) ((n) >> 8))
  57. #define UINT32_TO_BYTE2(n) ((u8_t) ((n) >> 16))
  58. #define UINT32_TO_BYTE3(n) ((u8_t) ((n) >> 24))
  59. /*!
  60. * Macros for converting a little endian byte stream to integers, with increment.
  61. */
  62. #define BSTREAM_TO_INT8(n, p) {n = (int8_t)(*(p)++);}
  63. #define BSTREAM_TO_UINT8(n, p) {n = (u8_t)(*(p)++);}
  64. #define BSTREAM_TO_UINT16(n, p) {BYTES_TO_UINT16(n, p); p += 2;}
  65. #define BSTREAM_TO_UINT24(n, p) {BYTES_TO_UINT24(n, p); p += 3;}
  66. #define BSTREAM_TO_UINT32(n, p) {BYTES_TO_UINT32(n, p); p += 4;}
  67. #define BSTREAM_TO_UINT40(n, p) {BYTES_TO_UINT40(n, p); p += 5;}
  68. /*!
  69. * Macros for converting integers to a little endian byte stream, with increment.
  70. */
  71. #define UINT8_TO_BSTREAM(p, n) {*(p)++ = (u8_t)(n);}
  72. #define UINT16_TO_BSTREAM(p, n) {*(p)++ = (u8_t)(n); *(p)++ = (u8_t)((n) >> 8);}
  73. #define UINT24_TO_BSTREAM(p, n) {*(p)++ = (u8_t)(n); *(p)++ = (u8_t)((n) >> 8); \
  74. *(p)++ = (u8_t)((n) >> 16);}
  75. #define UINT32_TO_BSTREAM(p, n) {*(p)++ = (u8_t)(n); *(p)++ = (u8_t)((n) >> 8); \
  76. *(p)++ = (u8_t)((n) >> 16); *(p)++ = (u8_t)((n) >> 24);}
  77. #define UINT40_TO_BSTREAM(p, n) {*(p)++ = (u8_t)(n); *(p)++ = (u8_t)((n) >> 8); \
  78. *(p)++ = (u8_t)((n) >> 16); *(p)++ = (u8_t)((n) >> 24); \
  79. *(p)++ = (u8_t)((n) >> 32);}
  80. /*!
  81. * Macros for converting integers to a little endian byte stream, without increment.
  82. */
  83. #define UINT16_TO_BUF(p, n) {(p)[0] = (u8_t)(n); (p)[1] = (u8_t)((n) >> 8);}
  84. #define UINT24_TO_BUF(p, n) {(p)[0] = (u8_t)(n); (p)[1] = (u8_t)((n) >> 8); \
  85. (p)[2] = (u8_t)((n) >> 16);}
  86. #define UINT32_TO_BUF(p, n) {(p)[0] = (u8_t)(n); (p)[1] = (u8_t)((n) >> 8); \
  87. (p)[2] = (u8_t)((n) >> 16); (p)[3] = (u8_t)((n) >> 24);}
  88. /*!
  89. * Macros for comparing a little endian byte buffer to integers.
  90. */
  91. #define BYTES_UINT16_CMP(p, n) ((p)[1] == UINT16_TO_BYTE1(n) && (p)[0] == UINT16_TO_BYTE0(n))
  92. /*!
  93. * Macros for IEEE FLOAT type: exponent = byte 3, mantissa = bytes 2-0
  94. */
  95. #define FLT_TO_UINT32(m, e) ((m) | ((int32_t)(e) << 24))
  96. #define UINT32_TO_FLT(m, e, n) {m = UINT32_TO_FLT_M(n); e = UINT32_TO_FLT_E(n);}
  97. #define UINT32_TO_FLT_M(n) ((((n) & 0x00FFFFFF) >= 0x00800000) ? \
  98. ((int32_t)(((n) | 0xFF000000))) : ((int32_t)((n) & 0x00FFFFFF)))
  99. #define UINT32_TO_FLT_E(n) ((int8_t)(n >> 24))
  100. /*!
  101. * Macros for IEEE SFLOAT type: exponent = bits 15-12, mantissa = bits 11-0
  102. */
  103. #define SFLT_TO_UINT16(m, e) ((m) | ((s16_t)(e) << 12))
  104. #define UINT16_TO_SFLT(m, e, n) {m = UINT16_TO_SFLT_M(n); e = UINT16_TO_SFLT_E(n);}
  105. #define UINT16_TO_SFLT_M(n) ((((n) & 0x0FFF) >= 0x0800) ? \
  106. ((s16_t)(((n) | 0xF000))) : ((s16_t)((n) & 0x0FFF)))
  107. #define UINT16_TO_SFLT_E(n) (((n >> 12) >= 0x0008) ? \
  108. ((int8_t)(((n >> 12) | 0xF0))) : ((int8_t)(n >> 12)))
  109. /**************************************************************************************************
  110. Function Declarations
  111. **************************************************************************************************/
  112. #ifdef __cplusplus
  113. };
  114. #endif
  115. #endif /* BSTREAM_H */