github.com/thanos-io/thanos@v0.32.5/pkg/cacheutil/jump_hash.go (about)

     1  // Copyright (c) The Thanos Authors.
     2  // Licensed under the Apache License 2.0.
     3  
     4  package cacheutil
     5  
     6  // jumpHash consistently chooses a hash bucket number in the range
     7  // [0, numBuckets) for the given key. numBuckets must be >= 1.
     8  //
     9  // Copied from github.com/dgryski/go-jump/blob/master/jump.go (MIT license).
    10  func jumpHash(key uint64, numBuckets int) int32 {
    11  	var b int64 = -1
    12  	var j int64
    13  
    14  	for j < int64(numBuckets) {
    15  		b = j
    16  		key = key*2862933555777941757 + 1
    17  		j = int64(float64(b+1) * (float64(int64(1)<<31) / float64((key>>33)+1)))
    18  	}
    19  
    20  	return int32(b)
    21  }