Let bitset all functions inline (#3904)

Signed-off-by: shengjun.li <shengjun.li@zilliz.com>
This commit is contained in:
shengjun.li 2020-09-28 22:23:45 +08:00 committed by GitHub
parent 5e60e61042
commit 77aecc31bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 161 additions and 247 deletions

View File

@ -1,217 +0,0 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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.
#include <cstring>
#include "ConcurrentBitset.h"
namespace faiss {
ConcurrentBitset::ConcurrentBitset(id_type_t capacity, uint8_t init_value) : capacity_(capacity), bitset_(((capacity + 8 - 1) >> 3)) {
if (init_value) {
memset(mutable_data(), init_value, (capacity + 8 - 1) >> 3);
}
}
ConcurrentBitset&
ConcurrentBitset::operator&=(const ConcurrentBitset& bitset) {
auto u8_1 = mutable_data();
auto u8_2 = bitset.data();
auto u64_1 = reinterpret_cast<uint64_t*>(u8_1);
auto u64_2 = reinterpret_cast<const uint64_t*>(u8_2);
size_t n8 = bitset_.size();
size_t n64 = n8 / 8;
for (size_t i = 0; i < n64; i++) {
u64_1[i] &= u64_2[i];
}
size_t remain = n8 % 8;
u8_1 += n64 * 8;
u8_2 += n64 * 8;
for (size_t i = 0; i < remain; i++) {
u8_1[i] &= u8_2[i];
}
return *this;
}
std::shared_ptr<ConcurrentBitset>
ConcurrentBitset::operator&(const ConcurrentBitset& bitset) const {
auto result_bitset = std::make_shared<ConcurrentBitset>(bitset.capacity());
auto result_8 = result_bitset->mutable_data();
auto result_64 = reinterpret_cast<uint64_t*>(result_8);
auto u8_1 = data();
auto u8_2 = bitset.data();
auto u64_1 = reinterpret_cast<const uint64_t*>(u8_1);
auto u64_2 = reinterpret_cast<const uint64_t*>(u8_2);
size_t n8 = bitset_.size();
size_t n64 = n8 / 8;
for (size_t i = 0; i < n64; i++) {
result_64[i] = u64_1[i] & u64_2[i];
}
size_t remain = n8 % 8;
u8_1 += n64 * 8;
u8_2 += n64 * 8;
result_8 += n64 * 8;
for (size_t i = 0; i < remain; i++) {
result_8[i] = u8_1[i] & u8_2[i];
}
return result_bitset;
}
ConcurrentBitset&
ConcurrentBitset::operator|=(const ConcurrentBitset& bitset) {
auto u8_1 = mutable_data();
auto u8_2 = bitset.data();
auto u64_1 = reinterpret_cast<uint64_t*>(u8_1);
auto u64_2 = reinterpret_cast<const uint64_t*>(u8_2);
size_t n8 = bitset_.size();
size_t n64 = n8 / 8;
for (size_t i = 0; i < n64; i++) {
u64_1[i] |= u64_2[i];
}
size_t remain = n8 % 8;
u8_1 += n64 * 8;
u8_2 += n64 * 8;
for (size_t i = 0; i < remain; i++) {
u8_1[i] |= u8_2[i];
}
return *this;
}
std::shared_ptr<ConcurrentBitset>
ConcurrentBitset::operator|(const ConcurrentBitset& bitset) const {
auto result_bitset = std::make_shared<ConcurrentBitset>(bitset.capacity());
auto result_8 = result_bitset->mutable_data();
auto result_64 = reinterpret_cast<uint64_t*>(result_8);
auto u8_1 = data();
auto u8_2 = bitset.data();
auto u64_1 = reinterpret_cast<const uint64_t*>(u8_1);
auto u64_2 = reinterpret_cast<const uint64_t*>(u8_2);
size_t n8 = bitset_.size();
size_t n64 = n8 / 8;
for (size_t i = 0; i < n64; i++) {
result_64[i] = u64_1[i] | u64_2[i];
}
size_t remain = n8 % 8;
u8_1 += n64 * 8;
u8_2 += n64 * 8;
result_8 += n64 * 8;
for (size_t i = 0; i < remain; i++) {
result_8[i] = u8_1[i] | u8_2[i];
}
return result_bitset;
}
ConcurrentBitset&
ConcurrentBitset::operator^=(const ConcurrentBitset& bitset) {
auto u8_1 = mutable_data();
auto u8_2 = bitset.data();
auto u64_1 = reinterpret_cast<uint64_t*>(u8_1);
auto u64_2 = reinterpret_cast<const uint64_t*>(u8_2);
size_t n8 = bitset_.size();
size_t n64 = n8 / 8;
for (size_t i = 0; i < n64; i++) {
u64_1[i] &= u64_2[i];
}
size_t remain = n8 % 8;
u8_1 += n64 * 8;
u8_2 += n64 * 8;
for (size_t i = 0; i < remain; i++) {
u8_1[i] ^= u8_2[i];
}
return *this;
}
ConcurrentBitset&
ConcurrentBitset::negate() {
auto u8_1 = mutable_data();
auto u64_1 = reinterpret_cast<uint64_t*>(u8_1);
size_t n8 = bitset_.size();
size_t n64 = n8 / 8;
for (size_t i = 0; i < n64; i++) {
u64_1[i] = ~u64_1[i];
}
size_t remain = n8 % 8;
u8_1 += n64 * 8;
for (size_t i = 0; i < remain; i++) {
u8_1[i] = ~u8_1[i];
}
return *this;
}
bool
ConcurrentBitset::test(id_type_t id) {
return bitset_[id >> 3].load() & (0x1 << (id & 0x7));
}
void
ConcurrentBitset::set(id_type_t id) {
bitset_[id >> 3].fetch_or(0x1 << (id & 0x7));
}
void
ConcurrentBitset::clear(id_type_t id) {
bitset_[id >> 3].fetch_and(~(0x1 << (id & 0x7)));
}
size_t
ConcurrentBitset::capacity() const {
return capacity_;
}
size_t
ConcurrentBitset::size() const {
return ((capacity_ + 8 - 1) >> 3);
}
const uint8_t*
ConcurrentBitset::data() const {
return reinterpret_cast<const uint8_t*>(bitset_.data());
}
uint8_t*
ConcurrentBitset::mutable_data() {
return reinterpret_cast<uint8_t*>(bitset_.data());
}
} // namespace faiss

View File

@ -1,24 +1,19 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
//
// http://www.apache.org/licenses/LICENSE-2.0
// 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
//
// 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.
// 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.
#pragma once
#include <atomic>
#include <memory>
#include <string.h>
#include <vector>
namespace faiss {
@ -27,49 +22,185 @@ class ConcurrentBitset {
public:
using id_type_t = int64_t;
explicit ConcurrentBitset(id_type_t size, uint8_t init_value = 0);
explicit ConcurrentBitset(size_t count, uint8_t init_value = 0)
: count_(count), bitset_(((count + 8 - 1) >> 3)) {
if (init_value) {
memset(mutable_data(), init_value, (count + 8 - 1) >> 3);
}
}
ConcurrentBitset&
operator&=(const ConcurrentBitset& bitset);
operator&=(const ConcurrentBitset& bitset) {
auto u8_1 = mutable_data();
auto u8_2 = bitset.data();
auto u64_1 = reinterpret_cast<uint64_t*>(u8_1);
auto u64_2 = reinterpret_cast<const uint64_t*>(u8_2);
size_t n8 = bitset_.size();
size_t n64 = n8 / 8;
for (size_t i = 0; i < n64; i++) {
u64_1[i] &= u64_2[i];
}
size_t remain = n8 % 8;
u8_1 += n64 * 8;
u8_2 += n64 * 8;
for (size_t i = 0; i < remain; i++) {
u8_1[i] &= u8_2[i];
}
return *this;
}
std::shared_ptr<ConcurrentBitset>
operator&(const ConcurrentBitset& bitset) const;
operator&(const ConcurrentBitset& bitset) const {
auto result_bitset = std::make_shared<ConcurrentBitset>(bitset.count());
auto result_8 = result_bitset->mutable_data();
auto result_64 = reinterpret_cast<uint64_t*>(result_8);
auto u8_1 = data();
auto u8_2 = bitset.data();
auto u64_1 = reinterpret_cast<const uint64_t*>(u8_1);
auto u64_2 = reinterpret_cast<const uint64_t*>(u8_2);
size_t n8 = bitset_.size();
size_t n64 = n8 / 8;
for (size_t i = 0; i < n64; i++) {
result_64[i] = u64_1[i] & u64_2[i];
}
size_t remain = n8 % 8;
u8_1 += n64 * 8;
u8_2 += n64 * 8;
result_8 += n64 * 8;
for (size_t i = 0; i < remain; i++) {
result_8[i] = u8_1[i] & u8_2[i];
}
return result_bitset;
}
ConcurrentBitset&
operator|=(const ConcurrentBitset& bitset);
operator|=(const ConcurrentBitset& bitset) {
auto u8_1 = mutable_data();
auto u8_2 = bitset.data();
auto u64_1 = reinterpret_cast<uint64_t*>(u8_1);
auto u64_2 = reinterpret_cast<const uint64_t*>(u8_2);
size_t n8 = bitset_.size();
size_t n64 = n8 / 8;
for (size_t i = 0; i < n64; i++) {
u64_1[i] |= u64_2[i];
}
size_t remain = n8 % 8;
u8_1 += n64 * 8;
u8_2 += n64 * 8;
for (size_t i = 0; i < remain; i++) {
u8_1[i] |= u8_2[i];
}
return *this;
}
std::shared_ptr<ConcurrentBitset>
operator|(const ConcurrentBitset& bitset) const;
operator|(const ConcurrentBitset& bitset) const {
auto result_bitset = std::make_shared<ConcurrentBitset>(bitset.count());
auto result_8 = result_bitset->mutable_data();
auto result_64 = reinterpret_cast<uint64_t*>(result_8);
auto u8_1 = data();
auto u8_2 = bitset.data();
auto u64_1 = reinterpret_cast<const uint64_t*>(u8_1);
auto u64_2 = reinterpret_cast<const uint64_t*>(u8_2);
size_t n8 = bitset_.size();
size_t n64 = n8 / 8;
for (size_t i = 0; i < n64; i++) {
result_64[i] = u64_1[i] | u64_2[i];
}
size_t remain = n8 % 8;
u8_1 += n64 * 8;
u8_2 += n64 * 8;
result_8 += n64 * 8;
for (size_t i = 0; i < remain; i++) {
result_8[i] = u8_1[i] | u8_2[i];
}
return result_bitset;
}
ConcurrentBitset&
operator^=(const ConcurrentBitset& bitset);
negate() {
auto u8_1 = mutable_data();
auto u64_1 = reinterpret_cast<uint64_t*>(u8_1);
size_t n8 = bitset_.size();
size_t n64 = n8 / 8;
for (size_t i = 0; i < n64; i++) {
u64_1[i] = ~u64_1[i];
}
size_t remain = n8 % 8;
u8_1 += n64 * 8;
for (size_t i = 0; i < remain; i++) {
u8_1[i] = ~u8_1[i];
}
return *this;
}
ConcurrentBitset&
negate();
bool
test(id_type_t id);
test(id_type_t id) {
unsigned char mask = (unsigned char)(0x01) << (id & 0x07);
return (bitset_[id >> 3].load() & mask);
}
void
set(id_type_t id);
set(id_type_t id) {
unsigned char mask = (unsigned char)(0x01) << (id & 0x07);
bitset_[id >> 3].fetch_or(mask);
}
void
clear(id_type_t id);
clear(id_type_t id) {
unsigned char mask = (unsigned char)(0x01) << (id & 0x07);
bitset_[id >> 3].fetch_and(~mask);
}
size_t
capacity() const;
count() const {
return count_;
}
size_t
size() const;
size() const {
return ((count_ + 8 - 1) >> 3);
}
const uint8_t*
data() const;
data() const {
return reinterpret_cast<const uint8_t*>(bitset_.data());
}
uint8_t*
mutable_data();
mutable_data() {
return reinterpret_cast<uint8_t*>(bitset_.data());
}
private:
size_t capacity_;
size_t count_;
std::vector<std::atomic<uint8_t>> bitset_;
};