Control Surface
main
MIDI Control Surface library for Arduino
Toggle main menu visibility
Loading...
Searching...
No Matches
src
AH
Math
Divide.hpp
Go to the documentation of this file.
1
#pragma once
2
3
#include <AH/STL/cstdint>
4
#include <AH/STL/type_traits>
5
#include <
AH/Settings/NamespaceSettings.hpp
>
6
#include <stddef.h>
7
8
BEGIN_AH_NAMESPACE
9
13
template
<u
int
8_t N,
class
T>
14
struct
round_div_default
{
15
static
T
div
(T val) {
return
val / N; }
16
};
17
19
template
<u
int
8_t N,
class
T>
20
struct
round_div_unsigned_int
{
21
static
T
div
(T val) {
22
return
(val + (N / 2)) / N;
23
static_assert
(std::is_unsigned<T>::value && std::is_integral<T>::value,
24
"This function is only valid for unsigned integers"
);
25
}
26
};
27
29
template
<u
int
8_t N,
class
T>
30
struct
round_div_signed_int
{
31
static
T
div
(T val) {
32
T offset = val >= 0 ? (N / 2) : (-N / 2);
33
return
(val + offset) / N;
34
}
35
};
36
39
template
<u
int
8_t N,
class
T>
40
struct
round_div_int
41
: std::conditional<std::is_signed<T>::value, round_div_signed_int<N, T>,
42
round_div_unsigned_int<N, T>>::type {};
43
46
template
<u
int
8_t N,
class
T>
47
struct
round_div_helper
48
: std::conditional<std::is_integral<T>::value, round_div_int<N, T>,
49
round_div_default<N, T>>::type {};
50
53
template
<
size_t
N,
class
T>
54
T
round_div
(T val) {
55
return
round_div_helper<N, T>::div
(val);
56
}
57
58
END_AH_NAMESPACE
NamespaceSettings.hpp
END_AH_NAMESPACE
#define END_AH_NAMESPACE
Definition
AH/Settings/NamespaceSettings.hpp:14
BEGIN_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
Definition
AH/Settings/NamespaceSettings.hpp:11
AH::round_div
T round_div(T val)
Divide a number by N and round the result.
Definition
Divide.hpp:54
::round_div_default
Divide by N using the default division operator, without explicit rounding This should be used for fl...
Definition
Divide.hpp:14
AH::round_div_default::div
static T div(T val)
Definition
Divide.hpp:15
::round_div_helper
Select the right rounding division operator, depending on whether T is an integer or not.
Definition
Divide.hpp:49
::round_div_int
Select the right rounding division operator, depending on whether T is a signed or unsigned integer.
Definition
Divide.hpp:42
::round_div_signed_int
Divide a signed integer by N, rounding the result.
Definition
Divide.hpp:30
AH::round_div_signed_int::div
static T div(T val)
Definition
Divide.hpp:31
::round_div_unsigned_int
Divide an unsigned integer by N, rounding the result.
Definition
Divide.hpp:20
AH::round_div_unsigned_int::div
static T div(T val)
Definition
Divide.hpp:21
Generated by
1.17.0