Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/sys Type macros providing min and max values for the giv...



details:   https://anonhg.NetBSD.org/src/rev/1419685be18a
branches:  trunk
changeset: 778037:1419685be18a
user:      christos <christos%NetBSD.org@localhost>
date:      Tue Mar 13 21:07:28 2012 +0000

description:
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.

diffstat:

 sys/sys/cdefs.h |  30 +++++++++++++++++++++++++++++-
 1 files changed, 29 insertions(+), 1 deletions(-)

diffs (42 lines):

diff -r a140d90c07c2 -r 1419685be18a sys/sys/cdefs.h
--- a/sys/sys/cdefs.h   Tue Mar 13 21:00:31 2012 +0000
+++ b/sys/sys/cdefs.h   Tue Mar 13 21:07:28 2012 +0000
@@ -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_ */



Home | Main Index | Thread Index | Old Index