~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/korr.h

  • Committer: Brian Aker
  • Date: 2008-11-04 15:39:09 UTC
  • mfrom: (575.1.2 devel)
  • Revision ID: brian@tangent.org-20081104153909-c72hn65udxs1ccal
Merge of Monty's work

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
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
#ifndef DRIZZLED_KORR_H
 
21
#define DRIZZLED_KORR_H
 
22
 
 
23
 
 
24
/*
 
25
 * Define-functions for reading and storing in machine independent format
 
26
 * (low byte first)
 
27
 *
 
28
 * No one seems to know what "korr" means in this context. A global search
 
29
 * and replace would be fine if someone can come up with a better description
 
30
 */
 
31
 
 
32
/* Optimized store functions for Intel x86 */
 
33
#if defined(__i386__)
 
34
#define sint2korr(A)    (*((int16_t *) (A)))
 
35
#define sint3korr(A)    ((int32_t) ((((unsigned char) (A)[2]) & 128) ?  \
 
36
                                    (((uint32_t) 255L << 24) |          \
 
37
                                     (((uint32_t) (unsigned char) (A)[2]) << 16) | \
 
38
                                     (((uint32_t) (unsigned char) (A)[1]) << 8) | \
 
39
                                     ((uint32_t) (unsigned char) (A)[0])) : \
 
40
                                    (((uint32_t) (unsigned char) (A)[2]) << 16) | \
 
41
                                    (((uint32_t) (unsigned char) (A)[1]) << 8) | \
 
42
                                    ((uint32_t) (unsigned char) (A)[0])))
 
43
#define sint4korr(A)    (*((long *) (A)))
 
44
#define uint2korr(A)    (*((uint16_t *) (A)))
 
45
#if defined(HAVE_purify)
 
46
#define uint3korr(A)    (uint32_t) (((uint32_t) ((unsigned char) (A)[0])) +\
 
47
                                  (((uint32_t) ((unsigned char) (A)[1])) << 8) +\
 
48
                                  (((uint32_t) ((unsigned char) (A)[2])) << 16))
 
49
#else
 
50
/*
 
51
   ATTENTION !
 
52
   
 
53
    Please, note, uint3korr reads 4 bytes (not 3) !
 
54
    It means, that you have to provide enough allocated space !
 
55
*/
 
56
#define uint3korr(A)    (long) (*((unsigned int *) (A)) & 0xFFFFFF)
 
57
#endif /* HAVE_purify */
 
58
#define uint4korr(A)    (*((uint32_t *) (A)))
 
59
#define uint5korr(A)    ((uint64_t)(((uint32_t) ((unsigned char) (A)[0])) +\
 
60
                                    (((uint32_t) ((unsigned char) (A)[1])) << 8) +\
 
61
                                    (((uint32_t) ((unsigned char) (A)[2])) << 16) +\
 
62
                                    (((uint32_t) ((unsigned char) (A)[3])) << 24)) +\
 
63
                                    (((uint64_t) ((unsigned char) (A)[4])) << 32))
 
64
#define uint6korr(A)    ((uint64_t)(((uint32_t)    ((unsigned char) (A)[0]))          + \
 
65
                                     (((uint32_t)    ((unsigned char) (A)[1])) << 8)   + \
 
66
                                     (((uint32_t)    ((unsigned char) (A)[2])) << 16)  + \
 
67
                                     (((uint32_t)    ((unsigned char) (A)[3])) << 24)) + \
 
68
                         (((uint64_t) ((unsigned char) (A)[4])) << 32) +       \
 
69
                         (((uint64_t) ((unsigned char) (A)[5])) << 40))
 
70
#define uint8korr(A)    (*((uint64_t *) (A)))
 
71
#define sint8korr(A)    (*((int64_t *) (A)))
 
72
#define int2store(T,A)  *((uint16_t*) (T))= (uint16_t) (A)
 
73
#define int3store(T,A)  do { *(T)=  (unsigned char) ((A));\
 
74
                            *(T+1)=(unsigned char) (((uint32_t) (A) >> 8));\
 
75
                            *(T+2)=(unsigned char) (((A) >> 16)); } while (0)
 
76
#define int4store(T,A)  *((long *) (T))= (long) (A)
 
77
#define int5store(T,A)  do { *(T)= (unsigned char)((A));\
 
78
                             *((T)+1)=(unsigned char) (((A) >> 8));\
 
79
                             *((T)+2)=(unsigned char) (((A) >> 16));\
 
80
                             *((T)+3)=(unsigned char) (((A) >> 24)); \
 
81
                             *((T)+4)=(unsigned char) (((A) >> 32)); } while(0)
 
82
#define int6store(T,A)  do { *(T)=    (unsigned char)((A));          \
 
83
                             *((T)+1)=(unsigned char) (((A) >> 8));  \
 
84
                             *((T)+2)=(unsigned char) (((A) >> 16)); \
 
85
                             *((T)+3)=(unsigned char) (((A) >> 24)); \
 
86
                             *((T)+4)=(unsigned char) (((A) >> 32)); \
 
87
                             *((T)+5)=(unsigned char) (((A) >> 40)); } while(0)
 
88
#define int8store(T,A)  *((uint64_t *) (T))= (uint64_t) (A)
 
89
 
 
90
typedef union {
 
91
  double v;
 
92
  long m[2];
 
93
} doubleget_union;
 
94
#define doubleget(V,M)  \
 
95
do { doubleget_union _tmp; \
 
96
     _tmp.m[0] = *((long*)(M)); \
 
97
     _tmp.m[1] = *(((long*) (M))+1); \
 
98
     (V) = _tmp.v; } while(0)
 
99
#define doublestore(T,V) do { *((long *) T) = ((doubleget_union *)&V)->m[0]; \
 
100
                             *(((long *) T)+1) = ((doubleget_union *)&V)->m[1]; \
 
101
                         } while (0)
 
102
#define float4get(V,M)   do { *((float *) &(V)) = *((float*) (M)); } while(0)
 
103
#define float8get(V,M)   doubleget((V),(M))
 
104
#define float4store(V,M) memcpy(V, (&M), sizeof(float))
 
105
#define floatstore(T,V)  memcpy((T), (&V), sizeof(float))
 
106
#define floatget(V,M)    memcpy(&V, (M), sizeof(float))
 
107
#define float8store(V,M) doublestore((V),(M))
 
108
#else
 
109
 
 
110
/*
 
111
  We're here if it's not a IA-32 architecture (Win32 and UNIX IA-32 defines
 
112
  were done before)
 
113
*/
 
114
#define sint2korr(A)    (int16_t) (((int16_t) ((unsigned char) (A)[0])) +\
 
115
                                 ((int16_t) ((int16_t) (A)[1]) << 8))
 
116
#define sint3korr(A)    ((int32_t) ((((unsigned char) (A)[2]) & 128) ? \
 
117
                                  (((uint32_t) 255L << 24) | \
 
118
                                   (((uint32_t) (unsigned char) (A)[2]) << 16) |\
 
119
                                   (((uint32_t) (unsigned char) (A)[1]) << 8) | \
 
120
                                   ((uint32_t) (unsigned char) (A)[0])) : \
 
121
                                  (((uint32_t) (unsigned char) (A)[2]) << 16) |\
 
122
                                  (((uint32_t) (unsigned char) (A)[1]) << 8) | \
 
123
                                  ((uint32_t) (unsigned char) (A)[0])))
 
124
#define sint4korr(A)    (int32_t) (((int32_t) ((unsigned char) (A)[0])) +\
 
125
                                (((int32_t) ((unsigned char) (A)[1]) << 8)) +\
 
126
                                (((int32_t) ((unsigned char) (A)[2]) << 16)) +\
 
127
                                (((int32_t) ((int16_t) (A)[3]) << 24)))
 
128
#define sint8korr(A)    (int64_t) uint8korr(A)
 
129
#define uint2korr(A)    (uint16_t) (((uint16_t) ((unsigned char) (A)[0])) +\
 
130
                                  ((uint16_t) ((unsigned char) (A)[1]) << 8))
 
131
#define uint3korr(A)    (uint32_t) (((uint32_t) ((unsigned char) (A)[0])) +\
 
132
                                  (((uint32_t) ((unsigned char) (A)[1])) << 8) +\
 
133
                                  (((uint32_t) ((unsigned char) (A)[2])) << 16))
 
134
#define uint4korr(A)    (uint32_t) (((uint32_t) ((unsigned char) (A)[0])) +\
 
135
                                  (((uint32_t) ((unsigned char) (A)[1])) << 8) +\
 
136
                                  (((uint32_t) ((unsigned char) (A)[2])) << 16) +\
 
137
                                  (((uint32_t) ((unsigned char) (A)[3])) << 24))
 
138
#define uint5korr(A)    ((uint64_t)(((uint32_t) ((unsigned char) (A)[0])) +\
 
