forked from External/ergo
Merge pull request #1231 from slingamn/buffer.2
more memory-efficient implementation of line reading
This commit is contained in:
commit
db100f1f91
7 changed files with 231 additions and 45 deletions
16
irc/utils/math.go
Normal file
16
irc/utils/math.go
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
// Copyright (c) 2020 Shivaram Lingamneni <slingamn@cs.stanford.edu>
|
||||
// released under the MIT license
|
||||
|
||||
package utils
|
||||
|
||||
// return n such that v <= n and n == 2**i for some i
|
||||
func RoundUpToPowerOfTwo(v int) int {
|
||||
// http://graphics.stanford.edu/~seander/bithacks.html
|
||||
v -= 1
|
||||
v |= v >> 1
|
||||
v |= v >> 2
|
||||
v |= v >> 4
|
||||
v |= v >> 8
|
||||
v |= v >> 16
|
||||
return v + 1
|
||||
}
|
||||
19
irc/utils/math_test.go
Normal file
19
irc/utils/math_test.go
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
// Copyright (c) 2020 Shivaram Lingamneni <slingamn@cs.stanford.edu>
|
||||
// released under the MIT license
|
||||
|
||||
package utils
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRoundUp(t *testing.T) {
|
||||
assertEqual(RoundUpToPowerOfTwo(2), 2, t)
|
||||
assertEqual(RoundUpToPowerOfTwo(3), 4, t)
|
||||
assertEqual(RoundUpToPowerOfTwo(64), 64, t)
|
||||
assertEqual(RoundUpToPowerOfTwo(65), 128, t)
|
||||
assertEqual(RoundUpToPowerOfTwo(100), 128, t)
|
||||
assertEqual(RoundUpToPowerOfTwo(1000), 1024, t)
|
||||
assertEqual(RoundUpToPowerOfTwo(1025), 2048, t)
|
||||
assertEqual(RoundUpToPowerOfTwo(269435457), 536870912, t)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue