Skip to content

Commit

Permalink
Fix overflow handling
Browse files Browse the repository at this point in the history
  • Loading branch information
betatim committed Jan 31, 2017
1 parent b26b242 commit f422e07
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions lib/storage.hh
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,8 @@ public:
const uint64_t idx = _table_index(khash, _tablesizes[i]);
const uint8_t mask = _mask(khash, _tablesizes[i]);
const uint8_t shift = _shift(khash, _tablesizes[i]);
const uint8_t current_count = (table[idx] & mask) >> shift;
uint8_t current_tbl = table[idx];
uint8_t current_count = (current_tbl & mask) >> shift;

if (!is_new_kmer) {
if (current_count == 0) {
Expand All @@ -340,8 +341,18 @@ public:
}

// increase count, no checking for overflow
const uint8_t new_count = (current_count + 1) << shift;
table[idx] = (table[idx] & ~mask) | (new_count & mask);
uint8_t new_count = (current_count + 1) << shift;
uint8_t new_tbl = (current_tbl & ~mask) | (new_count & mask);

while(!table[idx].compare_exchange_weak(current_tbl, new_tbl)) {
current_count = (current_tbl & mask) >> shift;
new_count = (current_count + 1);
if (new_count > _max_count) {
break;
}
new_count <<= shift;
new_tbl = (current_tbl & ~mask) | (new_count & mask);
}
}

if (is_new_kmer) {
Expand Down

0 comments on commit f422e07

Please sign in to comment.