foxBMS - Unit Tests  1.6.0
The foxBMS Unit Tests API Documentation
foxmath.c
Go to the documentation of this file.
1 /**
2  *
3  * @copyright © 2010 - 2023, Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V.
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright notice, this
12  * list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the documentation
16  * and/or other materials provided with the distribution.
17  *
18  * 3. Neither the name of the copyright holder nor the names of its
19  * contributors may be used to endorse or promote products derived from
20  * this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  * We kindly request you to use one or more of the following phrases to refer to
34  * foxBMS in your hardware, software, documentation or advertising materials:
35  *
36  * - ″This product uses parts of foxBMS®″
37  * - ″This product includes parts of foxBMS®″
38  * - ″This product is derived from foxBMS®″
39  *
40  */
41 
42 /**
43  * @file foxmath.c
44  * @author foxBMS Team
45  * @date 2018-01-18 (date of creation)
46  * @updated 2023-10-12 (date of last update)
47  * @version v1.6.0
48  * @ingroup DRIVERS
49  * @prefix MATH
50  *
51  * @brief mathlib function implementations
52  *
53  */
54 
55 /*========== Includes =======================================================*/
56 #include "foxmath.h"
57 
58 #include "fassert.h"
59 #include "utils.h"
60 
61 #include <math.h>
62 #include <stdint.h>
63 
64 /*========== Macros and Definitions =========================================*/
65 
66 /*========== Static Constant and Variable Definitions =======================*/
67 
68 /*========== Extern Constant and Variable Definitions =======================*/
69 
70 /*========== Static Function Prototypes =====================================*/
71 
72 /*========== Static Function Implementations ================================*/
73 
74 /*========== Extern Function Implementations ================================*/
75 
76 /* AXIVION Disable Style Generic-MissingParameterAssert: If not specified otherwise, functions in this lib are
77  designed to take full range input. No assert needed. */
78 
79 extern void MATH_StartupSelfTest(void) {
80  FAS_ASSERT(MATH_AbsInt64_t(INT64_MIN) == INT64_MAX);
81  FAS_ASSERT(MATH_AbsInt32_t(INT32_MIN) == INT32_MAX);
82 }
83 
84 extern float_t MATH_LinearInterpolation(
85  const float_t x1,
86  const float_t y1,
87  const float_t x2,
88  const float_t y2,
89  const float_t x_interpolate) {
90  float_t slope = 0.0f;
91 
92  if (fabsf(x1 - x2) >= FLT_EPSILON) {
93  /* Calculate slope */
94  slope = (y2 - y1) / (x2 - x1);
95  }
96  /* In the case that the if clause is not entered (x values are identical)
97  * -> no interpolation possible: return y1 value
98  * -> slope takes initialization value of 0.0f
99  */
100 
101  /* Interpolate starting from x1/y1 */
102  float_t y_interpolate = y1 + (slope * (x_interpolate - x1));
103 
104  return y_interpolate;
105 }
106 
107 extern uint16_t MATH_SwapBytesUint16_t(const uint16_t val) {
108  return (val << UTIL_SHIFT_ONE_BYTE) | (val >> UTIL_SHIFT_ONE_BYTE);
109 }
110 
111 extern uint32_t MATH_SwapBytesUint32_t(const uint32_t val) {
112  const uint32_t alternating2PatternStartFF = 0xFF00FF00u;
113  const uint32_t alternating2PatternStart00 = 0x00FF00FFu;
114  const uint32_t intermediate = ((val << UTIL_SHIFT_ONE_BYTE) & alternating2PatternStartFF) |
115  ((val >> UTIL_SHIFT_ONE_BYTE) & alternating2PatternStart00);
116  return (intermediate << UTIL_SHIFT_TWO_BYTES) | (intermediate >> UTIL_SHIFT_TWO_BYTES);
117 }
118 
119 extern uint64_t MATH_SwapBytesUint64_t(const uint64_t val) {
120  const uint64_t alternating2PatternStartFF = 0xFF00FF00FF00FF00uLL;
121  const uint64_t alternating2PatternStart00 = 0x00FF00FF00FF00FFuLL;
122  const uint64_t alternating4PatternStartFFFF = 0xFFFF0000FFFF0000uLL;
123  const uint64_t alternating4PatternStart0000 = 0x0000FFFF0000FFFFuLL;
124 
125  uint64_t intermediate = ((val << UTIL_SHIFT_ONE_BYTE) & alternating2PatternStartFF) |
126  ((val >> UTIL_SHIFT_ONE_BYTE) & alternating2PatternStart00);
127  intermediate = ((intermediate << UTIL_SHIFT_TWO_BYTES) & alternating4PatternStartFFFF) |
128  ((intermediate >> UTIL_SHIFT_TWO_BYTES) & alternating4PatternStart0000);
129  return (intermediate << UTIL_SHIFT_FOUR_BYTES) | (intermediate >> UTIL_SHIFT_FOUR_BYTES);
130 }
131 
132 extern float_t MATH_MinimumOfTwoFloats(const float_t value1, const float_t value2) {
133  return fminf(value1, value2);
134 }
135 
136 extern uint8_t MATH_MinimumOfTwoUint8_t(const uint8_t value1, const uint8_t value2) {
137  uint8_t minimumValue = value1;
138  if (minimumValue > value2) {
139  minimumValue = value2;
140  }
141  return minimumValue;
142 }
143 
144 extern uint16_t MATH_MinimumOfTwoUint16_t(const uint16_t value1, const uint16_t value2) {
145  uint16_t minimumValue = value1;
146  if (minimumValue > value2) {
147  minimumValue = value2;
148  }
149  return minimumValue;
150 }
151 
152 extern int32_t MATH_AbsInt32_t(const int32_t value) {
153  int32_t absoluteValue = INT32_MAX;
154  if (value != INT32_MIN) {
155  absoluteValue = labs(value);
156  }
157  return absoluteValue;
158 }
159 
160 extern int64_t MATH_AbsInt64_t(const int64_t value) {
161  int64_t absoluteValue = INT64_MAX;
162  if (value != INT64_MIN) {
163  absoluteValue = llabs(value);
164  }
165  return absoluteValue;
166 }
167 
168 /* AXIVION Enable Style Generic-MissingParameterAssert: */
169 
170 /*========== Externalized Static Function Implementations (Unit Test) =======*/
171 #ifdef UNITY_UNIT_TEST
172 #endif
Assert macro implementation.
#define FAS_ASSERT(x)
Assertion macro that asserts that x is true.
Definition: fassert.h:251
int32_t MATH_AbsInt32_t(const int32_t value)
Returns the absolute value of passed int32_t value.
Definition: foxmath.c:152
float_t MATH_MinimumOfTwoFloats(const float_t value1, const float_t value2)
Returns the minimum of the passed float values.
Definition: foxmath.c:132
uint16_t MATH_SwapBytesUint16_t(const uint16_t val)
Swap bytes of uint16_t value.
Definition: foxmath.c:107
int64_t MATH_AbsInt64_t(const int64_t value)
Returns the absolute value of passed int64_t value.
Definition: foxmath.c:160
uint16_t MATH_MinimumOfTwoUint16_t(const uint16_t value1, const uint16_t value2)
Returns the minimum of the passed uint16_t values.
Definition: foxmath.c:144
uint32_t MATH_SwapBytesUint32_t(const uint32_t val)
Swap bytes of uint32_t value.
Definition: foxmath.c:111
void MATH_StartupSelfTest(void)
: self test for math functions that can be called at startup
Definition: foxmath.c:79
float_t MATH_LinearInterpolation(const float_t x1, const float_t y1, const float_t x2, const float_t y2, const float_t x_interpolate)
Linear inter-/extrapolates a third point according to two given points.
Definition: foxmath.c:84
uint8_t MATH_MinimumOfTwoUint8_t(const uint8_t value1, const uint8_t value2)
Returns the minimum of the passed uint8_t values.
Definition: foxmath.c:136
uint64_t MATH_SwapBytesUint64_t(const uint64_t val)
Swap bytes of uint64_t value.
Definition: foxmath.c:119
math library for often used math functions
Utilities.
#define UTIL_SHIFT_FOUR_BYTES
Definition: utils.h:80
#define UTIL_SHIFT_TWO_BYTES
Definition: utils.h:74
#define UTIL_SHIFT_ONE_BYTE
Definition: utils.h:71