v8.run/go/exp@v0.0.26-0.20230226010534-afcdbd3f782d/util/mathutil/reduction.go (about)

     1  package mathutil
     2  
     3  // BoundUint32 performs a fast integer reduction,
     4  // For details, see: https://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/
     5  func BoundUint32(x uint32, N uint32) uint32 {
     6  	return uint32((uint64(x) * uint64(N)) >> 32)
     7  }
     8  
     9  // BoundUint16 performs a fast integer reduction,
    10  // For details, see: https://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/
    11  func BoundUint16(x uint16, N uint16) uint16 {
    12  	return uint16((uint32(x) * uint32(N)) >> 16)
    13  }