|
| 1 | +// Copyright (C) 2019-2020 Zilliz. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance |
| 4 | +// with the License. You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software distributed under the License |
| 9 | +// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 10 | +// or implied. See the License for the specific language governing permissions and limitations under the License. |
| 11 | + |
| 12 | +#include <cstring> |
| 13 | +#include <string> |
| 14 | +#include <memory> |
| 15 | +#include "Bitset2.h" |
| 16 | +#include "BitsetView.h" |
| 17 | + |
| 18 | +namespace faiss { |
| 19 | + |
| 20 | +ConcurrentBitset2& |
| 21 | +ConcurrentBitset2::operator&=(const ConcurrentBitset2& bitset) { |
| 22 | + auto u8_1 = mutable_data(); |
| 23 | + auto u8_2 = bitset.data(); |
| 24 | + auto u64_1 = reinterpret_cast<uint64_t*>(u8_1); |
| 25 | + auto u64_2 = reinterpret_cast<const uint64_t*>(u8_2); |
| 26 | + |
| 27 | + size_t n64 = bitset_.size(); |
| 28 | +// size_t n64 = n8 / 8; |
| 29 | + |
| 30 | + for (size_t i = 0; i < n64; i++) { |
| 31 | + u64_1[i] &= u64_2[i]; |
| 32 | + } |
| 33 | + |
| 34 | + return *this; |
| 35 | +} |
| 36 | + |
| 37 | +ConcurrentBitset2& |
| 38 | +ConcurrentBitset2::operator&=(const BitsetView& view) { |
| 39 | + auto u8_1 = mutable_data(); |
| 40 | + auto u8_2 = view.data(); |
| 41 | + auto u64_1 = reinterpret_cast<uint64_t*>(u8_1); |
| 42 | + auto u64_2 = reinterpret_cast<const uint64_t*>(u8_2); |
| 43 | + |
| 44 | + size_t n64 = bitset_.size(); |
| 45 | + |
| 46 | + for (size_t i = 0; i < n64; i++) { |
| 47 | + u64_1[i] &= u64_2[i]; |
| 48 | + } |
| 49 | + |
| 50 | + return *this; |
| 51 | +} |
| 52 | + |
| 53 | +std::shared_ptr<ConcurrentBitset2> |
| 54 | +ConcurrentBitset2::operator&(const ConcurrentBitset2& bitset) const { |
| 55 | +auto result_bitset = std::make_shared<ConcurrentBitset2>(bitset.size()); |
| 56 | + |
| 57 | +//auto result_8 = |
| 58 | +auto result_64 = reinterpret_cast<uint64_t*>(result_bitset->mutable_data()); |
| 59 | + |
| 60 | +auto u64_1 = reinterpret_cast<const uint64_t*>(data()); |
| 61 | +auto u64_2 = reinterpret_cast<const uint64_t*>(bitset.data()); |
| 62 | + |
| 63 | +size_t n64 = bitset_.size(); |
| 64 | + |
| 65 | +for (size_t i = 0; i < n64; i++) { |
| 66 | + result_64[i] = u64_1[i] & u64_2[i]; |
| 67 | +} |
| 68 | + |
| 69 | +return result_bitset; |
| 70 | +} |
| 71 | + |
| 72 | +std::shared_ptr<ConcurrentBitset2> |
| 73 | +ConcurrentBitset2::operator&(const BitsetView& view) const { |
| 74 | +auto result_bitset = std::make_shared<ConcurrentBitset2>(view.size()); |
| 75 | + |
| 76 | +auto result_64 = reinterpret_cast<uint64_t*>(result_bitset->mutable_data()); |
| 77 | + |
| 78 | +auto u64_1 = reinterpret_cast<const uint64_t*>(data()); |
| 79 | +auto u64_2 = reinterpret_cast<const uint64_t*>(view.data()); |
| 80 | + |
| 81 | +size_t n64 = bitset_.size(); |
| 82 | + |
| 83 | +for (size_t i = 0; i < n64; i++) { |
| 84 | + result_64[i] = u64_1[i] & u64_2[i]; |
| 85 | +} |
| 86 | + |
| 87 | +return result_bitset; |
| 88 | +} |
| 89 | + |
| 90 | + |
| 91 | +ConcurrentBitset2& |
| 92 | +ConcurrentBitset2::operator|=(const ConcurrentBitset2& bitset) { |
| 93 | +auto u64_1 = reinterpret_cast<uint64_t*>(mutable_data()); |
| 94 | +auto u64_2 = reinterpret_cast<const uint64_t*>(bitset.data()); |
| 95 | + |
| 96 | +size_t n64 = bitset_.size(); |
| 97 | + |
| 98 | +for (size_t i = 0; i < n64; i++) { |
| 99 | + u64_1[i] |= u64_2[i]; |
| 100 | +} |
| 101 | + |
| 102 | +return *this; |
| 103 | +} |
| 104 | + |
| 105 | +ConcurrentBitset2& |
| 106 | +ConcurrentBitset2::operator|=(const BitsetView& view) { |
| 107 | +auto u64_1 = reinterpret_cast<uint64_t*>(mutable_data()); |
| 108 | +auto u64_2 = reinterpret_cast<const uint64_t*>(view.data()); |
| 109 | + |
| 110 | +size_t n64 = bitset_.size(); |
| 111 | + |
| 112 | +for (size_t i = 0; i < n64; i++) { |
| 113 | + u64_1[i] |= u64_2[i]; |
| 114 | +} |
| 115 | + |
| 116 | +return *this; |
| 117 | +} |
| 118 | + |
| 119 | + |
| 120 | +std::shared_ptr<ConcurrentBitset2> |
| 121 | +ConcurrentBitset2::operator|(const ConcurrentBitset2& bitset) const { |
| 122 | +auto result_bitset = std::make_shared<ConcurrentBitset2>(bitset.size()); |
| 123 | + |
| 124 | +auto result_64 = reinterpret_cast<uint64_t*>(result_bitset->mutable_data()); |
| 125 | + |
| 126 | +auto u64_1 = reinterpret_cast<const uint64_t*>(data()); |
| 127 | +auto u64_2 = reinterpret_cast<const uint64_t*>(bitset.data()); |
| 128 | + |
| 129 | +size_t n64 = bitset_.size(); |
| 130 | + |
| 131 | +for (size_t i = 0; i < n64; i++) { |
| 132 | + result_64[i] = u64_1[i] | u64_2[i]; |
| 133 | +} |
| 134 | + |
| 135 | +return result_bitset; |
| 136 | +} |
| 137 | + |
| 138 | +std::shared_ptr<ConcurrentBitset2> |
| 139 | +ConcurrentBitset2::operator|(const BitsetView& view) const { |
| 140 | +auto result_bitset = std::make_shared<ConcurrentBitset2>(view.size()); |
| 141 | + |
| 142 | +auto result_64 = reinterpret_cast<uint64_t*>(result_bitset->mutable_data()); |
| 143 | + |
| 144 | +auto u64_1 = reinterpret_cast<const uint64_t*>(data()); |
| 145 | +auto u64_2 = reinterpret_cast<const uint64_t*>(view.data()); |
| 146 | + |
| 147 | +size_t n64 = bitset_.size(); |
| 148 | + |
| 149 | +for (size_t i = 0; i < n64; i++) { |
| 150 | + result_64[i] = u64_1[i] | u64_2[i]; |
| 151 | +} |
| 152 | + |
| 153 | +return result_bitset; |
| 154 | +} |
| 155 | + |
| 156 | + |
| 157 | +ConcurrentBitset2& |
| 158 | +ConcurrentBitset2::negate() { |
| 159 | +auto u64_1 = reinterpret_cast<uint64_t*>(mutable_data()); |
| 160 | + |
| 161 | +size_t n64 = bitset_.size(); |
| 162 | + |
| 163 | +for (size_t i = 0; i < n64; i++) { |
| 164 | + u64_1[i] = ~u64_1[i]; |
| 165 | +} |
| 166 | + |
| 167 | +return *this; |
| 168 | +} |
| 169 | + |
| 170 | +size_t |
| 171 | +ConcurrentBitset2::count() const { |
| 172 | +size_t ret = 0; |
| 173 | +auto p_data = reinterpret_cast<const uint64_t *>(data()); |
| 174 | + |
| 175 | +auto len = size() >> 3; |
| 176 | +auto popcount8 = [&](uint8_t x) -> int{ |
| 177 | + x = (x & 0x55) + ((x >> 1) & 0x55); |
| 178 | + x = (x & 0x33) + ((x >> 2) & 0x33); |
| 179 | + x = (x & 0x0F) + ((x >> 4) & 0x0F); |
| 180 | + return x; |
| 181 | +}; |
| 182 | + |
| 183 | +for (size_t i = 0; i < len; ++i) { |
| 184 | + ret += __builtin_popcountl(*p_data); |
| 185 | + p_data++; |
| 186 | +} |
| 187 | + |
| 188 | +auto p_byte = data() + (len << 3); |
| 189 | +for (auto i = (len << 3); i < size(); ++i) { |
| 190 | + ret += popcount8(*p_byte); |
| 191 | + p_byte++; |
| 192 | +} |
| 193 | +return ret; |
| 194 | +} |
| 195 | + |
| 196 | +ConcurrentBitset2::operator std::string() const { |
| 197 | + const char one = '1'; |
| 198 | + const char zero = '0'; |
| 199 | + const size_t len = size(); |
| 200 | + std::string s; |
| 201 | + s.assign (len, zero); |
| 202 | + |
| 203 | + for (size_t i = 0; i < len; ++i) { |
| 204 | + if (test(id_type_t(i))) |
| 205 | + s.assign(len - 1 - i, one); |
| 206 | + } |
| 207 | + return s; |
| 208 | +} |
| 209 | + |
| 210 | +bool operator==(const ConcurrentBitset2& lhs, const ConcurrentBitset2& rhs) { |
| 211 | + if (std::addressof(lhs) == std::addressof(rhs)){ |
| 212 | + return true; |
| 213 | + } |
| 214 | + |
| 215 | + if (lhs.size() != rhs.size()){ |
| 216 | + return false; |
| 217 | + } |
| 218 | + |
| 219 | + if (lhs.byte_size() != rhs.byte_size()){ |
| 220 | + return false; |
| 221 | + } |
| 222 | + |
| 223 | + |
| 224 | + auto ret = std::memcmp(lhs.data(), rhs.data(), lhs.byte_size()); |
| 225 | + return ret == 0; |
| 226 | +} |
| 227 | + |
| 228 | +bool operator!=(const ConcurrentBitset2& lhs, const ConcurrentBitset2& rhs){ |
| 229 | + return !(lhs == rhs); |
| 230 | +} |
| 231 | + |
| 232 | +std::ostream& operator<<(std::ostream& os, const ConcurrentBitset2& bitset) |
| 233 | +{ |
| 234 | + os << std::string(bitset); |
| 235 | + return os; |
| 236 | +} |
| 237 | + |
| 238 | + |
| 239 | +} // namespace faiss |
0 commit comments