From fcb1b0c85df1a095fbdcf201f79e08ed433b5299 Mon Sep 17 00:00:00 2001 From: Yuri D'Elia Date: Tue, 13 Jul 2021 01:08:33 +0200 Subject: [PATCH] cmath.h: convert AVR min/max/abs into templates At least for min/max this will ensure types for both arguments are the same. This should be a little bit closer to the definition that simply overloads these functions instead. --- src/cmath.h | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/cmath.h b/src/cmath.h index bf876b3..ffd6128 100644 --- a/src/cmath.h +++ b/src/cmath.h @@ -3,15 +3,26 @@ #pragma once #ifndef __AVR__ - #include +#include #else - // AVR libc doesn't support cmath - #include +// AVR libc doesn't support cmath +#include - // Use builtin functions for min/max/abs - #define min(a, b) __builtin_min((a, b)) - #define max(a, b) __builtin_max((a, b)) - #define abs(n) __builtin_abs((n)) +// Use builtin functions for min/max/abs +template +static inline const T min(T a, T b) { + return __builtin_min((a, b)); +} + +template +static inline const T max(T a, T b) { + return __builtin_max((a, b)); +} + +template +static inline const T abs(T n) { + return __builtin_abs((n)); +} #endif