On Thu, Nov 02, 2023 at 04:44:30PM +0000, Joseph Myers wrote: > On Thu, 2 Nov 2023, Richard Biener via Gcc wrote: > > > Note the semantic of __builtin_clz is _not_ altered by > > CLZ_DEFINED_VALUE_AT_ZERO, the behavior > > of __builtin_clz (x) is that is has undefined result for x == 0. > > Note also the discussion in bug 111309 of possible future type-generic > versions of operations such as clz (under a different name because > __builtin_clz is already taken), in order to support _BitInt, where > there's a plausible argument for defining the result for all argument > values rather than leaving it undefined at 0.
I plan to work on that soon. The current plan is to let the users choose what they want, by having the type-generic variants of the builtin support either one or two arguments (just for clz/ctz obviously), if only one is provided, it is undefined at 0, if two are provided, then the second argument is the value to be returned for 0. So, __builtin_clzg (0uwb) or __builtin_clzg (0ULL) would be UB, while __builtin_clzg (0uwb, 42) would return 42. We'd need something like __builtin_widthof as well because _Bitwithof or something similar hasn't been standardized yet, so people can use the type-generic function in macro and ask for the number of bits to be returned for 0 (or something based on that). Jakub