introduce "overflow" helpers
authorCyrill Gorcunov <gorcunov@gmail.com>
Sun, 11 Oct 2009 11:01:39 +0000 (15:01 +0400)
committerCyrill Gorcunov <gorcunov@gmail.com>
Tue, 13 Oct 2009 15:41:53 +0000 (19:41 +0400)
Suggested-by: H. Peter Anvin <hpa@zytor.com>
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
nasmlib.h

index 689485b..441ea20 100644 (file)
--- 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