From 1b385b01ec2632c5e8050fa5c6dab8f64b53dd35 Mon Sep 17 00:00:00 2001 From: MCUdude Date: Tue, 17 Jan 2023 22:45:52 +0100 Subject: [PATCH] Squashed 'avr/cores/MCUdude_corefiles/' changes from 5ddffca3f..0d56ecc53 0d56ecc53 Use template based functions for radians() and degrees() by default a9ade41df Use template functions instead of macros by default This will make it possible to use the previous "typeof" macros outside functions and structures b811cd119 Use template based min/max functions by default git-subtree-dir: avr/cores/MCUdude_corefiles git-subtree-split: 0d56ecc539d7047899835fdc040d8748c3afc564 --- Arduino.h | 78 ++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 60 insertions(+), 18 deletions(-) diff --git a/Arduino.h b/Arduino.h index e1d36fe25..4796f2197 100755 --- a/Arduino.h +++ b/Arduino.h @@ -139,24 +139,6 @@ void yield(void); #endif -// undefine stdlib's abs if encountered -#ifdef abs -#undef abs -#endif - -#define abs(x) ({ typeof (x) _x = (x); _x > 0 ? _x : -_x; }) -#define sq(x) ({ typeof (x) _x = (x); _x * _x; }) -#define min(a,b) ({ typeof (a) _a = (a); typeof (b) _b = (b); _a < _b ? _a : _b; }) -#define max(a,b) ({ typeof (a) _a = (a); typeof (b) _b = (b); _a > _b ? _a : _b; }) -#define round(x) ({ typeof (x) _x = (x); _x >= 0 ? (long)(_x + 0.5) : (long)(_x - 0.5); }) -#define radians(deg) ((deg) * DEG_TO_RAD) -#define degrees(rad) ((rad) * RAD_TO_DEG) -#define constrain(x,low,high) ({ \ - typeof (x) _x = (x); \ - typeof (low) _l = (low); \ - typeof (high) _h = (high); \ - _x < _l ? _l : _x > _h ? _h : _x; }) - #define interrupts() sei() #define noInterrupts() cli() @@ -316,6 +298,66 @@ extern const uint8_t PROGMEM digital_pin_to_timer_PGM[]; } // extern "C" #endif +// Undefine stdlib's abs if encountered +#ifdef abs + #undef abs +#endif + +#ifdef __cplusplus + template + auto abs(const T& x) -> decltype(x > 0 ? x : -x) { + return x > 0 ? x : -x; + } + + template + auto min(const T& a, const L& b) -> decltype((b < a) ? b : a) { + return (b < a) ? b : a; + } + + template + auto max(const T& a, const L& b) -> decltype((b < a) ? b : a) { + return (a < b) ? b : a; + } + + template + long round(const T& x) { + return (long)(x >= 0 ? (x + 0.5) : (x - 0.5)); + } + + template + auto sq(const T& x) -> decltype(x * x) { + return x * x; + } + + template + auto radians(const T& deg) -> decltype(deg * DEG_TO_RAD) { + return deg * DEG_TO_RAD; + } + + template + auto degrees(const T& rad) -> decltype(rad * RAD_TO_DEG) { + return rad * RAD_TO_DEG; + } + + template + auto constrain(const T& x, const L& l, const H& h) -> decltype((x < l) ? l : (x > h) ? h : x) { + return (x < l) ? l : (x > h) ? h : x; + } + +#else + #define abs(x) ({ typeof (x) _x = (x); _x > 0 ? _x : -_x; }) + #define min(a,b) ({ typeof (a) _a = (a); typeof (b) _b = (b); _a < _b ? _a : _b; }) + #define max(a,b) ({ typeof (a) _a = (a); typeof (b) _b = (b); _a > _b ? _a : _b; }) + #define sq(x) ({ typeof (x) _x = (x); _x * _x; }) + #define radians(deg) ((deg) * DEG_TO_RAD) + #define degrees(rad) ((rad) * RAD_TO_DEG) + #define constrain(x,low,high) ({ \ + typeof (x) _x = (x); \ + typeof (low) _l = (low); \ + typeof (high) _h = (high); \ + _x < _l ? _l : _x > _h ? _h : _x; }) +#endif // __cplusplus + #ifdef __cplusplus #include "WCharacter.h" #include "WString.h"