-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add BitpackedUintVector, add count_leading_zeros(), improve count_tra…
…iling_zeros(), add choice of standard library to build_tests.sh, fix warnings from GTEST caused by google's recent changes
- Loading branch information
Showing
15 changed files
with
1,633 additions
and
308 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// --- This file is distributed under the MIT Open Source License, as detailed | ||
// by the file "LICENSE.TXT" in the root of this repository --- | ||
|
||
#ifndef HURCHALLA_UTIL_BITPACKED_UINT_VECTOR_H_INCLUDED | ||
#define HURCHALLA_UTIL_BITPACKED_UINT_VECTOR_H_INCLUDED | ||
|
||
|
||
#include "hurchalla/util/detail/ImplBitpackedUintVector.h" | ||
#include "hurchalla/util/programming_by_contract.h" | ||
#include <cstdint> | ||
#include <limits> | ||
#include <memory> | ||
|
||
namespace hurchalla { | ||
|
||
|
||
template <typename U, unsigned int element_bitlen> | ||
struct BitpackedUintVector | ||
{ | ||
static_assert(std::numeric_limits<U>::is_integer, ""); | ||
static_assert(!std::numeric_limits<U>::is_signed, ""); | ||
static_assert(element_bitlen <= std::numeric_limits<U>::digits, ""); | ||
static_assert(0 < element_bitlen && element_bitlen <= 32, ""); | ||
using size_type = | ||
typename ImplBitpackedUintVector<U, element_bitlen>::size_type; | ||
|
||
BitpackedUintVector(size_type count) : impl_buv(count) {} | ||
|
||
// constructor for deserialization. Note: use data(), dataSizeBytes(), and | ||
// size() to serialize. | ||
BitpackedUintVector(std::unique_ptr<unsigned char[]> data, | ||
std::size_t data_bytes, | ||
size_type element_count) : | ||
impl_buv(std::move(data), data_bytes, element_count) {} | ||
|
||
|
||
HURCHALLA_FORCE_INLINE void setAt(size_type index, U value) | ||
{ | ||
HPBC_PRECONDITION2(value <= max_allowed_value()); | ||
HPBC_PRECONDITION2(index < size()); | ||
impl_buv.setAt(index, value); | ||
} | ||
|
||
HURCHALLA_FORCE_INLINE U getAt(size_type index) const | ||
{ | ||
HPBC_PRECONDITION2(index < size()); | ||
U value = impl_buv.getAt(index); | ||
HPBC_POSTCONDITION2(value <= max_allowed_value()); | ||
return value; | ||
} | ||
|
||
// returns the number of packed elements in this vector | ||
HURCHALLA_FORCE_INLINE size_type size() const | ||
{ | ||
return impl_buv.size(); | ||
} | ||
|
||
// returns the maximum value that fits within element_bitlen bits. | ||
HURCHALLA_FORCE_INLINE static constexpr U max_allowed_value() | ||
{ | ||
return ImplBitpackedUintVector<U, element_bitlen>::max_allowed_value(); | ||
} | ||
|
||
HURCHALLA_FORCE_INLINE std::size_t dataSizeBytes() const | ||
{ | ||
return impl_buv.dataSizeBytes(); | ||
} | ||
|
||
HURCHALLA_FORCE_INLINE const unsigned char* data() const | ||
{ | ||
return impl_buv.data(); | ||
} | ||
|
||
// You can use getFormatID() to get a data format ID to store (or transmit) | ||
// along with serialized data that you got from data(). | ||
// Then later you or another person can call getFormatID() as part of a | ||
// handshake to ensure matching format ID, prior to using that serialized | ||
// data (for the BitpackedUintVector constructor). | ||
HURCHALLA_FORCE_INLINE static constexpr uint64_t getFormatID() | ||
{ | ||
return ImplBitpackedUintVector<U, element_bitlen>::getFormatID(); | ||
} | ||
|
||
private: | ||
ImplBitpackedUintVector<U, element_bitlen> impl_buv; | ||
}; | ||
|
||
|
||
} // end namespace | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// --- This file is distributed under the MIT Open Source License, as detailed | ||
// by the file "LICENSE.TXT" in the root of this repository --- | ||
|
||
#ifndef HURCHALLA_UTIL_COUNT_LEADING_ZEROS_H_INCLUDED | ||
#define HURCHALLA_UTIL_COUNT_LEADING_ZEROS_H_INCLUDED | ||
|
||
|
||
#include "hurchalla/util/detail/impl_count_leading_zeros.h" | ||
#include "hurchalla/util/traits/ut_numeric_limits.h" | ||
#include "hurchalla/util/programming_by_contract.h" | ||
#include "hurchalla/util/compiler_macros.h" | ||
|
||
namespace hurchalla { | ||
|
||
|
||
// From the gcc docs: | ||
// "Returns the number of leading 0-bits in x, starting at the most | ||
// significant bit position. If x is 0, the result is undefined." | ||
template <typename T> | ||
HURCHALLA_FORCE_INLINE int count_leading_zeros(T x) | ||
{ | ||
static_assert(ut_numeric_limits<T>::is_integer, ""); | ||
static_assert(!ut_numeric_limits<T>::is_signed, ""); | ||
HPBC_PRECONDITION2(x != 0); | ||
|
||
return detail::impl_count_leading_zeros::call(x); | ||
} | ||
|
||
|
||
} // end namespace | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.