Changeset: 58a20b13390f for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/58a20b13390f
Modified Files:
        common/utils/mstring.h
        gdk/gdk_aggr.c
        gdk/gdk_analytic_func.c
        gdk/gdk_analytic_statistics.c
        gdk/gdk_calc_private.h
        gdk/gdk_cand.h
        gdk/gdk_group.c
        gdk/gdk_join.c
        gdk/gdk_private.h
        gdk/gdk_string.c
        gdk/gdk_system.c
        gdk/gdk_system.h
        monetdb5/modules/mal/tablet.c
        monetdb5/modules/mal/txtsim.c
        sql/server/rel_optimize_sel.c
        sql/server/sql_mvc.c
        sql/server/sql_mvc.h
Branch: Aug2024
Log Message:

Improve tests whether we have compiler builtins by asking compiler directly.


diffs (truncated from 664 to 300 lines):

diff --git a/common/utils/mstring.h b/common/utils/mstring.h
--- a/common/utils/mstring.h
+++ b/common/utils/mstring.h
@@ -83,10 +83,17 @@ strconcat_len(char *restrict dst, size_t
        return i;
 }
 
-#ifndef __GNUC__
+#ifdef __has_builtin
+#if __has_builtin(__builtin_expect)
 /* __builtin_expect returns its first argument; it is expected to be
  * equal to the second argument */
