~ubuntu-branches/ubuntu/trusty/drizzle/trusty

« back to all changes in this revision

Viewing changes to drizzled/korr.h

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2010-03-18 12:12:31 UTC
  • Revision ID: james.westby@ubuntu.com-20100318121231-k6g1xe6cshbwa0f8
Tags: upstream-2010.03.1347
ImportĀ upstreamĀ versionĀ 2010.03.1347

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 uint8korr(A)    (*((uint64_t *) (A)))
 
60
#define sint8korr(A)    (*((int64_t *) (A)))
 
61
#define int2store(T,A)  *((uint16_t*) (T))= (uint16_t) (A)
 
62
#define int3store(T,A)  do { *(T)=  (unsigned char) ((A));\
 
63
                            *(T+1)=(unsigned char) (((uint32_t) (A) >> 8));\
 
64
                            *(T+2)=(unsigned char) (((A) >> 16)); } while (0)
 
65
#define int4store(T,A)  *((long *) (T))= (long) (A)
 
66
#define int8store(T,A)  *((uint64_t *) (T))= (uint64_t) (A)
 
67
 
 
68
typedef union {
 
69
  double v;
 
70
  long m[2];
 
71
} doubleget_union;
 
72
#define doubleget(V,M)  \
 
73
do { doubleget_union _tmp; \
 
74
     _tmp.m[0] = *((long*)(M)); \
 
75
     _tmp.m[1] = *(((long*) (M))+1); \
 
76
     (V) = _tmp.v; } while(0)
 
77
#define doublestore(T,V) do { *((long *) T) = ((doubleget_union *)&V)->m[0]; \
 
78
                             *(((long *) T)+1) = ((doubleget_union *)&V)->m[1]; \
 
79
                         } while (0)
 
80
#define float4get(V,M)   do { *((float *) &(V)) = *((float*) (M)); } while(0)
 
81
#define float8get(V,M)   doubleget((V),(M))
 
82
#define floatstore(T,V)  memcpy((T), (&V), sizeof(float))
 
83
#define float8store(V,M) doublestore((V),(M))
 
84
#else
 
85
 
 
86
/*
 
87
  We're here if it's not a IA-32 architecture (Win32 and UNIX IA-32 defines
 
88
  were done before)
 
89
*/
 
90
#define sint2korr(A)    (int16_t) (((int16_t) ((unsigned char) (A)[0])) +\
 
91
                                 ((int16_t) ((int16_t) (A)[1]) << 8))
 
92
#define sint3korr(A)    ((int32_t) ((((unsigned char) (A)[2]) & 128) ? \
 
93
                                  (((uint32_t) 255L << 24) | \
 
94
                                   (((uint32_t) (unsigned char) (A)[2]) << 16) |\
 
95
                                   (((uint32_t) (unsigned char) (A)[1]) << 8) | \
 
96
                                   ((uint32_t) (unsigned char) (A)[0])) : \
 
97
                                  (((uint32_t) (unsigned char) (A)[2]) << 16) |\
 
98
                                  (((uint32_t) (unsigned char) (A)[1]) << 8) | \
 
99
                                  ((uint32_t) (unsigned char) (A)[0])))
 
100
#define sint4korr(A)    (int32_t) (((int32_t) ((unsigned char) (A)[0])) +\
 
101
                                (((int32_t) ((unsigned char) (A)[1]) << 8)) +\
 
102
                                (((int32_t) ((unsigned char) (A)[2]) << 16)) +\
 
103
                                (((int32_t) ((int16_t) (A)[3]) << 24)))
 
104
#define sint8korr(A)    (int64_t) uint8korr(A)
 
105
#define uint2korr(A)    (uint16_t) (((uint16_t) ((unsigned char) (A)[0])) +\
 
106
                                  ((uint16_t) ((unsigned char) (A)[1]) << 8))
 
107
#define uint3korr(A)    (uint32_t) (((uint32_t) ((unsigned char) (A)[0])) +\
 
108
                                  (((uint32_t) ((unsigned char) (A)[1])) << 8) +\
 
109
                                  (((uint32_t) ((unsigned char) (A)[2])) << 16))
 
110
#define uint4korr(A)    (uint32_t) (((uint32_t) ((unsigned char) (A)[0])) +\
 
111
                                  (((uint32_t) ((unsigned char) (A)[1])) << 8) +\
 
112
                                  (((uint32_t) ((unsigned char) (A)[2])) << 16) +\
 
113
                                  (((uint32_t) ((unsigned char) (A)[3])) << 24))
 
114
#define uint8korr(A)    ((uint64_t)(((uint32_t) ((unsigned char) (A)[0])) +\
 
115
                                    (((uint32_t) ((unsigned char) (A)[1])) << 8) +\
 
116
                                    (((uint32_t) ((unsigned char) (A)[2])) << 16) +\
 
117
                                    (((uint32_t) ((unsigned char) (A)[3])) << 24)) +\
 
118
                        (((uint64_t) (((uint32_t) ((unsigned char) (A)[4])) +\
 
119
                                    (((uint32_t) ((unsigned char) (A)[5])) << 8) +\
 
120
                                    (((uint32_t) ((unsigned char) (A)[6])) << 16) +\
 
121
                                    (((uint32_t) ((unsigned char) (A)[7])) << 24))) <<\
 
122
                                    32))
 
123
#define int2store(T,A)       do { uint32_t def_temp= (uint32_t) (A) ;\
 
124
                                  *((unsigned char*) (T))=  (unsigned char)(def_temp); \
 
125
                                   *((unsigned char*) (T)+1)=(unsigned char)((def_temp >> 8)); \
 
126
                             } while(0)
 
127
#define int3store(T,A)       do { /*lint -save -e734 */\
 
128
                                  *((unsigned char*)(T))=(unsigned char) ((A));\
 
129
                                  *((unsigned char*) (T)+1)=(unsigned char) (((A) >> 8));\
 
130
                                  *((unsigned char*)(T)+2)=(unsigned char) (((A) >> 16)); \
 
131
                                  /*lint -restore */} while(0)
 
132
#define int4store(T,A)       do { *((char *)(T))=(char) ((A));\
 
133
                                  *(((char *)(T))+1)=(char) (((A) >> 8));\
 
134
                                  *(((char *)(T))+2)=(char) (((A) >> 16));\
 
135
                                  *(((char *)(T))+3)=(char) (((A) >> 24)); } while(0)
 
136
#define int8store(T,A)       do { uint32_t def_temp= (uint32_t) (A), def_temp2= (uint32_t) ((A) >> 32); \
 
137
                                  int4store((T),def_temp); \
 
138
                                  int4store((T+4),def_temp2); } while(0)
 
139
#ifdef WORDS_BIGENDIAN
 
140
#define float4get(V,M)   do { float def_temp;\
 
141
                              ((unsigned char*) &def_temp)[0]=(M)[3];\
 
142
                              ((unsigned char*) &def_temp)[1]=(M)[2];\
 
143
                              ((unsigned char*) &def_temp)[2]=(M)[1];\
 
144
                              ((unsigned char*) &def_temp)[3]=(M)[0];\
 
145
                              (V)=def_temp; } while(0)
 
146
#define float8store(T,V) do { *(T)= ((unsigned char *) &V)[7];\
 
147
                              *((T)+1)=(char) ((unsigned char *) &V)[6];\
 
148
                              *((T)+2)=(char) ((unsigned char *) &V)[5];\
 
149
                              *((T)+3)=(char) ((unsigned char *) &V)[4];\
 
150
                              *((T)+4)=(char) ((unsigned char *) &V)[3];\
 
151
                              *((T)+5)=(char) ((unsigned char *) &V)[2];\
 
152
                              *((T)+6)=(char) ((unsigned char *) &V)[1];\
 
153
                              *((T)+7)=(char) ((unsigned char *) &V)[0]; } while(0)
 
154
 
 
155
#define float8get(V,M)   do { double def_temp;\
 
156
                              ((unsigned char*) &def_temp)[0]=(M)[7];\
 
157
                              ((unsigned char*) &def_temp)[1]=(M)[6];\
 
158
                              ((unsigned char*) &def_temp)[2]=(M)[5];\
 
159
                              ((unsigned char*) &def_temp)[3]=(M)[4];\
 
160
                              ((unsigned char*) &def_temp)[4]=(M)[3];\
 
161
                              ((unsigned char*) &def_temp)[5]=(M)[2];\
 
162
                              ((unsigned char*) &def_temp)[6]=(M)[1];\
 
163
                              ((unsigned char*) &def_temp)[7]=(M)[0];\
 
164
                              (V) = def_temp; } while(0)
 
165
#else
 
166
#define float4get(V,M)   memcpy(&V, (M), sizeof(float))
 
167
 
 
168
#if defined(__FLOAT_WORD_ORDER) && (__FLOAT_WORD_ORDER == __BIG_ENDIAN)
 
169
#define doublestore(T,V) do { *(((char*)T)+0)=(char) ((unsigned char *) &V)[4];\
 
170
                              *(((char*)T)+1)=(char) ((unsigned char *) &V)[5];\
 
171
                              *(((char*)T)+2)=(char) ((unsigned char *) &V)[6];\
 
