github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/bench/growslice/misc.go (about)

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  const MaxUintptr = ^uintptr(0)
     8  
     9  // MulUintptr returns a * b and whether the multiplication overflowed.
    10  // On supported platforms this is an intrinsic lowered by the compiler.
    11  func math_MulUintptr(a, b uintptr) (uintptr, bool) {
    12  	if a|b < 1<<(4*goarch_PtrSize) || a == 0 {
    13  		return a * b, false
    14  	}
    15  	overflow := b > MaxUintptr/a
    16  	return a * b, overflow
    17  }
    18  
    19  // alignUp rounds n up to a multiple of a. a must be a power of 2.
    20  func alignUp(n, a uintptr) uintptr {
    21  	return (n + a - 1) &^ (a - 1)
    22  }
    23  
    24  // alignDown rounds n down to a multiple of a. a must be a power of 2.
    25  func alignDown(n, a uintptr) uintptr {
    26  	return n &^ (a - 1)
    27  }
    28  
    29  // divRoundUp returns ceil(n / a).
    30  func divRoundUp(n, a uintptr) uintptr {
    31  	// a is generally a power of two. This will get inlined and
    32  	// the compiler will optimize the division.
    33  	return (n + a - 1) / a
    34  }
    35  
    36  func isPowerOfTwo(x uintptr) bool {
    37  	return x&(x-1) == 0
    38  }