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 <cmath> definition that
simply overloads these functions instead.
pull/47/head
Yuri D'Elia 2021-07-13 01:08:33 +02:00
parent 98845008aa
commit fcb1b0c85d
1 changed files with 18 additions and 7 deletions

View File

@ -3,15 +3,26 @@
#pragma once #pragma once
#ifndef __AVR__ #ifndef __AVR__
#include <cmath> #include <cmath>
#else #else
// AVR libc doesn't support cmath // AVR libc doesn't support cmath
#include <math.h> #include <math.h>
// Use builtin functions for min/max/abs // Use builtin functions for min/max/abs
#define min(a, b) __builtin_min((a, b)) template <typename T>
#define max(a, b) __builtin_max((a, b)) static inline const T min(T a, T b) {
#define abs(n) __builtin_abs((n)) return __builtin_min((a, b));
}
template <typename T>
static inline const T max(T a, T b) {
return __builtin_max((a, b));
}
template <typename T>
static inline const T abs(T n) {
return __builtin_abs((n));
}
#endif #endif