Clone of PhatAC @ https://github.com/floaterxk/PhatAC

my_byteorder.h 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #ifndef MY_BYTEORDER_INCLUDED
  2. #define MY_BYTEORDER_INCLUDED
  3. /* Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; version 2 of the License.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  14. /*
  15. Functions for reading and storing in machine independent
  16. format (low byte first). There are 'korr' (assume 'corrector') variants
  17. for integer types, but 'get' (assume 'getter') for floating point types.
  18. */
  19. #if defined(__i386__) || defined(_WIN32) || defined(__x86_64__)
  20. #include "byte_order_generic_x86.h"
  21. #else
  22. #include "byte_order_generic.h"
  23. #endif
  24. static inline int32 sint3korr(const uchar *A)
  25. {
  26. return
  27. ((int32) (((A[2]) & 128) ?
  28. (((uint32) 255L << 24) |
  29. (((uint32) A[2]) << 16) |
  30. (((uint32) A[1]) << 8) |
  31. ((uint32) A[0])) :
  32. (((uint32) A[2]) << 16) |
  33. (((uint32) A[1]) << 8) |
  34. ((uint32) A[0])))
  35. ;
  36. }
  37. static inline uint32 uint3korr(const uchar *A)
  38. {
  39. return
  40. (uint32) (((uint32) (A[0])) +
  41. (((uint32) (A[1])) << 8) +
  42. (((uint32) (A[2])) << 16))
  43. ;
  44. }
  45. static inline ulonglong uint5korr(const uchar *A)
  46. {
  47. return
  48. ((ulonglong)(((uint32) (A[0])) +
  49. (((uint32) (A[1])) << 8) +
  50. (((uint32) (A[2])) << 16) +
  51. (((uint32) (A[3])) << 24)) +
  52. (((ulonglong) (A[4])) << 32))
  53. ;
  54. }
  55. static inline ulonglong uint6korr(const uchar *A)
  56. {
  57. return
  58. ((ulonglong)(((uint32) (A[0])) +
  59. (((uint32) (A[1])) << 8) +
  60. (((uint32) (A[2])) << 16) +
  61. (((uint32) (A[3])) << 24)) +
  62. (((ulonglong) (A[4])) << 32) +
  63. (((ulonglong) (A[5])) << 40))
  64. ;
  65. }
  66. static inline void int3store(uchar *T, uint A)
  67. {
  68. *(T)= (uchar) (A);
  69. *(T+1)= (uchar) (A >> 8);
  70. *(T+2)= (uchar) (A >> 16);
  71. }
  72. static inline void int5store(uchar *T, ulonglong A)
  73. {
  74. *(T)= (uchar) (A);
  75. *(T+1)= (uchar) (A >> 8);
  76. *(T+2)= (uchar) (A >> 16);
  77. *(T+3)= (uchar) (A >> 24);
  78. *(T+4)= (uchar) (A >> 32);
  79. }
  80. static inline void int6store(uchar *T, ulonglong A)
  81. {
  82. *(T)= (uchar) (A);
  83. *(T+1)= (uchar) (A >> 8);
  84. *(T+2)= (uchar) (A >> 16);
  85. *(T+3)= (uchar) (A >> 24);
  86. *(T+4)= (uchar) (A >> 32);
  87. *(T+5)= (uchar) (A >> 40);
  88. }
  89. #ifdef __cplusplus
  90. static inline int16 sint2korr(const char *pT)
  91. {
  92. return sint2korr(static_cast<const uchar*>(static_cast<const void*>(pT)));
  93. }
  94. static inline uint16 uint2korr(const char *pT)
  95. {
  96. return uint2korr(static_cast<const uchar*>(static_cast<const void*>(pT)));
  97. }
  98. static inline uint32 uint3korr(const char *pT)
  99. {
  100. return uint3korr(static_cast<const uchar*>(static_cast<const void*>(pT)));
  101. }
  102. static inline int32 sint3korr(const char *pT)
  103. {
  104. return sint3korr(static_cast<const uchar*>(static_cast<const void*>(pT)));
  105. }
  106. static inline uint32 uint4korr(const char *pT)
  107. {
  108. return uint4korr(static_cast<const uchar*>(static_cast<const void*>(pT)));
  109. }
  110. static inline int32 sint4korr(const char *pT)
  111. {
  112. return sint4korr(static_cast<const uchar*>(static_cast<const void*>(pT)));
  113. }
  114. static inline ulonglong uint6korr(const char *pT)
  115. {
  116. return uint6korr(static_cast<const uchar*>(static_cast<const void*>(pT)));
  117. }
  118. static inline ulonglong uint8korr(const char *pT)
  119. {
  120. return uint8korr(static_cast<const uchar*>(static_cast<const void*>(pT)));
  121. }
  122. static inline longlong sint8korr(const char *pT)
  123. {
  124. return sint8korr(static_cast<const uchar*>(static_cast<const void*>(pT)));
  125. }
  126. static inline void int2store(char *pT, uint16 A)
  127. {
  128. int2store(static_cast<uchar*>(static_cast<void*>(pT)), A);
  129. }
  130. static inline void int3store(char *pT, uint A)
  131. {
  132. int3store(static_cast<uchar*>(static_cast<void*>(pT)), A);
  133. }
  134. static inline void int4store(char *pT, uint32 A)
  135. {
  136. int4store(static_cast<uchar*>(static_cast<void*>(pT)), A);
  137. }
  138. static inline void int5store(char *pT, ulonglong A)
  139. {
  140. int5store(static_cast<uchar*>(static_cast<void*>(pT)), A);
  141. }
  142. static inline void int6store(char *pT, ulonglong A)
  143. {
  144. int6store(static_cast<uchar*>(static_cast<void*>(pT)), A);
  145. }
  146. static inline void int8store(char *pT, ulonglong A)
  147. {
  148. int8store(static_cast<uchar*>(static_cast<void*>(pT)), A);
  149. }
  150. #endif /* __cplusplus */
  151. /*
  152. Functions for reading and storing in machine format from/to
  153. short/long to/from some place in memory V should be a variable
  154. and M a pointer to byte.
  155. */
  156. #ifdef WORDS_BIGENDIAN
  157. #include "big_endian.h"
  158. #else
  159. #include "little_endian.h"
  160. #endif
  161. #ifdef __cplusplus
  162. static inline void float4store(char *V, float M)
  163. {
  164. float4store(static_cast<uchar*>(static_cast<void*>(V)), M);
  165. }
  166. static inline void float8get(double *V, const char *M)
  167. {
  168. float8get(V, static_cast<const uchar*>(static_cast<const void*>(M)));
  169. }
  170. static inline void float8store(char *V, double M)
  171. {
  172. float8store(static_cast<uchar*>(static_cast<void*>(V)), M);
  173. }
  174. #endif /* __cplusplus */
  175. #endif /* MY_BYTEORDER_INCLUDED */