drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
korr.h
1/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright (C) 2008 Sun Microsystems, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#pragma once
21
22
23/*
24 * Define-functions for reading and storing in machine independent format
25 * (low byte first)
26 *
27 * No one seems to know what "korr" means in this context. A global search
28 * and replace would be fine if someone can come up with a better description
29 */
30
31/* Optimized store functions for Intel x86 */
32#if defined(__i386__)
33#define sint2korr(A) (*((int16_t *) (A)))
34#define sint3korr(A) ((int32_t) ((((unsigned char) (A)[2]) & 128) ? \
35 (((uint32_t) 255L << 24) | \
36 (((uint32_t) (unsigned char) (A)[2]) << 16) | \
37 (((uint32_t) (unsigned char) (A)[1]) << 8) | \
38 ((uint32_t) (unsigned char) (A)[0])) : \
39 (((uint32_t) (unsigned char) (A)[2]) << 16) | \
40 (((uint32_t) (unsigned char) (A)[1]) << 8) | \
41 ((uint32_t) (unsigned char) (A)[0])))
42#define sint4korr(A) (*((long *) (A)))
43#define uint2korr(A) (*((uint16_t *) (A)))
44#if defined(HAVE_VALGRIND)
45#define uint3korr(A) (uint32_t) (((uint32_t) ((unsigned char) (A)[0])) +\
46 (((uint32_t) ((unsigned char) (A)[1])) << 8) +\
47 (((uint32_t) ((unsigned char) (A)[2])) << 16))
48#else
49/*
50 ATTENTION !
51
52 Please, note, uint3korr reads 4 bytes (not 3) !
53 It means, that you have to provide enough allocated space !
54*/
55#define uint3korr(A) (long) (*((unsigned int *) (A)) & 0xFFFFFF)
56#endif /* HAVE_VALGRIND */
57#define uint4korr(A) (*((uint32_t *) (A)))
58#define uint8korr(A) (*((uint64_t *) (A)))
59#define sint8korr(A) (*((int64_t *) (A)))
60#define int2store(T,A) *((uint16_t*) (T))= (uint16_t) (A)
61#define int3store(T,A) do { *(T)= (unsigned char) ((A));\
62 *(T+1)=(unsigned char) (((uint32_t) (A) >> 8));\
63 *(T+2)=(unsigned char) (((A) >> 16)); } while (0)
64#define int4store(T,A) *((long *) (T))= (long) (A)
65#define int8store(T,A) *((uint64_t *) (T))= (uint64_t) (A)
66
67typedef union {
68 double v;
69 long m[2];
70} doubleget_union;
71#define doubleget(V,M) \
72do { doubleget_union _tmp; \
73 _tmp.m[0] = *((long*)(M)); \
74 _tmp.m[1] = *(((long*) (M))+1); \
75 (V) = _tmp.v; } while(0)
76#define doublestore(T,V) do { *((long *) T) = ((doubleget_union *)&V)->m[0]; \
77 *(((long *) T)+1) = ((doubleget_union *)&V)->m[1]; \
78 } while (0)
79#define float8get(V,M) doubleget((V),(M))
80#define floatstore(T,V) memcpy((T), (&V), sizeof(float))
81#define float8store(V,M) doublestore((V),(M))
82#else
83
84/*
85 We're here if it's not a IA-32 architecture (Win32 and UNIX IA-32 defines
86 were done before)
87*/
88#define sint2korr(A) (int16_t) (((int16_t) ((unsigned char) (A)[0])) +\
89 ((int16_t) ((int16_t) (A)[1]) << 8))
90#define sint4korr(A) (int32_t) (((int32_t) ((unsigned char) (A)[0])) +\
91 (((int32_t) ((unsigned char) (A)[1]) << 8)) +\
92 (((int32_t) ((unsigned char) (A)[2]) << 16)) +\
93 (((int32_t) ((int16_t) (A)[3]) << 24)))
94#define sint8korr(A) (int64_t) uint8korr(A)
95#define uint2korr(A) (uint16_t) (((uint16_t) ((unsigned char) (A)[0])) +\
96 ((uint16_t) ((unsigned char) (A)[1]) << 8))
97#define uint3korr(A) (uint32_t) (((uint32_t) ((unsigned char) (A)[0])) +\
98 (((uint32_t) ((unsigned char) (A)[1])) << 8) +\
99 (((uint32_t) ((unsigned char) (A)[2])) << 16))
100#define uint4korr(A) (uint32_t) (((uint32_t) ((unsigned char) (A)[0])) +\
101 (((uint32_t) ((unsigned char) (A)[1])) << 8) +\
102 (((uint32_t) ((unsigned char) (A)[2])) << 16) +\
103 (((uint32_t) ((unsigned char) (A)[3])) << 24))
104#define uint8korr(A) ((uint64_t)(((uint32_t) ((unsigned char) (A)[0])) +\
105 (((uint32_t) ((unsigned char) (A)[1])) << 8) +\
106 (((uint32_t) ((unsigned char) (A)[2])) << 16) +\
107 (((uint32_t) ((unsigned char) (A)[3])) << 24)) +\
108 (((uint64_t) (((uint32_t) ((unsigned char) (A)[4])) +\
109 (((uint32_t) ((unsigned char) (A)[5])) << 8) +\
110 (((uint32_t) ((unsigned char) (A)[6])) << 16) +\
111 (((uint32_t) ((unsigned char) (A)[7])) << 24))) <<\
112 32))
113#define int2store(T,A) do { uint32_t def_temp= (uint32_t) (A) ;\
114 *((unsigned char*) (T))= (unsigned char)(def_temp); \
115 *((unsigned char*) (T)+1)=(unsigned char)((def_temp >> 8)); \
116 } while(0)
117#define int3store(T,A) do { /*lint -save -e734 */\
118 *((unsigned char*)(T))=(unsigned char) ((A));\
119 *((unsigned char*) (T)+1)=(unsigned char) (((A) >> 8));\
120 *((unsigned char*)(T)+2)=(unsigned char) (((A) >> 16)); \
121 /*lint -restore */} while(0)
122#define int4store(T,A) do { *((char *)(T))=(char) ((A));\
123 *(((char *)(T))+1)=(char) (((A) >> 8));\
124 *(((char *)(T))+2)=(char) (((A) >> 16));\
125 *(((char *)(T))+3)=(char) (((A) >> 24)); } while(0)
126#define int8store(T,A) do { uint32_t def_temp= (uint32_t) (A), def_temp2= (uint32_t) ((A) >> 32); \
127 int4store((T),def_temp); \
128 int4store((T+4),def_temp2); } while(0)
129#ifdef WORDS_BIGENDIAN
130#define float4get(V,M) do { float def_temp;\
131 ((unsigned char*) &def_temp)[0]=(M)[3];\
132 ((unsigned char*) &def_temp)[1]=(M)[2];\
133 ((unsigned char*) &def_temp)[2]=(M)[1];\
134 ((unsigned char*) &def_temp)[3]=(M)[0];\
135 (V)=def_temp; } while(0)
136#define float8store(T,V) do { *(T)= ((unsigned char *) &V)[7];\
137 *((T)+1)=(char) ((unsigned char *) &V)[6];\
138 *((T)+2)=(char) ((unsigned char *) &V)[5];\
139 *((T)+3)=(char) ((unsigned char *) &V)[4];\
140 *((T)+4)=(char) ((unsigned char *) &V)[3];\
141 *((T)+5)=(char) ((unsigned char *) &V)[2];\
142 *((T)+6)=(char) ((unsigned char *) &V)[1];\
143 *((T)+7)=(char) ((unsigned char *) &V)[0]; } while(0)
144
145#define float8get(V,M) do { double def_temp;\
146 ((unsigned char*) &def_temp)[0]=(M)[7];\
147 ((unsigned char*) &def_temp)[1]=(M)[6];\
148 ((unsigned char*) &def_temp)[2]=(M)[5];\
149 ((unsigned char*) &def_temp)[3]=(M)[4];\
150 ((unsigned char*) &def_temp)[4]=(M)[3];\
151 ((unsigned char*) &def_temp)[5]=(M)[2];\
152 ((unsigned char*) &def_temp)[6]=(M)[1];\
153 ((unsigned char*) &def_temp)[7]=(M)[0];\
154 (V) = def_temp; } while(0)
155#else
156#define float4get(V,M) memcpy(&V, (M), sizeof(float))
157
158#if defined(__FLOAT_WORD_ORDER) && (__FLOAT_WORD_ORDER == __BIG_ENDIAN)
159#define doublestore(T,V) do { *(((char*)T)+0)=(char) ((unsigned char *) &V)[4];\
160 *(((char*)T)+1)=(char) ((unsigned char *) &V)[5];\
161 *(((char*)T)+2)=(char) ((unsigned char *) &V)[6];\
162 *(((char*)T)+3)=(char) ((unsigned char *) &V)[7];\
163 *(((char*)T)+4)=(char) ((unsigned char *) &V)[0];\
164 *(((char*)T)+5)=(char) ((unsigned char *) &V)[1];\
165 *(((char*)T)+6)=(char) ((unsigned char *) &V)[2];\
166 *(((char*)T)+7)=(char) ((unsigned char *) &V)[3]; }\
167 while(0)
168#define doubleget(V,M) do { double def_temp;\
169 ((unsigned char*) &def_temp)[0]=(M)[4];\
170 ((unsigned char*) &def_temp)[1]=(M)[5];\
171 ((unsigned char*) &def_temp)[2]=(M)[6];\
172 ((unsigned char*) &def_temp)[3]=(M)[7];\
173 ((unsigned char*) &def_temp)[4]=(M)[0];\
174 ((unsigned char*) &def_temp)[5]=(M)[1];\
175 ((unsigned char*) &def_temp)[6]=(M)[2];\
176 ((unsigned char*) &def_temp)[7]=(M)[3];\
177 (V) = def_temp; } while(0)
178#endif /* __FLOAT_WORD_ORDER */
179
180#define float8get(V,M) doubleget((V),(M))
181#define float8store(V,M) doublestore((V),(M))
182#endif /* WORDS_BIGENDIAN */
183
184#endif /* __i386__ */
185
186/*
187 Define-funktions for reading and storing in machine format from/to
188 short/long to/from some place in memory V should be a (not
189 register) variable, M is a pointer to byte
190*/
191
192#ifdef WORDS_BIGENDIAN
193
194#define shortget(V,M) do { V = (short) (((short) ((unsigned char) (M)[1]))+\
195 ((short) ((short) (M)[0]) << 8)); } while(0)
196#define longget(V,M) do { int32_t def_temp;\
197 ((unsigned char*) &def_temp)[0]=(M)[0];\
198 ((unsigned char*) &def_temp)[1]=(M)[1];\
199 ((unsigned char*) &def_temp)[2]=(M)[2];\
200 ((unsigned char*) &def_temp)[3]=(M)[3];\
201 (V)=def_temp; } while(0)
202#define shortstore(T,A) do { uint32_t def_temp=(uint32_t) (A) ;\
203 *(((char*)T)+1)=(char)(def_temp); \
204 *(((char*)T)+0)=(char)(def_temp >> 8); } while(0)
205#define longstore(T,A) do { *(((char*)T)+3)=((A));\
206 *(((char*)T)+2)=(((A) >> 8));\
207 *(((char*)T)+1)=(((A) >> 16));\
208 *(((char*)T)+0)=(((A) >> 24)); } while(0)
209
210#define floatstore(T, V) memcpy((T), (&V), sizeof(float))
211#define doubleget(V, M) memcpy(&V, (M), sizeof(double))
212#define doublestore(T, V) memcpy((T), &V, sizeof(double))
213#define int64_tget(V, M) memcpy(&V, (M), sizeof(uint64_t))
214#define int64_tstore(T, V) memcpy((T), &V, sizeof(uint64_t))
215
216#else
217
218#define shortget(V,M) do { V = sint2korr(M); } while(0)
219#define longget(V,M) do { V = sint4korr(M); } while(0)
220#define shortstore(T,V) int2store(T,V)
221#define longstore(T,V) int4store(T,V)
222#ifndef floatstore
223#define floatstore(T,V) memcpy((T), (&V), sizeof(float))
224#endif
225#ifndef doubleget
226#define doubleget(V, M) memcpy(&V, (M), sizeof(double))
227#define doublestore(T,V) memcpy((T), &V, sizeof(double))
228#endif /* doubleget */
229#define int64_tget(V,M) memcpy(&V, (M), sizeof(uint64_t))
230#define int64_tstore(T,V) memcpy((T), &V, sizeof(uint64_t))
231
232#endif /* WORDS_BIGENDIAN */
233