4 ms·
#define VERY_LIKELY(x) __builtin_expect_with_probability(!!(x), 1, 0.999) What's the "!!" for? I've seen that in JavaScript, but wasn't expecting it in C.
by twic 4y ago
#define VERY_LIKELY(x) __builtin_expect_with_probability(!!(x), 1, 0.999)
What's the "!!" for? I've seen that in JavaScript, but wasn't expecting it in C.
- majke 4y agoIt's to make sure the result is either value 0 or value 1. Without this, the result could be like 2, and this is not wanted here.
- deleted 4y ago[deleted]
- deleted 4y ago[deleted]
- twic 4y agoWhy does it matter, though? This macro just tells the compiler how to compile the branch, right?
- dzaima 4y ago__builtin_expect_with_probability knows nothing about booleans or branches. It assigns a probability for the first and second argument being equal, but 2!=1 while 2 is truthy.
- IshKebab 4y agoPretty much the same thing really. Coerces the value to true or false (or 1/0 in C). E.g. !!2 is 1/true.
- tempodox 4y agoThe author of the macro cannot know what form the argument expression to the `x` parameter will have, and the double Boolean negation (`!!`) forces the result to be a Boolean. Ideally, this will be just the Zero bit in the condition register after `x` has been compared to zero.
- deleted 4y ago[deleted]
- deleted 4y ago[deleted]