libyggdrasil  v1.0.0
math.hpp
Go to the documentation of this file.
1  /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * _____.___. .___ .__.__ *
3  * \__ | | ____ ____ __| _/___________ _____|__| | *
4  * / | |/ ___\ / ___\ / __ |\_ __ \__ \ / ___/ | | *
5  * \____ / /_/ > /_/ > /_/ | | | \// __ \_\___ \| | |__ *
6  * / ______\___ /\___ /\____ | |__| (____ /____ >__|____/ *
7  * \/ /_____//_____/ \/ \/ \/ *
8  * - Common - *
9  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
10  * This software can be used by students and other personal of the *
11  * Bern University of Applied Sciences under the terms of the MIT *
12  * license. *
13  * For other persons this software is under the terms of the GNU *
14  * General Public License version 2. *
15  * *
16  * Copyright © 2021, Bern University of Applied Sciences. *
17  * All rights reserved. *
18  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
26 #pragma once
27 
28 #include <array>
29 #include <cmath>
30 #include <numeric>
31 #include <type_traits>
32 
34 #include <cpp/common/types.hpp>
35 
36 namespace bsp::math {
37 
44  template<typename T>
45  constexpr static inline T Pi = T(3.14159265358979323846264338327950288419716939937510);
46 
53  template<typename T>
54  constexpr static inline T e = T(2.71828182845904523536028747135266249775724709369995);
55 
56 
65  template<typename T>
66  constexpr T clamp(T value, T min, T max) {
67  return std::min(std::max(value, min), max);
68  }
69 
76  template<typename T>
77  constexpr T degreeToRadian(T deg) {
78  return (deg / 360.0) * 2 * Pi<T>;
79  }
80 
87  template<typename T>
88  constexpr T radianToDegree(T rad) {
89  return (rad / (2 * Pi<T>)) * 360.0;
90  }
91 
95  ALWAYS_INLINE constexpr u64 operator ""_kB(u64 n) {
96  return n * u64(1024);
97  }
98 
102  ALWAYS_INLINE constexpr u64 operator ""_MB(u64 n) {
103  return operator""_kB(n) * u64(1024);
104  }
105 
109  ALWAYS_INLINE constexpr u64 operator ""_GB(u64 n) {
110  return operator""_MB(n) * u64(1024);
111  }
112 
122  template<u16 Polynomial = 0x8005>
123  constexpr u16 crc16(const auto &data, u16 initialValue = 0x00) {
124  // Lookup table generation
125  constexpr auto Table = [] {
126  std::array<u16, 256> table;
127 
128  for (u16 i = 0; i < 256; i++) {
129  u16 crc = 0;
130  u16 c = i;
131 
132  for (u16 j = 0; j < 8; j++) {
133  if (((crc ^ c) & 0x0001U) != 0)
134  crc = (crc >> 1U) ^ Polynomial;
135  else
136  crc >>= 1U;
137 
138  c >>= 1U;
139  }
140 
141  table[i] = crc;
142  }
143 
144  return table;
145  }();
146 
147  // CRC16 calculation
148  u16 crc = initialValue;
149  for (u8 byte : data) {
150  crc = (crc >> 8) ^ Table[(crc ^ u16(byte)) & 0x00FF];
151  }
152 
153  return crc;
154  }
155 
164  template<u32 Polynomial = 0x04C11DB7>
165  u32 crc32(const auto &data, u32 initialValue = 0x00) {
166  // Lookup table generation
167  constexpr auto Table = [] {
168  std::array<u32, 256> table = {0};
169 
170  for (u32 i = 0; i < 256; i++) {
171  u32 c = i;
172  for (size_t j = 0; j < 8; j++) {
173  if (c & 1)
174  c = Polynomial ^ (c >> 1);
175  else
176  c >>= 1;
177  }
178  table[i] = c;
179  }
180 
181  return table;
182  }();
183 
184  // CRC32 calculation
185  u32 crc = initialValue;
186  for (u8 byte : data) {
187  crc = Table[(crc ^ byte) & 0xFF] ^ (crc >> 8);
188  }
189 
190  return ~crc;
191  }
192 
199  ALWAYS_INLINE constexpr u8 bcdToBinary(u8 bcd) {
200  return (bcd & 0x0F) + 10 * ((bcd >> 4) & 0x0F);
201  }
202 
209  ALWAYS_INLINE constexpr u8 binaryToBcd(u8 bin) {
210  return (bin % 10) | ((bin / 10) << 4);
211  }
212 
213 }
u16
uint16_t u16
Definition: types.h:37
u8
uint8_t u8
Unsigned integer definitions.
Definition: types.h:36
bsp::math::crc32
u32 crc32(const auto &data, u32 initialValue=0x00)
Calculates the CRC32 Checksum of the data in a container.
Definition: math.hpp:165
bsp::math::bcdToBinary
constexpr ALWAYS_INLINE u8 bcdToBinary(u8 bcd)
Converts a Binary Coded Decimal value to Binary.
Definition: math.hpp:199
bsp::math::binaryToBcd
constexpr ALWAYS_INLINE u8 binaryToBcd(u8 bin)
Converts a Binary value to Binary Coded Decimal.
Definition: math.hpp:209
bsp::math::radianToDegree
constexpr T radianToDegree(T rad)
Conversion between radian and degree.
Definition: math.hpp:88
bsp::math::degreeToRadian
constexpr T degreeToRadian(T deg)
Conversion between degree and radian.
Definition: math.hpp:77
types.hpp
Commonly used type definitions and helper templates.
u64
uint64_t u64
Definition: types.h:39
bsp::math
Definition: math.hpp:36
ALWAYS_INLINE
#define ALWAYS_INLINE
Definition: attributes.h:34
u32
uint32_t u32
Definition: types.h:38
bsp::math::crc16
constexpr u16 crc16(const auto &data, u16 initialValue=0x00)
Calculates the CRC16 Checksum of the data in a container.
Definition: math.hpp:123
bsp::math::clamp
constexpr T clamp(T value, T min, T max)
Clamps a input value between a min and max value.
Definition: math.hpp:66
attributes.hpp
Commonly used C++ and GNU attributes.