Skip to content

Commit be460bc

Browse files
committed
make C macros evaluate arguments once; reduce unnecessary parentheses
1 parent 57dec20 commit be460bc

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

api/Common.h

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,22 @@ typedef enum {
4444
#ifdef __cplusplus
4545

4646
template<class T, class L>
47-
auto min(const T& a, const L& b) -> decltype((b < a) ? b : a)
47+
auto min(const T& a, const L& b) -> decltype(b < a ? b : a)
4848
{
4949
return (b < a) ? b : a;
5050
}
5151

5252
template<class T, class L>
53-
auto max(const T& a, const L& b) -> decltype((b < a) ? b : a)
53+
auto max(const T& a, const L& b) -> decltype(b < a ? b : a)
5454
{
5555
return (a < b) ? b : a;
5656
}
5757

5858

5959
template<class T, class U, class V>
60-
auto constrain(const T& amt, const U& low, const V& high) -> decltype((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
60+
auto constrain(const T& amt, const U& low, const V& high) -> decltype(amt < low ? low : (amt > high ? high : amt))
6161
{
62-
return (amt)<(low)?(low):((amt)>(high)?(high):(amt));
62+
return amt < low ? low : (amt > high ? high : amt);
6363
}
6464

6565
template<class T>
@@ -81,19 +81,29 @@ typedef enum {
8181
}
8282
#else
8383
#ifndef constrain
84-
#define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
84+
#define constrain(amt,low,high) \
85+
({ __typeof__ (amt) _amt = (amt); \
86+
__typeof__ (low) _low = (low); \
87+
__typeof__ (high) _high = (high); \
88+
_amt < _low ? _low : (_amt > _high ? _high :_amt); }
8589
#endif
8690

8791
#ifndef radians
88-
#define radians(deg) ((deg)*DEG_TO_RAD)
92+
#define radians(deg) \
93+
({ __typeof__ (deg) _deg = deg; \
94+
_deg * DEG_TO_RAD; })
8995
#endif
9096

9197
#ifndef degrees
92-
#define degrees(rad) ((rad)*RAD_TO_DEG)
98+
#define degrees(rad) \
99+
({ __typeof__ (rad) _rad = rad; \
100+
_rad * RAD_TO_DEG; })
93101
#endif
94102

95103
#ifndef sq
96-
#define sq(x) ((x)*(x))
104+
#define sq(x) \
105+
({ __typeof__ (x) _x = x; \
106+
_x * _x; })
97107
#endif
98108

99109
#ifndef min

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy