Branch data MC/DC data Line data Source code
1 : : : #ifndef ROCKETLIB_MATHOPS_H
2 : : : #define ROCKETLIB_MATHOPS_H
3 : : :
4 : : : #include <stdint.h>
5 : : :
6 : : : #include "common.h"
7 : : :
8 : : : /**
9 : : : * @brief Clamps a 32 bit unsigned integer between a lower and upper bound.
10 : : : *
11 : : : * @param x The value to clamp
12 : : : * @param low The lower bound, inclusive
13 : : : * @param high The upper bound, inclusive
14 : : : * @return The clamped value
15 : : : */
16 : : : static inline uint32_t value_clamp_uint32(uint32_t x, uint32_t low, uint32_t high) {
17 : : : if (x < low) {
18 : : : return low;
19 : : : } else if (x > high) {
20 : : : return high;
21 : : : } else {
22 : : : return x;
23 : : : }
24 : : : }
25 : : :
26 : : : /**
27 : : : * @brief Clamps a 32 bit signed integer between a lower and upper bound.
28 : : : *
29 : : : * @param x The value to clamp
30 : : : * @param low The lower bound, inclusive
31 : : : * @param high The upper bound, inclusive
32 : : : * @return The clamped value
33 : : : */
34 : : : static inline int32_t value_clamp_int32(int32_t x, int32_t low, int32_t high) {
35 : : : if (x < low) {
36 : : : return low;
37 : : : } else if (x > high) {
38 : : : return high;
39 : : : } else {
40 : : : return x;
41 : : : }
42 : : : }
43 : : :
44 : : : /**
45 : : : * @brief Clamps a 32 bit float between a lower and upper bound.
46 : : : *
47 : : : * @param x The value to clamp
48 : : : * @param low The lower bound, inclusive
49 : : : * @param high The upper bound, inclusive
50 : : : * @return The clamped value
51 : : : */
52 : : : static inline float32_t value_clamp_float32(float32_t x, float32_t low, float32_t high) {
53 [ - + ]: [ t F ]: 1 : if (x < low) {
54 : : : return low;
55 : : : } else if (x > high) {
56 : : : return high;
57 : : : } else {
58 : : : return x;
59 : : : }
60 : : : }
61 : : :
62 : : : #endif
|