LCOV - code coverage report
Current view: top level - src/Helpers - IncreaseBitDepth.hpp (source / functions) Hit Total Coverage
Test: 19d2efc7037c2e176feca44750a12594c76f466f Lines: 6 6 100.0 %
Date: 2019-11-24 14:50:27 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : #pragma once
       2             : 
       3             : #include <Helpers/enable_if.hpp>
       4             : #include <limits.h> // CHAR_BIT
       5             : #include <stddef.h> // size_t
       6             : 
       7             : template <size_t Bits_out, size_t Bits_in, class T_out, class T_in>
       8             : enable_if_t<(Bits_out <= 2 * Bits_in), T_out> increaseBitDepthImpl(T_in in);
       9             : 
      10             : template <size_t Bits_out, size_t Bits_in, class T_out, class T_in>
      11             : enable_if_t<(Bits_out > 2 * Bits_in), T_out> increaseBitDepthImpl(T_in in) {
      12             :     constexpr size_t leftShift = Bits_out - Bits_in;
      13             :     return (T_out(in) << leftShift) |
      14             :            increaseBitDepthImpl<leftShift, Bits_in, T_out>(in);
      15             : }
      16             : 
      17             : template <size_t Bits_out, size_t Bits_in, class T_out, class T_in>
      18          27 : enable_if_t<(Bits_out <= 2 * Bits_in), T_out> increaseBitDepthImpl(T_in in) {
      19          27 :     constexpr size_t leftShift = Bits_out - Bits_in;
      20          27 :     constexpr size_t rightShift = Bits_in - leftShift;
      21          27 :     return (T_out(in) << leftShift) | (in >> rightShift);
      22             : }
      23             : 
      24             : /// @addtogroup    Utilities
      25             : /// @{
      26             : 
      27             : /**
      28             :  * @brief   Increase the bit depth of the given value from `Bits_in` bits wide
      29             :  *          to `Bits_out` bits wide, evenly distributing the error across the 
      30             :  *          entire range, such that the error for each element is between -0.5
      31             :  *          and +0.5.
      32             :  * 
      33             :  * For example, converting 3-bit numbers to 7-bit numbers would result in the
      34             :  * following:
      35             :  * 
      36             :  * | in (dec) | in (bin) | out (bin) | out (dec) | exact  | error |
      37             :  * |:--------:|:--------:|:---------:|:---------:|:------:|:-----:|
      38             :  * | 0        | 000      | 000'0000  | 0         | 0.00   | +0.00 |
      39             :  * | 1        | 001      | 001'0010  | 18        | 18.14  | +0.14 |
      40             :  * | 2        | 010      | 010'0100  | 36        | 36.29  | +0.29 |
      41             :  * | 3        | 011      | 011'0110  | 54        | 54.43  | +0.43 |
      42             :  * | 4        | 100      | 100'1001  | 73        | 72.57  | -0.43 |
      43             :  * | 5        | 101      | 101'1011  | 91        | 90.71  | -0.29 |
      44             :  * | 6        | 110      | 110'1101  | 109       | 108.86 | -0.14 |
      45             :  * | 7        | 111      | 111'1111  | 127       | 127.00 | +0.00 |
      46             :  * 
      47             :  * @tparam  Bits_out 
      48             :  *          The number of bits of the output range.
      49             :  * @tparam  Bits_in 
      50             :  *          The number of bits of the input range.
      51             :  * @tparam  T_out 
      52             :  *          The type of the output (return type).
      53             :  * @tparam  T_in 
      54             :  *          The type of the input.
      55             :  * @param   in 
      56             :  *          The value to scale up.
      57             :  * @return  The scaled up value.
      58             :  */
      59             : template <size_t Bits_out, size_t Bits_in, class T_out, class T_in>
      60          27 : T_out increaseBitDepth(T_in in) {
      61             :     static_assert(Bits_in <= sizeof(T_in) * CHAR_BIT,
      62             :                   "Error: Bits_in > bits(T_in)");
      63             :     static_assert(Bits_out <= sizeof(T_out) * CHAR_BIT,
      64             :                   "Error: Bits_out > bits(T_out)");
      65          27 :     return increaseBitDepthImpl<Bits_out, Bits_in, T_out>(in);
      66             : }
      67             : 
      68             : /// @}

Generated by: LCOV version 1.14-5-g4ff2ed6