1 Star 0 Fork 0

wkk/self

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
math.go 999 Bytes
一键复制 编辑 原始数据 按行查看 历史
1231 提交于 2021-08-02 10:08 . 更新gnet到1.5.0
// Copyright (c) 2017-2019 Sergey Kamardin <gobwas@gmail.com>
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package internal
const (
bitsize = 32 << (^uint(0) >> 63)
maxintHeadBit = 1 << (bitsize - 2)
)
// IsPowerOfTwo reports whether given integer is a power of two.
func IsPowerOfTwo(n int) bool {
return n&(n-1) == 0
}
// CeilToPowerOfTwo returns the least power of two integer value greater than
// or equal to n.
func CeilToPowerOfTwo(n int) int {
if n&maxintHeadBit != 0 && n > maxintHeadBit {
panic("argument is too large")
}
if n <= 2 {
return 2
}
n--
n = fillBits(n)
n++
return n
}
// FloorToPowerOfTwo returns the greatest power of two integer value less than
// or equal to n.
func FloorToPowerOfTwo(n int) int {
if n <= 2 {
return 2
}
n = fillBits(n)
n >>= 1
n++
return n
}
func fillBits(n int) int {
n |= n >> 1
n |= n >> 2
n |= n >> 4
n |= n >> 8
n |= n >> 16
n |= n >> 32
return n
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/wkkcool/self.git
git@gitee.com:wkkcool/self.git
wkkcool
self
self
v0.2.0

搜索帮助