1 // Copyright (c) 2015-2022 Clearmatics Technologies Ltd
 
    3 // SPDX-License-Identifier: LGPL-3.0+
 
    5 #ifndef __ZETH_CORE_UTILS_TCC__
 
    6 #define __ZETH_CORE_UTILS_TCC__
 
    8 #include "libzeth/core/utils.hpp"
 
   11 #include <stdexcept> // required for std::length_error on linux
 
   16 template<> constexpr size_t bit_utils<0>::bit_size() { return 0; }
 
   18 template<> constexpr size_t bit_utils<0>::hamming_weight() { return 0; }
 
   20 template<size_t X> constexpr size_t bit_utils<X>::bit_size()
 
   22     return 1 + bit_utils<(X >> 1)>::bit_size();
 
   25 template<size_t X> constexpr size_t bit_utils<X>::hamming_weight()
 
   27     return (X & 1) + bit_utils<(X >> 1)>::hamming_weight();
 
   30 template<typename T> T swap_byte_endianness(T v)
 
   32     size_t len = v.size();
 
   34         throw std::length_error(
 
   35             "Invalid bit length for the given boolean vector (should be > 0)");
 
   38         throw std::length_error("Invalid bit length for the given boolean "
 
   39                                 "vector (should be multiple of 8)");
 
   42     size_t byte_mid_length = std::ceil((len / 8) / 2);
 
   43     for (size_t i = 0; i < byte_mid_length; i++) {
 
   44         for (size_t j = 0; j < 8; j++) {
 
   45             std::swap(v[8 * i + j], v[len - 8 * (i + 1) + j]);
 
   51 template<typename StructuredTs>
 
   52 bool container_is_well_formed(const StructuredTs &values)
 
   54     for (const auto &v : values) {
 
   55         if (!v.is_well_formed()) {
 
   63 template<typename StructuredT>
 
   64 void check_well_formed(const StructuredT &v, const char *name)
 
   66     if (!v.is_well_formed()) {
 
   67         throw std::invalid_argument(std::string(name) + " not well-formed");
 
   71 } // namespace libzeth
 
   73 #endif // __ZETH_CORE_UTILS_TCC__