Changeset: 46090f10e511 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/46090f10e511
Modified Files:
        clients/Tests/exports.stable.out
        gdk/gdk.h
        gdk/gdk_calc_addsub.c
        gdk/gdk_calc_mul.c
        gdk/gdk_calc_private.h
        monetdb_config.h.in
        sql/test/emptydb/Tests/check.stable.out.int128
Branch: default
Log Message:

Implemented functions {add,sub,mul}_int{8,16,32,64,128)_overflow.
All these functions take two arguments of the obvious int type which are
the operands, one pointer to such an int type where the result goes of
the obvious arithmetic operation.  If overflow occurs, these functions
return true, else false.
Usage:
int16_t a, b, c; a = ...; b = ...;
if (add_int16_overflow(a, b, &c)) /* there was overlow */
else /* there was no overflow and c contains the sum of a and b */


diffs (truncated from 422 to 300 lines):

diff --git a/clients/Tests/exports.stable.out b/clients/Tests/exports.stable.out
--- a/clients/Tests/exports.stable.out
+++ b/clients/Tests/exports.stable.out
@@ -463,6 +463,11 @@ void VIEWbounds(BAT *b, BAT *view, BUN l
 BAT *VIEWcreate(oid seq, BAT *b, BUN l, BUN h);
 size_t _MT_npages;
 size_t _MT_pagesize;
+bool add_int128_overflow(int128_t a, int128_t b, int128_t *d);
+bool add_int16_overflow(int16_t a, int16_t b, int16_t *d);
+bool add_int32_overflow(int32_t a, int32_t b, int32_t *d);
+bool add_int64_overflow(int64_t a, int64_t b, int64_t *d);
+bool add_int8_overflow(int8_t a, int8_t b, int8_t *d);
 char *asctime_r(const struct tm *restrict, char *restrict);
 double bat_guess_uniques(BAT *b, BATiter *bi, struct canditer *bci);
 ssize_t bitFromStr(allocator *ma, const char *src, size_t *len, bit **dst, 
bool external);
@@ -592,6 +597,11 @@ const char *mo_find_option(opt *set, int
 void mo_free_options(opt *set, int setlen);
 void mo_print_options(opt *set, int setlen);
 int mo_system_config(opt **Set, int setlen);
+bool mul_int128_overflow(int128_t a, int128_t b, int128_t *d);
+bool mul_int16_overflow(int16_t a, int16_t b, int16_t *d);
+bool mul_int32_overflow(int32_t a, int32_t b, int32_t *d);
+bool mul_int64_overflow(int64_t a, int64_t b, int64_t *d);
+bool mul_int8_overflow(int8_t a, int8_t b, int8_t *d);
 const oid oid_nil;
 ssize_t ptrFromStr(allocator *ma, const char *src, size_t *len, ptr **dst, 
bool external);
 ssize_t ptrToStr(allocator *ma, char **dst, size_t *len, const ptr *src, bool 
external);
@@ -606,6 +616,11 @@ ssize_t sql_timestamp_fromstr(allocator 
 ssize_t strFromStr(allocator *ma, const char *restrict src, size_t *restrict 
len, char **restrict dst, bool external);
 const char str_nil[2];
 int strerror_r(int errnum, char *buf, size_t buflen);
+bool sub_int128_overflow(int128_t a, int128_t b, int128_t *d);
+bool sub_int16_overflow(int16_t a, int16_t b, int16_t *d);
+bool sub_int32_overflow(int32_t a, int32_t b, int32_t *d);
+bool sub_int64_overflow(int64_t a, int64_t b, int64_t *d);
+bool sub_int8_overflow(int8_t a, int8_t b, int8_t *d);
 timestamp timestamp_add_month(timestamp t, int m);
 timestamp timestamp_add_usec(timestamp t, lng usec);
 timestamp timestamp_create(date dt, daytime tm);
diff --git a/gdk/gdk.h b/gdk/gdk.h
--- a/gdk/gdk.h
+++ b/gdk/gdk.h
@@ -1953,8 +1953,8 @@ gdk_export size_t escapedStr(char *restr
 #define GDK_lng_max ((lng) INT64_MAX)
 #define GDK_lng_min ((lng) INT64_MIN+1)
 #ifdef HAVE_HGE
-#define GDK_hge_max ((((hge) 1) << 126) - 1 + (((hge) 1) << 126))
-#define GDK_hge_min (-GDK_hge_max)
+#define GDK_hge_max ((hge) INT128_MAX)
+#define GDK_hge_min ((hge) INT128_MIN+1)
 #endif
 #define GDK_flt_max ((flt) FLT_MAX)
 #define GDK_flt_min ((flt) -FLT_MAX)
@@ -2267,6 +2267,28 @@ VarHeapVal(const void *b, BUN p, int w)
        }
 }
 
+gdk_export bool add_int8_overflow(int8_t a, int8_t b, int8_t *d);
+gdk_export bool add_int16_overflow(int16_t a, int16_t b, int16_t *d);
+gdk_export bool add_int32_overflow(int32_t a, int32_t b, int32_t *d);
+gdk_export bool add_int64_overflow(int64_t a, int64_t b, int64_t *d);
+#ifdef HAVE_INT128_T
+gdk_export bool add_int128_overflow(int128_t a, int128_t b, int128_t *d);
+#endif
+gdk_export bool sub_int8_overflow(int8_t a, int8_t b, int8_t *d);
+gdk_export bool sub_int16_overflow(int16_t a, int16_t b, int16_t *d);
+gdk_export bool sub_int32_overflow(int32_t a, int32_t b, int32_t *d);
+gdk_export bool sub_int64_overflow(int64_t a, int64_t b, int64_t *d);
+#ifdef HAVE_INT128_T
+gdk_export bool sub_int128_overflow(int128_t a, int128_t b, int128_t *d);
+#endif
+gdk_export bool mul_int8_overflow(int8_t a, int8_t b, int8_t *d);
+gdk_export bool mul_int16_overflow(int16_t a, int16_t b, int16_t *d);
+gdk_export bool mul_int32_overflow(int32_t a, int32_t b, int32_t *d);
+gdk_export bool mul_int64_overflow(int64_t a, int64_t b, int64_t *d);
+#ifdef HAVE_INT128_T
+gdk_export bool mul_int128_overflow(int128_t a, int128_t b, int128_t *d);
+#endif
+
 /* BAT iterator, also protects use of BAT heaps with reference counts.
  *
  * A BAT iterator has to be used with caution, but it does have to be
diff --git a/gdk/gdk_calc_addsub.c b/gdk/gdk_calc_addsub.c
--- a/gdk/gdk_calc_addsub.c
+++ b/gdk/gdk_calc_addsub.c
@@ -13,6 +13,80 @@
 #include "gdk_private.h"
 #include "gdk_calc_private.h"
 
+bool
+add_int8_overflow(int8_t a, int8_t b, int8_t *d)
+{
+       ADDI_WITH_CHECK(a, b, int8_t, *d, INT8_MAX, return true);
+       return false;
+}
+
+bool
+add_int16_overflow(int16_t a, int16_t b, int16_t *d)
+{
+       ADDI_WITH_CHECK(a, b, int16_t, *d, INT16_MAX, return true);
+       return false;
+}
+
+bool
+add_int32_overflow(int32_t a, int32_t b, int32_t *d)
+{
+       ADDI_WITH_CHECK(a, b, int32_t, *d, INT32_MAX, return true);
+       return false;
+}
+
+bool
+add_int64_overflow(int64_t a, int64_t b, int64_t *d)
+{
+       ADDI_WITH_CHECK(a, b, int64_t, *d, INT64_MAX, return true);
+       return false;
+}
+
+#ifdef HAVE_INT128_T
+bool
+add_int128_overflow(int128_t a, int128_t b, int128_t *d)
+{
+       ADDI_WITH_CHECK(a, b, int128_t, *d, INT128_MAX, return true);
+       return false;
+}
+#endif
+
+bool
+sub_int8_overflow(int8_t a, int8_t b, int8_t *d)
+{
+       SUBI_WITH_CHECK(a, b, int8_t, *d, INT8_MAX, return true);
+       return false;
+}
+
+bool
+sub_int16_overflow(int16_t a, int16_t b, int16_t *d)
+{
+       SUBI_WITH_CHECK(a, b, int16_t, *d, INT16_MAX, return true);
+       return false;
+}
+
+bool
+sub_int32_overflow(int32_t a, int32_t b, int32_t *d)
+{
+       SUBI_WITH_CHECK(a, b, int32_t, *d, INT32_MAX, return true);
+       return false;
+}
+
+bool
+sub_int64_overflow(int64_t a, int64_t b, int64_t *d)
+{
+       SUBI_WITH_CHECK(a, b, int64_t, *d, INT64_MAX, return true);
+       return false;
+}
+
+#ifdef HAVE_INT128_T
+bool
+sub_int128_overflow(int128_t a, int128_t b, int128_t *d)
+{
+       SUBI_WITH_CHECK(a, b, int128_t, *d, INT128_MAX, return true);
+       return false;
+}
+#endif
+
 /* ---------------------------------------------------------------------- */
 /* addition (any numeric type) */
 
diff --git a/gdk/gdk_calc_mul.c b/gdk/gdk_calc_mul.c
--- a/gdk/gdk_calc_mul.c
+++ b/gdk/gdk_calc_mul.c
@@ -13,6 +13,43 @@
 #include "gdk_private.h"
 #include "gdk_calc_private.h"
 
+bool
+mul_int8_overflow(int8_t a, int8_t b, int8_t *d)
+{
+       MULI4_WITH_CHECK(a, b, int8_t, *d, INT8_MAX, int16_t, return true);
+       return false;
+}
+
+bool
+mul_int16_overflow(int16_t a, int16_t b, int16_t *d)
+{
+       MULI4_WITH_CHECK(a, b, int16_t, *d, INT16_MAX, int32_t, return true);
+       return false;
+}
+
+bool
+mul_int32_overflow(int32_t a, int32_t b, int32_t *d)
+{
+       MULI4_WITH_CHECK(a, b, int32_t, *d, INT32_MAX, int64_t, return true);
+       return false;
+}
+
+bool
+mul_int64_overflow(int64_t a, int64_t b, int64_t *d)
+{
+       LNGMUL_CHECK(a, b, *d, INT64_MAX, return true);
+       return false;
+}
+
+#ifdef HAVE_INT128_T
+bool
+mul_int128_overflow(int128_t a, int128_t b, int128_t *d)
+{
+       HGEMUL_CHECK(a, b, *d, INT128_MAX, return true);
+       return false;
+}
+#endif
+
 /* ---------------------------------------------------------------------- */
 /* multiplication (any numeric type) */
 
diff --git a/gdk/gdk_calc_private.h b/gdk/gdk_calc_private.h
--- a/gdk/gdk_calc_private.h
+++ b/gdk/gdk_calc_private.h
@@ -146,10 +146,7 @@
 #define MULI4_WITH_CHECK(lft, rgt, TYPE3, dst, max, TYPE4, on_overflow) \
        MUL4_WITH_CHECK(lft, rgt, TYPE3, dst, max, TYPE4, on_overflow)
 #endif
-#ifdef HAVE_HGE
-#define LNGMUL_CHECK(lft, rgt, dst, max, on_overflow)                  \
-       MULI4_WITH_CHECK(lft, rgt, lng, dst, max, hge, on_overflow)
-#elif defined(HAVE_INT128_T)
+#ifdef HAVE_INT128_T
 #define LNGMUL_CHECK(lft, rgt, dst, max, on_overflow)                  \
        MULI4_WITH_CHECK(lft, rgt, lng, dst, max, int128_t, on_overflow)
 #elif defined(_MSC_VER) && defined(_M_AMD64) && !defined(__INTEL_COMPILER)
@@ -157,11 +154,11 @@
 #pragma intrinsic(_mul128)
 #define LNGMUL_CHECK(lft, rgt, dst, max, on_overflow)                  \
        do {                                                            \
-               __int64 clo, chi;                                       \
-               clo = _mul128((__int64) (lft), (__int64) (rgt), &chi);  \
-               if ((chi == 0 && clo >= 0 /*&& clo <= (max)*/) ||       \
-                   (chi == -1 && clo < 0 && clo >= -(max))) {          \
-                       (dst) = (lng) clo;                              \
+               __int64 _clo, _chi;                                     \
+               _clo = _mul128((__int64) (lft), (__int64) (rgt), &_chi); \
+               if ((_chi == 0 && _clo >= 0 /*&& _clo <= (max)*/) ||    \
+                   (_chi == -1 && _clo < 0 && _clo >= -(max))) {       \
+                       (dst) = (lng) _clo;                             \
                } else {                                                \
                        on_overflow;                                    \
                }                                                       \
@@ -169,29 +166,29 @@
 #else
 #define LNGMUL_CHECK(lft, rgt, dst, max, on_overflow)                  \
        do {                                                            \
-               lng a = (lft), b = (rgt);                               \
-               unsigned int a1, a2, b1, b2;                            \
-               ulng c;                                                 \
-               int sign = 1;                                           \
+               lng _a = (lft), _b = (rgt);                             \
+               unsigned int _a1, _a2, _b1, _b2;                        \
+               ulng _c;                                                \
+               int _sign = 1;                                          \
                                                                        \
-               if (a < 0) {                                            \
-                       sign = -sign;                                   \
-                       a = -a;                                         \
+               if (_a < 0) {                                           \
+                       _sign = -_sign;                                 \
+                       _a = -_a;                                       \
                }                                                       \
-               if (b < 0) {                                            \
-                       sign = -sign;                                   \
-                       b = -b;                                         \
+               if (_b < 0) {                                           \
+                       _sign = -_sign;                                 \
+                       _b = -_b;                                       \
                }                                                       \
-               a1 = (unsigned int) (a >> 32);                          \
-               a2 = (unsigned int) a;                                  \
-               b1 = (unsigned int) (b >> 32);                          \
-               b2 = (unsigned int) b;                                  \
+               _a1 = (unsigned int) (_a >> 32);                        \
+               _a2 = (unsigned int) _a;                                \
+               _b1 = (unsigned int) (_b >> 32);                        \
+               _b2 = (unsigned int) _b;                                \
                /* result = (a1*b1<<64) + (a1*b2+a2*b1<<32) + a2*b2 */  \
-               if ((a1 == 0 || b1 == 0) &&                             \
-                   ((c = (ulng) a1 * b2 + (ulng) a2 * b1) & (~(ulng)0 << 31)) 
== 0 && \
-                   (((c = (c << 32) + (ulng) a2 * b2) & ((ulng) 1 << 63)) == 0 
&& \
-                    (c) <= (ulng) (max))) {                            \
-                       (dst) = sign * (lng) c;                         \
+               if ((_a1 == 0 || _b1 == 0) &&                           \
+                   ((_c = (ulng) _a1 * _b2 + (ulng) _a2 * _b1) & (~(ulng)0 << 
31)) == 0 && \
+                   (((_c = (_c << 32) + (ulng) _a2 * _b2) & ((ulng) 1 << 63)) 
== 0 && \
+                    (_c) <= (ulng) (max))) {                           \
+                       (dst) = _sign * (lng) _c;                       \
                } else {                                                \
                        on_overflow;                                    \
                }                                                       \
@@ -207,29 +204,29 @@
 #else
 #define HGEMUL_CHECK(lft, rgt, dst, max, on_overflow)                  \
        do {                                                            \
-               hge a = (lft), b = (rgt);                               \
-               ulng a1, a2, b1, b2;                                    \
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to