Skip to content

Commit 4e63612

Browse files
committed
pbio/math: Keep implementation in source.
This makes everything easier to find, and ensures that Doxygen does not ignore them. We can do things like f4dacb1 when we are 20 bytes short or if there is a major runtime difference.
1 parent 341d0bc commit 4e63612

File tree

2 files changed

+72
-71
lines changed

2 files changed

+72
-71
lines changed

lib/pbio/include/pbio/math.h

Lines changed: 5 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -14,81 +14,15 @@
1414
#include <stdint.h>
1515
#include <stdbool.h>
1616

17-
/**
18-
* Gets the absolute value.
19-
*
20-
* @param [in] value The value.
21-
* @return The absolute (positive) value.
22-
*/
23-
static inline int32_t pbio_math_abs(int32_t value) {
24-
return __builtin_abs(value);
25-
}
26-
27-
/**
28-
* Gets the maximum of two values.
29-
*
30-
* @param [in] a Value.
31-
* @param [in] b Value.
32-
* @return a if it is greater than b, else b.
33-
*/
34-
static inline int32_t pbio_math_max(int32_t a, int32_t b) {
35-
if (a > b) {
36-
return a;
37-
}
38-
39-
return b;
40-
}
41-
42-
/**
43-
* Gets the minimum of two values.
44-
*
45-
* @param [in] a Value.
46-
* @param [in] b Value.
47-
* @return a if it is less than b, else b.
48-
*/
49-
static inline int32_t pbio_math_min(int32_t a, int32_t b) {
50-
if (a < b) {
51-
return a;
52-
}
53-
54-
return b;
55-
}
56-
57-
/**
58-
* Get the sign of @p a.
59-
*
60-
* @param [in] a A signed integer value.
61-
* @return 1 if @p a is positive, -1 if @p a is negative or 0 if @p a
62-
* is 0.
63-
*/
64-
static inline int32_t pbio_math_sign(int32_t a) {
65-
if (a == 0) {
66-
return 0;
67-
}
68-
69-
return a > 0 ? 1 : -1;
70-
}
71-
72-
/**
73-
* Checks that the signs of @p a and @p b are not opposite.
74-
*
75-
* @param [in] a A signed integer value.
76-
* @param [in] b A signed integer value.
77-
* @return True if either value is zero or if the signs are the same,
78-
* else false.
79-
*/
80-
static inline bool pbio_math_sign_not_opposite(int32_t a, int32_t b) {
81-
if (a == 0 || b == 0) {
82-
return true;
83-
}
84-
85-
return (a > 0) == (b > 0);
86-
}
87-
17+
bool pbio_math_sign_not_opposite(int32_t a, int32_t b);
18+
int32_t pbio_math_abs(int32_t value);
8819
int32_t pbio_math_atan2(int32_t y, int32_t x);
8920
int32_t pbio_math_bind(int32_t value, int32_t min, int32_t max);
9021
int32_t pbio_math_clamp(int32_t value, int32_t abs_max);
22+
int32_t pbio_math_max(int32_t a, int32_t b);
23+
int32_t pbio_math_min(int32_t a, int32_t b);
9124
int32_t pbio_math_mult_then_div(int32_t a, int32_t b, int32_t c);
25+
int32_t pbio_math_sign(int32_t a);
9226
int32_t pbio_math_sqrt(int32_t n);
9327

9428
#endif // _PBIO_MATH_H_

lib/pbio/src/math.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,73 @@
88
#include <pbio/math.h>
99
#include <pbio/util.h>
1010

11+
/**
12+
* Gets the absolute value.
13+
*
14+
* @param [in] value The value.
15+
* @return The absolute (positive) value.
16+
*/
17+
int32_t pbio_math_abs(int32_t value) {
18+
return __builtin_abs(value);
19+
}
20+
21+
/**
22+
* Gets the maximum of two values.
23+
*
24+
* @param [in] a Value.
25+
* @param [in] b Value.
26+
* @return a if it is greater than b, else b.
27+
*/
28+
int32_t pbio_math_max(int32_t a, int32_t b) {
29+
if (a > b) {
30+
return a;
31+
}
32+
return b;
33+
}
34+
35+
/**
36+
* Gets the minimum of two values.
37+
*
38+
* @param [in] a Value.
39+
* @param [in] b Value.
40+
* @return a if it is less than b, else b.
41+
*/
42+
int32_t pbio_math_min(int32_t a, int32_t b) {
43+
if (a < b) {
44+
return a;
45+
}
46+
return b;
47+
}
48+
49+
/**
50+
* Get the sign of @p a.
51+
*
52+
* @param [in] a A signed integer value.
53+
* @return 1 if @p a is positive, -1 if @p a is negative or 0 if @p a
54+
* is 0.
55+
*/
56+
int32_t pbio_math_sign(int32_t a) {
57+
if (a == 0) {
58+
return 0;
59+
}
60+
return a > 0 ? 1 : -1;
61+
}
62+
63+
/**
64+
* Checks that the signs of @p a and @p b are not opposite.
65+
*
66+
* @param [in] a A signed integer value.
67+
* @param [in] b A signed integer value.
68+
* @return True if either value is zero or if the signs are the same,
69+
* else false.
70+
*/
71+
bool pbio_math_sign_not_opposite(int32_t a, int32_t b) {
72+
if (a == 0 || b == 0) {
73+
return true;
74+
}
75+
return (a > 0) == (b > 0);
76+
}
77+
1178
/**
1279
* Binds a value between a lower and upper limit.
1380
*

0 commit comments

Comments
 (0)