Changeset 75ff31607066f7bcb0b471c3652443662c0f985f

Show
Ignore:
Timestamp:
04/30/08 14:32:42 (8 months ago)
Author:
krayouva <krayouva@…>
Parents:
1c4d613e749d9e677b5d4a13214e93bfee485937
Children:
1bc545c8942b47e034f824644b4834a991178afc
git-committer:
krayouva <krayouva@gmail.com> / 2008-04-30T00:32:42Z-0400
Message:

* Minor code refactoring

- Added the frt_round2() method that rounds to the next 2nd power

Location:
c
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • c/include/global.h

    red4cf5 r75ff31  
    133133 
    134134/** 
     135 * Returns the count of leading [MSB] 0 bits in +word+. 
     136 */ 
     137extern FRT_ATTR_ALWAYS_INLINE FRT_ATTR_CONST 
     138int frt_count_leading_zeros(frt_u32 word) 
     139{ 
     140#ifdef __GNUC__ 
     141    return __builtin_clz(word); 
     142#else 
     143    static const int count_leading_zeros[] = { 
     144        8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 
     145        3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
     146        2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
     147        2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
     148        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
     149        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
     150        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
     151        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
     152        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
     153        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
     154        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
     155        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
     156        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
     157        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
     158        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
     159        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 
     160    }; 
     161                if (word & 0xff) return count_leading_zeros[word & 0xff]; 
     162    word >>= 8; if (word & 0xff) return count_leading_zeros[word & 0xff] + 8; 
     163    word >>= 8; if (word & 0xff) return count_leading_zeros[word & 0xff] + 16; 
     164    word >>= 8;                  return count_leading_zeros[word & 0xff] + 24; 
     165#endif 
     166} 
     167 
     168extern FRT_ATTR_ALWAYS_INLINE FRT_ATTR_CONST 
     169int frt_count_leading_ones(frt_u32 word) 
     170{ 
     171    return frt_count_leading_zeros(~word); 
     172} 
     173 
     174/** 
    135175 * Return the count of trailing [LSB] 0 bits in +word+. 
    136176 */ 
     
    211251 
    212252/** 
     253 * Round up to the next power of 2 
     254 */ 
     255extern FRT_ATTR_ALWAYS_INLINE FRT_ATTR_CONST 
     256int frt_round2(frt_u32 word) 
     257{ 
     258    return 1 << (32 - frt_count_leading_zeros(word)); 
     259} 
     260 
     261/** 
    213262 * For coverage, we don't want FRT_XEXIT to actually exit on uncaught 
    214263 * exceptions.  +frt_x_abort_on_exception+ is +true+ by default, set it to 
  • c/src/bitvector.c

    r1c4d61 r75ff31  
    11#include "bitvector.h" 
     2#include "internal.h" 
    23#include <string.h> 
    3 #include "internal.h" 
    44 
    55BitVector *bv_new_capa(int capa) 
     
    126126        } 
    127127    } 
    128     hash = (hash << 1) | bv->extends_as_ones; 
    129     return hash; 
     128    return (hash << 1) | bv->extends_as_ones; 
     129} 
     130 
     131static INLINE void bv_capa(BitVector *bv, int capa, int size) 
     132{ 
     133    int word_size = (size >> 5) + 1; 
     134    REALLOC_N(bv->bits, u32, capa); 
     135    bv->capa = capa; 
     136    bv->size = size; 
     137    memset(bv->bits + word_size, (bv->extends_as_ones ? 0xFF : 0), 
     138           sizeof(u32) * (capa - word_size)); 
    130139} 
    131140 
     
    147156    int capa = 4; 
    148157 
    149     if (bv1->extends_as_ones && bv2->extends_as_ones) { 
    150         size = max2(bv1->size, bv2->size); 
    151         bv->extends_as_ones = true; 
    152     } 
    153     else if (bv1->extends_as_ones || bv2->extends_as_ones) { 
    154         size = max2(bv1->size, bv2->size); 
    155         bv->extends_as_ones = false; 
    156     } 
    157     else { 
    158         size = min2(bv1->size, bv2->size); 
    159         bv->extends_as_ones = false; 
    160     } 
     158    bv->extends_as_ones = (bv1->extends_as_ones & bv2->extends_as_ones); 
     159 
     160    if (bv1->extends_as_ones || bv2->extends_as_ones) 
     161         size = max2(bv1->size, bv2->size); 
     162    else size = min2(bv1->size, bv2->size); 
    161163 
    162164    word_size = (size >> 5) + 1; 
    163     while (capa < word_size) { 
    164         capa <<= 1; 
    165     } 
     165    capa = max2(frt_round2(word_size), 4); 
     166 
    166167    bv_recapa(bv1, capa); 
    167168    bv_recapa(bv2, capa); 
    168     REALLOC_N(bv->bits, u32, capa); 
    169     bv->capa = capa; 
    170     bv->size = size; 
    171  
    172     memset(bv->bits + word_size, (bv->extends_as_ones ? 0xFF : 0), 
    173            sizeof(u32) * (capa - word_size)); 
     169    bv_capa(bv, capa, size); 
    174170 
    175171    for (i = 0; i < word_size; i++) { 
     
    196192    int max_size = max2(bv1->size, bv2->size); 
    197193    int word_size = (max_size >> 5) + 1; 
    198     int capa = 4; 
    199     while (capa < word_size) { 
    200         capa <<= 1; 
    201     } 
    202     REALLOC_N(bv->bits, u32, capa); 
    203     bv->capa = capa; 
    204     bv->size = max_size; 
    205  
     194    int capa = max2(frt_round2(word_size), 4); 
     195 
     196    bv->extends_as_ones = (bv1->extends_as_ones | bv2->extends_as_ones); 
    206197    bv_recapa(bv1, capa); 
    207198    bv_recapa(bv2, capa); 
    208  
    209     if (bv1->extends_as_ones || bv2->extends_as_ones) { 
    210         bv->extends_as_ones = true; 
    211     } 
    212     else { 
    213         bv->extends_as_ones = false; 
    214     } 
    215  
    216     memset(bv->bits + word_size, (bv->extends_as_ones ? 0xFF : 0), 
    217            sizeof(u32) * (capa - word_size)); 
     199    bv_capa(bv, capa, max_size); 
    218200 
    219201    for (i = 0; i < word_size; i++) { 
     
    239221    int max_size = max2(bv1->size, bv2->size); 
    240222    int word_size = (max_size >> 5) + 1; 
    241     int capa = 4; 
    242     while (capa < word_size) { 
    243         capa <<= 1; 
    244     } 
    245     REALLOC_N(bv->bits, u32, capa); 
    246     bv->capa = capa; 
    247     bv->size = max_size; 
    248  
     223    int capa = max2(frt_round2(word_size), 4); 
     224 
     225    bv->extends_as_ones = (bv1->extends_as_ones ^ bv2->extends_as_ones); 
    249226    bv_recapa(bv1, capa); 
    250227    bv_recapa(bv2, capa); 
    251  
    252     if (bv1->extends_as_ones != bv2->extends_as_ones) { 
    253         bv->extends_as_ones = true; 
    254     } 
    255     else { 
    256         bv->extends_as_ones = false; 
    257     } 
    258  
    259     memset(bv->bits + word_size, (bv->extends_as_ones ? 0xFF : 0), 
    260            sizeof(u32) * (capa - word_size)); 
     228    bv_capa(bv, capa, max_size); 
    261229 
    262230    for (i = 0; i < word_size; i++) { 
     
    281249    int i; 
    282250    int word_size = (bv1->size >> 5) + 1; 
    283     int capa = 4; 
    284     while (capa < word_size) { 
    285         capa <<= 1; 
    286     } 
    287     REALLOC_N(bv->bits, u32, capa); 
    288     bv->capa = capa; 
    289     bv->size = bv1->size; 
     251    int capa = max2(frt_round2(word_size), 4); 
     252 
    290253    bv->extends_as_ones = !bv1->extends_as_ones; 
    291     memset(bv->bits + word_size, (bv->extends_as_ones ? 0xFF : 0), 
    292            sizeof(u32) * (capa - word_size)); 
     254    bv_capa(bv, capa, bv1->size); 
    293255 
    294256    for (i = 0; i < word_size; i++) {