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 23#ifndef ewah_malloc 24# define ewah_malloc xmalloc 25#endif 26#ifndef ewah_realloc 27# define ewah_realloc xrealloc 28#endif 29#ifndef ewah_calloc 30# define ewah_calloc xcalloc 31#endif 32 33typedefuint64_t eword_t; 34#define BITS_IN_WORD (sizeof(eword_t) * 8) 35 36/** 37 * Do not use __builtin_popcountll. The GCC implementation 38 * is notoriously slow on all platforms. 39 * 40 * See: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36041 41 */ 42staticinlineuint32_tewah_bit_popcount64(uint64_t x) 43{ 44 x = (x &0x5555555555555555ULL) + ((x >>1) &0x5555555555555555ULL); 45 x = (x &0x3333333333333333ULL) + ((x >>2) &0x3333333333333333ULL); 46 x = (x &0x0F0F0F0F0F0F0F0FULL) + ((x >>4) &0x0F0F0F0F0F0F0F0FULL); 47return(x *0x0101010101010101ULL) >>56; 48} 49 50#ifdef __GNUC__ 51#define ewah_bit_ctz64(x) __builtin_ctzll(x) 52#else 53staticinlineintewah_bit_ctz64(uint64_t x) 54{ 55int n =0; 56if((x &0xffffffff) ==0) { x >>=32; n +=32; } 57if((x &0xffff) ==0) { x >>=16; n +=16; } 58if((x &0xff) ==0) { x >>=8; n +=8; } 59if((x &0xf) ==0) { x >>=4; n +=4; } 60if((x &0x3) ==0) { x >>=2; n +=2; } 61if((x &0x1) ==0) { x >>=1; n +=1; } 62return n + !x; 63} 64#endif 65 66struct ewah_bitmap { 67 eword_t *buffer; 68size_t buffer_size; 69size_t alloc_size; 70size_t bit_size; 71 eword_t *rlw; 72}; 73 74typedefvoid(*ewah_callback)(size_t pos,void*); 75 76struct ewah_bitmap *ewah_pool_new(void); 77voidewah_pool_free(struct ewah_bitmap *self); 78 79/** 80 * Allocate a new EWAH Compressed bitmap 81 */ 82struct ewah_bitmap *ewah_new(void); 83 84/** 85 * Clear all the bits in the bitmap. Does not free or resize 86 * memory. 87 */ 88voidewah_clear(struct ewah_bitmap *self); 89 90/** 91 * Free all the memory of the bitmap 92 */ 93voidewah_free(struct ewah_bitmap *self); 94 95intewah_serialize_to(struct ewah_bitmap *self, 96int(*write_fun)(void*out,const void*buf,size_t len), 97void*out); 98intewah_serialize(struct ewah_bitmap *self,int fd); 99intewah_serialize_native(struct ewah_bitmap *self,int fd); 100 101intewah_deserialize(struct ewah_bitmap *self,int fd); 102intewah_read_mmap(struct ewah_bitmap *self,const void*map,size_t len); 103 104uint32_tewah_checksum(struct ewah_bitmap *self); 105 106/** 107 * Logical not (bitwise negation) in-place on the bitmap 108 * 109 * This operation is linear time based on the size of the bitmap. 110 */ 111voidewah_not(struct ewah_bitmap *self); 112 113/** 114 * Call the given callback with the position of every single bit 115 * that has been set on the bitmap. 116 * 117 * This is an efficient operation that does not fully decompress 118 * the bitmap. 119 */ 120voidewah_each_bit(struct ewah_bitmap *self, ewah_callback callback,void*payload); 121 122/** 123 * Set a given bit on the bitmap. 124 * 125 * The bit at position `pos` will be set to true. Because of the 126 * way that the bitmap is compressed, a set bit cannot be unset 127 * later on. 128 * 129 * Furthermore, since the bitmap uses streaming compression, bits 130 * can only set incrementally. 131 * 132 * E.g. 133 * ewah_set(bitmap, 1); // ok 134 * ewah_set(bitmap, 76); // ok 135 * ewah_set(bitmap, 77); // ok 136 * ewah_set(bitmap, 8712800127); // ok 137 * ewah_set(bitmap, 25); // failed, assert raised 138 */ 139voidewah_set(struct ewah_bitmap *self,size_t i); 140 141struct ewah_iterator { 142const eword_t *buffer; 143size_t buffer_size; 144 145size_t pointer; 146 eword_t compressed, literals; 147 eword_t rl, lw; 148int b; 149}; 150 151/** 152 * Initialize a new iterator to run through the bitmap in uncompressed form. 153 * 154 * The iterator can be stack allocated. The underlying bitmap must not be freed 155 * before the iteration is over. 156 * 157 * E.g. 158 * 159 * struct ewah_bitmap *bitmap = ewah_new(); 160 * struct ewah_iterator it; 161 * 162 * ewah_iterator_init(&it, bitmap); 163 */ 164voidewah_iterator_init(struct ewah_iterator *it,struct ewah_bitmap *parent); 165 166/** 167 * Yield every single word in the bitmap in uncompressed form. This is: 168 * yield single words (32-64 bits) where each bit represents an actual 169 * bit from the bitmap. 170 * 171 * Return: true if a word was yield, false if there are no words left 172 */ 173intewah_iterator_next(eword_t *next,struct ewah_iterator *it); 174 175voidewah_or( 176struct ewah_bitmap *ewah_i, 177struct ewah_bitmap *ewah_j, 178struct ewah_bitmap *out); 179 180voidewah_and_not( 181struct ewah_bitmap *ewah_i, 182struct ewah_bitmap *ewah_j, 183struct ewah_bitmap *out); 184 185voidewah_xor( 186struct ewah_bitmap *ewah_i, 187struct ewah_bitmap *ewah_j, 188struct ewah_bitmap *out); 189 190voidewah_and( 191struct ewah_bitmap *ewah_i, 192struct ewah_bitmap *ewah_j, 193struct ewah_bitmap *out); 194 195/** 196 * Direct word access 197 */ 198size_tewah_add_empty_words(struct ewah_bitmap *self,int v,size_t number); 199voidewah_add_dirty_words( 200struct ewah_bitmap *self,const eword_t *buffer,size_t number,int negate); 201size_tewah_add(struct ewah_bitmap *self, eword_t word); 202 203 204/** 205 * Uncompressed, old-school bitmap that can be efficiently compressed 206 * into an `ewah_bitmap`. 207 */ 208struct bitmap { 209 eword_t *words; 210size_t word_alloc; 211}; 212 213struct bitmap *bitmap_new(void); 214voidbitmap_set(struct bitmap *self,size_t pos); 215voidbitmap_clear(struct bitmap *self,size_t pos); 216intbitmap_get(struct bitmap *self,size_t pos); 217voidbitmap_reset(struct bitmap *self); 218voidbitmap_free(struct bitmap *self); 219intbitmap_equals(struct bitmap *self,struct bitmap *other); 220intbitmap_is_subset(struct bitmap *self,struct bitmap *super); 221 222struct ewah_bitmap *bitmap_to_ewah(struct bitmap *bitmap); 223struct bitmap *ewah_to_bitmap(struct ewah_bitmap *ewah); 224 225voidbitmap_and_not(struct bitmap *self,struct bitmap *other); 226voidbitmap_or_ewah(struct bitmap *self,struct ewah_bitmap *other); 227voidbitmap_or(struct bitmap *self,const struct bitmap *other); 228 229voidbitmap_each_bit(struct bitmap *self, ewah_callback callback,void*data); 230size_tbitmap_popcount(struct bitmap *self); 231 232#endif