-#define __builtin_expect(expr, expect) (expr)
+#define unlikely(expr) __builtin_expect((expr) != 0, 0)
+#define likely(expr)   __builtin_expect((expr) != 0, 1)
+#endif
+#endif
+#ifndef unlikely
+#define unlikely(expr) (expr)
+#define likely(expr)   (expr)
 #endif
 
 /*
@@ -105,12 +112,11 @@ strconcat_len(char *restrict dst, size_t
 static inline bool
 checkUTF8(const char *v)
 {
-       /* It is unlikely that this functions returns false, because
-        * it is likely that the string presented is a correctly coded
-        * UTF-8 string.  So we annotate the tests that are very
-        * unlikely to succeed, i.e. the ones that lead to a return of
-        * false, as being expected to return 0 using the
-        * __builtin_expect function. */
+       /* It is unlikely that this functions returns false, because it is
+        * likely that the string presented is a correctly coded UTF-8
+        * string.  So we annotate the tests that are very (un)likely to
+        * succeed, i.e. the ones that lead to a return of false.  This can
+        * help the compiler produce more efficient code. */
        if (v != NULL) {
                if (v[0] != '\200' || v[1] != '\0') {
                        /* check that string is correctly encoded UTF-8 */
@@ -121,30 +127,30 @@ checkUTF8(const char *v)
                                if ((v[i] & 0x80) == 0) {
                                        ;
                                } else if ((v[i] & 0xE0) == 0xC0) {
-                                       if (__builtin_expect(((v[i] & 0x1E) == 
0), 0))
+                                       if (unlikely(((v[i] & 0x1E) == 0)))
                                                return false;
-                                       if (__builtin_expect(((v[++i] & 0xC0) 
!= 0x80), 0))
+                                       if (unlikely(((v[++i] & 0xC0) != 0x80)))
                                                return false;
                                } else if ((v[i] & 0xF0) == 0xE0) {
                                        if ((v[i++] & 0x0F) == 0) {
-                                               if (__builtin_expect(((v[i] & 
0xE0) != 0xA0), 0))
+                                               if (unlikely(((v[i] & 0xE0) != 
0xA0)))
                                                        return false;
                                        } else {
-                                               if (__builtin_expect(((v[i] & 
0xC0) != 0x80), 0))
+                                               if (unlikely(((v[i] & 0xC0) != 
0x80)))
                                                        return false;
                                        }
-                                       if (__builtin_expect(((v[++i] & 0xC0) 
!= 0x80), 0))
+                                       if (unlikely(((v[++i] & 0xC0) != 0x80)))
                                                return false;
-                               } else if (__builtin_expect(((v[i] & 0xF8) == 
0xF0), 1)) {
+                               } else if (likely(((v[i] & 0xF8) == 0xF0))) {
                                        if ((v[i++] & 0x07) == 0) {
-                                               if (__builtin_expect(((v[i] & 
0x30) == 0), 0))
+                                               if (unlikely(((v[i] & 0x30) == 
0)))
                                                        return false;
                                        }
-                                       if (__builtin_expect(((v[i] & 0xC0) != 
0x80), 0))
+                                       if (unlikely(((v[i] & 0xC0) != 0x80)))
                                                return false;
-                                       if (__builtin_expect(((v[++i] & 0xC0) 
!= 0x80), 0))
+                                       if (unlikely(((v[++i] & 0xC0) != 0x80)))
                                                return false;
-                                       if (__builtin_expect(((v[++i] & 0xC0) 
!= 0x80), 0))
+                                       if (unlikely(((v[++i] & 0xC0) != 0x80)))
                                                return false;
                                } else {
                                        return false;
@@ -222,10 +228,7 @@ reallocprintf(char **buf, size_t *pos, s
        return n;
 }
 
-
-
-#ifndef __GNUC__
-#undef __builtin_expect
-#endif
+#undef unlikely
+#undef likely
 
 #endif
diff --git a/gdk/gdk_aggr.c b/gdk/gdk_aggr.c
--- a/gdk/gdk_aggr.c
+++ b/gdk/gdk_aggr.c
@@ -439,7 +439,7 @@ dofsum(const void *restrict values, oid 
                                *seen = ci->ncand > 0;                  \
                                TIMEOUT_LOOP_IDX(i, ci->ncand, qry_ctx) { \
                                        x = vals[ci->seq + i - seqb];   \
-                                       ADD_WITH_CHECK(x, sum,          \
+                                       ADDI_WITH_CHECK(x, sum,         \
                                                       TYPE2, sum,      \
                                                       GDK_##TYPE2##_max, \
                                                       goto overflow);  \
@@ -457,7 +457,7 @@ dofsum(const void *restrict values, oid 
                                                        TIMEOUT_LOOP_BREAK; \
                                                }                       \
                                        } else {                        \
-                                               ADD_WITH_CHECK(x, sum,  \
+                                               ADDI_WITH_CHECK(x, sum, \
                                                               TYPE2, sum, \
                                                               
GDK_##TYPE2##_max, \
                                                               goto overflow); \
@@ -485,7 +485,7 @@ dofsum(const void *restrict values, oid 
                                                TIMEOUT_LOOP_BREAK;     \
                                        }                               \
                                } else {                                \
-                                       ADD_WITH_CHECK(x, sum,          \
+                                       ADDI_WITH_CHECK(x, sum,         \
                                                       TYPE2, sum,      \
                                                       GDK_##TYPE2##_max, \
                                                       goto overflow);  \
@@ -516,7 +516,7 @@ dofsum(const void *restrict values, oid 
                                                        sums[gid] = 0;  \
                                                }                       \
                                                if 
(!is_##TYPE2##_nil(sums[gid])) { \
-                                                       ADD_WITH_CHECK( \
+                                                       ADDI_WITH_CHECK( \
                                                                x,      \
                                                                sums[gid], \
                                                                TYPE2,  \
@@ -550,7 +550,7 @@ dofsum(const void *restrict values, oid 
                                                        sums[gid] = 0;  \
                                                }                       \
                                                if 
(!is_##TYPE2##_nil(sums[gid])) { \
-                                                       ADD_WITH_CHECK( \
+                                                       ADDI_WITH_CHECK( \
                                                                x,      \
                                                                sums[gid], \
                                                                TYPE2,  \
@@ -1211,7 +1211,7 @@ BATsum(void *res, int tp, BAT *b, BAT *s
                                                prods[gid] = 1;         \
                                        }                               \
                                        if (!is_##TYPE2##_nil(prods[gid])) { \
-                                               MUL4_WITH_CHECK(        \
+                                               MULI4_WITH_CHECK(       \
                                                        vals[i],        \
                                                        prods[gid],     \
                                                        TYPE2, prods[gid], \
@@ -2913,7 +2913,7 @@ BATgroupavg3combine(BAT *avg, BAT *rem, 
                        x = ((const TYPE *) src)[i];                    \
                        if (is_##TYPE##_nil(x))                         \
                                continue;                               \
-                       ADD_WITH_CHECK(x, sum,                          \
+                       ADDI_WITH_CHECK(x, sum,                         \
                                       lng_hge, sum,                    \
                                       GDK_##lng_hge##_max,             \
                                       goto overflow##TYPE);            \
diff --git a/gdk/gdk_analytic_func.c b/gdk/gdk_analytic_func.c
--- a/gdk/gdk_analytic_func.c
+++ b/gdk/gdk_analytic_func.c
@@ -1734,7 +1734,7 @@ cleanup:
                                if (is_##TPE2##_nil(curval))            \
                                        curval = (TPE2) v;              \
                                else                                    \
-                                       ADD_WITH_CHECK(v, curval, TPE2, curval, 
GDK_##TPE2##_max, goto calc_overflow); \
+                                       ADDI_WITH_CHECK(v, curval, TPE2, 
curval, GDK_##TPE2##_max, goto calc_overflow); \
                        }                                               \
                }                                                       \
                for (; k < i; k++)                                      \
@@ -2025,7 +2025,7 @@ nosupport:
                        if (is_##TPE2##_nil(curval))                    \
                                curval = (TPE2) ARG;                    \
                        else                                            \
-                               MUL4_WITH_CHECK(ARG, curval, TPE2, curval, 
GDK_##TPE2##_max, TPE3, goto calc_overflow); \
+                               MULI4_WITH_CHECK(ARG, curval, TPE2, curval, 
GDK_##TPE2##_max, TPE3, goto calc_overflow); \
                }                                                       \
        } while(0)
 
@@ -2105,7 +2105,7 @@ nosupport:
                        if (is_##TPE2##_nil(computed))                  \
                                computed = VAL;                         \
                        else                                            \
-                               MUL4_WITH_CHECK(VAL, computed, TPE2, computed, 
GDK_##TPE2##_max, TPE3, goto calc_overflow); \
+                               MULI4_WITH_CHECK(VAL, computed, TPE2, computed, 
GDK_##TPE2##_max, TPE3, goto calc_overflow); \
                }                                                       \
        } while (0)
 #define FINALIZE_AGGREGATE_PROD(NOTHING1, TPE2, NOTHING2)      \
diff --git a/gdk/gdk_analytic_statistics.c b/gdk/gdk_analytic_statistics.c
--- a/gdk/gdk_analytic_statistics.c
+++ b/gdk/gdk_analytic_statistics.c
@@ -29,7 +29,7 @@
 #define ANALYTICAL_AVERAGE_CALC_NUM_STEP1(TPE, IMP, ARG)               \
        do {                                                            \
                if (!is_##TPE##_nil(ARG)) {                             \
-                       ADD_WITH_CHECK(ARG, sum, LNG_HGE, sum,          \
+                       ADDI_WITH_CHECK(ARG, sum, LNG_HGE, sum,         \
                                       GDK_LNG_HGE_max,                 \
                                       goto avg_overflow##TPE##IMP);    \
                        /* count only when no overflow occurs */        \
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
@@ -35,15 +35,17 @@
 
 #include "gdk_cand.h"
 
-#ifdef HAVE___BUILTIN_ADD_OVERFLOW
+#ifdef __has_builtin
+#if __has_builtin(__builtin_add_overflow)
 #define OP_WITH_CHECK(lft, rgt, dst, op, max, on_overflow)             \
        do {                                                            \
                if (__builtin_##op##_overflow(lft, rgt, &(dst)) ||      \
-                   (dst) < -(max) || (dst) > (max)) {                  \
+                   (dst) < -(max) /*|| (dst) > (max)*/) {              \
                        on_overflow;                                    \
                }                                                       \
        } while (0)
-#endif /* HAVE___BUILTIN_ADD_OVERFLOW */
+#endif
+#endif
 
 /* dst = lft + rgt with overflow check */
 
@@ -65,7 +67,7 @@
                }                                               \
        } while (0)
 
-#ifdef HAVE___BUILTIN_ADD_OVERFLOW
+#ifdef OP_WITH_CHECK
 /* integer version using Gnu CC builtin function for overflow check */
 #define ADDI_WITH_CHECK(lft, rgt, TYPE3, dst, max, on_overflow)                
\
        OP_WITH_CHECK(lft, rgt, dst, add, max, on_overflow)
@@ -99,7 +101,7 @@
                }                                               \
        } while (0)
 
-#ifdef HAVE___BUILTIN_ADD_OVERFLOW
+#ifdef OP_WITH_CHECK
 /* integer version using Gnu CC builtin function for overflow check */
 #define SUBI_WITH_CHECK(lft, rgt, TYPE3, dst, max, on_overflow)                
\
        OP_WITH_CHECK(lft, rgt, dst, sub, max, on_overflow)
@@ -119,15 +121,14 @@
 #define MUL4_WITH_CHECK(lft, rgt, TYPE3, dst, max, TYPE4, on_overflow) \
        do {                                                            \
                TYPE4 c = (TYPE4) (lft) * (rgt);                        \
-               if (c < (TYPE4) -(max) ||                               \
-                   c > (TYPE4) (max)) {                                \
+               if (c < (TYPE4) -(max) /*|| c > (TYPE4) (max)*/) {      \
                        on_overflow;                                    \
                } else {                                                \
                        (dst) = (TYPE3) c;                              \
                }                                                       \
        } while (0)
 
-#ifdef HAVE___BUILTIN_ADD_OVERFLOW
+#ifdef OP_WITH_CHECK
 /* integer version using Gnu CC builtin function for overflow check */
 #define MULI4_WITH_CHECK(lft, rgt, TYPE3, dst, max, TYPE4, on_overflow) \
        OP_WITH_CHECK(lft, rgt, dst, mul, max, on_overflow)
@@ -150,7 +151,7 @@
        do {                                                            \
                __int64 clo, chi;                                       \
                clo = _mul128((__int64) (lft), (__int64) (rgt), &chi);  \
-               if ((chi == 0 && clo >= 0 && clo <= (max)) ||           \
+               if ((chi == 0 && clo >= 0 /*&& clo <= (max)*/) ||       \
                    (chi == -1 && clo < 0 && clo >= -(max))) {          \
                        (dst) = (lng) clo;                              \
                } else {                                                \
@@ -193,7 +194,7 @@
        MUL4_WITH_CHECK(lft, rgt, TYPE3, dst, max, TYPE4, on_overflow)
 
 #ifdef HAVE_HGE
-#ifdef HAVE___BUILTIN_ADD_OVERFLOW
+#ifdef OP_WITH_CHECK
 #define HGEMUL_CHECK(lft, rgt, dst, max, on_overflow)                  \
        OP_WITH_CHECK(lft, rgt, dst, mul, max, on_overflow)
 #else
diff --git a/gdk/gdk_cand.h b/gdk/gdk_cand.h
--- a/gdk/gdk_cand.h
+++ b/gdk/gdk_cand.h
@@ -82,9 +82,14 @@ static inline int __attribute__((__const
 candmask_lobit(uint32_t x)
 {
        assert(x != 0);
-#if defined(__GNUC__)
+#ifdef __has_builtin
+#if __has_builtin(__builtin_ctz)
        return __builtin_ctz(x) /* ffs(x) - 1 */;
-#elif defined(_MSC_VER)
+#define BUILTIN_USED
+#endif
+#endif
+#ifndef BUILTIN_USED
+#if defined(_MSC_VER)
        unsigned long idx;
        if (_BitScanForward(&idx, x))
_______________________________________________
checkin-list mailing list -- checkin-list@monetdb.org
To unsubscribe send an email to checkin-list-le...@monetdb.org

Reply via email to