michallenc commented on code in PR #18347:
URL: https://github.com/apache/nuttx/pull/18347#discussion_r2762753994


##########
include/nuttx/lib/stdbit.h:
##########
@@ -0,0 +1,189 @@
+/****************************************************************************
+ * include/nuttx/lib/stdbit.h
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_NUTTX_LIB_STDBIT_H
+#define __INCLUDE_NUTTX_LIB_STDBIT_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+/* If CONFIG_ARCH_STDBIT_H is defined, then the top-level Makefile will copy
+ * this header file to include/stdbit.h.  In that case the architecture may
+ * provide arch/<architecture>/include/stdbit.h which will be included below.
+ * If CONFIG_LIBC_STDBIT_GENERIC is defined (or arch does not provide the
+ * header), the generic C23 implementation is used.
+ */
+
+#if defined(CONFIG_ARCH_STDBIT_H) && defined(CONFIG_ARCH_HAVE_STDBIT_H)
+#  include <arch/stdbit.h>
+#else
+
+/****************************************************************************
+ * Preprocessor Definitions
+ ****************************************************************************/
+
+/* C23 endianness.  Prefer compiler definitions if present. */
+
+#ifndef __STDC_ENDIAN_NATIVE__
+#  if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
+#    define __STDC_ENDIAN_NATIVE__ __STDC_ENDIAN_LITTLE__
+#    define __STDC_ENDIAN_LITTLE__ 1
+#    define __STDC_ENDIAN_BIG__    2
+#  elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
+#    define __STDC_ENDIAN_NATIVE__ __STDC_ENDIAN_BIG__
+#    define __STDC_ENDIAN_LITTLE__ 1
+#    define __STDC_ENDIAN_BIG__    2
+#  else
+#    define __STDC_ENDIAN_LITTLE__ 1
+#    define __STDC_ENDIAN_BIG__    2
+#    define __STDC_ENDIAN_NATIVE__ 0
+#  endif
+#endif
+
+/****************************************************************************
+ * Generic C23 stdbit implementation (type-specific macros)
+ ****************************************************************************/
+
+#include <stdint.h>
+
+#define _STDBIT_WIDTH_UC   (sizeof(unsigned char) * 8)
+#define _STDBIT_WIDTH_US  (sizeof(unsigned short) * 8)
+#define _STDBIT_WIDTH_UI  (sizeof(unsigned int) * 8)
+#define _STDBIT_WIDTH_UL  (sizeof(unsigned long) * 8)
+#define _STDBIT_WIDTH_ULL (sizeof(unsigned long long) * 8)
+
+/* Leading zeros: result is width when x == 0 (C23). */
+
+#define stdc_leading_zeros_uc(x)  ((x) ? (unsigned 
int)(__builtin_clz((unsigned int)(x)) - (_STDBIT_WIDTH_UI - _STDBIT_WIDTH_UC)) 
: _STDBIT_WIDTH_UC)

Review Comment:
   NuttX still supports builds with older GNUC where `__builtin_clz` and others 
might not be defined (look at `nuttx/compiler.h`). We get 
`CONFIG_HAVE_BUILTIN_CTZ` and other defines that tell us if the builtin is 
available.
   
   We probably don't have to support older GNUC right away, but I think at 
least some compilation time error could be raised here.



##########
Kconfig:
##########
@@ -625,6 +625,39 @@ config ARCH_STDARG_H
                ARCH_STDARG_H=y and providing. If ARCH_STDARG_H, is not 
defined, then
                the stdarg.h header file will stay out-of-the-way in 
include/nuttx/.
 
+config ARCH_HAVE_STDBIT_H
+       bool
+       default n
+       ---help---
+               Selected by architecture specific logic if the architecture 
provides
+               a stdbit.h header file.
+
+config ARCH_STDBIT_H
+       bool "stdbit.h"
+       depends on ARCH_HAVE_STDBIT_H
+       default n
+       ---help---
+               There is a redirecting version of stdbit.h in the source tree. 
It
+               resides out-of-the-way at include/nuttx/lib/stdbit.h.  This is
+               because you should normally use your toolchain's stdbit.h file 
if
+               available (C23). When CONFIG_ARCH_STDBIT_H=y is defined, the
+               top-level makefile will copy the redirecting stdbit.h header 
file
+               from include/nuttx/lib/stdbit.h to include/stdbit.h.  The header
+               will then include the architecture-specific stdbit.h from
+               arch/<architecture>/include/stdbit.h if the architecture selects
+               ARCH_HAVE_STDBIT_H.  Otherwise a generic C23 implementation is
+               used.
+
+config LIBC_STDBIT_GENERIC

Review Comment:
   I am not sure if this belongs here. I suppose it's ok to  make `stdbit.h` 
not mandatory, but maybe it could be done similarly to `libm` and put the 
configuration option to `libs/libc/stdbit/ `directory?



##########
tools/Unix.mk:
##########
@@ -222,6 +222,19 @@ include/setjmp.h: include/nuttx/lib/setjmp.h
        $(Q) cp -f include/nuttx/lib/setjmp.h include/setjmp.h
 endif
 
+# Target used to copy include/nuttx/lib/stdbit.h.  If CONFIG_ARCH_STDBIT_H or
+# CONFIG_LIBC_STDBIT_GENERIC is defined, copy stdbit.h to include/ for C23
+# bit utilities.
+
+ifeq ($(CONFIG_ARCH_STDBIT_H),y)

Review Comment:
   You could do something like
   
   ```
   ifeq ($(firstword $(filter y,$(CONFIG_ARCH_STDBIT_H) 
$(CONFIG_LIBC_STDBIT_GENERIC))),y)
   ```
   
   to get logical OR and avoid unnecessary code duplication.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to