summaryrefslogtreecommitdiff
path: root/handy.h
diff options
context:
space:
mode:
authorKarl Williamson <public@khwilliamson.com>2010-08-31 19:34:50 -0600
committerRafael Garcia-Suarez <rgs@consttype.org>2010-09-13 13:56:03 +0200
commitcf301eb7d17e4d58d25337e997b1c7b0133e3c91 (patch)
tree3156e3b8a22c05eb1b4dbcaab4513f6be57db9cd /handy.h
parent526fd1b4d7270fff44588238f2411032c109da6e (diff)
downloadperl-cf301eb7d17e4d58d25337e997b1c7b0133e3c91.tar.gz
handy.h: Add FITS_IN_8_BITS() macro
This macro is designed to be optimized out if the argument is byte-length, but otherwise to be a bomb-proof way of making sure that the argument occupies only 8 bits or fewer in whatever storage class it is in.
Diffstat (limited to 'handy.h')
-rw-r--r--handy.h14
1 files changed, 14 insertions, 0 deletions
diff --git a/handy.h b/handy.h
index bbeb1ff0f5..b46b844dba 100644
--- a/handy.h
+++ b/handy.h
@@ -482,6 +482,20 @@ patched there. The file as of this writing is cpan/Devel-PPPort/parts/inc/misc
*/
+/* FITS_IN_8_BITS(c) returns true if c occupies no more than 8 bits. It is
+ * designed to be hopefully bomb-proof, making sure that no bits of
+ * information are lost even on a 64-bit machine, but to get the compiler to
+ * optimize it out if possible. This is because Configure makes sure that the
+ * machine has an 8-bit byte, so if c is stored in a byte, the sizeof()
+ * guarantees that this evaluates to a constant true at compile time. The use
+ * of the mask instead of '< 256' keeps gcc from complaining that it is alway
+ * true, when c's storage class is a byte */
+#ifdef HAS_QUAD
+# define FITS_IN_8_BITS(c) ((sizeof(c) == 1) || (((U64)(c) & 0xFF) == (U64)(c)))
+#else
+# define FITS_IN_8_BITS(c) ((sizeof(c) == 1) || (((U32)(c) & 0xFF) == (U32)(c)))
+#endif
+
#define isALNUM(c) (isALPHA(c) || isDIGIT(c) || (c) == '_')
#define isIDFIRST(c) (isALPHA(c) || (c) == '_')
#define isALPHA(c) (isUPPER(c) || isLOWER(c))