diff options
Diffstat (limited to 'pkg/bits')
-rw-r--r-- | pkg/bits/bits.go | 16 | ||||
-rwxr-xr-x | pkg/bits/bits32.go | 25 | ||||
-rwxr-xr-x | pkg/bits/bits64.go | 25 | ||||
-rwxr-xr-x | pkg/bits/bits_state_autogen.go | 4 | ||||
-rw-r--r-- | pkg/bits/uint64_arch_amd64.go | 36 | ||||
-rw-r--r-- | pkg/bits/uint64_arch_amd64_asm.s | 31 | ||||
-rw-r--r-- | pkg/bits/uint64_arch_generic.go | 55 |
7 files changed, 192 insertions, 0 deletions
diff --git a/pkg/bits/bits.go b/pkg/bits/bits.go new file mode 100644 index 000000000..a26433ad6 --- /dev/null +++ b/pkg/bits/bits.go @@ -0,0 +1,16 @@ +// Copyright 2018 The gVisor Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Package bits includes all bit related types and operations. +package bits diff --git a/pkg/bits/bits32.go b/pkg/bits/bits32.go new file mode 100755 index 000000000..4e9e45dce --- /dev/null +++ b/pkg/bits/bits32.go @@ -0,0 +1,25 @@ +package bits + +// IsOn returns true if *all* bits set in 'bits' are set in 'mask'. +func IsOn32(mask, bits uint32) bool { + return mask&bits == bits +} + +// IsAnyOn returns true if *any* bit set in 'bits' is set in 'mask'. +func IsAnyOn32(mask, bits uint32) bool { + return mask&bits != 0 +} + +// Mask returns a T with all of the given bits set. +func Mask32(is ...int) uint32 { + ret := uint32(0) + for _, i := range is { + ret |= MaskOf32(i) + } + return ret +} + +// MaskOf is like Mask, but sets only a single bit (more efficiently). +func MaskOf32(i int) uint32 { + return uint32(1) << uint32(i) +} diff --git a/pkg/bits/bits64.go b/pkg/bits/bits64.go new file mode 100755 index 000000000..f49158792 --- /dev/null +++ b/pkg/bits/bits64.go @@ -0,0 +1,25 @@ +package bits + +// IsOn returns true if *all* bits set in 'bits' are set in 'mask'. +func IsOn64(mask, bits uint64) bool { + return mask&bits == bits +} + +// IsAnyOn returns true if *any* bit set in 'bits' is set in 'mask'. +func IsAnyOn64(mask, bits uint64) bool { + return mask&bits != 0 +} + +// Mask returns a T with all of the given bits set. +func Mask64(is ...int) uint64 { + ret := uint64(0) + for _, i := range is { + ret |= MaskOf64(i) + } + return ret +} + +// MaskOf is like Mask, but sets only a single bit (more efficiently). +func MaskOf64(i int) uint64 { + return uint64(1) << uint64(i) +} diff --git a/pkg/bits/bits_state_autogen.go b/pkg/bits/bits_state_autogen.go new file mode 100755 index 000000000..2abb1291b --- /dev/null +++ b/pkg/bits/bits_state_autogen.go @@ -0,0 +1,4 @@ +// automatically generated by stateify. + +package bits + diff --git a/pkg/bits/uint64_arch_amd64.go b/pkg/bits/uint64_arch_amd64.go new file mode 100644 index 000000000..faccaa61a --- /dev/null +++ b/pkg/bits/uint64_arch_amd64.go @@ -0,0 +1,36 @@ +// Copyright 2018 The gVisor Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// +build amd64 + +package bits + +// TrailingZeros64 returns the number of bits before the least significant 1 +// bit in x; in other words, it returns the index of the least significant 1 +// bit in x. If x is 0, TrailingZeros64 returns 64. +func TrailingZeros64(x uint64) int + +// MostSignificantOne64 returns the index of the most significant 1 bit in +// x. If x is 0, MostSignificantOne64 returns 64. +func MostSignificantOne64(x uint64) int + +// ForEachSetBit64 calls f once for each set bit in x, with argument i equal to +// the set bit's index. +func ForEachSetBit64(x uint64, f func(i int)) { + for x != 0 { + i := TrailingZeros64(x) + f(i) + x &^= MaskOf64(i) + } +} diff --git a/pkg/bits/uint64_arch_amd64_asm.s b/pkg/bits/uint64_arch_amd64_asm.s new file mode 100644 index 000000000..8ff364181 --- /dev/null +++ b/pkg/bits/uint64_arch_amd64_asm.s @@ -0,0 +1,31 @@ +// Copyright 2018 The gVisor Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// +build amd64 + +TEXT ·TrailingZeros64(SB),$0-16 + BSFQ x+0(FP), AX + JNZ end + MOVQ $64, AX +end: + MOVQ AX, ret+8(FP) + RET + +TEXT ·MostSignificantOne64(SB),$0-16 + BSRQ x+0(FP), AX + JNZ end + MOVQ $64, AX +end: + MOVQ AX, ret+8(FP) + RET diff --git a/pkg/bits/uint64_arch_generic.go b/pkg/bits/uint64_arch_generic.go new file mode 100644 index 000000000..7dd2d1480 --- /dev/null +++ b/pkg/bits/uint64_arch_generic.go @@ -0,0 +1,55 @@ +// Copyright 2018 The gVisor Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// +build !amd64 + +package bits + +// TrailingZeros64 returns the number of bits before the least significant 1 +// bit in x; in other words, it returns the index of the least significant 1 +// bit in x. If x is 0, TrailingZeros64 returns 64. +func TrailingZeros64(x uint64) int { + if x == 0 { + return 64 + } + i := 0 + for ; x&1 == 0; i++ { + x >>= 1 + } + return i +} + +// MostSignificantOne64 returns the index of the most significant 1 bit in +// x. If x is 0, MostSignificantOne64 returns 64. +func MostSignificantOne64(x uint64) int { + if x == 0 { + return 64 + } + i := 63 + for ; x&(1<<63) == 0; i-- { + x <<= 1 + } + return i +} + +// ForEachSetBit64 calls f once for each set bit in x, with argument i equal to +// the set bit's index. +func ForEachSetBit64(x uint64, f func(i int)) { + for i := 0; x != 0; i++ { + if x&1 != 0 { + f(i) + } + x >>= 1 + } +} |