Drizzled Public API Documentation

korr.h
00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2008 Sun Microsystems, Inc.
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; version 2 of the License.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  */
00019 
00020 #pragma once
00021 
00022 
00023 /*
00024  * Define-functions for reading and storing in machine independent format
00025  * (low byte first)
00026  *
00027  * No one seems to know what "korr" means in this context. A global search
00028  * and replace would be fine if someone can come up with a better description
00029  */
00030 
00031 /* Optimized store functions for Intel x86 */
00032 #if defined(__i386__)
00033 #define sint2korr(A)  (*((int16_t *) (A)))
00034 #define sint3korr(A)  ((int32_t) ((((unsigned char) (A)[2]) & 128) ?  \
00035                                     (((uint32_t) 255L << 24) |          \
00036                                      (((uint32_t) (unsigned char) (A)[2]) << 16) | \
00037                                      (((uint32_t) (unsigned char) (A)[1]) << 8) | \
00038                                      ((uint32_t) (unsigned char) (A)[0])) : \
00039                                     (((uint32_t) (unsigned char) (A)[2]) << 16) | \
00040                                     (((uint32_t) (unsigned char) (A)[1]) << 8) | \
00041                                     ((uint32_t) (unsigned char) (A)[0])))
00042 #define sint4korr(A)  (*((long *) (A)))
00043 #define uint2korr(A)  (*((uint16_t *) (A)))
00044 #if defined(HAVE_VALGRIND)
00045 #define uint3korr(A)  (uint32_t) (((uint32_t) ((unsigned char) (A)[0])) +\
00046           (((uint32_t) ((unsigned char) (A)[1])) << 8) +\
00047           (((uint32_t) ((unsigned char) (A)[2])) << 16))
00048 #else
00049 /*
00050    ATTENTION !
00051 
00052     Please, note, uint3korr reads 4 bytes (not 3) !
00053     It means, that you have to provide enough allocated space !
00054 */
00055 #define uint3korr(A)  (long) (*((unsigned int *) (A)) & 0xFFFFFF)
00056 #endif /* HAVE_VALGRIND */
00057 #define uint4korr(A)  (*((uint32_t *) (A)))
00058 #define uint8korr(A)  (*((uint64_t *) (A)))
00059 #define sint8korr(A)  (*((int64_t *) (A)))
00060 #define int2store(T,A)  *((uint16_t*) (T))= (uint16_t) (A)
00061 #define int3store(T,A)  do { *(T)=  (unsigned char) ((A));\
00062                             *(T+1)=(unsigned char) (((uint32_t) (A) >> 8));\
00063                             *(T+2)=(unsigned char) (((A) >> 16)); } while (0)
00064 #define int4store(T,A)  *((long *) (T))= (long) (A)
00065 #define int8store(T,A)  *((uint64_t *) (T))= (uint64_t) (A)
00066 
00067 typedef union {
00068   double v;
00069   long m[2];
00070 } doubleget_union;
00071 #define doubleget(V,M)  \
00072 do { doubleget_union _tmp; \
00073      _tmp.m[0] = *((long*)(M)); \
00074      _tmp.m[1] = *(((long*) (M))+1); \
00075      (V) = _tmp.v; } while(0)
00076 #define doublestore(T,V) do { *((long *) T) = ((doubleget_union *)&V)->m[0]; \
00077            *(((long *) T)+1) = ((doubleget_union *)&V)->m[1]; \
00078                          } while (0)
00079 #define float8get(V,M)   doubleget((V),(M))
00080 #define floatstore(T,V)  memcpy((T), (&V), sizeof(float))
00081 #define float8store(V,M) doublestore((V),(M))
00082 #else
00083 
00084 /*
00085   We're here if it's not a IA-32 architecture (Win32 and UNIX IA-32 defines
00086   were done before)
00087 */
00088 #define sint2korr(A)  (int16_t) (((int16_t) ((unsigned char) (A)[0])) +\
00089          ((int16_t) ((int16_t) (A)[1]) << 8))
00090 #define sint4korr(A)  (int32_t) (((int32_t) ((unsigned char) (A)[0])) +\
00091         (((int32_t) ((unsigned char) (A)[1]) << 8)) +\
00092         (((int32_t) ((unsigned char) (A)[2]) << 16)) +\
00093         (((int32_t) ((int16_t) (A)[3]) << 24)))
00094 #define sint8korr(A)  (int64_t) uint8korr(A)
00095 #define uint2korr(A)  (uint16_t) (((uint16_t) ((unsigned char) (A)[0])) +\
00096           ((uint16_t) ((unsigned char) (A)[1]) << 8))
00097 #define uint3korr(A)  (uint32_t) (((uint32_t) ((unsigned char) (A)[0])) +\
00098           (((uint32_t) ((unsigned char) (A)[1])) << 8) +\
00099           (((uint32_t) ((unsigned char) (A)[2])) << 16))
00100 #define uint4korr(A)  (uint32_t) (((uint32_t) ((unsigned char) (A)[0])) +\
00101           (((uint32_t) ((unsigned char) (A)[1])) << 8) +\
00102           (((uint32_t) ((unsigned char) (A)[2])) << 16) +\
00103           (((uint32_t) ((unsigned char) (A)[3])) << 24))
00104 #define uint8korr(A)  ((uint64_t)(((uint32_t) ((unsigned char) (A)[0])) +\
00105             (((uint32_t) ((unsigned char) (A)[1])) << 8) +\
00106             (((uint32_t) ((unsigned char) (A)[2])) << 16) +\
00107             (((uint32_t) ((unsigned char) (A)[3])) << 24)) +\
00108       (((uint64_t) (((uint32_t) ((unsigned char) (A)[4])) +\
00109             (((uint32_t) ((unsigned char) (A)[5])) << 8) +\
00110             (((uint32_t) ((unsigned char) (A)[6])) << 16) +\
00111             (((uint32_t) ((unsigned char) (A)[7])) << 24))) <<\
00112             32))
00113 #define int2store(T,A)       do { uint32_t def_temp= (uint32_t) (A) ;\
00114                                   *((unsigned char*) (T))=  (unsigned char)(def_temp); \
00115                                    *((unsigned char*) (T)+1)=(unsigned char)((def_temp >> 8)); \
00116                              } while(0)
00117 #define int3store(T,A)       do { /*lint -save -e734 */\
00118                                   *((unsigned char*)(T))=(unsigned char) ((A));\
00119                                   *((unsigned char*) (T)+1)=(unsigned char) (((A) >> 8));\
00120                                   *((unsigned char*)(T)+2)=(unsigned char) (((A) >> 16)); \
00121                                   /*lint -restore */} while(0)
00122 #define int4store(T,A)       do { *((char *)(T))=(char) ((A));\
00123                                   *(((char *)(T))+1)=(char) (((A) >> 8));\
00124                                   *(((char *)(T))+2)=(char) (((A) >> 16));\
00125                                   *(((char *)(T))+3)=(char) (((A) >> 24)); } while(0)
00126 #define int8store(T,A)       do { uint32_t def_temp= (uint32_t) (A), def_temp2= (uint32_t) ((A) >> 32); \
00127                                   int4store((T),def_temp); \
00128                                   int4store((T+4),def_temp2); } while(0)
00129 #ifdef WORDS_BIGENDIAN
00130 #define float4get(V,M)   do { float def_temp;\
00131                               ((unsigned char*) &def_temp)[0]=(M)[3];\
00132                               ((unsigned char*) &def_temp)[1]=(M)[2];\
00133                               ((unsigned char*) &def_temp)[2]=(M)[1];\
00134                               ((unsigned char*) &def_temp)[3]=(M)[0];\
00135                               (V)=def_temp; } while(0)
00136 #define float8store(T,V) do { *(T)= ((unsigned char *) &V)[7];\
00137                               *((T)+1)=(char) ((unsigned char *) &V)[6];\
00138                               *((T)+2)=(char) ((unsigned char *) &V)[5];\
00139                               *((T)+3)=(char) ((unsigned char *) &V)[4];\
00140                               *((T)+4)=(char) ((unsigned char *) &V)[3];\
00141                               *((T)+5)=(char) ((unsigned char *) &V)[2];\
00142                               *((T)+6)=(char) ((unsigned char *) &V)[1];\
00143                               *((T)+7)=(char) ((unsigned char *) &V)[0]; } while(0)
00144 
00145 #define float8get(V,M)   do { double def_temp;\
00146                               ((unsigned char*) &def_temp)[0]=(M)[7];\
00147                               ((unsigned char*) &def_temp)[1]=(M)[6];\
00148                               ((unsigned char*) &def_temp)[2]=(M)[5];\
00149                               ((unsigned char*) &def_temp)[3]=(M)[4];\
00150                               ((unsigned char*) &def_temp)[4]=(M)[3];\
00151                               ((unsigned char*) &def_temp)[5]=(M)[2];\
00152                               ((unsigned char*) &def_temp)[6]=(M)[1];\
00153                               ((unsigned char*) &def_temp)[7]=(M)[0];\
00154                               (V) = def_temp; } while(0)
00155 #else
00156 #define float4get(V,M)   memcpy(&V, (M), sizeof(float))
00157 
00158 #if defined(__FLOAT_WORD_ORDER) && (__FLOAT_WORD_ORDER == __BIG_ENDIAN)
00159 #define doublestore(T,V) do { *(((char*)T)+0)=(char) ((unsigned char *) &V)[4];\
00160                               *(((char*)T)+1)=(char) ((unsigned char *) &V)[5];\
00161                               *(((char*)T)+2)=(char) ((unsigned char *) &V)[6];\
00162                               *(((char*)T)+3)=(char) ((unsigned char *) &V)[7];\
00163                               *(((char*)T)+4)=(char) ((unsigned char *) &V)[0];\
00164                               *(((char*)T)+5)=(char) ((unsigned char *) &V)[1];\
00165                               *(((char*)T)+6)=(char) ((unsigned char *) &V)[2];\
00166                               *(((char*)T)+7)=(char) ((unsigned char *) &V)[3]; }\
00167                          while(0)
00168 #define doubleget(V,M)   do { double def_temp;\
00169                               ((unsigned char*) &def_temp)[0]=(M)[4];\
00170                               ((unsigned char*) &def_temp)[1]=(M)[5];\
00171                               ((unsigned char*) &def_temp)[2]=(M)[6];\
00172                               ((unsigned char*) &def_temp)[3]=(M)[7];\
00173                               ((unsigned char*) &def_temp)[4]=(M)[0];\
00174                               ((unsigned char*) &def_temp)[5]=(M)[1];\
00175                               ((unsigned char*) &def_temp)[6]=(M)[2];\
00176                               ((unsigned char*) &def_temp)[7]=(M)[3];\
00177                               (V) = def_temp; } while(0)
00178 #endif /* __FLOAT_WORD_ORDER */
00179 
00180 #define float8get(V,M)   doubleget((V),(M))
00181 #define float8store(V,M) doublestore((V),(M))
00182 #endif /* WORDS_BIGENDIAN */
00183 
00184 #endif /* __i386__ */
00185 
00186 /*
00187   Define-funktions for reading and storing in machine format from/to
00188   short/long to/from some place in memory V should be a (not
00189   register) variable, M is a pointer to byte
00190 */
00191 
00192 #ifdef WORDS_BIGENDIAN
00193 
00194 #define shortget(V,M)   do { V = (short) (((short) ((unsigned char) (M)[1]))+\
00195                                  ((short) ((short) (M)[0]) << 8)); } while(0)
00196 #define longget(V,M)    do { int32_t def_temp;\
00197                              ((unsigned char*) &def_temp)[0]=(M)[0];\
00198                              ((unsigned char*) &def_temp)[1]=(M)[1];\
00199                              ((unsigned char*) &def_temp)[2]=(M)[2];\
00200                              ((unsigned char*) &def_temp)[3]=(M)[3];\
00201                              (V)=def_temp; } while(0)
00202 #define shortstore(T,A) do { uint32_t def_temp=(uint32_t) (A) ;\
00203                              *(((char*)T)+1)=(char)(def_temp); \
00204                              *(((char*)T)+0)=(char)(def_temp >> 8); } while(0)
00205 #define longstore(T,A)  do { *(((char*)T)+3)=((A));\
00206                              *(((char*)T)+2)=(((A) >> 8));\
00207                              *(((char*)T)+1)=(((A) >> 16));\
00208                              *(((char*)T)+0)=(((A) >> 24)); } while(0)
00209 
00210 #define floatstore(T, V)   memcpy((T), (&V), sizeof(float))
00211 #define doubleget(V, M)   memcpy(&V, (M), sizeof(double))
00212 #define doublestore(T, V)  memcpy((T), &V, sizeof(double))
00213 #define int64_tget(V, M)   memcpy(&V, (M), sizeof(uint64_t))
00214 #define int64_tstore(T, V) memcpy((T), &V, sizeof(uint64_t))
00215 
00216 #else
00217 
00218 #define shortget(V,M) do { V = sint2korr(M); } while(0)
00219 #define longget(V,M)  do { V = sint4korr(M); } while(0)
00220 #define shortstore(T,V) int2store(T,V)
00221 #define longstore(T,V)  int4store(T,V)
00222 #ifndef floatstore
00223 #define floatstore(T,V)   memcpy((T), (&V), sizeof(float))
00224 #endif
00225 #ifndef doubleget
00226 #define doubleget(V, M)   memcpy(&V, (M), sizeof(double))
00227 #define doublestore(T,V)  memcpy((T), &V, sizeof(double))
00228 #endif /* doubleget */
00229 #define int64_tget(V,M)   memcpy(&V, (M), sizeof(uint64_t))
00230 #define int64_tstore(T,V) memcpy((T), &V, sizeof(uint64_t))
00231 
00232 #endif /* WORDS_BIGENDIAN */
00233