The joy of max()
#define __typecheck(x, y) \ (!!(sizeof((typeof(x)*)1 == (typeof(y)*)1))) #define __is_constant(x) \ (sizeof(int) == sizeof(*(1 ? ((void*)((long)(x) * 0l)) : (int*)1))) #define __no_side_effects(x, y) \ (__is_constant(x) && __is_constant(y)) #define __safe_cmp(x, y) \ (__typecheck(x, y) && __no_side_effects(x, y)) #define __cmp(x, y, op) ((x) op (y) ? (x) : (y)) #define __cmp_once(x, y, op) ({ \ typeof(x) __x = (x); \ typeof(y) __y = (y); \ __cmp(__x, __y, op); }) #define __careful_cmp(x, y, op) \ __builtin_choose_expr(__safe_cmp(x, y), \ __cmp(x, y, op), __cmp_once(x, y, op)) #define max(x, y) __careful_cmp(x, y, >)
The above definitions should, of course, be immediately obvious to any LWN
reader. For those who want an extra hint or two, though, the patch posting
includes a few explanatory comments.
(Log in to post comments)
The joy of max()
Posted Mar 29, 2018 8:46 UTC (Thu) by flewellyn (subscriber, #5047) [Link]
The joy of max()
Posted Mar 30, 2018 22:26 UTC (Fri) by simcop2387 (subscriber, #101710) [Link]
The joy of max()
Posted Mar 31, 2018 1:50 UTC (Sat) by lambda (subscriber, #40735) [Link]
If you follow the thread, you'll find that the "why some things were chosen the way they were" is a combination of some very careful reading of the C standard, some clever usage of some obscure rules of the difference in type between (int *) 0
and (int *) 1
(the first is actually of type void *
), along with a lot of testing out what worked and what didn't with different versions of GCC on godbolt.org because a lot of it depended on exactly how clever the compiler constant expression evaluator was and how that interacted with type checking.
In other words, why some things were done the way they were is just that different closely related things were tried until a version could be found that did the right thing on a broad enough range of compiler versions. This isn't likely to be highly portable C code; but it works on enough versions of GCC that it can be built on the GCC included in the oldest supported versions of various major Linux distros.
The joy of max()
Posted Apr 5, 2018 14:35 UTC (Thu) by HelloWorld (guest, #56129) [Link]
The joy of max()
Posted Mar 29, 2018 14:14 UTC (Thu) by bfields (subscriber, #19510) [Link]
That does deserve some kind of prize. What exactly I'm not sure.
The joy of max()
Posted Mar 29, 2018 16:20 UTC (Thu) by smitty_one_each (subscriber, #28989) [Link]
Credits to: Martin Uecker
Posted Mar 29, 2018 20:52 UTC (Thu) by david.a.wheeler (guest, #72896) [Link]
I think Linus Torvalds said it best: "That is either genius, or a seriously diseased mind. I can't quite tell which." - https://lkml.org/lkml/2018/3/20/845
Credits to: Martin Uecker
Posted Mar 29, 2018 21:32 UTC (Thu) by gerdesj (subscriber, #5446) [Link]
It is clearly a work of genius, with a touch of "bwou HA HA Ha ha haaaaa".
Credits to: Martin Uecker
Posted Mar 30, 2018 20:30 UTC (Fri) by nix (subscriber, #2304) [Link]
Credits to: Martin Uecker
Posted Mar 31, 2018 1:35 UTC (Sat) by gerdesj (subscriber, #5446) [Link]
Now you now what it is like reading a paper of a mathematic or similar proof. It seems to me that IT is gradually growing up.
Credits to: Martin Uecker
Posted Apr 1, 2018 17:04 UTC (Sun) by nix (subscriber, #2304) [Link]
The joy of max()
Posted Mar 30, 2018 18:14 UTC (Fri) by flussence (subscriber, #85566) [Link]
The joy of max()
Posted Mar 30, 2018 22:03 UTC (Fri) by tbodt (subscriber, #120821) [Link]
The joy of max()
Posted Mar 30, 2018 23:17 UTC (Fri) by tux1968 (guest, #58956) [Link]
The joy of max()
Posted Mar 30, 2018 22:45 UTC (Fri) by adobriyan (subscriber, #30858) [Link]
The joy of max()
Posted Mar 30, 2018 23:38 UTC (Fri) by adobriyan (subscriber, #30858) [Link]
size_t a = min_t(size_t, PAGE_SIZE, count);
please remind me why C is so much better than C++?
Posted Apr 5, 2018 14:29 UTC (Thu) by HelloWorld (guest, #56129) [Link]
constexpr const T& max( const T& a, const T& b ) {
return a > b ? a : b;
}
Done.
please remind me why C is so much better than C++?
Posted Apr 7, 2018 21:19 UTC (Sat) by rleigh (guest, #14622) [Link]
please remind me why C is so much better than C++?
Posted Apr 8, 2018 11:28 UTC (Sun) by HelloWorld (guest, #56129) [Link]
That's the definition from the standard library (<algorithm> header), so you wouldn't really have to write it at all. It also makes sense, because this way the types don't need to be copy-constructible and the compiler will optimise the references away in most cases when dealing with simple types.