LcsBitOps.h

Go to the documentation of this file.
00001 /*
00002 // $Id: //open/dev/fennel/lucidera/colstore/LcsBitOps.h#9 $
00003 // Fennel is a library of data storage and processing components.
00004 // Copyright (C) 2005-2009 LucidEra, Inc.
00005 // Copyright (C) 2005-2009 The Eigenbase Project
00006 //
00007 // This program is free software; you can redistribute it and/or modify it
00008 // under the terms of the GNU General Public License as published by the Free
00009 // Software Foundation; either version 2 of the License, or (at your option)
00010 // any later version approved by The Eigenbase Project.
00011 //
00012 // This program is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License
00018 // along with this program; if not, write to the Free Software
00019 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 */
00021 
00022 #ifndef Fennel_LcsBitOps_Included
00023 #define Fennel_LcsBitOps_Included
00024 
00025 #include "math.h"
00026 
00027 FENNEL_BEGIN_NAMESPACE
00028 
00032 inline void setBits(uint8_t *pB, uint nBits, uint whatBits, uint16_t v)
00033 {
00034     *pB |= ((v & ((1 << nBits) -1)) << whatBits);
00035 }
00036 
00041 inline void readBits(
00042     uint8_t b, uint nBits, uint fromBits, uint16_t *v, uint toBits)
00043 {
00044     *v |= (((b & (((1 << nBits) -1) << fromBits)) >> fromBits) << toBits);
00045 }
00046 
00054 inline uint calcWidth(uint n)
00055 {
00056     uint w;
00057 
00058     // find out how many bits are needed to represent n
00059     w = 0;
00060     if (n > 0) {
00061         n--;
00062     }
00063     while (n) {
00064         w++;
00065         n >>= 1;
00066     }
00067 
00068     // round up the width to a value which can be
00069     // represented by two bit vectors (where each vector
00070     // has length 1, 2, 4, 8, or 16
00071     switch (w) {
00072     case  7:
00073         w = 8;
00074         break;
00075     case 11:
00076         w = 12;
00077         break;
00078     case 13:
00079     case 14:
00080     case 15:
00081         w = 16;
00082         break;
00083     default:
00084         break;
00085     }
00086 
00087     return w;
00088 }
00089 
00090 // WidthVec stores width in bits of bit vectors
00091 const uint          WIDTH_VECTOR_SIZE = 4;
00092 typedef uint8_t     WidthVec[WIDTH_VECTOR_SIZE];
00093 typedef WidthVec    *PWidthVec;
00094 typedef uint8_t     *PtrVec[WIDTH_VECTOR_SIZE];
00095 typedef PtrVec      *PPtrVec;
00096 
00097 typedef void (*PBitVecFuncPtr)(uint16_t *v, const PtrVec p, uint pos);
00098 typedef void (*PByteBitVecFuncPtr)(uint8_t *v, const PtrVec p, uint pos);
00099 
00100 /*
00101  * Creates a vector of widths required to represent l bits
00102  */
00103 inline uint bitVecWidth(uint l, WidthVec w)
00104 {
00105     uint8_t po2;
00106     uint iW;
00107     WidthVec t;
00108     int i,j;
00109 
00110     for (po2 = 1, iW = 0; l ; l >>= 1, po2 *= 2) {
00111         if (l & 0x1) {
00112             t[iW++] = po2;
00113         }
00114     }
00115 
00116     for (i = iW - 1, j = 0; i >= 0 ; w[j++] = t[i--]) {
00117     }
00118     return iW;
00119 }
00120 
00135 inline uint bitVecPtr(
00136     uint iCount, uint iW, WidthVec w, PtrVec p, uint8_t *pVec)
00137 {
00138     uint i;
00139     uint8_t *t;
00140 
00141     for (i = 0, t = pVec ; i < iW ; i++) {
00142         p[i] = t;
00143         t += ((w[i] * iCount + 7) / 8);
00144     }
00145 
00146     return t - pVec;
00147 }
00148 
00158 inline uint sizeofBitVec(uint nRow, uint iW, WidthVec w)
00159 {
00160     uint t;
00161     uint i;
00162 
00163     for (i = 0, t = 0; i < iW; i++) {
00164         t += ((w[i] * nRow + 7) / 8);
00165     }
00166     return t;
00167 }
00168 
00184 inline void readBitVecs(
00185     uint16_t *v, uint iV, const WidthVec w, const PtrVec p, uint pos,
00186     uint count)
00187 {
00188     uint        i, j, k;
00189     uint        b;
00190 
00191     // clear the destination
00192     memset(v, 0, sizeof(uint16_t) * count);
00193 
00194     // read bit arrays
00195     for (i = 0, b = 0; i < iV; i++) {
00196         // w[i] contains the width of the bit vector
00197         // read append each vector bits into v[i], b is the bit position
00198         // of the next append
00199         switch (w[i]) {
00200         case 16:
00201             memcpy(v, p[i] + pos*2, sizeof(uint16_t) * count);
00202             break;
00203 
00204         case 8:
00205             for (j = 0; j < count; j++) {
00206                 v[j] = (p[i] + pos)[j];
00207             }
00208             break;
00209 
00210         case 4:
00211             for (j = 0, k = pos*4;  j < count; j++, k += 4) {
00212                 readBits(p[i][k / 8], 4, k % 8, &v[j], b);
00213             }
00214             break;
00215 
00216         case 2:
00217             for (j = 0, k = pos*2; j < count; j++, k += 2) {
00218                 readBits(p[i][k / 8], 2, k % 8, &v[j], b);
00219             }
00220             break;
00221 
00222         case 1:
00223             for (j = 0, k = pos; j < count; j++, k++) {
00224                 readBits(p[i][k / 8], 1, k % 8, &v[j], b);
00225             }
00226             break;
00227 
00228         default:
00229             assert(false);          // unsupported width
00230             break;
00231         }
00232 
00233         b += w[i];
00234     }
00235 }
00236 
00246 inline void readBitVec0(uint16_t *v, const PtrVec p, uint pos)
00247 {
00248     // ARG_USED(p);
00249     // ARG_USED(pos);
00250     *v = 0;
00251 }
00252 
00262 inline void readBitVec16(uint16_t *v, const PtrVec p, uint pos)
00263 {
00264     *v = *(p[0] + pos*2);
00265 }
00266 
00276 inline void readBitVec8(uint16_t *v, const PtrVec p, uint pos)
00277 {
00278     *v = *(p[0] + pos);
00279 }
00280 
00290 inline void readBitVec4(uint16_t *v, const PtrVec p, uint pos)
00291 {
00292     // clear the destination
00293     *v = 0;
00294     readBits(p[0][pos/2], 4, (pos*4) % 8, v, 0);
00295 }
00296 
00306 inline void readBitVec2(uint16_t *v, const PtrVec p, uint pos)
00307 {
00308     // clear the destination
00309     *v = 0;
00310     readBits(p[0][pos/4], 2, (pos*2) % 8, v, 0);
00311 }
00312 
00322 inline void readBitVec1(uint16_t *v, const PtrVec p, uint pos)
00323 {
00324     // clear the destination
00325     *v = 0;
00326     readBits(p[0][pos / 8], 1, pos % 8, v, 0);
00327 }
00328 
00338 inline void readBitVec12(uint16_t *v, const PtrVec p, uint pos)
00339 {
00340     *v = *(p[0] + pos);
00341     readBits(p[1][pos/2], 4, (pos*4) % 8, v, 8);
00342 }
00343 
00353 inline void readBitVec10(uint16_t *v, const PtrVec p, uint pos)
00354 {
00355     *v = *(p[0] + pos);
00356     readBits(p[1][pos/4], 2, (pos*2) % 8, v, 8);
00357 }
00358 
00368 inline void readBitVec9(uint16_t *v, const PtrVec p, uint pos)
00369 {
00370     *v = *(p[0] + pos);
00371     readBits(p[1][pos / 8], 1, pos % 8, v, 8);
00372 }
00373 
00383 inline void readBitVec6(uint16_t *v, const PtrVec p, uint pos)
00384 {
00385     // clear the destination
00386     *v = 0;
00387     readBits(p[0][pos/2], 4, (pos*4) % 8, v, 0);
00388     readBits(p[1][pos/4], 2, (pos*2) % 8, v, 4);
00389 }
00390 
00400 inline void readBitVec5(uint16_t *v, const PtrVec p, uint pos)
00401 {
00402     // clear the destination
00403     *v = 0;
00404     readBits(p[0][pos / 2], 4, (pos * 4) % 8, v, 0);
00405     readBits(p[1][pos / 8], 1, pos % 8, v, 4);
00406 }
00407 
00417 inline void readBitVec3(uint16_t *v, const PtrVec p, uint pos)
00418 {
00419     // clear the destination
00420     *v = 0;
00421     readBits(p[0][pos / 4], 2, (pos * 2) % 8, v, 0);
00422     readBits(p[1][pos / 8], 1, pos % 8, v, 2);
00423 }
00424 
00425 FENNEL_END_NAMESPACE
00426 
00427 #endif
00428 
00429 // End LcsBitOps.h

Generated on Mon Jun 22 04:00:19 2009 for Fennel by  doxygen 1.5.1