Created
March 15, 2018 08:20
-
-
Save jnareb/9d42b3f4095d9f543d4df89844ccfa54 to your computer and use it in GitHub Desktop.
max() preprocessor macro with single-evaluation, constant expression for constants
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* https://lwn.net/Articles/748870/ */ | |
#define __single_eval_max(t1, t2, max1, max2, x, y) ({ \ | |
t1 max1 = (x); \ | |
t2 max2 = (y); \ | |
(void) (&max1 == &max2); \ | |
max1 > max2 ? max1 : max2; }) | |
#define __max(t1, t2, x, y) \ | |
__builtin_choose_expr(__builtin_constant_p(x) && \ | |
__builtin_constant_p(y), \ | |
(t1)(x) > (t2)(y) ? (t1)(x) : (t2)(y), \ | |
__single_eval_max(t1, t2, \ | |
__UNIQUE_ID(max1_), \ | |
__UNIQUE_ID(max2_), \ | |
x, y)) | |
#define max(x, y) __max(typeof(x), typeof(y), x, y) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment