Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F160518434
D38034.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
1002 B
Referenced Files
None
Subscribers
None
D38034.diff
View Options
diff --git a/sys/sys/limits.h b/sys/sys/limits.h
--- a/sys/sys/limits.h
+++ b/sys/sys/limits.h
@@ -90,6 +90,26 @@
#define QUAD_MIN (__QUAD_MIN) /* min value for a quad_t */
#endif
+/*
+ * Programmatically determine the maximum value of
+ * an unsigned and signed type, without integer overflow.
+ *
+ * After determining the byte-size of the type,
+ * e.g. 2 for uint16_t, set the left-most bit. (0x8000)
+ * Set all bits but the highest by subtracting one. (0x7FFF)
+ * Shift all set bits left by one by way of muliply by 2. (0xFFFE)
+ * Finally, set the lowest bit by adding one. (0xFFFF)
+ *
+ * This is a portable way to programmatically determine
+ * the maximum value of an unsigned variable of arbitrary size.
+ */
+#define UTYPE_MAX(x) \
+ ((((1ULL << (sizeof((x)) * 8 - 1)) - 1) * 2) + 1)
+
+#define TYPE_MAX(x) \
+ ((((1ULL << (sizeof((x)) * 8 - 2)) - 1) * 2) + 1)
+
+
#if __XSI_VISIBLE || __POSIX_VISIBLE >= 200809
#define LONG_BIT __LONG_BIT
#define WORD_BIT __WORD_BIT
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Fri, Jun 26, 7:26 AM (22 h, 5 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
34348230
Default Alt Text
D38034.diff (1002 B)
Attached To
Mode
D38034: Have a type-agnostic macro to determine the [u]int maximum value
Attached
Detach File
Event Timeline
Log In to Comment