github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/dbnode/encoding/encoding.go (about) 1 // Copyright (c) 2016 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package encoding 22 23 import "math/bits" 24 25 // Bit is just a byte. 26 type Bit byte 27 28 // NumSig returns the number of significant bits in a uint64. 29 func NumSig(v uint64) uint8 { 30 return uint8(64 - bits.LeadingZeros64(v)) 31 } 32 33 // LeadingAndTrailingZeros calculates the number of leading and trailing 0s 34 // for a uint64. 35 func LeadingAndTrailingZeros(v uint64) (int, int) { 36 if v == 0 { 37 return 64, 0 38 } 39 40 numLeading := bits.LeadingZeros64(v) 41 numTrailing := bits.TrailingZeros64(v) 42 return numLeading, numTrailing 43 } 44 45 // SignExtend sign extends the highest bit of v which has numBits (<=64). 46 func SignExtend(v uint64, numBits uint8) int64 { 47 shift := 64 - numBits 48 return (int64(v) << shift) >> shift 49 }