my_byteorder.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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, version 2.0,
  6. as published by the Free Software Foundation.
  7. This program is also distributed with certain software (including
  8. but not limited to OpenSSL) that is licensed under separate terms,
  9. as designated in a particular file or component or in included license
  10. documentation. The authors of MySQL hereby grant you an additional
  11. permission to link the program and your derivative works with the
  12. separately licensed software that they have included with MySQL.
  13. Without limiting anything contained in the foregoing, this file,
  14. which is part of C Driver for MySQL (Connector/C), is also subject to the
  15. Universal FOSS Exception, version 1.0, a copy of which can be found at
  16. http://oss.oracle.com/licenses/universal-foss-exception.
  17. This program is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. GNU General Public License, version 2.0, for more details.
  21. You should have received a copy of the GNU General Public License
  22. along with this program; if not, write to the Free Software
  23. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  24. /*
  25. Functions for reading and storing in machine independent
  26. format (low byte first). There are 'korr' (assume 'corrector') variants
  27. for integer types, but 'get' (assume 'getter') for floating point types.
  28. */
  29. #if defined(__i386__) || defined(_WIN32) || defined(__x86_64__)
  30. #include "byte_order_generic_x86.h"
  31. #else
  32. #include "byte_order_generic.h"
  33. #endif
  34. static inline int32 sint3korr(const uchar *A)
  35. {
  36. return
  37. ((int32) (((A[2]) & 128) ?
  38. (((uint32) 255L << 24) |
  39. (((uint32) A[2]) << 16) |
  40. (((uint32) A[1]) << 8) |
  41. ((uint32) A[0])) :
  42. (((uint32) A[2]) << 16) |
  43. (((uint32) A[1]) << 8) |
  44. ((uint32) A[0])))
  45. ;
  46. }
  47. static inline uint32 uint3korr(const uchar *A)
  48. {
  49. return
  50. (uint32) (((uint32) (A[0])) +
  51. (((uint32) (A[1])) << 8) +
  52. (((uint32) (A[2])) << 16))
  53. ;
  54. }
  55. static inline ulonglong uint5korr(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. ;
  64. }
  65. static inline ulonglong uint6korr(const uchar *A)
  66. {
  67. return
  68. ((ulonglong)(((uint32) (A[0])) +
  69. (((uint32) (A[1])) << 8) +
  70. (((uint32) (A[2])) << 16) +
  71. (((uint32) (A[3])) << 24)) +
  72. (((ulonglong) (A[4])) << 32) +
  73. (((ulonglong) (A[5])) << 40))
  74. ;
  75. }
  76. static inline void int3store(uchar *T, uint A)
  77. {
  78. *(T)= (uchar) (A);
  79. *(T+1)= (uchar) (A >> 8);
  80. *(T+2)= (uchar) (A >> 16);
  81. }
  82. static inline void int5store(uchar *T, ulonglong A)
  83. {
  84. *(T)= (uchar) (A);
  85. *(T+1)= (uchar) (A >> 8);
  86. *(T+2)= (uchar) (A >> 16);
  87. *(T+3)= (uchar) (A >> 24);
  88. *(T+4)= (uchar) (A >> 32);
  89. }
  90. static inline void int6store(uchar *T, ulonglong A)
  91. {
  92. *(T)= (uchar) (A);
  93. *(T+1)= (uchar) (A >> 8);
  94. *(T+2)= (uchar) (A >> 16);
  95. *(T+3)= (uchar) (A >> 24);
  96. *(T+4)= (uchar) (A >> 32);
  97. *(T+5)= (uchar) (A >> 40);
  98. }
  99. #ifdef __cplusplus
  100. static inline int16 sint2korr(const char *pT)
  101. {
  102. return sint2korr(static_cast<const uchar*>(static_cast<const void*>(pT)));
  103. }
  104. static inline uint16 uint2korr(const char *pT)
  105. {
  106. return uint2korr(static_cast<const uchar*>(static_cast<const void*>(pT)));
  107. }
  108. static inline uint32 uint3korr(const char *pT)
  109. {
  110. return uint3korr(static_cast<const uchar*>(static_cast<const void*>(pT)));
  111. }
  112. static inline int32 sint3korr(const char *pT)
  113. {
  114. return sint3korr(static_cast<const uchar*>(static_cast<const void*>(pT)));
  115. }
  116. static inline uint32 uint4korr(const char *pT)
  117. {
  118. return uint4korr(static_cast<const uchar*>(static_cast<const void*>(pT)));
  119. }
  120. static inline int32 sint4korr(const char *pT)
  121. {
  122. return sint4korr(static_cast<const uchar*>(static_cast<const void*>(pT)));
  123. }
  124. static inline ulonglong uint6korr(const char *pT)
  125. {
  126. return uint6korr(static_cast<const uchar*>(static_cast<const void*>(pT)));
  127. }
  128. static inline ulonglong uint8korr(const char *pT)
  129. {
  130. return uint8korr(static_cast<const uchar*>(static_cast<const void*>(pT)));
  131. }
  132. static inline longlong sint8korr(const char *pT)
  133. {
  134. return sint8korr(static_cast<const uchar*>(static_cast<const void*>(pT)));
  135. }
  136. static inline void int2store(char *pT, uint16 A)
  137. {
  138. int2store(static_cast<uchar*>(static_cast<void*>(pT)), A);
  139. }
  140. static inline void int3store(char *pT, uint A)
  141. {
  142. int3store(static_cast<uchar*>(static_cast<void*>(pT)), A);
  143. }
  144. static inline void int4store(char *pT, uint32 A)
  145. {
  146. int4store(static_cast<uchar*>(static_cast<void*>(pT)), A);
  147. }
  148. static inline void int5store(char *pT, ulonglong A)
  149. {
  150. int5store(static_cast<uchar*>(static_cast<void*>(pT)), A);
  151. }
  152. static inline void int6store(char *pT, ulonglong A)
  153. {
  154. int6store(static_cast<uchar*>(static_cast<void*>(pT)), A);
  155. }
  156. static inline void int8store(char *pT, ulonglong A)
  157. {
  158. int8store(static_cast<uchar*>(static_cast<void*>(pT)), A);
  159. }
  160. #endif /* __cplusplus */
  161. /*
  162. Functions for reading and storing in machine format from/to
  163. short/long to/from some place in memory V should be a variable
  164. and M a pointer to byte.
  165. */
  166. #ifdef WORDS_BIGENDIAN
  167. #include "big_endian.h"
  168. #else
  169. #include "little_endian.h"
  170. #endif
  171. #ifdef __cplusplus
  172. static inline void float4store(char *V, float M)
  173. {
  174. float4store(static_cast<uchar*>(static_cast<void*>(V)), M);
  175. }
  176. static inline void float8get(double *V, const char *M)
  177. {
  178. float8get(V, static_cast<const uchar*>(static_cast<const void*>(M)));
  179. }
  180. static inline void float8store(char *V, double M)
  181. {
  182. float8store(static_cast<uchar*>(static_cast<void*>(V)), M);
  183. }
  184. #endif /* __cplusplus */
  185. #endif /* MY_BYTEORDER_INCLUDED */