|
1 | | -package roaring |
2 | | - |
3 | | -// Reuse of portions of go/src/math/big standard lib code |
4 | | -// under this license: |
5 | | -/* |
6 | | -Copyright (c) 2009 The Go Authors. All rights reserved. |
7 | | -
|
8 | | -Redistribution and use in source and binary forms, with or without |
9 | | -modification, are permitted provided that the following conditions are |
10 | | -met: |
11 | | -
|
12 | | - * Redistributions of source code must retain the above copyright |
13 | | -notice, this list of conditions and the following disclaimer. |
14 | | - * Redistributions in binary form must reproduce the above |
15 | | -copyright notice, this list of conditions and the following disclaimer |
16 | | -in the documentation and/or other materials provided with the |
17 | | -distribution. |
18 | | - * Neither the name of Google Inc. nor the names of its |
19 | | -contributors may be used to endorse or promote products derived from |
20 | | -this software without specific prior written permission. |
21 | | -
|
22 | | -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
23 | | -"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
24 | | -LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
25 | | -A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
26 | | -OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
27 | | -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
28 | | -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
29 | | -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
30 | | -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
31 | | -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
32 | | -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
33 | | -*/ |
| 1 | +// +build go1.9 |
| 2 | +// "go1.9", from Go version 1.9 onward |
| 3 | +// See https://golang.org/pkg/go/build/#hdr-Build_Constraints |
34 | 4 |
|
35 | | -const deBruijn32 = 0x077CB531 |
36 | | - |
37 | | -var deBruijn32Lookup = []byte{ |
38 | | - 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8, |
39 | | - 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9, |
40 | | -} |
41 | | - |
42 | | -const deBruijn64 = 0x03f79d71b4ca8b09 |
| 5 | +package roaring |
43 | 6 |
|
44 | | -var deBruijn64Lookup = []byte{ |
45 | | - 0, 1, 56, 2, 57, 49, 28, 3, 61, 58, 42, 50, 38, 29, 17, 4, |
46 | | - 62, 47, 59, 36, 45, 43, 51, 22, 53, 39, 33, 30, 24, 18, 12, 5, |
47 | | - 63, 55, 48, 27, 60, 41, 37, 16, 46, 35, 44, 21, 52, 32, 23, 11, |
48 | | - 54, 26, 40, 15, 34, 20, 31, 10, 25, 14, 19, 9, 13, 8, 7, 6, |
49 | | -} |
| 7 | +import "math/bits" |
50 | 8 |
|
51 | | -// trailingZeroBits returns the number of consecutive least significant zero |
52 | | -// bits of x. |
53 | | -func countTrailingZerosDeBruijn(x uint64) int { |
54 | | - // x & -x leaves only the right-most bit set in the word. Let k be the |
55 | | - // index of that bit. Since only a single bit is set, the value is two |
56 | | - // to the power of k. Multiplying by a power of two is equivalent to |
57 | | - // left shifting, in this case by k bits. The de Bruijn constant is |
58 | | - // such that all six bit, consecutive substrings are distinct. |
59 | | - // Therefore, if we have a left shifted version of this constant we can |
60 | | - // find by how many bits it was shifted by looking at which six bit |
61 | | - // substring ended up at the top of the word. |
62 | | - // (Knuth, volume 4, section 7.3.1) |
63 | | - if x == 0 { |
64 | | - // We have to special case 0; the fomula |
65 | | - // below doesn't work for 0. |
66 | | - return 64 |
67 | | - } |
68 | | - return int(deBruijn64Lookup[((x&-x)*(deBruijn64))>>58]) |
| 9 | +func countTrailingZeros(x uint64) int { |
| 10 | + return bits.TrailingZeros64(x) |
69 | 11 | } |
0 commit comments