diff options
| author | christos <christos@NetBSD.org> | 2012-03-13 21:07:28 +0000 |
|---|---|---|
| committer | christos <christos@NetBSD.org> | 2012-03-13 21:07:28 +0000 |
| commit | dc654a7d2d7cf5bb66c22d38f400a8cb6d03a45a (patch) | |
| tree | 66f09bbf188f893f1aa746df90f76dc7ba6f3933 | |
| parent | ce4cd1027e818e6aa4e5529170d41b6a64b3abb0 (diff) | |
Type macros providing min and max values for the given type, plus one that
returns if a value can be represented in a given type.
| -rw-r--r-- | sys/sys/cdefs.h | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/sys/sys/cdefs.h b/sys/sys/cdefs.h index 2bf94f145a9..60082609e24 100644 --- a/sys/sys/cdefs.h +++ b/sys/sys/cdefs.h @@ -1,4 +1,4 @@ -/* $NetBSD: cdefs.h,v 1.92 2012/02/22 17:52:58 martin Exp $ */ +/* $NetBSD: cdefs.h,v 1.93 2012/03/13 21:07:28 christos Exp $ */ /* * Copyright (c) 1991, 1993 @@ -530,4 +530,32 @@ #define __CAST(__dt, __st) ((__dt)(__st)) #endif +#define __type_mask(t) (/*LINTED*/sizeof(t) < sizeof(intmax_t) ? \ + (~((1ULL << (sizeof(t) * NBBY)) - 1)) : 0ULL) + +static inline long long __zero(void) { return 0; } +static __inline int __negative(double x) { return 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_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) +#define __type_min(t) (__type_is_signed(t) ? __type_min_s(t) : __type_min_u(t)) +#define __type_max(t) (__type_is_signed(t) ? __type_max_s(t) : __type_max_u(t)) + + +#define __type_fit_u(t, a) (/*LINTED*/sizeof(t) < sizeof(intmax_t) ? \ + (((a) & __type_mask(t)) == 0) : !__negative(a)) + +#define __type_fit_s(t, a) (/*LINTED*/__negative(a) ? \ + ((intmax_t)((a) + __zero()) >= (intmax_t)__type_min_s(t)) : \ + ((intmax_t)((a) + __zero()) <= (intmax_t)__type_max_s(t))) + +/* + * return true if value 'a' fits in type 't' + */ +#define __type_fit(t, a) (__type_is_signed(t) ? \ + __type_fit_s(t, a) : __type_fit_u(t, a)) + #endif /* !_SYS_CDEFS_H_ */ |
