This is described in http://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/ (I'm looking for a fast Bloom filter implementation, and found your implementation uses modulo which is a bit slow.)