From 8c9947eec2b2619a47d85f7e374c56967446cf5e Mon Sep 17 00:00:00 2001 From: Keating Reid Date: Sat, 23 Jan 2021 12:16:25 -0500 Subject: [PATCH] Update math macros to avoid computing callable arguments more than once --- cores/arduino/Arduino.h | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/cores/arduino/Arduino.h b/cores/arduino/Arduino.h index 91eeb16bc..00e0582f7 100644 --- a/cores/arduino/Arduino.h +++ b/cores/arduino/Arduino.h @@ -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) -#define sq(x) ((x)*(x)) #define interrupts() sei() #define noInterrupts() cli() 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