139
                                    (((uint32_t) ((unsigned char) (A)[1])) << 8) +\
 
140
                                    (((uint32_t) ((unsigned char) (A)[2])) << 16) +\
 
141
                                    (((uint32_t) ((unsigned char) (A)[3])) << 24)) +\
 
142
                                    (((uint64_t) ((unsigned char) (A)[4])) << 32))
 
143
#define uint6korr(A)    ((uint64_t)(((uint32_t)    ((unsigned char) (A)[0]))          + \
 
144
                                     (((uint32_t)    ((unsigned char) (A)[1])) << 8)   + \
 
145
                                     (((uint32_t)    ((unsigned char) (A)[2])) << 16)  + \
 
146
                                     (((uint32_t)    ((unsigned char) (A)[3])) << 24)) + \
 
147
                         (((uint64_t) ((unsigned char) (A)[4])) << 32) +       \
 
148
                         (((uint64_t) ((unsigned char) (A)[5])) << 40))
 
149
#define uint8korr(A)    ((uint64_t)(((uint32_t) ((unsigned char) (A)[0])) +\
 
150
                                    (((uint32_t) ((unsigned char) (A)[1])) << 8) +\
 
151
                                    (((uint32_t) ((unsigned char) (A)[2])) << 16) +\
 
152
                                    (((uint32_t) ((unsigned char) (A)[3])) << 24)) +\
 
153
                        (((uint64_t) (((uint32_t) ((unsigned char) (A)[4])) +\
 
154
                                    (((uint32_t) ((unsigned char) (A)[5])) << 8) +\
 
155
                                    (((uint32_t) ((unsigned char) (A)[6])) << 16) +\
 
156
                                    (((uint32_t) ((unsigned char) (A)[7])) << 24))) <<\
 
157
                                    32))
 
158
#define int2store(T,A)       do { uint32_t def_temp= (uint32_t) (A) ;\
 
159
                                  *((unsigned char*) (T))=  (unsigned char)(def_temp); \
 
160
                                   *((unsigned char*) (T)+1)=(unsigned char)((def_temp >> 8)); \
 
161
                             } while(0)
 
162
#define int3store(T,A)       do { /*lint -save -e734 */\
 
163
                                  *((unsigned char*)(T))=(unsigned char) ((A));\
 
164
                                  *((unsigned char*) (T)+1)=(unsigned char) (((A) >> 8));\
 
165
                                  *((unsigned char*)(T)+2)=(unsigned char) (((A) >> 16)); \
 
166
                                  /*lint -restore */} while(0)
 
167
#define int4store(T,A)       do { *((char *)(T))=(char) ((A));\
 
168
                                  *(((char *)(T))+1)=(char) (((A) >> 8));\
 
169
                                  *(((char *)(T))+2)=(char) (((A) >> 16));\
 
170
                                  *(((char *)(T))+3)=(char) (((A) >> 24)); } while(0)
 
171
#define int5store(T,A)       do { *((char *)(T))=     (char)((A));  \
 
172
                                  *(((char *)(T))+1)= (char)(((A) >> 8)); \
 
173
                                  *(((char *)(T))+2)= (char)(((A) >> 16)); \
 
174
                                  *(((char *)(T))+3)= (char)(((A) >> 24)); \
 
175
                                  *(((char *)(T))+4)= (char)(((A) >> 32)); \
 
176
                                } while(0)
 
177
#define int6store(T,A)       do { *((char *)(T))=     (char)((A)); \
 
178
                                  *(((char *)(T))+1)= (char)(((A) >> 8)); \
 
179
                                  *(((char *)(T))+2)= (char)(((A) >> 16)); \
 
180
                                  *(((char *)(T))+3)= (char)(((A) >> 24)); \
 
181
                                  *(((char *)(T))+4)= (char)(((A) >> 32)); \
 
182
                                  *(((char *)(T))+5)= (char)(((A) >> 40)); \
 
183
                                } while(0)
 
184
#define int8store(T,A)       do { uint32_t def_temp= (uint32_t) (A), def_temp2= (uint32_t) ((A) >> 32); \
 
185
                                  int4store((T),def_temp); \
 
186
                                  int4store((T+4),def_temp2); } while(0)
 
187
#ifdef WORDS_BIGENDIAN
 
188
#define float4store(T,A) do { *(T)= ((unsigned char *) &A)[3];\
 
189
                              *((T)+1)=(char) ((unsigned char *) &A)[2];\
 
190
                              *((T)+2)=(char) ((unsigned char *) &A)[1];\
 
191
                              *((T)+3)=(char) ((unsigned char *) &A)[0]; } while(0)
 
192
 
 
193
#define float4get(V,M)   do { float def_temp;\
 
194
                              ((unsigned char*) &def_temp)[0]=(M)[3];\
 
195
                              ((unsigned char*) &def_temp)[1]=(M)[2];\
 
196
                              ((unsigned char*) &def_temp)[2]=(M)[1];\
 
197
                              ((unsigned char*) &def_temp)[3]=(M)[0];\
 
198
                              (V)=def_temp; } while(0)
 
199
#define float8store(T,V) do { *(T)= ((unsigned char *) &V)[7];\
 
200
                              *((T)+1)=(char) ((unsigned char *) &V)[6];\
 
201
                              *((T)+2)=(char) ((unsigned char *) &V)[5];\
 
202
                              *((T)+3)=(char) ((unsigned char *) &V)[4];\
 
203
                              *((T)+4)=(char) ((unsigned char *) &V)[3];\
 
204
                              *((T)+5)=(char) ((unsigned char *) &V)[2];\
 
205
                              *((T)+6)=(char) ((unsigned char *) &V)[1];\
 
206
                              *((T)+7)=(char) ((unsigned char *) &V)[0]; } while(0)
 
207
 
 
208
#define float8get(V,M)   do { double def_temp;\
 
209
                              ((unsigned char*) &def_temp)[0]=(M)[7];\
 
210
                              ((unsigned char*) &def_temp)[1]=(M)[6];\
 
211
                              ((unsigned char*) &def_temp)[2]=(M)[5];\
 
212
                              ((unsigned char*) &def_temp)[3]=(M)[4];\
 
213
                              ((unsigned char*) &def_temp)[4]=(M)[3];\
 
214
                              ((unsigned char*) &def_temp)[5]=(M)[2];\
 
215
                              ((unsigned char*) &def_temp)[6]=(M)[1];\
 
216
                              ((unsigned char*) &def_temp)[7]=(M)[0];\
 
217
                              (V) = def_temp; } while(0)
 
218
#else
 
219
#define float4get(V,M)   memcpy(&V, (M), sizeof(float))
 
220
#define float4store(V,M) memcpy(V, (&M), sizeof(float))
 
221
 
 
222
#if defined(__FLOAT_WORD_ORDER) && (__FLOAT_WORD_ORDER == __BIG_ENDIAN)
 
223
#define doublestore(T,V) do { *(((char*)T)+0)=(char) ((unsigned char *) &V)[4];\
 
224
                              *(((char*)T)+1)=(char) ((unsigned char *) &V)[5];\
 
225
                              *(((char*)T)+2)=(char) ((unsigned char *) &V)[6];\
 
226
                              *(((char*)T)+3)=(char) ((unsigned char *) &V)[7];\
 
227
                              *(((char*)T)+4)=(char) ((unsigned char *) &V)[0];\
 
228
                              *(((char*)T)+5)=(char) ((unsigned char *) &V)[1];\
 
229
                              *(((char*)T)+6)=(char) ((unsigned char *) &V)[2];\
 
230
                              *(((char*)T)+7)=(char) ((unsigned char *) &V)[3]; }\
 
231
                         while(0)
 
232
#define doubleget(V,M)   do { double def_temp;\
 
233
                              ((unsigned char*) &def_temp)[0]=(M)[4];\
 
234
                              ((unsigned char*) &def_temp)[1]=(M)[5];\
 
235
                              ((unsigned char*) &def_temp)[2]=(M)[6];\
 
236
                              ((unsigned char*) &def_temp)[3]=(M)[7];\
 
237
                              ((unsigned char*) &def_temp)[4]=(M)[0];\
 
238
                              ((unsigned char*) &def_temp)[5]=(M)[1];\
 
239
                              ((unsigned char*) &def_temp)[6]=(M)[2];\
 
240
                              ((unsigned char*) &def_temp)[7]=(M)[3];\
 
241
                              (V) = def_temp; } while(0)
 
242
#endif /* __FLOAT_WORD_ORDER */
 
243
 
 
244
#define float8get(V,M)   doubleget((V),(M))
 
245
#define float8store(V,M) doublestore((V),(M))
 
246
#endif /* WORDS_BIGENDIAN */
 
247
 
 
248
#endif /* __i386__ */
 
249
 
 
250
/*
 
251
  Macro for reading 32-bit integer from network byte order (big-endian)
 
252
  from unaligned memory location.
 
253
*/
 
254
#define int4net(A)        (int32_t) (((uint32_t) ((unsigned char) (A)[3]))        |\
 
255
                                  (((uint32_t) ((unsigned char) (A)[2])) << 8)  |\
 
256
                                  (((uint32_t) ((unsigned char) (A)[1])) << 16) |\
 
257
                                  (((uint32_t) ((unsigned char) (A)[0])) << 24))
 
258
/*
 
259
  Define-funktions for reading and storing in machine format from/to
 
260
  short/long to/from some place in memory V should be a (not
 
261
  register) variable, M is a pointer to byte
 
262
*/
 
263
 
 
264
#ifdef WORDS_BIGENDIAN
 
265
 
 
266
#define ushortget(V,M)  do { V = (uint16_t) (((uint16_t) ((unsigned char) (M)[1]))+\
 
267
                                 ((uint16_t) ((uint16_t) (M)[0]) << 8)); } while(0)
 
268
#define shortget(V,M)   do { V = (short) (((short) ((unsigned char) (M)[1]))+\
 
269
                                 ((short) ((short) (M)[0]) << 8)); } while(0)
 
270
#define longget(V,M)    do { int32_t def_temp;\
 
271
                             ((unsigned char*) &def_temp)[0]=(M)[0];\
 
272
                             ((unsigned char*) &def_temp)[1]=(M)[1];\
 
273
                             ((unsigned char*) &def_temp)[2]=(M)[2];\
 
274
                             ((unsigned char*) &def_temp)[3]=(M)[3];\
 
275
                             (V)=def_temp; } while(0)
 
276
#define ulongget(V,M)   do { uint32_t def_temp;\
 
277
                            ((unsigned char*) &def_temp)[0]=(M)[0];\
 
278
                            ((unsigned char*) &def_temp)[1]=(M)[1];\
 
279
                            ((unsigned char*) &def_temp)[2]=(M)[2];\
 
280
                            ((unsigned char*) &def_temp)[3]=(M)[3];\
 
281
                            (V)=def_temp; } while(0)
 
282
#define shortstore(T,A) do { uint32_t def_temp=(uint32_t) (A) ;\
 
283
                             *(((char*)T)+1)=(char)(def_temp); \
 
284
                             *(((char*)T)+0)=(char)(def_temp >> 8); } while(0)
 
285
#define longstore(T,A)  do { *(((char*)T)+3)=((A));\
 
286
                             *(((char*)T)+2)=(((A) >> 8));\
 
287
                             *(((char*)T)+1)=(((A) >> 16));\
 
288
                             *(((char*)T)+0)=(((A) >> 24)); } while(0)
 
289
 
 
290
#define floatget(V,M)     memcpy(&V, (M), sizeof(float))
 
291
#define floatstore(T, V)   memcpy((T), (&V), sizeof(float))
 
292
#define doubleget(V, M)   memcpy(&V, (M), sizeof(double))
 
293
#define doublestore(T, V)  memcpy((T), &V, sizeof(double))
 
294
#define int64_tget(V, M)   memcpy(&V, (M), sizeof(uint64_t))
 
295
#define int64_tstore(T, V) memcpy((T), &V, sizeof(uint64_t))
 
296
 
 
297
#else
 
298
 
 
299
#define ushortget(V,M)  do { V = uint2korr(M); } while(0)
 
300
#define shortget(V,M)   do { V = sint2korr(M); } while(0)
 
301
#define longget(V,M)    do { V = sint4korr(M); } while(0)
 
302
#define ulongget(V,M)   do { V = uint4korr(M); } while(0)
 
303
#define shortstore(T,V) int2store(T,V)
 
304
#define longstore(T,V)  int4store(T,V)
 
305
#ifndef floatstore
 
306
#define floatstore(T,V)   memcpy((T), (&V), sizeof(float))
 
307
#define floatget(V,M)     memcpy(&V, (M), sizeof(float))
 
308
#endif
 
309
#ifndef doubleget
 
310
#define doubleget(V, M)   memcpy(&V, (M), sizeof(double))
 
311
#define doublestore(T,V)  memcpy((T), &V, sizeof(double))
 
312
#endif /* doubleget */
 
313
#define int64_tget(V,M)   memcpy(&V, (M), sizeof(uint64_t))
 
314
#define int64_tstore(T,V) memcpy((T), &V, sizeof(uint64_t))
 
315
 
 
316
#endif /* WORDS_BIGENDIAN */
 
317
 
 
318
#endif /* DRIZZLED_KORR_H */