1/** 2 * Copyright 2013, GitHub, Inc 3 * Copyright 2009-2013, Daniel Lemire, Cliff Moon, 4 * David McIntosh, Robert Becho, Google Inc. and Veronika Zenz 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License 8 * as published by the Free Software Foundation; either version 2 9 * of the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 19 */ 20#ifndef __EWOK_BITMAP_H__ 21#define __EWOK_BITMAP_H__ 22 23struct strbuf; 24typedefuint64_t eword_t; 25#define BITS_IN_EWORD (sizeof(eword_t) * 8) 26 27/** 28 * Do not use __builtin_popcountll. The GCC implementation 29 * is notoriously slow on all platforms. 30 * 31 * See: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36041 32 */ 33staticinlineuint32_tewah_bit_popcount64(uint64_t x) 34{ 35 x = (x &0x5555555555555555ULL) + ((x >>1) &0x5555555555555555ULL); 36 x = (x &0x3333333333333333ULL) + ((x >>2) &0x3333333333333333ULL); 37 x = (x &0x0F0F0F0F0F0F0F0FULL) + ((x >>4) &0x0F0F0F0F0F0F0F0FULL); 38return(x *0x0101010101010101ULL) >>56; 39} 40 41/* __builtin_ctzll was not available until 3.4.0 */ 42#if defined(__GNUC__) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR > 3)) 43#define ewah_bit_ctz64(x) __builtin_ctzll(x) 44#else 45staticinlineintewah_bit_ctz64(uint64_t x) 46{ 47int n =0; 48if((x &0xffffffff) ==0) { x >>=32; n +=32; } 49if((x &0xffff) ==0) { x >>=16; n +=16; } 50if((x &0xff) ==0) { x >>=8; n +=8; } 51if((x &0xf) ==0) { x >>=4; n +=4; } 52if((x &0x3) ==0) { x >>=2; n +=2; } 53if((x &0x1) ==0) { x >>=1; n +=1; } 54return n + !x; 55} 56#endif 57 58struct ewah_bitmap { 59 eword_t *buffer; 60size_t buffer_size; 61size_t alloc_size; 62size_t bit_size; 63 eword_t *rlw; 64}; 65 66typedefvoid(*ewah_callback)(size_t pos,void*); 67 68struct ewah_bitmap *ewah_pool_new(void); 69voidewah_pool_free(struct ewah_bitmap *self); 70 71/** 72 * Allocate a new EWAH Compressed bitmap 73 */ 74struct ewah_bitmap *ewah_new(void); 75 76/** 77 * Clear all the bits in the bitmap. Does not free or resize 78 * memory. 79 */ 80voidewah_clear(struct ewah_bitmap *self); 81 82/** 83 * Free all the memory of the bitmap 84 */ 85voidewah_free(struct ewah_bitmap *self); 86 87intewah_serialize_to(struct ewah_bitmap *self, 88int(*write_fun)(void*out,const void*buf,size_t len), 89void*out); 90intewah_serialize(struct ewah_bitmap *self,int fd); 91intewah_serialize_native(struct ewah_bitmap *self,int fd); 92intewah_serialize_strbuf(struct ewah_bitmap *self,struct strbuf *); 93 94intewah_deserialize(struct ewah_bitmap *self,int fd); 95intewah_read_mmap(struct ewah_bitmap *self,const void*map,size_t len); 96 97uint32_tewah_checksum(struct ewah_bitmap *self); 98 99/** 100 * Logical not (bitwise negation) in-place on the bitmap 101 * 102 * This operation is linear time based on the size of the bitmap. 103 */ 104voidewah_not(struct ewah_bitmap *self); 105 106/** 107 * Call the given callback with the position of every single bit 108 * that has been set on the bitmap. 109 * 110 * This is an efficient operation that does not fully decompress 111 * the bitmap. 112 */ 113voidewah_each_bit(struct ewah_bitmap *self, ewah_callback callback,void*payload); 114 115/** 116 * Set a given bit on the bitmap. 117 * 118 * The bit at position `pos` will be set to true. Because of the 119 * way that the bitmap is compressed, a set bit cannot be unset 120 * later on. 121 * 122 * Furthermore, since the bitmap uses streaming compression, bits 123 * can only set incrementally. 124 * 125 * E.g. 126 * ewah_set(bitmap, 1); // ok 127 * ewah_set(bitmap, 76); // ok 128 * ewah_set(bitmap, 77); // ok 129 * ewah_set(bitmap, 8712800127); // ok 130 * ewah_set(bitmap, 25); // failed, assert raised 131 */ 132voidewah_set(struct ewah_bitmap *self,size_t i); 133 134struct ewah_iterator { 135const eword_t *buffer; 136size_t buffer_size; 137 138size_t pointer; 139 eword_t compressed, literals; 140 eword_t rl, lw; 141int b; 142}; 143 144/** 145 * Initialize a new iterator to run through the bitmap in uncompressed form. 146 * 147 * The iterator can be stack allocated. The underlying bitmap must not be freed 148 * before the iteration is over. 149 * 150 * E.g. 151 * 152 * struct ewah_bitmap *bitmap = ewah_new(); 153 * struct ewah_iterator it; 154 * 155 * ewah_iterator_init(&it, bitmap); 156 */ 157voidewah_iterator_init(struct ewah_iterator *it,struct ewah_bitmap *parent); 158 159/** 160 * Yield every single word in the bitmap in uncompressed form. This is: 161 * yield single words (32-64 bits) where each bit represents an actual 162 * bit from the bitmap. 163 * 164 * Return: true if a word was yield, false if there are no words left 165 */ 166intewah_iterator_next(eword_t *next,struct ewah_iterator *it); 167 168voidewah_or( 169struct ewah_bitmap *ewah_i, 170struct ewah_bitmap *ewah_j, 171struct ewah_bitmap *out); 172 173voidewah_and_not( 174struct ewah_bitmap *ewah_i, 175struct ewah_bitmap *ewah_j, 176struct ewah_bitmap *out); 177 178voidewah_xor( 179struct ewah_bitmap *ewah_i, 180struct ewah_bitmap *ewah_j, 181struct ewah_bitmap *out); 182 183voidewah_and( 184struct ewah_bitmap *ewah_i, 185struct ewah_bitmap *ewah_j, 186struct ewah_bitmap *out); 187 188/** 189 * Direct word access 190 */ 191size_tewah_add_empty_words(struct ewah_bitmap *self,int v,size_t number); 192voidewah_add_dirty_words( 193struct ewah_bitmap *self,const eword_t *buffer,size_t number,int negate); 194size_tewah_add(struct ewah_bitmap *self, eword_t word); 195 196 197/** 198 * Uncompressed, old-school bitmap that can be efficiently compressed 199 * into an `ewah_bitmap`. 200 */ 201struct bitmap { 202 eword_t *words; 203size_t word_alloc; 204}; 205 206struct bitmap *bitmap_new(void); 207voidbitmap_set(struct bitmap *self,size_t pos); 208voidbitmap_clear(struct bitmap *self,size_t pos); 209intbitmap_get(struct bitmap *self,size_t pos); 210voidbitmap_reset(struct bitmap *self); 211voidbitmap_free(struct bitmap *self); 212intbitmap_equals(struct bitmap *self,struct bitmap *other); 213intbitmap_is_subset(struct bitmap *self,struct bitmap *super); 214 215struct ewah_bitmap *bitmap_to_ewah(struct bitmap *bitmap); 216struct bitmap *ewah_to_bitmap(struct ewah_bitmap *ewah); 217 218voidbitmap_and_not(struct bitmap *self,struct bitmap *other); 219voidbitmap_or_ewah(struct bitmap *self,struct ewah_bitmap *other); 220voidbitmap_or(struct bitmap *self,const struct bitmap *other); 221 222voidbitmap_each_bit(struct bitmap *self, ewah_callback callback,void*data); 223size_tbitmap_popcount(struct bitmap *self); 224 225#endif