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 float4get(V,M)   do { *((float *) &(V)) = *((float*) (M)); } while(0)
00080 #define float8get(V,M)   doubleget((V),(M))
00081 #define floatstore(T,V)  memcpy((T), (&V), sizeof(float))
00082 #define float8store(V,M) doublestore((V),(M))
00083 #else
00084 
00085 /*
00086   We're here if it's not a IA-32 architecture (Win32 and UNIX IA-32 defines
00087   were done before)
00088 */
00089 #define sint2korr(A)  (int16_t) (((int16_t) ((unsigned char) (A)[0])) +\
00090          ((int16_t) ((int16_t) (A)[1]) << 8))
00091 #define sint3korr(A)  ((int32_t) ((((unsigned char) (A)[2]) & 128) ? \
00092           (((uint32_t) 255L << 24) | \
00093            (((uint32_t) (unsigned char) (A)[2]) << 16) |\
00094            (((uint32_t) (unsigned char) (A)[1]) << 8) | \
00095            ((uint32_t) (unsigned char) (A)[0])) : \
00096           (((uint32_t) (unsigned char) (A)[2]) << 16) |\
00097           (((uint32_t) (unsigned char) (A)[1]) << 8) | \
00098           ((uint32_t) (unsigned char) (A)[0])))
00099 #define sint4korr(A)  (int32_t) (((int32_t) ((unsigned char) (A)[0])) +\
00100         (((int32_t) ((unsigned char) (A)[1]) << 8)) +\
00101         (((int32_t) ((unsigned char) (A)[2]) << 16)) +\
00102         (((int32_t) ((int16_t) (A)[3]) << 24)))
00103 #define sint8korr(A)  (int64_t) uint8korr(A)
00104 #define uint2korr(A)  (uint16_t) (((uint16_t) ((unsigned char) (A)[0])) +\
00105           ((uint16_t) ((unsigned char) (A)[1]) << 8))
00106 #define uint3korr(A)  (uint32_t) (((uint32_t) ((unsigned char) (A)[0])) +\
00107           (((uint32_t) ((unsigned char) (A)[1])) << 8) +\
00108           (((uint32_t) ((unsigned char) (A)[2])) << 16))
00109 #define uint4korr(A)  (uint32_t) (((uint32_t) ((unsigned char) (A)[0])) +\
00110           (((uint32_t) ((unsigned char) (A)[1])) << 8) +\
00111           (((uint32_t) ((unsigned char) (A)[2])) << 16) +\
00112           (((uint32_t) ((unsigned char) (A)[3])) << 24))
00113 #define uint8korr(A)  ((uint64_t)(((uint32_t) ((unsigned char) (A)[0])) +\
00114             (((uint32_t) ((unsigned char) (A)[1])) << 8) +\
00115             (((uint32_t) ((unsigned char) (A)[2])) << 16) +\
00116             (((uint32_t) ((unsigned char) (A)[3])) << 24)) +\
00117       (((uint64_t) (((uint32_t) ((unsigned char) (A)[4])) +\
00118             (((uint32_t) ((unsigned char) (A)[5])) << 8) +\
00119             (((uint32_t) ((unsigned char) (A)[6])) << 16) +\
00120             (((uint32_t) ((unsigned char) (A)[7])) << 24))) <<\
00121             32))
00122 #define int2store(T,A)       do { uint32_t def_temp= (uint32_t) (A) ;\
00123                                   *((unsigned char*) (T))=  (unsigned char)(def_temp); \
00124                                    *((unsigned char*) (T)+1)=(unsigned char)((def_temp >> 8)); \
00125                              } while(0)
00126 #define int3store(T,A)       do { /*lint -save -e734 */\
00127                                   *((unsigned char*)(T))=(unsigned char) ((A));\
00128                                   *((unsigned char*) (T)+1)=(unsigned char) (((A) >> 8));\
00129                                   *((unsigned char*)(T)+2)=(unsigned char) (((A) >> 16)); \
00130                                   /*lint -restore */} while(0)
00131 #define int4store(T,A)       do { *((char *)(T))=(char) ((A));\
00132                                   *(((char *)(T))+1)=(char) (((A) >> 8));\
00133                                   *(((char *)(T))+2)=(char) (((A) >> 16));\
00134                                   *(((char *)(T))+3)=(char) (((A) >> 24)); } while(0)
00135 #define int8store(T,A)       do { uint32_t def_temp= (uint32_t) (A), def_temp2= (uint32_t) ((A) >> 32); \
00136                                   int4store((T),def_temp); \
00137                                   int4store((T+4),def_temp2); } while(0)
00138 #ifdef WORDS_BIGENDIAN
00139 #define float4get(V,M)   do { float def_temp;\
00140                               ((unsigned char*) &def_temp)[0]=(M)[3];\
00141                               ((unsigned char*) &def_temp)[1]=(M)[2];\
00142                               ((unsigned char*) &def_temp)[2]=(M)[1];\
00143                               ((unsigned char*) &def_temp)[3]=(M)[0];\
00144                               (V)=def_temp; } while(0)
00145 #define float8store(T,V) do { *(T)= ((unsigned char *) &V)[7];\
00146                               *((T)+1)=(char) ((unsigned char *) &V)[6];\
00147                               *((T)+2)=(char) ((unsigned char *) &V)[5];\
00148                               *((T)+3)=(char) ((unsigned char *) &V)[4];\
00149                               *((T)+4)=(char) ((unsigned char *) &V)[3];\
00150                               *((T)+5)=(char) ((unsigned char *) &V)[2];\
00151                               *((T)+6)=(char) ((unsigned char *) &V)[1];\
00152                               *((T)+7)=(char) ((unsigned char *) &V)[0]; } while(0)
00153 
00154 #define float8get(V,M)   do { double def_temp;\
00155                               ((unsigned char*) &def_temp)[0]=(M)[7];\
00156                               ((unsigned char*) &def_temp)[1]=(M)[6];\
00157                               ((unsigned char*) &def_temp)[2]=(M)[5];\
00158                               ((unsigned char*) &def_temp)[3]=(M)[4];\
00159                               ((unsigned char*) &def_temp)[4]=(M)[3];\
00160                               ((unsigned char*) &def_temp)[5]=(M)[2];\
00161                               ((unsigned char*) &def_temp)[6]=(M)[1];\
00162                               ((unsigned char*) &def_temp)[7]=(M)[0];\
00163                               (V) = def_temp; } while(0)
00164 #else
00165 #define float4get(V,M)   memcpy(&V, (M), sizeof(float))
00166 
00167 #if defined(__FLOAT_WORD_ORDER) && (__FLOAT_WORD_ORDER == __BIG_ENDIAN)
00168 #define doublestore(T,V) do { *(((char*)T)+0)=(char) ((unsigned char *) &V)[4];\
00169                               *(((char*)T)+1)=(char) ((unsigned char *) &V)[5];\
00170                               *(((char*)T)+2)=(char) ((unsigned char *) &V)[6];\
00171                               *(((char*)T)+3)=(char) ((unsigned char *) &V)[7];\
00172                               *(((char*)T)+4)=(char) ((unsigned char *) &V)[0];\
00173                               *(((char*)T)+5)=(char) ((unsigned char *) &V)[1];\
00174                               *(((char*)T)+6)=(char) ((unsigned char *) &V)[2];\
00175                               *(((char*)T)+7)=(char) ((unsigned char *) &V)[3]; }\
00176                          while(0)
00177 #define doubleget(V,M)   do { double def_temp;\
00178                               ((unsigned char*) &def_temp)[0]=(M)[4];\
00179                               ((unsigned char*) &def_temp)[1]=(M)[5];\
00180                               ((unsigned char*) &def_temp)[2]=(M)[6];\
00181                               ((unsigned char*) &def_temp)[3]=(M)[7];\
00182                               ((unsigned char*) &def_temp)[4]=(M)[0];\
00183                               ((unsigned char*) &def_temp)[5]=(M)[1];\
00184                               ((unsigned char*) &def_temp)[6]=(M)[2];\
00185                               ((unsigned char*) &def_temp)[7]=(M)[3];\
00186                               (V) = def_temp; } while(0)
00187 #endif /* __FLOAT_WORD_ORDER */
00188 
00189 #define float8get(V,M)   doubleget((V),(M))
00190 #define float8store(V,M) doublestore((V),(M))
00191 #endif /* WORDS_BIGENDIAN */
00192 
00193 #endif /* __i386__ */
00194 
00195 /*
00196   Define-funktions for reading and storing in machine format from/to
00197   short/long to/from some place in memory V should be a (not
00198   register) variable, M is a pointer to byte
00199 */
00200 
00201 #ifdef WORDS_BIGENDIAN
00202 
00203 #define shortget(V,M)   do { V = (short) (((short) ((unsigned char) (M)[1]))+\
00204                                  ((short) ((short) (M)[0]) << 8)); } while(0)
00205 #define longget(V,M)    do { int32_t def_temp;\
00206                              ((unsigned char*) &def_temp)[0]=(M)[0];\
00207                              ((unsigned char*) &def_temp)[1]=(M)[1];\
00208                              ((unsigned char*) &def_temp)[2]=(M)[2];\
00209                              ((unsigned char*) &def_temp)[3]=(M)[3];\
00210                              (V)=def_temp; } while(0)
00211 #define shortstore(T,A) do { uint32_t def_temp=(uint32_t) (A) ;\
00212                              *(((char*)T)+1)=(char)(def_temp); \
00213                              *(((char*)T)+0)=(char)(def_temp >> 8); } while(0)
00214 #define longstore(T,A)  do { *(((char*)T)+3)=((A));\
00215                              *(((char*)T)+2)=(((A) >> 8));\
00216                              *(((char*)T)+1)=(((A) >> 16));\
00217                              *(((char*)T)+0)=(((A) >> 24)); } while(0)
00218 
00219 #define floatstore(T, V)   memcpy((T), (&V), sizeof(float))
00220 #define doubleget(V, M)   memcpy(&V, (M), sizeof(double))
00221 #define doublestore(T, V)  memcpy((T), &V, sizeof(double))
00222 #define int64_tget(V, M)   memcpy(&V, (M), sizeof(uint64_t))
00223 #define int64_tstore(T, V) memcpy((T), &V, sizeof(uint64_t))
00224 
00225 #else
00226 
00227 #define shortget(V,M) do { V = sint2korr(M); } while(0)
00228 #define longget(V,M)  do { V = sint4korr(M); } while(0)
00229 #define shortstore(T,V) int2store(T,V)
00230 #define longstore(T,V)  int4store(T,V)
00231 #ifndef floatstore
00232 #define floatstore(T,V)   memcpy((T), (&V), sizeof(float))
00233 #endif
00234 #ifndef doubleget
00235 #define doubleget(V, M)   memcpy(&V, (M), sizeof(double))
00236 #define doublestore(T,V)  memcpy((T), &V, sizeof(double))
00237 #endif /* doubleget */
00238 #define int64_tget(V,M)   memcpy(&V, (M), sizeof(uint64_t))
00239 #define int64_tstore(T,V) memcpy((T), &V, sizeof(uint64_t))
00240 
00241 #endif /* WORDS_BIGENDIAN */
00242