[v3,4/6] newlib/libc/stdbit: Make compile for C99 and newer

Message ID 20260416210047.2198615-5-joel@rtems.org
State New
Headers
Series Newlib and Cygwin C23 stdbit.h Support |

Commit Message

Joel Sherrill April 16, 2026, 9 p.m. UTC
  This involved two issues. First, is that C23 adds multiple
_WIDTH constants which are used by the implementation. This
is addressed by stdbit_internal.h defining them if needed.
Second, C99 does not have static_assert(). Changed to use
newlib's _Static_assert() adapter which has implementations
for C99 and newer.
---
 newlib/libc/stdbit/stdbit_internal.h          | 47 +++++++++++++++++++
 newlib/libc/stdbit/stdc_bit_ceil.c            |  6 ++-
 newlib/libc/stdbit/stdc_bit_floor.c           |  2 +
 newlib/libc/stdbit/stdc_bit_width.c           |  2 +
 newlib/libc/stdbit/stdc_count_ones.c          |  2 +
 newlib/libc/stdbit/stdc_count_zeros.c         |  2 +
 newlib/libc/stdbit/stdc_first_leading_one.c   |  2 +
 newlib/libc/stdbit/stdc_first_leading_zero.c  |  2 +
 newlib/libc/stdbit/stdc_first_trailing_one.c  |  2 +
 newlib/libc/stdbit/stdc_first_trailing_zero.c |  2 +
 newlib/libc/stdbit/stdc_has_single_bit.c      |  2 +
 newlib/libc/stdbit/stdc_leading_ones.c        |  6 ++-
 newlib/libc/stdbit/stdc_leading_zeros.c       |  6 ++-
 newlib/libc/stdbit/stdc_trailing_ones.c       |  6 ++-
 newlib/libc/stdbit/stdc_trailing_zeros.c      |  6 ++-
 15 files changed, 85 insertions(+), 10 deletions(-)
 create mode 100644 newlib/libc/stdbit/stdbit_internal.h
  

Patch

diff --git a/newlib/libc/stdbit/stdbit_internal.h b/newlib/libc/stdbit/stdbit_internal.h
new file mode 100644
index 000000000..0d99c691b
--- /dev/null
+++ b/newlib/libc/stdbit/stdbit_internal.h
@@ -0,0 +1,47 @@ 
+/*
+ * Newlib specific addition to ensure the C23 *_WIDTH constants
+ * used in the stdbits implementation are available even when
+ * compiling using another version of the C Programming Language.
+ */
+
+/*
+ *  Written by Joel Sherrill <joel.sherrill@OARcorp.com>.
+ *
+ *  COPYRIGHT (c) 2026.
+ *  On-Line Applications Research Corporation (OAR).
+ *
+ *  Permission to use, copy, modify, and distribute this software for any
+ *  purpose without fee is hereby granted, provided that this entire notice
+ *  is included in all copies of any software which is or includes a copy
+ *  or modification of this software.
+ *
+ *  THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
+ *  WARRANTY.  IN PARTICULAR,  THE AUTHOR MAKES NO REPRESENTATION
+ *  OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY OF THIS
+ *  SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
+ */
+
+#ifndef STDBIT_INTERNAL_h
+#define STDBIT_INTERNAL_h
+
+#ifndef UCHAR_WIDTH
+#define UCHAR_WIDTH __SCHAR_WIDTH__
+#endif
+
+#ifndef USHRT_WIDTH
+#define USHRT_WIDTH __SHRT_WIDTH__
+#endif
+
+#ifndef UINT_WIDTH
+#define UINT_WIDTH __INT_WIDTH__
+#endif
+
+#ifndef ULONG_WIDTH
+#define ULONG_WIDTH __LONG_LONG_WIDTH__
+#endif
+
+#ifndef ULLONG_WIDTH
+#define ULLONG_WIDTH __LONG_LONG_WIDTH__
+#endif
+
+#endif
diff --git a/newlib/libc/stdbit/stdc_bit_ceil.c b/newlib/libc/stdbit/stdc_bit_ceil.c
index 2dfd7bbcb..f25d184af 100644
--- a/newlib/libc/stdbit/stdc_bit_ceil.c
+++ b/newlib/libc/stdbit/stdc_bit_ceil.c
@@ -8,8 +8,10 @@ 
 #include <limits.h>
 #include <stdbit.h>
 
+#include "stdbit_internal.h"
+
 /* Ensure we don't shift 1U out of range. */
-static_assert(UCHAR_WIDTH < UINT_WIDTH,
+_Static_assert(UCHAR_WIDTH < UINT_WIDTH,
     "stdc_bit_ceil_uc needs UCHAR_WIDTH < UINT_WIDTH");
 
 unsigned char
@@ -22,7 +24,7 @@  stdc_bit_ceil_uc(unsigned char x)
 }
 
 /* Ensure we don't shift 1U out of range. */
-static_assert(USHRT_WIDTH < UINT_WIDTH,
+_Static_assert(USHRT_WIDTH < UINT_WIDTH,
     "stdc_bit_ceil_us needs USHRT_WIDTH < UINT_WIDTH");
 
 unsigned short
diff --git a/newlib/libc/stdbit/stdc_bit_floor.c b/newlib/libc/stdbit/stdc_bit_floor.c
index 0a491d6ec..ab9156056 100644
--- a/newlib/libc/stdbit/stdc_bit_floor.c
+++ b/newlib/libc/stdbit/stdc_bit_floor.c
@@ -7,6 +7,8 @@ 
 #include <limits.h>
 #include <stdbit.h>
 
+#include "stdbit_internal.h"
+
 unsigned char
 stdc_bit_floor_uc(unsigned char x)
 {
diff --git a/newlib/libc/stdbit/stdc_bit_width.c b/newlib/libc/stdbit/stdc_bit_width.c
index e248fae48..602916d21 100644
--- a/newlib/libc/stdbit/stdc_bit_width.c
+++ b/newlib/libc/stdbit/stdc_bit_width.c
@@ -7,6 +7,8 @@ 
 #include <limits.h>
 #include <stdbit.h>
 
+#include "stdbit_internal.h"
+
 unsigned int
 stdc_bit_width_uc(unsigned char x)
 {
diff --git a/newlib/libc/stdbit/stdc_count_ones.c b/newlib/libc/stdbit/stdc_count_ones.c
index c5198dca8..6dfcbfd45 100644
--- a/newlib/libc/stdbit/stdc_count_ones.c
+++ b/newlib/libc/stdbit/stdc_count_ones.c
@@ -7,6 +7,8 @@ 
 #include <limits.h>
 #include <stdbit.h>
 
+#include "stdbit_internal.h"
+
 unsigned int
 stdc_count_ones_uc(unsigned char x)
 {
diff --git a/newlib/libc/stdbit/stdc_count_zeros.c b/newlib/libc/stdbit/stdc_count_zeros.c
index 35db965af..49f1ff851 100644
--- a/newlib/libc/stdbit/stdc_count_zeros.c
+++ b/newlib/libc/stdbit/stdc_count_zeros.c
@@ -7,6 +7,8 @@ 
 #include <limits.h>
 #include <stdbit.h>
 
+#include "stdbit_internal.h"
+
 unsigned int
 stdc_count_zeros_uc(unsigned char x)
 {
diff --git a/newlib/libc/stdbit/stdc_first_leading_one.c b/newlib/libc/stdbit/stdc_first_leading_one.c
index de4368565..ad95b3cfd 100644
--- a/newlib/libc/stdbit/stdc_first_leading_one.c
+++ b/newlib/libc/stdbit/stdc_first_leading_one.c
@@ -7,6 +7,8 @@ 
 #include <limits.h>
 #include <stdbit.h>
 
+#include "stdbit_internal.h"
+
 unsigned int
 stdc_first_leading_one_uc(unsigned char x)
 {
diff --git a/newlib/libc/stdbit/stdc_first_leading_zero.c b/newlib/libc/stdbit/stdc_first_leading_zero.c
index e6609b51b..4e3d29002 100644
--- a/newlib/libc/stdbit/stdc_first_leading_zero.c
+++ b/newlib/libc/stdbit/stdc_first_leading_zero.c
@@ -7,6 +7,8 @@ 
 #include <limits.h>
 #include <stdbit.h>
 
+#include "stdbit_internal.h"
+
 unsigned int
 stdc_first_leading_zero_uc(unsigned char x)
 {
diff --git a/newlib/libc/stdbit/stdc_first_trailing_one.c b/newlib/libc/stdbit/stdc_first_trailing_one.c
index 22972758d..23d2329ea 100644
--- a/newlib/libc/stdbit/stdc_first_trailing_one.c
+++ b/newlib/libc/stdbit/stdc_first_trailing_one.c
@@ -6,6 +6,8 @@ 
 
 #include <stdbit.h>
 
+#include "stdbit_internal.h"
+
 unsigned int
 stdc_first_trailing_one_uc(unsigned char x)
 {
diff --git a/newlib/libc/stdbit/stdc_first_trailing_zero.c b/newlib/libc/stdbit/stdc_first_trailing_zero.c
index 27395c697..248f8d99b 100644
--- a/newlib/libc/stdbit/stdc_first_trailing_zero.c
+++ b/newlib/libc/stdbit/stdc_first_trailing_zero.c
@@ -7,6 +7,8 @@ 
 #include <limits.h>
 #include <stdbit.h>
 
+#include "stdbit_internal.h"
+
 unsigned int
 stdc_first_trailing_zero_uc(unsigned char x)
 {
diff --git a/newlib/libc/stdbit/stdc_has_single_bit.c b/newlib/libc/stdbit/stdc_has_single_bit.c
index a448464b0..9a235c023 100644
--- a/newlib/libc/stdbit/stdc_has_single_bit.c
+++ b/newlib/libc/stdbit/stdc_has_single_bit.c
@@ -7,6 +7,8 @@ 
 #include <stdbit.h>
 #include <stdbool.h>
 
+#include "stdbit_internal.h"
+
 bool
 stdc_has_single_bit_uc(unsigned char x)
 {
diff --git a/newlib/libc/stdbit/stdc_leading_ones.c b/newlib/libc/stdbit/stdc_leading_ones.c
index 79d3ec7f2..c7ab75b98 100644
--- a/newlib/libc/stdbit/stdc_leading_ones.c
+++ b/newlib/libc/stdbit/stdc_leading_ones.c
@@ -8,8 +8,10 @@ 
 #include <limits.h>
 #include <stdbit.h>
 
+#include "stdbit_internal.h"
+
 /* Avoid triggering undefined behavior if x == 0. */
-static_assert(UCHAR_WIDTH < UINT_WIDTH,
+_Static_assert(UCHAR_WIDTH < UINT_WIDTH,
     "stdc_leading_ones_uc needs UCHAR_WIDTH < UINT_WIDTH");
 
 unsigned int
@@ -21,7 +23,7 @@  stdc_leading_ones_uc(unsigned char x)
 }
 
 /* Avoid triggering undefined behavior if x == 0. */
-static_assert(USHRT_WIDTH < UINT_WIDTH,
+_Static_assert(USHRT_WIDTH < UINT_WIDTH,
     "stdc_leading_ones_us needs USHRT_WIDTH < UINT_WIDTH");
 
 unsigned int
diff --git a/newlib/libc/stdbit/stdc_leading_zeros.c b/newlib/libc/stdbit/stdc_leading_zeros.c
index 2fdf64ec9..dba6a14e2 100644
--- a/newlib/libc/stdbit/stdc_leading_zeros.c
+++ b/newlib/libc/stdbit/stdc_leading_zeros.c
@@ -8,8 +8,10 @@ 
 #include <limits.h>
 #include <stdbit.h>
 
+#include "stdbit_internal.h"
+
 /* Offset must be greater than zero. */
-static_assert(UCHAR_WIDTH < UINT_WIDTH,
+_Static_assert(UCHAR_WIDTH < UINT_WIDTH,
     "stdc_leading_zeros_uc needs UCHAR_WIDTH < UINT_WIDTH");
 
 unsigned int
@@ -21,7 +23,7 @@  stdc_leading_zeros_uc(unsigned char x)
 }
 
 /* Offset must be greater than zero. */
-static_assert(USHRT_WIDTH < UINT_WIDTH,
+_Static_assert(USHRT_WIDTH < UINT_WIDTH,
     "stdc_leading_zeros_us needs USHRT_WIDTH < UINT_WIDTH");
 
 unsigned int
diff --git a/newlib/libc/stdbit/stdc_trailing_ones.c b/newlib/libc/stdbit/stdc_trailing_ones.c
index c1a0e6b99..0f7ccb553 100644
--- a/newlib/libc/stdbit/stdc_trailing_ones.c
+++ b/newlib/libc/stdbit/stdc_trailing_ones.c
@@ -8,8 +8,10 @@ 
 #include <limits.h>
 #include <stdbit.h>
 
+#include "stdbit_internal.h"
+
 /* Avoid triggering undefined behavior if x == ~0. */
-static_assert(UCHAR_WIDTH < UINT_WIDTH,
+_Static_assert(UCHAR_WIDTH < UINT_WIDTH,
     "stdc_trailing_ones_uc needs UCHAR_WIDTH < UINT_WIDTH");
 
 unsigned int
@@ -19,7 +21,7 @@  stdc_trailing_ones_uc(unsigned char x)
 }
 
 /* Avoid triggering undefined behavior if x == ~0. */
-static_assert(USHRT_WIDTH < UINT_WIDTH,
+_Static_assert(USHRT_WIDTH < UINT_WIDTH,
     "stdc_trailing_ones_uc needs USHRT_WIDTH < UINT_WIDTH");
 
 unsigned int
diff --git a/newlib/libc/stdbit/stdc_trailing_zeros.c b/newlib/libc/stdbit/stdc_trailing_zeros.c
index 6213f62cf..396333e17 100644
--- a/newlib/libc/stdbit/stdc_trailing_zeros.c
+++ b/newlib/libc/stdbit/stdc_trailing_zeros.c
@@ -8,8 +8,10 @@ 
 #include <limits.h>
 #include <stdbit.h>
 
+#include "stdbit_internal.h"
+
 /* Ensure we do not shift 1U out of range. */
-static_assert(UCHAR_WIDTH < UINT_WIDTH,
+_Static_assert(UCHAR_WIDTH < UINT_WIDTH,
     "stdc_trailing_zeros_uc needs UCHAR_WIDTH < UINT_WIDTH");
 
 unsigned int
@@ -19,7 +21,7 @@  stdc_trailing_zeros_uc(unsigned char x)
 }
 
 /* Ensure we do not shift 1U out of range. */
-static_assert(USHRT_WIDTH < UINT_WIDTH,
+_Static_assert(USHRT_WIDTH < UINT_WIDTH,
     "stdc_trailing_zeros_uc needs USHRT_WIDTH < UINT_WIDTH");
 
 unsigned int