This is an old version of the documentation. View the latest version here.
Control Surface  1.0.0
MIDI Control Surface library for Arduino
IncreaseBitDepth.hpp
Go to the documentation of this file.
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 enable_if_t<(Bits_out <= 2 * Bits_in), T_out> increaseBitDepthImpl(T_in in) {
19  constexpr size_t leftShift = Bits_out - Bits_in;
20  constexpr size_t rightShift = Bits_in - leftShift;
21  return (T_out(in) << leftShift) | (in >> rightShift);
22 }
23 
26 
59 template <size_t Bits_out, size_t Bits_in, class T_out, class T_in>
60 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  return increaseBitDepthImpl<Bits_out, Bits_in, T_out>(in);
66 }
67 
68 /// @}
increaseBitDepthImpl
enable_if_t<(Bits_out<=2 *Bits_in), T_out > increaseBitDepthImpl(T_in in)
Definition: IncreaseBitDepth.hpp:8
enable_if.hpp
AVR Arduinos don't have access to the STL, so this is a simple implementation of some of the SFINAE s...
enable_if_t
typename enable_if< B, T >::type enable_if_t
Definition: enable_if.hpp:21
increaseBitDepth
T_out increaseBitDepth(T_in in)
Increase the bit depth of the given value from Bits_in bits wide to Bits_out bits wide,...
Definition: IncreaseBitDepth.hpp:60