172
                              *(((char*)T)+3)=(char) ((unsigned char *) &V)[7];\
 
173
                              *(((char*)T)+4)=(char) ((unsigned char *) &V)[0];\
 
174
                              *(((char*)T)+5)=(char) ((unsigned char *) &V)[1];\
 
175
                              *(((char*)T)+6)=(char) ((unsigned char *) &V)[2];\
 
176
                              *(((char*)T)+7)=(char) ((unsigned char *) &V)[3]; }\
 
177
                         while(0)
 
178
#define doubleget(V,M)   do { double def_temp;\
 
179
                              ((unsigned char*) &def_temp)[0]=(M)[4];\
 
180
                              ((unsigned char*) &def_temp)[1]=(M)[5];\
 
181
                              ((unsigned char*) &def_temp)[2]=(M)[6];\
 
182
                              ((unsigned char*) &def_temp)[3]=(M)[7];\
 
183
                              ((unsigned char*) &def_temp)[4]=(M)[0];\
 
184
                              ((unsigned char*) &def_temp)[5]=(M)[1];\
 
185
                              ((unsigned char*) &def_temp)[6]=(M)[2];\
 
186
                              ((unsigned char*) &def_temp)[7]=(M)[3];\
 
187
                              (V) = def_temp; } while(0)
 
188
#endif /* __FLOAT_WORD_ORDER */
 
189
 
 
190
#define float8get(V,M)   doubleget((V),(M))
 
191
#define float8store(V,M) doublestore((V),(M))
 
192
#endif /* WORDS_BIGENDIAN */
 
193
 
 
194
#endif /* __i386__ */
 
195
 
 
196
/*
 
197
  Define-funktions for reading and storing in machine format from/to
 
198
  short/long to/from some place in memory V should be a (not
 
199
  register) variable, M is a pointer to byte
 
200
*/
 
201
 
 
202
#ifdef WORDS_BIGENDIAN
 
203
 
 
204
#define shortget(V,M)   do { V = (short) (((short) ((unsigned char) (M)[1]))+\
 
205
                                 ((short) ((short) (M)[0]) << 8)); } while(0)
 
206
#define longget(V,M)    do { int32_t def_temp;\
 
207
                             ((unsigned char*) &def_temp)[0]=(M)[0];\
 
208
                             ((unsigned char*) &def_temp)[1]=(M)[1];\
 
209
                             ((unsigned char*) &def_temp)[2]=(M)[2];\
 
210
                             ((unsigned char*) &def_temp)[3]=(M)[3];\
 
211
                             (V)=def_temp; } while(0)
 
212
#define shortstore(T,A) do { uint32_t def_temp=(uint32_t) (A) ;\
 
213
                             *(((char*)T)+1)=(char)(def_temp); \
 
214
                             *(((char*)T)+0)=(char)(def_temp >> 8); } while(0)
 
215
#define longstore(T,A)  do { *(((char*)T)+3)=((A));\
 
216
                             *(((char*)T)+2)=(((A) >> 8));\
 
217
                             *(((char*)T)+1)=(((A) >> 16));\
 
218
                             *(((char*)T)+0)=(((A) >> 24)); } while(0)
 
219
 
 
220
#define floatstore(T, V)   memcpy((T), (&V), sizeof(float))
 
221
#define doubleget(V, M)   memcpy(&V, (M), sizeof(double))
 
222
#define doublestore(T, V)  memcpy((T), &V, sizeof(double))
 
223
#define int64_tget(V, M)   memcpy(&V, (M), sizeof(uint64_t))
 
224
#define int64_tstore(T, V) memcpy((T), &V, sizeof(uint64_t))
 
225
 
 
226
#else
 
227
 
 
228
#define shortget(V,M)   do { V = sint2korr(M); } while(0)
 
229
#define longget(V,M)    do { V = sint4korr(M); } while(0)
 
230
#define shortstore(T,V) int2store(T,V)
 
231
#define longstore(T,V)  int4store(T,V)
 
232
#ifndef floatstore
 
233
#define floatstore(T,V)   memcpy((T), (&V), sizeof(float))
 
234
#endif
 
235
#ifndef doubleget
 
236
#define doubleget(V, M)   memcpy(&V, (M), sizeof(double))
 
237
#define doublestore(T,V)  memcpy((T), &V, sizeof(double))
 
238
#endif /* doubleget */
 
239
#define int64_tget(V,M)   memcpy(&V, (M), sizeof(uint64_t))
 
240
#define int64_tstore(T,V) memcpy((T), &V, sizeof(uint64_t))
 
241
 
 
242
#endif /* WORDS_BIGENDIAN */
 
243
 
 
244
#endif /* DRIZZLED_KORR_H */