Skip to content

Commit

Permalink
hash_pair: Ensure pair order is consistent for hashing.
Browse files Browse the repository at this point in the history
  • Loading branch information
silentbicycle committed Jan 5, 2024
1 parent dfab0de commit 9cba560
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/libfsm/determinise.c
Original file line number Diff line number Diff line change
Expand Up @@ -1659,10 +1659,14 @@ hash_pair(fsm_state_t a, fsm_state_t b)
assert(a != b);
assert(a & RESULT_BIT);
assert(b & RESULT_BIT);
a &=~ RESULT_BIT;
b &=~ RESULT_BIT;
assert(a != b);
const uint64_t ab = ((uint64_t)a << 32) | (uint64_t)b;
const uint64_t ma = (uint64_t)(a & ~RESULT_BIT); /* m: masked */
const uint64_t mb = (uint64_t)(b & ~RESULT_BIT);
assert(ma != mb);

/* Left-shift the smaller ID, so the pair order is consistent for hashing. */
const uint64_t ab = (ma < mb)
? ((ma << 32) | mb)
: ((mb << 32) | ma);
const uint64_t res = hash_id(ab);
/* fprintf(stderr, "%s: a %d, b %d -> %016lx\n", __func__, a, b, res); */
return res;
Expand Down

0 comments on commit 9cba560

Please sign in to comment.