YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/gutil/bits.cc
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2002 and onwards Google Inc.
2
//
3
// The following only applies to changes made to this file as part of YugaByte development.
4
//
5
// Portions Copyright (c) YugaByte, Inc.
6
//
7
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8
// in compliance with the License.  You may obtain a copy of the License at
9
//
10
// http://www.apache.org/licenses/LICENSE-2.0
11
//
12
// Unless required by applicable law or agreed to in writing, software distributed under the License
13
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14
// or implied.  See the License for the specific language governing permissions and limitations
15
// under the License.
16
//
17
// Derived from code by Moses Charikar
18
19
#include "yb/gutil/bits.h"
20
21
#include <assert.h>
22
23
// this array gives the number of bits for any number from 0 to 255
24
// (We could make these ints.  The tradeoff is size (eg does it overwhelm
25
// the cache?) vs efficiency in referencing sub-word-sized array elements)
26
const char Bits::num_bits[] = {
27
  0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
28
  1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
29
  1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
30
  2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
31
  1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
32
  2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
33
  2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
34
  3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
35
  1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
36
  2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
37
  2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
38
  3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
39
  2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
40
  3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
41
  3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
42
  4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8 };
43
44
0
int Bits::Count(const void *m, int num_bytes) {
45
0
  int nbits = 0;
46
0
  const uint8 *s = (const uint8 *) m;
47
0
  for (int i = 0; i < num_bytes; i++)
48
0
    nbits += num_bits[*s++];
49
0
  return nbits;
50
0
}
51
52
0
int Bits::Difference(const void *m1, const void *m2, int num_bytes) {
53
0
  int nbits = 0;
54
0
  const uint8 *s1 = (const uint8 *) m1;
55
0
  const uint8 *s2 = (const uint8 *) m2;
56
0
  for (int i = 0; i < num_bytes; i++)
57
0
    nbits += num_bits[(*s1++) ^ (*s2++)];
58
0
  return nbits;
59
0
}
60
61
int Bits::CappedDifference(const void *m1, const void *m2,
62
0
                           int num_bytes, int cap) {
63
0
  int nbits = 0;
64
0
  const uint8 *s1 = (const uint8 *) m1;
65
0
  const uint8 *s2 = (const uint8 *) m2;
66
0
  for (int i = 0; i < num_bytes && nbits <= cap; i++)
67
0
    nbits += num_bits[(*s1++) ^ (*s2++)];
68
0
  return nbits;
69
0
}
70
71
0
int Bits::Log2Floor_Portable(uint32 n) {
72
0
  if (n == 0)
73
0
    return -1;
74
0
  int log = 0;
75
0
  uint32 value = n;
76
0
  for (int i = 4; i >= 0; --i) {
77
0
    int shift = (1 << i);
78
0
    uint32 x = value >> shift;
79
0
    if (x != 0) {
80
0
      value = x;
81
0
      log += shift;
82
0
    }
83
0
  }
84
0
  assert(value == 1);
85
0
  return log;
86
0
}
87
88
7.30M
int Bits::Log2Ceiling(uint32 n) {
89
7.30M
  int floor = Log2Floor(n);
90
7.30M
  if (n == (n &~ (n - 1)))              // zero or a power of two
91
0
    return floor;
92
7.30M
  else
93
7.30M
    return floor + 1;
94
7.30M
}
95
96
271M
int Bits::Log2Ceiling64(uint64 n) {
97
271M
  int floor = Log2Floor64(n);
98
271M
  if (n == (n &~ (n - 1)))              // zero or a power of two
99
0
    return floor;
100
271M
  else
101
271M
    return floor + 1;
102
271M
}
103
104
0
int Bits::FindLSBSetNonZero_Portable(uint32 n) {
105
0
  int rc = 31;
106
0
  for (int i = 4, shift = 1 << 4; i >= 0; --i) {
107
0
    const uint32 x = n << shift;
108
0
    if (x != 0) {
109
0
      n = x;
110
0
      rc -= shift;
111
0
    }
112
0
    shift >>= 1;
113
0
  }
114
0
  return rc;
115
0
}