Module Name: src Committed By: martin Date: Sun Oct 13 16:15:07 UTC 2024
Modified Files: src/sys/sys [netbsd-10]: cdefs.h Log Message: Pull up following revision(s) (requested by riastradh in ticket #973): sys/sys/cdefs.h: revision 1.162 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.159 -r1.159.4.1 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.159 src/sys/sys/cdefs.h:1.159.4.1 --- src/sys/sys/cdefs.h:1.159 Sat Jan 22 08:58:48 2022 +++ src/sys/sys/cdefs.h Sun Oct 13 16:15:07 2024 @@ -1,4 +1,4 @@ -/* $NetBSD: cdefs.h,v 1.159 2022/01/22 08:58:48 skrll Exp $ */ +/* $NetBSD: cdefs.h,v 1.159.4.1 2024/10/13 16:15:07 martin 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) @@ -689,8 +692,8 @@ #define __USE(a) (/*LINTED*/(void)(a)) -#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; } @@ -702,8 +705,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) @@ -711,13 +714,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'