Control Surface stm32
MIDI Control Surface library for Arduino
IncreaseBitDepth.hpp
Go to the documentation of this file.
1#pragma once
2
4AH_DIAGNOSTIC_WERROR() // Enable errors on warnings
5
6#include <AH/STL/type_traits> // enable_if
7#include <limits.h> // CHAR_BIT
8#include <stddef.h> // size_t
9
11
12template <size_t Bits_out, size_t Bits_in, class T_out, class T_in>
13std::enable_if_t<(Bits_out <= 2 * Bits_in), T_out>
15 constexpr size_t leftShift = Bits_out - Bits_in;
16 constexpr size_t rightShift = Bits_in - leftShift;
17 return (T_out(in) << leftShift) | (in >> rightShift);
18}
19
20template <size_t Bits_out, size_t Bits_in, class T_out, class T_in>
21std::enable_if_t<(Bits_out > 2 * Bits_in), T_out>
23 constexpr size_t leftShift = Bits_out - Bits_in;
24 return (T_out(in) << leftShift) |
25 increaseBitDepthImpl<leftShift, Bits_in, T_out>(in);
26}
27
30
63template <size_t Bits_out, size_t Bits_in, class T_out, class T_in>
64T_out increaseBitDepth(T_in in) {
65 static_assert(Bits_in <= sizeof(T_in) * CHAR_BIT,
66 "Error: Bits_in > bits(T_in)");
67 static_assert(Bits_out <= sizeof(T_out) * CHAR_BIT,
68 "Error: Bits_out > bits(T_out)");
69 return increaseBitDepthImpl<Bits_out, Bits_in, T_out>(in);
70}
71
73
75
#define END_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
#define AH_DIAGNOSTIC_POP()
Definition: Warnings.hpp:37
#define AH_DIAGNOSTIC_WERROR()
Definition: Warnings.hpp:36
T_out increaseBitDepth(T_in in)
Increase the bit depth of the given value from Bits_in bits wide to Bits_out bits wide,...
std::enable_if_t<(Bits_out > 2 *Bits_in), T_out > increaseBitDepthImpl(T_in in)