github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/vendor_skip/go.mongodb.org/mongo-driver/internal/randutil/rand/bits.go (about)

     1  // Copied from https://cs.opensource.google/go/go/+/946b4baaf6521d521928500b2b57429c149854e7:src/math/bits.go
     2  
     3  // Copyright 2017 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  package rand
     8  
     9  // Add64 returns the sum with carry of x, y and carry: sum = x + y + carry.
    10  // The carry input must be 0 or 1; otherwise the behavior is undefined.
    11  // The carryOut output is guaranteed to be 0 or 1.
    12  func Add64(x, y, carry uint64) (sum, carryOut uint64) {
    13  	yc := y + carry
    14  	sum = x + yc
    15  	if sum < x || yc < y {
    16  		carryOut = 1
    17  	}
    18  	return
    19  }
    20  
    21  // Mul64 returns the 128-bit product of x and y: (hi, lo) = x * y
    22  // with the product bits' upper half returned in hi and the lower
    23  // half returned in lo.
    24  func Mul64(x, y uint64) (hi, lo uint64) {
    25  	const mask32 = 1<<32 - 1
    26  	x0 := x & mask32
    27  	x1 := x >> 32
    28  	y0 := y & mask32
    29  	y1 := y >> 32
    30  	w0 := x0 * y0
    31  	t := x1*y0 + w0>>32
    32  	w1 := t & mask32
    33  	w2 := t >> 32
    34  	w1 += x0 * y1
    35  	hi = x1*y1 + w2 + w1>>32
    36  	lo = x * y
    37  	return
    38  }