<commit_before>///\file

/******************************************************************************
The MIT License(MIT)

Embedded Template Library.
https://github.com/ETLCPP/etl

Copyright(c) 2015 jwellbelove

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/

#ifndef __ETL_BINARY__
#define __ETL_BINARY__

///\defgroup binary binary
/// Binary utilities
///\ingroup utilities

#include "type_traits.h"
#include "integral_limits.h"
#include "static_assert.h"
#include "log.h"
#include "power.h"

namespace etl
{
  //***************************************************************************
  /// Rotate left.
  //***************************************************************************
  template <typename T>
  T rotate_left(T value)
  {
    STATIC_ASSERT(etl::is_integral<T>::value, "Not an integral type");

    const size_t SHIFT = etl::integral_limits<typename etl::make_unsigned<T>::type>::bits - 1;

    return (value << 1) | (value >> SHIFT);
  }

  //***************************************************************************
  /// Rotate left.
  //***************************************************************************
  template <typename T>
  T rotate_left(T value, size_t distance)
  {
    STATIC_ASSERT(etl::is_integral<T>::value, "Not an integral type");

    const size_t BITS = etl::integral_limits<typename etl::make_unsigned<T>::type>::bits;
    distance %= BITS;
    const size_t SHIFT = BITS - distance;

    return (value << distance) | (value >> SHIFT);
  }

  //***************************************************************************
  /// Rotate right.
  //***************************************************************************
  template <typename T>
  T rotate_right(T value)
  {
    STATIC_ASSERT(etl::is_integral<T>::value, "Not an integral type");

    const size_t SHIFT = etl::integral_limits<typename etl::make_unsigned<T>::type>::bits - 1;

    return (value >> 1) | (value << SHIFT);
  }

  //***************************************************************************
  /// Rotate right.
  //***************************************************************************
  template <typename T>
  T rotate_right(T value, size_t distance)
  {
    STATIC_ASSERT(etl::is_integral<T>::value, "Not an integral type");

    const size_t BITS = etl::integral_limits<typename etl::make_unsigned<T>::type>::bits;
    distance %= BITS;
    const size_t SHIFT = BITS - distance;

    return (value >> distance) | (value << SHIFT);
  }

  //***************************************************************************
  /// Rotate.
  /// Positive is left, negative is right.
  //***************************************************************************
  template <typename T>
  T rotate(T value, typename etl::make_signed<size_t>::type distance)
  {
    STATIC_ASSERT(etl::is_integral<T>::value, "Not an integral type");

    if (distance > 0)
    {
      return rotate_left(value, size_t(distance));
    }
    else
    {
      return rotate_right(value, size_t(-distance));
    }
  }

  //***************************************************************************
  /// Reverse 8 bits.
  //***************************************************************************
  template <typename T>
  typename etl::enable_if<etl::is_same<typename etl::make_unsigned<T>::type, uint8_t>::value, T>::type
  reverse_bits(T value)
  {
    value = ((value & 0xAA) >> 1) | ((value & 0x55) << 1);
    value = ((value & 0xCC) >> 2) | ((value & 0x33) << 2);
    value = (value >> 4) | (value << 4);

    return value;
  }

  //***************************************************************************
  /// Reverse 16 bits.
  //***************************************************************************
  template <typename T>
  typename etl::enable_if<etl::is_same<typename etl::make_unsigned<T>::type, uint16_t>::value, T>::type
  reverse_bits(T value)
  {
    value = ((value & 0xAAAA) >> 1) | ((value & 0x5555) << 1);
    value = ((value & 0xCCCC) >> 2) | ((value & 0x3333) << 2);
    value = ((value & 0xF0F0) >> 4) | ((value & 0x0F0F) << 4);
    value = (value >> 8) | (value << 8);

    return value;
  }

  //***************************************************************************
  /// Reverse 32 bits.
  //***************************************************************************
  template <typename T>
  typename etl::enable_if<etl::is_same<typename etl::make_unsigned<T>::type, uint32_t>::value, T>::type
  reverse_bits(T value)
  {
    value = ((value & 0xAAAAAAAA) >>  1) | ((value & 0x55555555) <<  1);
    value = ((value & 0xCCCCCCCC) >>  2) | ((value & 0x33333333) <<  2);
    value = ((value & 0xF0F0F0F0) >>  4) | ((value & 0x0F0F0F0F) <<  4);
    value = ((value & 0xFF00FF00) >>  8) | ((value & 0x00FF00FF) <<  8);
    value = (value >> 16) | (value << 16);

    return value;
  }

  //***************************************************************************
  /// Reverse 64 bits.
  //***************************************************************************
  template <typename T>
  typename etl::enable_if<etl::is_same<typename etl::make_unsigned<T>::type, uint64_t>::value, T>::type
  reverse_bits(T value)
  {
    value = ((value & 0xAAAAAAAAAAAAAAAA) >>  1) | ((value & 0x5555555555555555) <<  1);
    value = ((value & 0xCCCCCCCCCCCCCCCC) >>  2) | ((value & 0x3333333333333333) <<  2);
    value = ((value & 0xF0F0F0F0F0F0F0F0) >>  4) | ((value & 0x0F0F0F0F0F0F0F0F) <<  4);
    value = ((value & 0xFF00FF00FF00FF00) >>  8) | ((value & 0x00FF00FF00FF00FF) <<  8);
    value = ((value & 0xFFFF0000FFFF0000) >> 16) | ((value & 0x0000FFFF0000FFFF) << 16);
    value = (value >> 32) | (value << 32);

    return value;
  }

  //***************************************************************************
  /// Reverse bytes 16 bit.
  //***************************************************************************
  template <typename T>
  typename etl::enable_if<etl::is_same<typename etl::make_unsigned<T>::type, uint16_t>::value, T>::type
  reverse_bytes(T value)
  {
    value = (value >> 8) | (value << 8);

    return value;
  }

  //***************************************************************************
  /// Reverse bytes 32 bit.
  //***************************************************************************
  template <typename T>
  typename etl::enable_if<etl::is_same<typename etl::make_unsigned<T>::type, uint32_t>::value, T>::type
  reverse_bytes(T value)
  {
    value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8);
    value = (value >> 16) | (value << 16);

    return value;
  }

  //***************************************************************************
  /// Reverse bytes 64 bit.
  //***************************************************************************
  template <typename T>
  typename etl::enable_if<etl::is_same<typename etl::make_unsigned<T>::type, uint64_t>::value, T>::type
  reverse_bytes(T value)
  {
    value = ((value & 0xFF00FF00FF00FF00) >> 8)  | ((value & 0x00FF00FF00FF00FF) << 8);
    value = ((value & 0xFFFF0000FFFF0000) >> 16) | ((value & 0x0000FFFF0000FFFF) << 16);
    value = (value >> 32) | (value << 32);

    return value;
  }

  //***************************************************************************
  /// Converts binary to Gray code.
  //***************************************************************************
  template <typename T>
  T binary_to_gray(T value)
  {
    STATIC_ASSERT(etl::is_integral<T>::value, "Not an integral type");

    return (value >> 1) ^ value;
  }

  //***************************************************************************
  /// Converts Gray code to binary.
  //***************************************************************************
  template <typename T>
  typename etl::enable_if<etl::is_same<typename etl::make_unsigned<T>::type, uint8_t>::value, T>::type
  gray_to_binary(T value)
  {
    value ^= (value >> 4);
    value ^= (value >> 2);
    value ^= (value >> 1);

    return value;
  }

  //***************************************************************************
  /// Converts Gray code to binary.
  //***************************************************************************
  template <typename T>
  typename etl::enable_if<etl::is_same<typename etl::make_unsigned<T>::type, uint16_t>::value, T>::type
  gray_to_binary(T value)
  {
    value ^= (value >> 8);
    value ^= (value >> 4);
    value ^= (value >> 2);
    value ^= (value >> 1);

    return value;
  }

  //***************************************************************************
  /// Converts Gray code to binary.
  //***************************************************************************
  template <typename T>
  typename etl::enable_if<etl::is_same<typename etl::make_unsigned<T>::type, uint32_t>::value, T>::type
  gray_to_binary(T value)
  {
    value ^= (value >> 16);
    value ^= (value >> 8);
    value ^= (value >> 4);
    value ^= (value >> 2);
    value ^= (value >> 1);

    return value;
  }

  //***************************************************************************
  /// Converts Gray code to binary.
  //***************************************************************************
  template <typename T>
  typename etl::enable_if<etl::is_same<typename etl::make_unsigned<T>::type, uint64_t>::value, T>::type
  gray_to_binary(T value)
  {
    value ^= (value >> 32);
    value ^= (value >> 16);
    value ^= (value >> 8);
    value ^= (value >> 4);
    value ^= (value >> 2);
    value ^= (value >> 1);

    return value;
  }

  //***************************************************************************
  /// Count set bits. 8 bits.
  //***************************************************************************
  template <typename T>
  typename etl::enable_if<etl::is_same<typename etl::make_unsigned<T>::type, uint8_t>::value, size_t>::type
  count_bits(T value)
  {
    uint32_t count;
    static const int S[] = { 1, 2, 4 };
    static const int B[] = { 0x55, 0x33, 0x0F };

    count = value - ((value >> 1) & B[0]);
    count = ((count >> S[1]) & B[1]) + (count & B[1]);
    count = ((count >> S[2]) + count) & B[2];

    return count;
  }

  //***************************************************************************
  /// Count set bits. 16 bits.
  //***************************************************************************
  template <typename T>
  typename etl::enable_if<etl::is_same<typename etl::make_unsigned<T>::type, uint16_t>::value, size_t>::type
  count_bits(T value)
  {
    uint32_t count;
    static const int S[] = { 1, 2, 4, 8 };
    static const int B[] = { 0x5555, 0x3333, 0x0F0F, 0x00FF };

    count = value - ((value >> 1) & B[0]);
    count = ((count >> S[1]) & B[1]) + (count & B[1]);
    count = ((count >> S[2]) + count) & B[2];
    count = ((count >> S[3]) + count) & B[3];

    return count;
  }

  //***************************************************************************
  /// Count set bits. 32 bits.
  //***************************************************************************
  template <typename T>
  typename etl::enable_if<etl::is_same<typename etl::make_unsigned<T>::type, uint32_t>::value, size_t>::type
  count_bits(T value)
  {
    uint32_t count;
    static const int S[] = { 1, 2, 4, 8, 16 };
    static const int B[] = { 0x55555555, 0x33333333, 0x0F0F0F0F, 0x00FF00FF, 0x0000FFFF };

    count = value - ((value >> 1) & B[0]);
    count = ((count >> S[1]) & B[1]) + (count & B[1]);
    count = ((count >> S[2]) + count) & B[2];
    count = ((count >> S[3]) + count) & B[3];
    count = ((count >> S[4]) + count) & B[4];

    return count;
  }

  //***************************************************************************
  /// Count set bits. 64 bits.
  //***************************************************************************
  template <typename T>
  typename etl::enable_if<etl::is_same<typename etl::make_unsigned<T>::type, uint64_t>::value, size_t>::type
  count_bits(T value)
  {
    uint64_t count;
    static const int S[] = { 1, 2, 4, 8, 16, 32 };
    static const uint64_t B[] = { 0x5555555555555555, 0x3333333333333333, 0x0F0F0F0F0F0F0F0F, 0x00FF00FF00FF00FF, 0x0000FFFF0000FFFF, 0x00000000FFFFFFFF };

    count = value - ((value >> 1) & B[0]);
    count = ((count >> S[1]) & B[1]) + (count & B[1]);
    count = ((count >> S[2]) + count) & B[2];
    count = ((count >> S[3]) + count) & B[3];
    count = ((count >> S[4]) + count) & B[4];
    count = ((count >> S[5]) + count) & B[5];

    return size_t(count);
  }

  //***************************************************************************
  /// Parity. 8bits. 0 = even, 1 = odd
  //***************************************************************************
  template <typename T>
  typename etl::enable_if<etl::is_same<typename etl::make_unsigned<T>::type, uint8_t>::value, size_t>::type
  parity(T value)
  {
    value ^= value >> 4;
    value &= 0x0F;
    return (0x6996 >> value) & 1;
  }

  //***************************************************************************
  /// Parity. 16bits. 0 = even, 1 = odd
  //***************************************************************************
  template <typename T>
  typename etl::enable_if<etl::is_same<typename etl::make_unsigned<T>::type, uint16_t>::value, size_t>::type
  parity(T value)
  {
    value ^= value >> 8;
    value ^= value >> 4;
    value &= 0x0F;
    return (0x96 >> value) & 1;
  }

  //***************************************************************************
  /// Parity. 32bits. 0 = even, 1 = odd
  //***************************************************************************
  template <typename T>
  typename etl::enable_if<etl::is_same<typename etl::make_unsigned<T>::type, uint32_t>::value, size_t>::type
  parity(T value)
  {
    value ^= value >> 16;
    value ^= value >> 8;
    value ^= value >> 4;
    value &= 0x0F;
    return (0x6996 >> value) & 1;
  }

  //***************************************************************************
  /// Parity. 64bits. 0 = even, 1 = odd
  //***************************************************************************
  template <typename T>
  typename etl::enable_if<etl::is_same<typename etl::make_unsigned<T>::type, uint64_t>::value, size_t>::type
  parity(T value)
  {
    value ^= value >> 32;
    value ^= value >> 16;
    value ^= value >> 8;
    value ^= value >> 4;
    value &= 0x0F;
    return (0x69966996 >> value) & 1;
  }

  //***************************************************************************
  /// Fold a binary number down to a set number of bits using XOR.
  //***************************************************************************
  template <typename TReturn, const size_t NBITS, typename TValue>
  TReturn fold_bits(TValue value)
  {
    STATIC_ASSERT(integral_limits<TReturn>::bits >= NBITS, "Return type too small to hold result");

    const size_t mask  = etl::power<2, NBITS>::value - 1;
    const size_t shift = NBITS;

    // Fold the value down to fit the width.
    TReturn folded_value = 0;

    // Keep shifting down and XORing the lower bits.
    while (value >= NBITS)
    {
      folded_value ^= value & mask;
      value >>= shift;
    }

    // Fold the remaining bits.<commit_msg>Changed size_t to TValue in 'fold_bits'<commit_after>///\file

/******************************************************************************
The MIT License(MIT)

Embedded Template Library.
https://github.com/ETLCPP/etl

Copyright(c) 2015 jwellbelove

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/

#ifndef __ETL_BINARY__
#define __ETL_BINARY__

///\defgroup binary binary
/// Binary utilities
///\ingroup utilities

#include "type_traits.h"
#include "integral_limits.h"
#include "static_assert.h"
#include "log.h"
#include "power.h"

namespace etl
{
  //***************************************************************************
  /// Rotate left.
  //***************************************************************************
  template <typename T>
  T rotate_left(T value)
  {
    STATIC_ASSERT(etl::is_integral<T>::value, "Not an integral type");

    const size_t SHIFT = etl::integral_limits<typename etl::make_unsigned<T>::type>::bits - 1;

    return (value << 1) | (value >> SHIFT);
  }

  //***************************************************************************
  /// Rotate left.
  //***************************************************************************
  template <typename T>
  T rotate_left(T value, size_t distance)
  {
    STATIC_ASSERT(etl::is_integral<T>::value, "Not an integral type");

    const size_t BITS = etl::integral_limits<typename etl::make_unsigned<T>::type>::bits;
    distance %= BITS;
    const size_t SHIFT = BITS - distance;

    return (value << distance) | (value >> SHIFT);
  }

  //***************************************************************************
  /// Rotate right.
  //***************************************************************************
  template <typename T>
  T rotate_right(T value)
  {
    STATIC_ASSERT(etl::is_integral<T>::value, "Not an integral type");

    const size_t SHIFT = etl::integral_limits<typename etl::make_unsigned<T>::type>::bits - 1;

    return (value >> 1) | (value << SHIFT);
  }

  //***************************************************************************
  /// Rotate right.
  //***************************************************************************
  template <typename T>
  T rotate_right(T value, size_t distance)
  {
    STATIC_ASSERT(etl::is_integral<T>::value, "Not an integral type");

    const size_t BITS = etl::integral_limits<typename etl::make_unsigned<T>::type>::bits;
    distance %= BITS;
    const size_t SHIFT = BITS - distance;

    return (value >> distance) | (value << SHIFT);
  }

  //***************************************************************************
  /// Rotate.
  /// Positive is left, negative is right.
  //***************************************************************************
  template <typename T>
  T rotate(T value, typename etl::make_signed<size_t>::type distance)
  {
    STATIC_ASSERT(etl::is_integral<T>::value, "Not an integral type");

    if (distance > 0)
    {
      return rotate_left(value, size_t(distance));
    }
    else
    {
      return rotate_right(value, size_t(-distance));
    }
  }

  //***************************************************************************
  /// Reverse 8 bits.
  //***************************************************************************
  template <typename T>
  typename etl::enable_if<etl::is_same<typename etl::make_unsigned<T>::type, uint8_t>::value, T>::type
  reverse_bits(T value)
  {
    value = ((value & 0xAA) >> 1) | ((value & 0x55) << 1);
    value = ((value & 0xCC) >> 2) | ((value & 0x33) << 2);
    value = (value >> 4) | (value << 4);

    return value;
  }

  //***************************************************************************
  /// Reverse 16 bits.
  //***************************************************************************
  template <typename T>
  typename etl::enable_if<etl::is_same<typename etl::make_unsigned<T>::type, uint16_t>::value, T>::type
  reverse_bits(T value)
  {
    value = ((value & 0xAAAA) >> 1) | ((value & 0x5555) << 1);
    value = ((value & 0xCCCC) >> 2) | ((value & 0x3333) << 2);
    value = ((value & 0xF0F0) >> 4) | ((value & 0x0F0F) << 4);
    value = (value >> 8) | (value << 8);

    return value;
  }

  //***************************************************************************
  /// Reverse 32 bits.
  //***************************************************************************
  template <typename T>
  typename etl::enable_if<etl::is_same<typename etl::make_unsigned<T>::type, uint32_t>::value, T>::type
  reverse_bits(T value)
  {
    value = ((value & 0xAAAAAAAA) >>  1) | ((value & 0x55555555) <<  1);
    value = ((value & 0xCCCCCCCC) >>  2) | ((value & 0x33333333) <<  2);
    value = ((value & 0xF0F0F0F0) >>  4) | ((value & 0x0F0F0F0F) <<  4);
    value = ((value & 0xFF00FF00) >>  8) | ((value & 0x00FF00FF) <<  8);
    value = (value >> 16) | (value << 16);

    return value;
  }

  //***************************************************************************
  /// Reverse 64 bits.
  //***************************************************************************
  template <typename T>
  typename etl::enable_if<etl::is_same<typename etl::make_unsigned<T>::type, uint64_t>::value, T>::type
  reverse_bits(T value)
  {
    value = ((value & 0xAAAAAAAAAAAAAAAA) >>  1) | ((value & 0x5555555555555555) <<  1);
    value = ((value & 0xCCCCCCCCCCCCCCCC) >>  2) | ((value & 0x3333333333333333) <<  2);
    value = ((value & 0xF0F0F0F0F0F0F0F0) >>  4) | ((value & 0x0F0F0F0F0F0F0F0F) <<  4);
    value = ((value & 0xFF00FF00FF00FF00) >>  8) | ((value & 0x00FF00FF00FF00FF) <<  8);
    value = ((value & 0xFFFF0000FFFF0000) >> 16) | ((value & 0x0000FFFF0000FFFF) << 16);
    value = (value >> 32) | (value << 32);

    return value;
  }

  //***************************************************************************
  /// Reverse bytes 16 bit.
  //***************************************************************************
  template <typename T>
  typename etl::enable_if<etl::is_same<typename etl::make_unsigned<T>::type, uint16_t>::value, T>::type
  reverse_bytes(T value)
  {
    value = (value >> 8) | (value << 8);

    return value;
  }

  //***************************************************************************
  /// Reverse bytes 32 bit.
  //***************************************************************************
  template <typename T>
  typename etl::enable_if<etl::is_same<typename etl::make_unsigned<T>::type, uint32_t>::value, T>::type
  reverse_bytes(T value)
  {
    value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8);
    value = (value >> 16) | (value << 16);

    return value;
  }

  //***************************************************************************
  /// Reverse bytes 64 bit.
  //***************************************************************************
  template <typename T>
  typename etl::enable_if<etl::is_same<typename etl::make_unsigned<T>::type, uint64_t>::value, T>::type
  reverse_bytes(T value)
  {
    value = ((value & 0xFF00FF00FF00FF00) >> 8)  | ((value & 0x00FF00FF00FF00FF) << 8);
    value = ((value & 0xFFFF0000FFFF0000) >> 16) | ((value & 0x0000FFFF0000FFFF) << 16);
    value = (value >> 32) | (value << 32);

    return value;
  }

  //***************************************************************************
  /// Converts binary to Gray code.
  //***************************************************************************
  template <typename T>
  T binary_to_gray(T value)
  {
    STATIC_ASSERT(etl::is_integral<T>::value, "Not an integral type");

    return (value >> 1) ^ value;
  }

  //***************************************************************************
  /// Converts Gray code to binary.
  //***************************************************************************
  template <typename T>
  typename etl::enable_if<etl::is_same<typename etl::make_unsigned<T>::type, uint8_t>::value, T>::type
  gray_to_binary(T value)
  {
    value ^= (value >> 4);
    value ^= (value >> 2);
    value ^= (value >> 1);

    return value;
  }

  //***************************************************************************
  /// Converts Gray code to binary.
  //***************************************************************************
  template <typename T>
  typename etl::enable_if<etl::is_same<typename etl::make_unsigned<T>::type, uint16_t>::value, T>::type
  gray_to_binary(T value)
  {
    value ^= (value >> 8);
    value ^= (value >> 4);
    value ^= (value >> 2);
    value ^= (value >> 1);

    return value;
  }

  //***************************************************************************
  /// Converts Gray code to binary.
  //***************************************************************************
  template <typename T>
  typename etl::enable_if<etl::is_same<typename etl::make_unsigned<T>::type, uint32_t>::value, T>::type
  gray_to_binary(T value)
  {
    value ^= (value >> 16);
    value ^= (value >> 8);
    value ^= (value >> 4);
    value ^= (value >> 2);
    value ^= (value >> 1);

    return value;
  }

  //***************************************************************************
  /// Converts Gray code to binary.
  //***************************************************************************
  template <typename T>
  typename etl::enable_if<etl::is_same<typename etl::make_unsigned<T>::type, uint64_t>::value, T>::type
  gray_to_binary(T value)
  {
    value ^= (value >> 32);
    value ^= (value >> 16);
    value ^= (value >> 8);
    value ^= (value >> 4);
    value ^= (value >> 2);
    value ^= (value >> 1);

    return value;
  }

  //***************************************************************************
  /// Count set bits. 8 bits.
  //***************************************************************************
  template <typename T>
  typename etl::enable_if<etl::is_same<typename etl::make_unsigned<T>::type, uint8_t>::value, size_t>::type
  count_bits(T value)
  {
    uint32_t count;
    static const int S[] = { 1, 2, 4 };
    static const int B[] = { 0x55, 0x33, 0x0F };

    count = value - ((value >> 1) & B[0]);
    count = ((count >> S[1]) & B[1]) + (count & B[1]);
    count = ((count >> S[2]) + count) & B[2];

    return count;
  }

  //***************************************************************************
  /// Count set bits. 16 bits.
  //***************************************************************************
  template <typename T>
  typename etl::enable_if<etl::is_same<typename etl::make_unsigned<T>::type, uint16_t>::value, size_t>::type
  count_bits(T value)
  {
    uint32_t count;
    static const int S[] = { 1, 2, 4, 8 };
    static const int B[] = { 0x5555, 0x3333, 0x0F0F, 0x00FF };

    count = value - ((value >> 1) & B[0]);
    count = ((count >> S[1]) & B[1]) + (count & B[1]);
    count = ((count >> S[2]) + count) & B[2];
    count = ((count >> S[3]) + count) & B[3];

    return count;
  }

  //***************************************************************************
  /// Count set bits. 32 bits.
  //***************************************************************************
  template <typename T>
  typename etl::enable_if<etl::is_same<typename etl::make_unsigned<T>::type, uint32_t>::value, size_t>::type
  count_bits(T value)
  {
    uint32_t count;
    static const int S[] = { 1, 2, 4, 8, 16 };
    static const int B[] = { 0x55555555, 0x33333333, 0x0F0F0F0F, 0x00FF00FF, 0x0000FFFF };

    count = value - ((value >> 1) & B[0]);
    count = ((count >> S[1]) & B[1]) + (count & B[1]);
    count = ((count >> S[2]) + count) & B[2];
    count = ((count >> S[3]) + count) & B[3];
    count = ((count >> S[4]) + count) & B[4];

    return count;
  }

  //***************************************************************************
  /// Count set bits. 64 bits.
  //***************************************************************************
  template <typename T>
  typename etl::enable_if<etl::is_same<typename etl::make_unsigned<T>::type, uint64_t>::value, size_t>::type
  count_bits(T value)
  {
    uint64_t count;
    static const int S[] = { 1, 2, 4, 8, 16, 32 };
    static const uint64_t B[] = { 0x5555555555555555, 0x3333333333333333, 0x0F0F0F0F0F0F0F0F, 0x00FF00FF00FF00FF, 0x0000FFFF0000FFFF, 0x00000000FFFFFFFF };

    count = value - ((value >> 1) & B[0]);
    count = ((count >> S[1]) & B[1]) + (count & B[1]);
    count = ((count >> S[2]) + count) & B[2];
    count = ((count >> S[3]) + count) & B[3];
    count = ((count >> S[4]) + count) & B[4];
    count = ((count >> S[5]) + count) & B[5];

    return size_t(count);
  }

  //***************************************************************************
  /// Parity. 8bits. 0 = even, 1 = odd
  //***************************************************************************
  template <typename T>
  typename etl::enable_if<etl::is_same<typename etl::make_unsigned<T>::type, uint8_t>::value, size_t>::type
  parity(T value)
  {
    value ^= value >> 4;
    value &= 0x0F;
    return (0x6996 >> value) & 1;
  }

  //***************************************************************************
  /// Parity. 16bits. 0 = even, 1 = odd
  //***************************************************************************
  template <typename T>
  typename etl::enable_if<etl::is_same<typename etl::make_unsigned<T>::type, uint16_t>::value, size_t>::type
  parity(T value)
  {
    value ^= value >> 8;
    value ^= value >> 4;
    value &= 0x0F;
    return (0x96 >> value) & 1;
  }

  //***************************************************************************
  /// Parity. 32bits. 0 = even, 1 = odd
  //***************************************************************************
  template <typename T>
  typename etl::enable_if<etl::is_same<typename etl::make_unsigned<T>::type, uint32_t>::value, size_t>::type
  parity(T value)
  {
    value ^= value >> 16;
    value ^= value >> 8;
    value ^= value >> 4;
    value &= 0x0F;
    return (0x6996 >> value) & 1;
  }

  //***************************************************************************
  /// Parity. 64bits. 0 = even, 1 = odd
  //***************************************************************************
  template <typename T>
  typename etl::enable_if<etl::is_same<typename etl::make_unsigned<T>::type, uint64_t>::value, size_t>::type
  parity(T value)
  {
    value ^= value >> 32;
    value ^= value >> 16;
    value ^= value >> 8;
    value ^= value >> 4;
    value &= 0x0F;
    return (0x69966996 >> value) & 1;
  }

  //***************************************************************************
  /// Fold a binary number down to a set number of bits using XOR.
  //***************************************************************************
  template <typename TReturn, const size_t NBITS, typename TValue>
  TReturn fold_bits(TValue value)
  {
    STATIC_ASSERT(integral_limits<TReturn>::bits >= NBITS, "Return type too small to hold result");

    const TValue mask  = etl::power<2, NBITS>::value - 1;
    const size_t shift = NBITS;

    // Fold the value down to fit the width.
    TReturn folded_value = 0;

    // Keep shifting down and XORing the lower bits.
    while (value >= NBITS)
    {
      folded_value ^= value & mask;
      value >>= shift;
    }

    // Fold the remaining bits.
{'fraction_non_alphanumeric': 0.3756984097746669, 'fraction_numerical': 0.03745318352059925, 'mean_word_length': 4.42193741677763, 'pattern_counts': {'":': 0, '<': 315, '<?xml version=': 0, '>': 529, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 28, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4238111', 'n_tokens_mistral': 10649, 'n_tokens_neox': 9144, 'n_words': 2348}