summaryrefslogtreecommitdiff
path: root/nasmlib.h
diff options
context:
space:
mode:
authorCyrill Gorcunov <gorcunov@gmail.com>2009-10-11 15:01:39 +0400
committerCyrill Gorcunov <gorcunov@gmail.com>2009-10-13 19:41:53 +0400
commit4b67cc371eefb4fd28f7b0800ee44313513b7997 (patch)
tree1dc7cf4336dc72a27f74d1a2447d21d48f64a271 /nasmlib.h
parent8a6345ca47af16f8657e55282e1c84e4b8e4e96b (diff)
downloadnasm-4b67cc371eefb4fd28f7b0800ee44313513b7997.tar.gz
nasm-4b67cc371eefb4fd28f7b0800ee44313513b7997.tar.bz2
nasm-4b67cc371eefb4fd28f7b0800ee44313513b7997.zip
introduce "overflow" helpers
Suggested-by: H. Peter Anvin <hpa@zytor.com> Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
Diffstat (limited to 'nasmlib.h')
-rw-r--r--nasmlib.h27
1 files changed, 27 insertions, 0 deletions
diff --git a/nasmlib.h b/nasmlib.h
index 689485b..441ea20 100644
--- a/nasmlib.h
+++ b/nasmlib.h
@@ -383,4 +383,31 @@ const char *prefix_name(int);
extern const uint8_t zero_buffer[ZERO_BUF_SIZE];
size_t fwritezero(size_t bytes, FILE *fp);
+static inline bool overflow_general(int64_t value, int bytes)
+{
+ int sbit = (bytes << 3) - 1;
+ int64_t vmax = ((int64_t)2 << sbit) - 1;
+ int64_t vmin = -((int64_t)1 << sbit);
+
+ return value < vmin || value > vmax;
+}
+
+static inline bool overflow_signed(int64_t value, int bytes)
+{
+ int sbit = (bytes << 3) - 1;
+ int64_t vmax = ((int64_t)1 << sbit) - 1;
+ int64_t vmin = -((int64_t)1 << sbit);
+
+ return value < vmin || value > vmax;
+}
+
+static inline bool overflow_unsigned(int64_t value, int bytes)
+{
+ int sbit = (bytes << 3) - 1;
+ int64_t vmax = ((int64_t)2 << sbit) - 1;
+ int64_t vmin = 0;
+
+ return value < vmin || value > vmax;
+}
+
#endif