Add C2x BOOL_MAX and BOOL_WIDTH to limits.h

Message ID alpine.DEB.2.21.2008191638340.32578@digraph.polyomino.org.uk
State Committed
Commit e5baabf57b1f6bcaf60068b3fd24af649e4e91c6
Headers
Series Add C2x BOOL_MAX and BOOL_WIDTH to limits.h |

Commit Message

Joseph Myers Aug. 19, 2020, 4:39 p.m. UTC
  C2x adds BOOL_MAX and BOOL_WIDTH macros to <limits.h>.  This patch
adds them to glibc's <limits.h> for the case when they aren't defined
by GCC's <limits.h>.

Tested for x86_64.
  

Comments

Paul Eggert Aug. 19, 2020, 10:46 p.m. UTC | #1
Thanks, this patch looks good.
  
Florian Weimer Aug. 20, 2020, 7:45 a.m. UTC | #2
* Joseph Myers:

> C2x adds BOOL_MAX and BOOL_WIDTH macros to <limits.h>.  This patch
> adds them to glibc's <limits.h> for the case when they aren't defined
> by GCC's <limits.h>.
>
> Tested for x86_64.
>
> diff --git a/include/limits.h b/include/limits.h
> index 8195da78a4..29e9b943b9 100644
> --- a/include/limits.h
> +++ b/include/limits.h
> @@ -178,6 +178,18 @@
>  # endif
>  #endif /* Use IEC_60559_BFP_EXT.  */
>  
> +/* The macros for _Bool are not defined by GCC's <limits.h> before GCC
> +   11, or if _GNU_SOURCE is defined rather than enabling C2x support
> +   with -std.  */
> +#if __GLIBC_USE (ISOC2X)
> +# ifndef BOOL_MAX
> +#  define BOOL_MAX 1
> +# endif
> +# ifndef BOOL_WIDTH
> +#  define BOOL_WIDTH 1
> +# endif
> +#endif

Do you know why the width should be 1 and not any other value?

If it has to be 1, irrespective of the storage size, why doesn't the
draft say so?
  
Joseph Myers Aug. 20, 2020, 5:41 p.m. UTC | #3
On Thu, 20 Aug 2020, Florian Weimer wrote:

> Do you know why the width should be 1 and not any other value?

A width greater than 1 would only be valid if the implementation defines 
representations in _Bool for values greater than 1 (which could be stored 
there by memcpy, but not by normal scalar assignment because the rules for 
conversion to _Bool would apply for normal scalar assignment and always 
store 0 or 1 there).

As far as I know, no implementations or ABIs define such representations.

_Bool is also constrained to have rank less than unsigned char, but as 
there is no corresponding signed type it's not clear the rules on ranks of 
integer types actually say its precision must thereby be at most that of 
unsigned char.
  

Patch

diff --git a/include/limits.h b/include/limits.h
index 8195da78a4..29e9b943b9 100644
--- a/include/limits.h
+++ b/include/limits.h
@@ -178,6 +178,18 @@ 
 # endif
 #endif /* Use IEC_60559_BFP_EXT.  */
 
+/* The macros for _Bool are not defined by GCC's <limits.h> before GCC
+   11, or if _GNU_SOURCE is defined rather than enabling C2x support
+   with -std.  */
+#if __GLIBC_USE (ISOC2X)
+# ifndef BOOL_MAX
+#  define BOOL_MAX 1
+# endif
+# ifndef BOOL_WIDTH
+#  define BOOL_WIDTH 1
+# endif
+#endif
+
 #ifdef	__USE_POSIX
 /* POSIX adds things to <limits.h>.  */
 # include <bits/posix1_lim.h>