math: replace MIN() and MAX() macros with safer versions

Using macro arguments multiple times within the macro definition body
can lead to repeated expressioon evaluation. To avoid that replace
MIN() and MAX() macros with safer versions.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
This commit is contained in:
Guennadi Liakhovetski 2019-02-20 10:03:48 +01:00 committed by Liam Girdwood
parent a06d4b1c2c
commit 1f79acd74c
1 changed files with 10 additions and 2 deletions

View File

@ -35,8 +35,16 @@
#include <stdint.h>
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define MIN(a, b) ({ \
typeof(a) __a = (a); \
typeof(b) __b = (b); \
__a > __b ? __b : __a; \
})
#define MAX(a, b) ({ \
typeof(a) __a = (a); \
typeof(b) __b = (b); \
__a < __b ? __b : __a; \
})
int gcd(int a, int b); /* Calculate greatest common divisor for a and b */