shithub: femtolisp

Download patch

ref: 267698bd3be39d182fa974697e3c34ec23dcefd7
parent: 4379daad09cb2fb5866c6eed2a5f226bcce56816
author: mag <mag-one@autistici.org>
date: Fri Jun 23 12:20:02 EDT 2023

homemade overflows detection ?!?

--- /dev/null
+++ b/overflows.h
@@ -1,0 +1,33 @@
+
+#define addof_int64(c,a,b) ( \
+  (b < 1)? \
+  ((INT64_MIN-(b) <= (a))?((c=(a)+(b))?0:1):1): \
+  ((INT64_MAX-(b) >= (a))?((c=(a)+(b))?0:1):1) \
+)
+
+#define subof_int64(c,a,b) ( \
+  (b < 1)? \
+  ((INT64_MAX+(b) >= (a))?((c=(a)-(b))?0:1):1): \
+  ((INT64_MIN+(b) <= (a))?((c=(a)-(b))?0:1):1) \
+)
+
+#define mulof_int64(c,a,b) ( \
+  (((a) != 0) && ((c=(a)*(b))/(a) != (b)))?1:0 \
+)
+
+#define addof_uint64(c,a,b) ( \
+  (b < 1)? \
+  ((0-(b) <= (a))?((c=(a)+(b))?0:1):1): \
+  ((UINT64_MAX-(b) >= (a))?((c=(a)+(b))?0:1):1) \
+)
+
+#define subof_uint64(c,a,b) ( \
+  (b < 1)? \
+  ((UINT64_MAX+(b) >= (a))?((c=(a)-(b))?0:1):1): \
+  (((b) <= (a))?((c=(a)-(b))?0:1):1) \
+)
+
+#define mulof_uint64(c,a,b) ( \
+  (((a) != 0) && ((c=(a)*(b))/(a) != (b)))?1:0 \
+)
+