Skip to content

Commit

Permalink
add BitpackedUintVector, add count_leading_zeros(), improve count_tra…
Browse files Browse the repository at this point in the history
…iling_zeros(), add choice of standard library to build_tests.sh, fix warnings from GTEST caused by google's recent changes
  • Loading branch information
hurchalla committed May 28, 2024
1 parent 77b396f commit 2d0f4e3
Show file tree
Hide file tree
Showing 15 changed files with 1,633 additions and 308 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,20 @@ add_library(hurchalla_util INTERFACE)

target_sources(hurchalla_util INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/hurchalla/util/assert_handler.h>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/hurchalla/util/BitpackedUintVector.h>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/hurchalla/util/compiler_macros.h>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/hurchalla/util/conditional_select.h>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/hurchalla/util/count_leading_zeros.h>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/hurchalla/util/count_trailing_zeros.h>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/hurchalla/util/programming_by_contract.h>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/hurchalla/util/signed_multiply_to_hilo_product.h>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/hurchalla/util/sized_uint.h>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/hurchalla/util/unreachable.h>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/hurchalla/util/Unroll.h>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/hurchalla/util/unsigned_multiply_to_hilo_product.h>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/hurchalla/util/detail/ImplBitpackedUintVector.h>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/hurchalla/util/detail/impl_conditional_select.h>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/hurchalla/util/detail/impl_count_leading_zeros.h>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/hurchalla/util/detail/impl_count_trailing_zeros.h>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/hurchalla/util/detail/platform_specific/impl_signed_multiply_to_hilo_product.h>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/hurchalla/util/detail/platform_specific/impl_unsigned_multiply_to_hilo_product.h>
Expand Down
5 changes: 3 additions & 2 deletions build_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# This is my working convenience script for invoking the testing builds and then
# running the tests.
# The syntax is
# ./build_tests [-c<compiler_name>] [-r] [-s] [-m<Release|Debug>]
# ./build_tests [-c<compiler_name>] [-r] [-s] [-m<Release|Debug>] [-l<standard_library_name>]
#
# -c allows you to select the compiler, rather than using the default.
# -r specifies to run all tests after the build. Without -r, no tests will run.
Expand All @@ -18,6 +18,7 @@
# conditional select or conditional move instructions.
# -m allows you to choose between Release and Debug build configuration, rather
# than using the default.
# -l allows you to choose between either libstdc++ or libc++ when using clang.
#
# Currently it supports clang, gcc, and icc but you'll need to customize the
# section under "#Compiler commands" to match the compilers on your system. The
Expand Down Expand Up @@ -162,7 +163,7 @@ while getopts ":m:l:c:h-:rs" opt; do
h)
;&
-)
echo "Usage: build_tests [-c<compiler_name>] [-r] [-s] [-m<Release|Debug>]" >&2
echo "Usage: build_tests [-c<compiler_name>] [-r] [-s] [-m<Release|Debug>] [-l<standard_library_name>]" >&2
exit 1
;;
c)
Expand Down
91 changes: 91 additions & 0 deletions include/hurchalla/util/BitpackedUintVector.h
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
32 changes: 32 additions & 0 deletions include/hurchalla/util/count_leading_zeros.h
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
2 changes: 1 addition & 1 deletion include/hurchalla/util/count_trailing_zeros.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ HURCHALLA_FORCE_INLINE int count_trailing_zeros(T x)
static_assert(!ut_numeric_limits<T>::is_signed, "");
HPBC_PRECONDITION2(x != 0);

return detail::impl_count_trailing_zeros<T>::call(x);
return detail::impl_count_trailing_zeros::call(x);
}


Expand Down
Loading

0 comments on commit 2d0f4e3

Please sign in to comment.