Module Name:    src
Committed By:   riastradh
Date:           Sat May 11 13:26:54 UTC 2024

Modified Files:
        src/sys/sys: cdefs.h

Log Message:
sys/cdefs.h: Make various macros work more robustly.

Use predefined __-namespace macros inside __BIT, __type_min,
__type_max, and __type_fit:

- Use __CHAR_BIT__ instead of NBBY so this works without sys/types.h
  and without _NETBSD_SOURCE.

- Use __INTMAX_TYPE__, __UINTMAX_TYPE__ instead of intmax_t, uintmax_t
  so this works without stdint.h.

No functional change intended.


To generate a diff of this commit:
cvs rdiff -u -r1.161 -r1.162 src/sys/sys/cdefs.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/sys/cdefs.h
diff -u src/sys/sys/cdefs.h:1.161 src/sys/sys/cdefs.h:1.162
--- src/sys/sys/cdefs.h:1.161	Wed May  1 07:43:41 2024
+++ src/sys/sys/cdefs.h	Sat May 11 13:26:54 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: cdefs.h,v 1.161 2024/05/01 07:43:41 rillig Exp $	*/
+/*	$NetBSD: cdefs.h,v 1.162 2024/05/11 13:26:54 riastradh Exp $	*/
 
 /* * Copyright (c) 1991, 1993
  *	The Regents of the University of California.  All rights reserved.
@@ -645,9 +645,12 @@
 
 #ifndef __ASSEMBLER__
 /* __BIT(n): nth bit, where __BIT(0) == 0x1. */
-#define	__BIT(__n)	\
-    (((uintmax_t)(__n) >= NBBY * sizeof(uintmax_t)) ? 0 : \
-    ((uintmax_t)1 << (uintmax_t)((__n) & (NBBY * sizeof(uintmax_t) - 1))))
+#define	__BIT(__n)							      \
+	(((__UINTMAX_TYPE__)(__n) >= __CHAR_BIT__ * sizeof(__UINTMAX_TYPE__)) \
+	    ? 0								      \
+	    : ((__UINTMAX_TYPE__)1 <<					      \
+		(__UINTMAX_TYPE__)((__n) &				      \
+		    (__CHAR_BIT__ * sizeof(__UINTMAX_TYPE__) - 1))))
 
 /* __MASK(n): first n bits all set, where __MASK(4) == 0b1111. */
 #define	__MASK(__n)	(__BIT(__n) - 1)
@@ -720,8 +723,8 @@
  */
 #define	__MACROUSE(e)	(/*LINTED*/(void)sizeof((long)(e)))
 
-#define __type_mask(t) (/*LINTED*/sizeof(t) < sizeof(intmax_t) ? \
-    (~((1ULL << (sizeof(t) * NBBY)) - 1)) : 0ULL)
+#define __type_mask(t) (/*LINTED*/sizeof(t) < sizeof(__INTMAX_TYPE__) ? \
+    (~((1ULL << (sizeof(t) * __CHAR_BIT__)) - 1)) : 0ULL)
 
 #ifndef __ASSEMBLER__
 static __inline long long __zeroll(void) { return 0; }
@@ -733,8 +736,8 @@ static __inline unsigned long long __zer
 
 #define __negative_p(x) (!((x) > 0) && ((x) != 0))
 
-#define __type_min_s(t) ((t)((1ULL << (sizeof(t) * NBBY - 1))))
-#define __type_max_s(t) ((t)~((1ULL << (sizeof(t) * NBBY - 1))))
+#define __type_min_s(t) ((t)((1ULL << (sizeof(t) * __CHAR_BIT__ - 1))))
+#define __type_max_s(t) ((t)~((1ULL << (sizeof(t) * __CHAR_BIT__ - 1))))
 #define __type_min_u(t) ((t)0ULL)
 #define __type_max_u(t) ((t)~0ULL)
 #define __type_is_signed(t) (/*LINTED*/__type_min_s(t) + (t)1 < (t)1)
@@ -742,13 +745,18 @@ static __inline unsigned long long __zer
 #define __type_max(t) (__type_is_signed(t) ? __type_max_s(t) : __type_max_u(t))
 
 
-#define __type_fit_u(t, a) (/*LINTED*/!__negative_p(a) && \
-    (uintmax_t)((a) + __zeroull()) <= (uintmax_t)__type_max_u(t))
-
-#define __type_fit_s(t, a) (/*LINTED*/__negative_p(a) ? \
-    ((intmax_t)((a) + __zeroll()) >= (intmax_t)__type_min_s(t)) : \
-    ((intmax_t)((a) + __zeroll()) >= (intmax_t)0 && \
-     (intmax_t)((a) + __zeroll()) <= (intmax_t)__type_max_s(t)))
+#define __type_fit_u(t, a)						      \
+	(/*LINTED*/!__negative_p(a) &&					      \
+	    ((__UINTMAX_TYPE__)((a) + __zeroull()) <=			      \
+		(__UINTMAX_TYPE__)__type_max_u(t)))
+
+#define __type_fit_s(t, a)						      \
+	(/*LINTED*/__negative_p(a)					      \
+	    ? ((__INTMAX_TYPE__)((a) + __zeroll()) >=			      \
+		(__INTMAX_TYPE__)__type_min_s(t))			      \
+	    : ((__INTMAX_TYPE__)((a) + __zeroll()) >= (__INTMAX_TYPE__)0 &&   \
+		((__INTMAX_TYPE__)((a) + __zeroll()) <=			      \
+		    (__INTMAX_TYPE__)__type_max_s(t))))
 
 /*
  * return true if value 'a' fits in type 't'

Reply via email to