Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 32 additions & 6 deletions cores/arduino/Arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,40 @@ void yield(void);
#undef abs
#endif

#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(x) ((x)>0?(x):-(x))
#define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
#define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
#ifdef __GNUC__
#define max(a,b) \
({ typeof (a) _a = (a); \
typeof (b) _b = (b); \
_a > _b ? _a : _b; })
#define min(a,b) \
({ typeof (a) _a = (a); \
typeof (b) _b = (b); \
_a < _b ? _a : _b; })
#define abs(a) \
({ typeof (a) _a = (a); \
_a > 0 ? _a : _a * -1; })
#define constrain(amt, low, high) \
({ typeof (amt) _amt = (amt); \
typeof (low) _low = (low); \
typeof (high) _high = (high); \
_amt < _low ? _low : _amt > _high ? _high : _amt; })
#define round(x) \
({ typeof (x) _x = (x); \
((_x)>=0?(long)((_x)+0.5):(long)((_x)-0.5)) })
#define sq(x) \
({ typeof (x) _x = (x); \
_x * _x })
#else
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(x) ((x)>0?(x):-(x))
#define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
#define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
#define sq(x) ((x)*(x))
#endif

#define radians(deg) ((deg)*DEG_TO_RAD)
#define degrees(rad) ((rad)*RAD_TO_DEG)
Comment on lines +123 to 125
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two lines (124, 125 for radians and degrees) expose the very same problem as do all macro calls. Why not define them as inline constexpr functions instead? Like so:
'
template<typename T> inline constexpr T min(T a, T b) { return a > b ? b : a; }

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because that doesn't work in C, and these macros are currently usable from C, so that would break compatibility.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was also concerned about binary size.

#define sq(x) ((x)*(x))

#define interrupts() sei()
#define noInterrupts() cli()
Expand Down