You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by ac...@apache.org on 2023/07/29 19:41:37 UTC

[nuttx] branch master updated: nuttx/bits.h: support GENMASK and GENMASK_ULL

This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 6ff4826492 nuttx/bits.h: support GENMASK and GENMASK_ULL
6ff4826492 is described below

commit 6ff4826492a1b06ebe85bcb097afe3c16e35949a
Author: dongjiuzhu1 <do...@xiaomi.com>
AuthorDate: Wed Apr 26 23:16:18 2023 +0800

    nuttx/bits.h: support GENMASK and GENMASK_ULL
    
    Ref to https://github.com/torvalds/linux/blob/master/include/linux/bits.h
    
    Signed-off-by: dongjiuzhu1 <do...@xiaomi.com>
---
 include/assert.h     |  8 +++++
 include/nuttx/bits.h | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 105 insertions(+)

diff --git a/include/assert.h b/include/assert.h
index a429507dc1..8cd1fa3531 100644
--- a/include/assert.h
+++ b/include/assert.h
@@ -132,6 +132,14 @@
 #  endif
 #endif
 
+/* Force a compilation error if condition is true, but also produce a
+ * result (of value 0 and type int), so the expression can be used
+ * e.g. in a structure initializer (or where-ever else comma expressions
+ * aren't permitted).
+ */
+
+#define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); })))
+
 /****************************************************************************
  * Included Files
  ****************************************************************************/
diff --git a/include/nuttx/bits.h b/include/nuttx/bits.h
new file mode 100644
index 0000000000..470a622ee6
--- /dev/null
+++ b/include/nuttx/bits.h
@@ -0,0 +1,97 @@
+/****************************************************************************
+ * include/nuttx/bits.h
+ *
+ * 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_BITS_H
+#define __INCLUDE_NUTTX_BITS_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <assert.h>
+#include <inttypes.h>
+#include <stdint.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#ifndef BITS_PER_BYTE
+# define BITS_PER_BYTE 8
+#endif
+
+#if UINTPTR_MAX > UINT32_MAX
+# define BITS_PER_LONG 64
+#else
+# define BITS_PER_LONG 32
+#endif
+
+#ifndef BITS_PER_LONG_LONG
+# define BITS_PER_LONG_LONG 64
+#endif
+
+#define BIT_MASK(nr)       (UINT32_C(1) << ((nr) % BITS_PER_LONG))
+#define BIT_WORD(nr)       ((nr) / BITS_PER_LONG)
+#define BIT_ULL_MASK(nr)   (UINT64_C(1) << ((nr) % BITS_PER_LONG_LONG))
+#define BIT_ULL_WORD(nr)   ((nr) / BITS_PER_LONG_LONG)
+
+/* Create a contiguous bitmask starting at bit position @l and ending at
+ * position @h. For example
+ * GENMASK_ULL(39, 21) gives us the 64bit vector 0x000000ffffe00000.
+ */
+
+#define __GENMASK(h, l) \
+        (((~UINT32_C(0)) - (UINT32_C(1) << (l)) + 1) & \
+         (~UINT32_C(0) >> (BITS_PER_LONG - 1 - (h))))
+#define GENMASK(h, l) \
+        (BUILD_BUG_ON_ZERO((l) > (h)) + __GENMASK(h, l))
+
+#define __GENMASK_ULL(h, l) \
+        (((~UINT64_C(0)) - (UINT64_C(1) << (l)) + 1) & \
+         (~UINT64_C(0) >> (BITS_PER_LONG_LONG - 1 - (h))))
+#define GENMASK_ULL(h, l) \
+        (BUILD_BUG_ON_ZERO((l) > (h)) + __GENMASK_ULL(h, l))
+
+/****************************************************************************
+ * Type Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+#ifdef __cplusplus
+#define EXTERN extern "C"
+extern "C"
+{
+#else
+#define EXTERN extern
+#endif
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+#undef EXTERN
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __INCLUDE_NUTTX_BITS_H */