Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/ewah/ewah_bitmap.c # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / ewah / ewah_bitmap.c` 508 lines ยท 11452 bytes Raw Blame History 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, see . 18 */ 19 20 #include "git-compat-util.h" 21 #include "ewok.h" 22 #include "ewok_rlw.h" 23 24 static inline size_t min_size(size_t a, size_t b) 25 { 26 return a < b ? a : b; 27 } 28 29 static inline size_t max_size(size_t a, size_t b) 30 { 31 return a > b ? a : b; 32 } 33 34 static inline void buffer_grow(struct ewah_bitmap *self, size_t new_size) 35 { 36 size_t rlw_offset = (uint8_t *)self->rlw - (uint8_t *)self->buffer; 37 ALLOC_GROW(self->buffer, new_size, self->alloc_size); 38 self->rlw = self->buffer + (rlw_offset / sizeof(eword_t)); 39 } 40 41 static inline void buffer_push(struct ewah_bitmap *self, eword_t value) 42 { 43 buffer_grow(self, self->buffer_size + 1); 44 self->buffer[self->buffer_size++] = value; 45 } 46 47 static void buffer_push_rlw(struct ewah_bitmap *self, eword_t value) 48 { 49 buffer_push(self, value); 50 self->rlw = self->buffer + self->buffer_size - 1; 51 } 52 53 static size_t add_empty_words(struct ewah_bitmap *self, int v, size_t number) 54 { 55 size_t added = 0; 56 eword_t runlen, can_add; 57 58 if (rlw_get_run_bit(self->rlw) != v && rlw_size(self->rlw) == 0) { 59 rlw_set_run_bit(self->rlw, v); 60 } else if (rlw_get_literal_words(self->rlw) != 0 || 61 rlw_get_run_bit(self->rlw) != v) { 62 buffer_push_rlw(self, 0); 63 if (v) rlw_set_run_bit(self->rlw, v); 64 added++; 65 } 66 67 runlen = rlw_get_running_len(self->rlw); 68 can_add = min_size(number, RLW_LARGEST_RUNNING_COUNT - runlen); 69 70 rlw_set_running_len(self->rlw, runlen + can_add); 71 number -= can_add; 72 73 while (number >= RLW_LARGEST_RUNNING_COUNT) { 74 buffer_push_rlw(self, 0); 75 added++; 76 if (v) rlw_set_run_bit(self->rlw, v); 77 rlw_set_running_len(self->rlw, RLW_LARGEST_RUNNING_COUNT); 78 number -= RLW_LARGEST_RUNNING_COUNT; 79 } 80 81 if (number > 0) { 82 buffer_push_rlw(self, 0); 83 added++; 84 85 if (v) rlw_set_run_bit(self->rlw, v); 86 rlw_set_running_len(self->rlw, number); 87 } 88 89 return added; 90 } 91 92 size_t ewah_add_empty_words(struct ewah_bitmap *self, int v, size_t number) 93 { 94 if (number == 0) 95 return 0; 96 97 self->bit_size += number * BITS_IN_EWORD; 98 return add_empty_words(self, v, number); 99 } 100 101 static size_t add_literal(struct ewah_bitmap *self, eword_t new_data) 102 { 103 eword_t current_num = rlw_get_literal_words(self->rlw); 104 105 if (current_num >= RLW_LARGEST_LITERAL_COUNT) { 106 buffer_push_rlw(self, 0); 107 108 rlw_set_literal_words(self->rlw, 1); 109 buffer_push(self, new_data); 110 return 2; 111 } 112 113 rlw_set_literal_words(self->rlw, current_num + 1); 114 115 /* sanity check */ 116 assert(rlw_get_literal_words(self->rlw) == current_num + 1); 117 118 buffer_push(self, new_data); 119 return 1; 120 } 121 122 void ewah_add_dirty_words( 123 struct ewah_bitmap *self, const eword_t *buffer, 124 size_t number, int negate) 125 { 126 size_t literals, can_add; 127 128 while (1) { 129 literals = rlw_get_literal_words(self->rlw); 130 can_add = min_size(number, RLW_LARGEST_LITERAL_COUNT - literals); 131 132 rlw_set_literal_words(self->rlw, literals + can_add); 133 134 buffer_grow(self, self->buffer_size + can_add); 135 136 if (negate) { 137 size_t i; 138 for (i = 0; i < can_add; ++i) 139 self->buffer[self->buffer_size++] = ~buffer[i]; 140 } else { 141 memcpy(self->buffer + self->buffer_size, 142 buffer, can_add * sizeof(eword_t)); 143 self->buffer_size += can_add; 144 } 145 146 self->bit_size += can_add * BITS_IN_EWORD; 147 148 if (number - can_add == 0) 149 break; 150 151 buffer_push_rlw(self, 0); 152 buffer += can_add; 153 number -= can_add; 154 } 155 } 156 157 static size_t add_empty_word(struct ewah_bitmap *self, int v) 158 { 159 int no_literal = (rlw_get_literal_words(self->rlw) == 0); 160 eword_t run_len = rlw_get_running_len(self->rlw); 161 162 if (no_literal && run_len == 0) { 163 rlw_set_run_bit(self->rlw, v); 164 assert(rlw_get_run_bit(self->rlw) == v); 165 } 166 167 if (no_literal && rlw_get_run_bit(self->rlw) == v && 168 run_len < RLW_LARGEST_RUNNING_COUNT) { 169 rlw_set_running_len(self->rlw, run_len + 1); 170 assert(rlw_get_running_len(self->rlw) == run_len + 1); 171 return 0; 172 } else { 173 buffer_push_rlw(self, 0); 174 175 assert(rlw_get_running_len(self->rlw) == 0); 176 assert(rlw_get_run_bit(self->rlw) == 0); 177 assert(rlw_get_literal_words(self->rlw) == 0); 178 179 rlw_set_run_bit(self->rlw, v); 180 assert(rlw_get_run_bit(self->rlw) == v); 181 182 rlw_set_running_len(self->rlw, 1); 183 assert(rlw_get_running_len(self->rlw) == 1); 184 assert(rlw_get_literal_words(self->rlw) == 0); 185 return 1; 186 } 187 } 188 189 size_t ewah_add(struct ewah_bitmap *self, eword_t word) 190 { 191 self->bit_size += BITS_IN_EWORD; 192 193 if (word == 0) 194 return add_empty_word(self, 0); 195 196 if (word == (eword_t)(~0)) 197 return add_empty_word(self, 1); 198 199 return add_literal(self, word); 200 } 201 202 void ewah_set(struct ewah_bitmap *self, size_t i) 203 { 204 const size_t dist = 205 DIV_ROUND_UP(i + 1, BITS_IN_EWORD) - 206 DIV_ROUND_UP(self->bit_size, BITS_IN_EWORD); 207 208 assert(i >= self->bit_size); 209 210 self->bit_size = i + 1; 211 212 if (dist > 0) { 213 if (dist > 1) 214 add_empty_words(self, 0, dist - 1); 215 216 add_literal(self, (eword_t)1 << (i % BITS_IN_EWORD)); 217 return; 218 } 219 220 if (rlw_get_literal_words(self->rlw) == 0) { 221 rlw_set_running_len(self->rlw, 222 rlw_get_running_len(self->rlw) - 1); 223 add_literal(self, (eword_t)1 << (i % BITS_IN_EWORD)); 224 return; 225 } 226 227 self->buffer[self->buffer_size - 1] |= 228 ((eword_t)1 << (i % BITS_IN_EWORD)); 229 230 /* check if we just completed a stream of 1s */ 231 if (self->buffer[self->buffer_size - 1] == (eword_t)(~0)) { 232 self->buffer[--self->buffer_size] = 0; 233 rlw_set_literal_words(self->rlw, 234 rlw_get_literal_words(self->rlw) - 1); 235 add_empty_word(self, 1); 236 } 237 } 238 239 void ewah_each_bit(struct ewah_bitmap *self, void (*callback)(size_t, void*), void *payload) 240 { 241 size_t pos = 0; 242 size_t pointer = 0; 243 size_t k; 244 245 while (pointer < self->buffer_size) { 246 eword_t *word = &self->buffer[pointer]; 247 248 if (rlw_get_run_bit(word)) { 249 size_t len = rlw_get_running_len(word) * BITS_IN_EWORD; 250 for (k = 0; k < len; ++k, ++pos) 251 callback(pos, payload); 252 } else { 253 pos += rlw_get_running_len(word) * BITS_IN_EWORD; 254 } 255 256 ++pointer; 257 258 for (k = 0; k < rlw_get_literal_words(word); ++k) { 259 /* todo: zero count optimization */ 260 for (size_t c = 0; c < BITS_IN_EWORD; ++c, ++pos) { 261 if ((self->buffer[pointer] & ((eword_t)1 << c)) != 0) 262 callback(pos, payload); 263 } 264 265 ++pointer; 266 } 267 } 268 } 269 270 /** 271 * Clear all the bits in the bitmap. Does not free or resize 272 * memory. 273 */ 274 static void ewah_clear(struct ewah_bitmap *self) 275 { 276 self->buffer_size = 1; 277 self->buffer[0] = 0; 278 self->bit_size = 0; 279 self->rlw = self->buffer; 280 } 281 282 struct ewah_bitmap *ewah_new(void) 283 { 284 struct ewah_bitmap *self; 285 286 self = xmalloc(sizeof(struct ewah_bitmap)); 287 self->alloc_size = 32; 288 ALLOC_ARRAY(self->buffer, self->alloc_size); 289 290 ewah_clear(self); 291 return self; 292 } 293 294 void ewah_free(struct ewah_bitmap *self) 295 { 296 if (!self) 297 return; 298 299 if (self->alloc_size) 300 free(self->buffer); 301 302 free(self); 303 } 304 305 static void read_new_rlw(struct ewah_iterator *it) 306 { 307 const eword_t *word = NULL; 308 309 it->literals = 0; 310 it->compressed = 0; 311 312 while (1) { 313 word = &it->buffer[it->pointer]; 314 315 it->rl = rlw_get_running_len(word); 316 it->lw = rlw_get_literal_words(word); 317 it->b = rlw_get_run_bit(word); 318 319 if (it->rl || it->lw) 320 return; 321 322 if (it->pointer < it->buffer_size - 1) { 323 it->pointer++; 324 } else { 325 it->pointer = it->buffer_size; 326 return; 327 } 328 } 329 } 330 331 int ewah_iterator_next(eword_t *next, struct ewah_iterator *it) 332 { 333 if (it->pointer >= it->buffer_size) 334 return 0; 335 336 if (it->compressed < it->rl) { 337 it->compressed++; 338 *next = it->b ? (eword_t)(~0) : 0; 339 } else { 340 assert(it->literals < it->lw); 341 342 it->literals++; 343 it->pointer++; 344 345 assert(it->pointer < it->buffer_size); 346 347 *next = it->buffer[it->pointer]; 348 } 349 350 if (it->compressed == it->rl && it->literals == it->lw) { 351 if (++it->pointer < it->buffer_size) 352 read_new_rlw(it); 353 } 354 355 return 1; 356 } 357 358 void ewah_iterator_init(struct ewah_iterator *it, struct ewah_bitmap *parent) 359 { 360 it->buffer = parent->buffer; 361 it->buffer_size = parent->buffer_size; 362 it->pointer = 0; 363 364 it->lw = 0; 365 it->rl = 0; 366 it->compressed = 0; 367 it->literals = 0; 368 it->b = 0; 369 370 if (it->pointer < it->buffer_size) 371 read_new_rlw(it); 372 } 373 374 void ewah_or_iterator_init(struct ewah_or_iterator *it, 375 struct ewah_bitmap **parents, size_t nr) 376 { 377 size_t i; 378 379 memset(it, 0, sizeof(*it)); 380 381 ALLOC_ARRAY(it->its, nr); 382 for (i = 0; i < nr; i++) 383 ewah_iterator_init(&it->its[it->nr++], parents[i]); 384 } 385 386 int ewah_or_iterator_next(eword_t *next, struct ewah_or_iterator *it) 387 { 388 eword_t buf, out = 0; 389 size_t i; 390 int ret = 0; 391 392 for (i = 0; i < it->nr; i++) 393 if (ewah_iterator_next(&buf, &it->its[i])) { 394 out |= buf; 395 ret = 1; 396 } 397 398 *next = out; 399 return ret; 400 } 401 402 void ewah_or_iterator_release(struct ewah_or_iterator *it) 403 { 404 free(it->its); 405 } 406 407 void ewah_xor( 408 struct ewah_bitmap *ewah_i, 409 struct ewah_bitmap *ewah_j, 410 struct ewah_bitmap *out) 411 { 412 struct rlw_iterator rlw_i; 413 struct rlw_iterator rlw_j; 414 size_t literals; 415 416 rlwit_init(&rlw_i, ewah_i); 417 rlwit_init(&rlw_j, ewah_j); 418 419 while (rlwit_word_size(&rlw_i) > 0 && rlwit_word_size(&rlw_j) > 0) { 420 while (rlw_i.rlw.running_len > 0 || rlw_j.rlw.running_len > 0) { 421 struct rlw_iterator *prey, *predator; 422 size_t index; 423 int negate_words; 424 425 if (rlw_i.rlw.running_len < rlw_j.rlw.running_len) { 426 prey = &rlw_i; 427 predator = &rlw_j; 428 } else { 429 prey = &rlw_j; 430 predator = &rlw_i; 431 } 432 433 negate_words = !!predator->rlw.running_bit; 434 index = rlwit_discharge(prey, out, 435 predator->rlw.running_len, negate_words); 436 437 ewah_add_empty_words(out, negate_words, 438 predator->rlw.running_len - index); 439 440 rlwit_discard_first_words(predator, 441 predator->rlw.running_len); 442 } 443 444 literals = min_size( 445 rlw_i.rlw.literal_words, 446 rlw_j.rlw.literal_words); 447 448 if (literals) { 449 size_t k; 450 451 for (k = 0; k < literals; ++k) { 452 ewah_add(out, 453 rlw_i.buffer[rlw_i.literal_word_start + k] ^ 454 rlw_j.buffer[rlw_j.literal_word_start + k] 455 ); 456 } 457 458 rlwit_discard_first_words(&rlw_i, literals); 459 rlwit_discard_first_words(&rlw_j, literals); 460 } 461 } 462 463 if (rlwit_word_size(&rlw_i) > 0) 464 rlwit_discharge(&rlw_i, out, ~0, 0); 465 else 466 rlwit_discharge(&rlw_j, out, ~0, 0); 467 468 out->bit_size = max_size(ewah_i->bit_size, ewah_j->bit_size); 469 } 470 471 #define BITMAP_POOL_MAX 16 472 static struct ewah_bitmap *bitmap_pool[BITMAP_POOL_MAX]; 473 static size_t bitmap_pool_size; 474 475 struct ewah_bitmap *ewah_pool_new(void) 476 { 477 if (bitmap_pool_size) 478 return bitmap_pool[--bitmap_pool_size]; 479 480 return ewah_new(); 481 } 482 483 void ewah_pool_free(struct ewah_bitmap *self) 484 { 485 if (!self) 486 return; 487 488 if (bitmap_pool_size == BITMAP_POOL_MAX || 489 self->alloc_size == 0) { 490 ewah_free(self); 491 return; 492 } 493 494 ewah_clear(self); 495 bitmap_pool[bitmap_pool_size++] = self; 496 } 497 498 uint32_t ewah_checksum(struct ewah_bitmap *self) 499 { 500 const uint8_t *p = (uint8_t *)self->buffer; 501 uint32_t crc = (uint32_t)self->bit_size; 502 size_t size = self->buffer_size * sizeof(eword_t); 503 504 while (size--) 505 crc = (crc << 5) - crc + (uint32_t)*p++; 506 507 return crc; 508 }