myisampack.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. #ifndef MYISAMPACK_INCLUDED
  2. #define MYISAMPACK_INCLUDED
  3. /* Copyright (c) 2000, 2019, 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. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License, version 2.0, for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  20. /**
  21. @file include/myisampack.h
  22. Storing of values in high byte first order.
  23. Integer keys and file pointers are stored with high byte first to get
  24. better compression.
  25. */
  26. #include "my_config.h"
  27. #ifdef HAVE_ENDIAN_H
  28. #include <endian.h>
  29. #endif
  30. #include <sys/types.h>
  31. #include "my_inttypes.h"
  32. /* these two are for uniformity */
  33. static inline int8 mi_sint1korr(const uchar *A) { return *A; }
  34. static inline uint8 mi_uint1korr(const uchar *A) { return *A; }
  35. static inline int16 mi_sint2korr(const uchar *A) {
  36. return (int16)((uint32)(A[1]) + ((uint32)(A[0]) << 8));
  37. }
  38. static inline int32 mi_sint3korr(const uchar *A) {
  39. return (int32)((A[0] & 128) ? ((255U << 24) | ((uint32)(A[0]) << 16) |
  40. ((uint32)(A[1]) << 8) | ((uint32)A[2]))
  41. : (((uint32)(A[0]) << 16) |
  42. ((uint32)(A[1]) << 8) | ((uint32)(A[2]))));
  43. }
  44. static inline int32 mi_sint4korr(const uchar *A) {
  45. return (int32)((uint32)(A[3]) + ((uint32)(A[2]) << 8) +
  46. ((uint32)(A[1]) << 16) + ((uint32)(A[0]) << 24));
  47. }
  48. static inline uint16 mi_uint2korr(const uchar *A) {
  49. return (uint16)((uint16)A[1]) + ((uint16)A[0] << 8);
  50. }
  51. static inline uint32 mi_uint3korr(const uchar *A) {
  52. return (uint32)((uint32)A[2] + ((uint32)A[1] << 8) + ((uint32)A[0] << 16));
  53. }
  54. static inline uint32 mi_uint4korr(const uchar *A) {
  55. return (uint32)((uint32)A[3] + ((uint32)A[2] << 8) + ((uint32)A[1] << 16) +
  56. ((uint32)A[0] << 24));
  57. }
  58. static inline ulonglong mi_uint5korr(const uchar *A) {
  59. return (ulonglong)((uint32)A[4] + ((uint32)A[3] << 8) + ((uint32)A[2] << 16) +
  60. ((uint32)A[1] << 24)) +
  61. ((ulonglong)A[0] << 32);
  62. }
  63. static inline ulonglong mi_uint6korr(const uchar *A) {
  64. return (ulonglong)((uint32)A[5] + ((uint32)A[4] << 8) + ((uint32)A[3] << 16) +
  65. ((uint32)A[2] << 24)) +
  66. (((ulonglong)((uint32)A[1] + ((uint32)A[0] << 8))) << 32);
  67. }
  68. static inline ulonglong mi_uint7korr(const uchar *A) {
  69. return (ulonglong)((uint32)A[6] + ((uint32)A[5] << 8) + ((uint32)A[4] << 16) +
  70. ((uint32)A[3] << 24)) +
  71. (((ulonglong)((uint32)A[2] + ((uint32)A[1] << 8) +
  72. ((uint32)A[0] << 16)))
  73. << 32);
  74. }
  75. static inline ulonglong mi_uint8korr(const uchar *A) {
  76. return (ulonglong)((uint32)A[7] + ((uint32)A[6] << 8) + ((uint32)A[5] << 16) +
  77. ((uint32)A[4] << 24)) +
  78. (((ulonglong)((uint32)A[3] + ((uint32)A[2] << 8) +
  79. ((uint32)A[1] << 16) + ((uint32)A[0] << 24)))
  80. << 32);
  81. }
  82. static inline longlong mi_sint8korr(const uchar *A) {
  83. return (longlong)mi_uint8korr(A);
  84. }
  85. /* This one is for uniformity */
  86. #define mi_int1store(T, A) *((uchar *)(T)) = (uchar)(A)
  87. #define mi_int2store(T, A) \
  88. { \
  89. uint def_temp = (uint)(A); \
  90. ((uchar *)(T))[1] = (uchar)(def_temp); \
  91. ((uchar *)(T))[0] = (uchar)(def_temp >> 8); \
  92. }
  93. #define mi_int3store(T, A) \
  94. { /*lint -save -e734 */ \
  95. ulong def_temp = (ulong)(A); \
  96. ((uchar *)(T))[2] = (uchar)(def_temp); \
  97. ((uchar *)(T))[1] = (uchar)(def_temp >> 8); \
  98. ((uchar *)(T))[0] = (uchar)(def_temp >> 16); \
  99. /*lint -restore */}
  100. #define mi_int4store(T, A) \
  101. { \
  102. ulong def_temp = (ulong)(A); \
  103. ((uchar *)(T))[3] = (uchar)(def_temp); \
  104. ((uchar *)(T))[2] = (uchar)(def_temp >> 8); \
  105. ((uchar *)(T))[1] = (uchar)(def_temp >> 16); \
  106. ((uchar *)(T))[0] = (uchar)(def_temp >> 24); \
  107. }
  108. #define mi_int5store(T, A) \
  109. { \
  110. ulong def_temp = (ulong)(A), def_temp2 = (ulong)((A) >> 32); \
  111. ((uchar *)(T))[4] = (uchar)(def_temp); \
  112. ((uchar *)(T))[3] = (uchar)(def_temp >> 8); \
  113. ((uchar *)(T))[2] = (uchar)(def_temp >> 16); \
  114. ((uchar *)(T))[1] = (uchar)(def_temp >> 24); \
  115. ((uchar *)(T))[0] = (uchar)(def_temp2); \
  116. }
  117. #define mi_int6store(T, A) \
  118. { \
  119. ulong def_temp = (ulong)(A), def_temp2 = (ulong)((A) >> 32); \
  120. ((uchar *)(T))[5] = (uchar)(def_temp); \
  121. ((uchar *)(T))[4] = (uchar)(def_temp >> 8); \
  122. ((uchar *)(T))[3] = (uchar)(def_temp >> 16); \
  123. ((uchar *)(T))[2] = (uchar)(def_temp >> 24); \
  124. ((uchar *)(T))[1] = (uchar)(def_temp2); \
  125. ((uchar *)(T))[0] = (uchar)(def_temp2 >> 8); \
  126. }
  127. #define mi_int7store(T, A) \
  128. { \
  129. ulong def_temp = (ulong)(A), def_temp2 = (ulong)((A) >> 32); \
  130. ((uchar *)(T))[6] = (uchar)(def_temp); \
  131. ((uchar *)(T))[5] = (uchar)(def_temp >> 8); \
  132. ((uchar *)(T))[4] = (uchar)(def_temp >> 16); \
  133. ((uchar *)(T))[3] = (uchar)(def_temp >> 24); \
  134. ((uchar *)(T))[2] = (uchar)(def_temp2); \
  135. ((uchar *)(T))[1] = (uchar)(def_temp2 >> 8); \
  136. ((uchar *)(T))[0] = (uchar)(def_temp2 >> 16); \
  137. }
  138. #define mi_int8store(T, A) \
  139. { \
  140. ulong def_temp3 = (ulong)(A), def_temp4 = (ulong)((A) >> 32); \
  141. mi_int4store((uchar *)(T) + 0, def_temp4); \
  142. mi_int4store((uchar *)(T) + 4, def_temp3); \
  143. }
  144. #ifdef WORDS_BIGENDIAN
  145. #define mi_float4store(T, A) \
  146. { \
  147. ((uchar *)(T))[0] = ((uchar *)&A)[0]; \
  148. ((uchar *)(T))[1] = ((uchar *)&A)[1]; \
  149. ((uchar *)(T))[2] = ((uchar *)&A)[2]; \
  150. ((uchar *)(T))[3] = ((uchar *)&A)[3]; \
  151. }
  152. static inline float mi_float4get(const uchar *M) {
  153. float def_temp;
  154. ((uchar *)&def_temp)[0] = M[0];
  155. ((uchar *)&def_temp)[1] = M[1];
  156. ((uchar *)&def_temp)[2] = M[2];
  157. ((uchar *)&def_temp)[3] = M[3];
  158. return def_temp;
  159. }
  160. #define mi_float8store(T, V) \
  161. { \
  162. ((uchar *)(T))[0] = ((uchar *)&V)[0]; \
  163. ((uchar *)(T))[1] = ((uchar *)&V)[1]; \
  164. ((uchar *)(T))[2] = ((uchar *)&V)[2]; \
  165. ((uchar *)(T))[3] = ((uchar *)&V)[3]; \
  166. ((uchar *)(T))[4] = ((uchar *)&V)[4]; \
  167. ((uchar *)(T))[5] = ((uchar *)&V)[5]; \
  168. ((uchar *)(T))[6] = ((uchar *)&V)[6]; \
  169. ((uchar *)(T))[7] = ((uchar *)&V)[7]; \
  170. }
  171. static inline double mi_float8get(const uchar *M) {
  172. double def_temp;
  173. ((uchar *)&def_temp)[0] = M[0];
  174. ((uchar *)&def_temp)[1] = M[1];
  175. ((uchar *)&def_temp)[2] = M[2];
  176. ((uchar *)&def_temp)[3] = M[3];
  177. ((uchar *)&def_temp)[4] = M[4];
  178. ((uchar *)&def_temp)[5] = M[5];
  179. ((uchar *)&def_temp)[6] = M[6];
  180. ((uchar *)&def_temp)[7] = M[7];
  181. return def_temp;
  182. }
  183. #else
  184. #define mi_float4store(T, A) \
  185. { \
  186. ((uchar *)(T))[0] = ((uchar *)&A)[3]; \
  187. ((uchar *)(T))[1] = ((uchar *)&A)[2]; \
  188. ((uchar *)(T))[2] = ((uchar *)&A)[1]; \
  189. ((uchar *)(T))[3] = ((uchar *)&A)[0]; \
  190. }
  191. static inline float mi_float4get(const uchar *M) {
  192. float def_temp;
  193. ((uchar *)&def_temp)[0] = M[3];
  194. ((uchar *)&def_temp)[1] = M[2];
  195. ((uchar *)&def_temp)[2] = M[1];
  196. ((uchar *)&def_temp)[3] = M[0];
  197. return def_temp;
  198. }
  199. #if defined(__FLOAT_WORD_ORDER) && (__FLOAT_WORD_ORDER == __BIG_ENDIAN)
  200. #define mi_float8store(T, V) \
  201. { \
  202. ((uchar *)(T))[0] = ((uchar *)&V)[3]; \
  203. ((uchar *)(T))[1] = ((uchar *)&V)[2]; \
  204. ((uchar *)(T))[2] = ((uchar *)&V)[1]; \
  205. ((uchar *)(T))[3] = ((uchar *)&V)[0]; \
  206. ((uchar *)(T))[4] = ((uchar *)&V)[7]; \
  207. ((uchar *)(T))[5] = ((uchar *)&V)[6]; \
  208. ((uchar *)(T))[6] = ((uchar *)&V)[5]; \
  209. ((uchar *)(T))[7] = ((uchar *)&V)[4]; \
  210. }
  211. static inline double mi_float8get(const uchar *M) {
  212. double def_temp;
  213. ((uchar *)&def_temp)[0] = M[3];
  214. ((uchar *)&def_temp)[1] = M[2];
  215. ((uchar *)&def_temp)[2] = M[1];
  216. ((uchar *)&def_temp)[3] = M[0];
  217. ((uchar *)&def_temp)[4] = M[7];
  218. ((uchar *)&def_temp)[5] = M[6];
  219. ((uchar *)&def_temp)[6] = M[5];
  220. ((uchar *)&def_temp)[7] = M[4];
  221. return def_temp;
  222. }
  223. #else
  224. #define mi_float8store(T, V) \
  225. { \
  226. ((uchar *)(T))[0] = ((uchar *)&V)[7]; \
  227. ((uchar *)(T))[1] = ((uchar *)&V)[6]; \
  228. ((uchar *)(T))[2] = ((uchar *)&V)[5]; \
  229. ((uchar *)(T))[3] = ((uchar *)&V)[4]; \
  230. ((uchar *)(T))[4] = ((uchar *)&V)[3]; \
  231. ((uchar *)(T))[5] = ((uchar *)&V)[2]; \
  232. ((uchar *)(T))[6] = ((uchar *)&V)[1]; \
  233. ((uchar *)(T))[7] = ((uchar *)&V)[0]; \
  234. }
  235. static inline double mi_float8get(const uchar *M) {
  236. double def_temp;
  237. ((uchar *)&def_temp)[0] = M[7];
  238. ((uchar *)&def_temp)[1] = M[6];
  239. ((uchar *)&def_temp)[2] = M[5];
  240. ((uchar *)&def_temp)[3] = M[4];
  241. ((uchar *)&def_temp)[4] = M[3];
  242. ((uchar *)&def_temp)[5] = M[2];
  243. ((uchar *)&def_temp)[6] = M[1];
  244. ((uchar *)&def_temp)[7] = M[0];
  245. return def_temp;
  246. }
  247. #endif /* __FLOAT_WORD_ORDER */
  248. #endif /* WORDS_BIGENDIAN */
  249. #define mi_rowstore(T, A) mi_int8store(T, A)
  250. #define mi_rowkorr(T) mi_uint8korr(T)
  251. #if SIZEOF_OFF_T > 4
  252. #define mi_sizestore(T, A) mi_int8store(T, A)
  253. #define mi_sizekorr(T) mi_uint8korr(T)
  254. #else
  255. #define mi_sizestore(T, A) \
  256. { \
  257. if ((A) == HA_OFFSET_ERROR) \
  258. memset((T), 255, 8); \
  259. else { \
  260. mi_int4store((T), 0); \
  261. mi_int4store(((T) + 4), A); \
  262. } \
  263. }
  264. #define mi_sizekorr(T) mi_uint4korr((uchar *)(T) + 4)
  265. #endif
  266. #endif /* MYISAMPACK_INCLUDED */