unaligned.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2009, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. * \brief Handle unaligned data access
  20. */
  21. #ifndef _ASTERISK_UNALIGNED_H
  22. #define _ASTERISK_UNALIGNED_H
  23. #if defined(__cplusplus) || defined(c_plusplus)
  24. extern "C" {
  25. #endif
  26. #ifdef __GNUC__
  27. /* If we just tell GCC what's going on, we can trust it to behave optimally */
  28. static inline uint64_t get_unaligned_uint64(const void *p)
  29. {
  30. const struct { uint64_t d; } __attribute__((packed)) *pp = p;
  31. return pp->d;
  32. }
  33. static inline unsigned int get_unaligned_uint32(const void *p)
  34. {
  35. const struct { unsigned int d; } __attribute__((packed)) *pp = p;
  36. return pp->d;
  37. }
  38. static inline unsigned short get_unaligned_uint16(const void *p)
  39. {
  40. const struct { unsigned short d; } __attribute__((packed)) *pp = p;
  41. return pp->d;
  42. }
  43. static inline void put_unaligned_uint64(void *p, uint64_t datum)
  44. {
  45. struct { uint64_t d; } __attribute__((packed,may_alias)) *pp = p;
  46. pp->d = datum;
  47. }
  48. static inline void put_unaligned_uint32(void *p, unsigned int datum)
  49. {
  50. struct { unsigned int d; } __attribute__((packed)) *pp = p;
  51. pp->d = datum;
  52. }
  53. static inline void put_unaligned_uint16(void *p, unsigned short datum)
  54. {
  55. struct { unsigned short d; } __attribute__((packed)) *pp = p;
  56. pp->d = datum;
  57. }
  58. #elif defined(SOLARIS) && defined(__sparc__)
  59. static inline uint64_t get_unaligned_uint64(const void *p)
  60. {
  61. const unsigned char *cp = p;
  62. return
  63. (((uint64_t) cp[0]) << 56) |
  64. (((uint64_t) cp[1]) << 48) |
  65. (((uint64_t) cp[2]) << 40) |
  66. (((uint64_t) cp[3]) << 32) |
  67. (((uint64_t) cp[4]) << 24) |
  68. (((uint64_t) cp[5]) << 16) |
  69. (((uint64_t) cp[6]) << 8) |
  70. (((uint64_t) cp[7]) << 0);
  71. }
  72. static inline unsigned int get_unaligned_uint32(const void *p)
  73. {
  74. const unsigned char *cp = p;
  75. return (cp[0] << 24) | (cp[1] << 16) | (cp[2] << 8) | cp[3];
  76. }
  77. static inline unsigned short get_unaligned_uint16(const void *p)
  78. {
  79. const unsigned char *cp = p;
  80. return (cp[0] << 8) | cp[1] ;
  81. }
  82. static inline void put_unaligned_uint64(void *p, uint64_t datum)
  83. {
  84. unsigned char *cp = p;
  85. cp[0] = (datum >> 56) & 0xff;
  86. cp[1] = (datum >> 48) & 0xff;
  87. cp[2] = (datum >> 40) & 0xff;
  88. cp[3] = (datum >> 32) & 0xff;
  89. cp[4] = (datum >> 24) & 0xff;
  90. cp[5] = (datum >> 16) & 0xff;
  91. cp[6] = (datum >> 8) & 0xff;
  92. cp[7] = (datum >> 0) & 0xff;
  93. }
  94. static inline void put_unaligned_uint32(void *p, unsigned int datum)
  95. {
  96. unsigned char *cp = p;
  97. cp[0] = datum >> 24;
  98. cp[1] = datum >> 16;
  99. cp[2] = datum >> 8;
  100. cp[3] = datum;
  101. }
  102. static inline void put_unaligned_uint16(void *p, unsigned int datum)
  103. {
  104. unsigned char *cp = p;
  105. cp[0] = datum >> 8;
  106. cp[1] = datum;
  107. }
  108. #else /* Not GCC, not Solaris/SPARC. Assume we can handle direct load/store. */
  109. #define get_unaligned_uint64(p) (*((uint64_t *)(p)))
  110. #define get_unaligned_uint32(p) (*((unsigned int *)(p)))
  111. #define get_unaligned_uint16(p) (*((unsigned short *)(p)))
  112. #define put_unaligned_uint64(p,d) do { uint64_t *__P = (p); *__P = d; } while(0)
  113. #define put_unaligned_uint32(p,d) do { unsigned int *__P = (p); *__P = d; } while(0)
  114. #define put_unaligned_uint16(p,d) do { unsigned short *__P = (p); *__P = d; } while(0)
  115. #endif
  116. #if defined(__cplusplus) || defined(c_plusplus)
  117. }
  118. #endif
  119. #endif /* _ASTERISK_UNALIGNED_H */