Changeset: e1be608c9f07 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/e1be608c9f07
Branch: default
Log Message:

merged


diffs (truncated from 353 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
@@ -461,8 +461,6 @@ 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;
-const union _dbl_nil_t _dbl_nil_;
-const union _flt_nil_t _flt_nil_;
 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);
diff --git a/cmake/monetdb-findpackages.cmake b/cmake/monetdb-findpackages.cmake
--- a/cmake/monetdb-findpackages.cmake
+++ b/cmake/monetdb-findpackages.cmake
@@ -18,6 +18,7 @@ if(${XXHASH_VERSION} VERSION_GREATER_EQU
   # with version at least 0.8.0 we inline all xxhash functions so we
   # don't need the library
   set(XXHASH_LDFLAGS "")
+  set(XXH_INLINE_ALL 1)
 endif()
 
 find_package(Python3 COMPONENTS Interpreter Development)
diff --git a/gdk/gdk.h b/gdk/gdk.h
--- a/gdk/gdk.h
+++ b/gdk/gdk.h
@@ -111,22 +111,9 @@
 # include <sys/mman.h>
 #endif
 
-/* the compiler on the Mac can't deal with including xxhash.h twice
- * because of identical redefinitions of types and we happen to know
- * that the xxhash version is high enough, so just define the magic
- * inline token and include the file only once */
-#ifndef __APPLE__
+/* we've defined XXH_INLINE_ALL in monetdb_config.h if the xxhash
+ * library is new enough */
 #include <xxhash.h>
-#endif
-
-#if defined(__APPLE__) || XXH_VERSION_NUMBER >= 0*100*100 + 8*100 + 0   /* at 
least 0.8.0 */
-/* in newer versions, we can define XXH_INLINE_ALL to inline all hash
- * functions before including xxhash.h again (we didn't need the first
- * include, except we need the version number to make the
- * distinction) */
-#define XXH_INLINE_ALL
-#include <xxhash.h>
-#endif
 
 #include "matomic.h"
 #include "mstring.h"
@@ -1888,25 +1875,8 @@ gdk_export size_t escapedStr(char *restr
 gdk_export const bte bte_nil;
 gdk_export const sht sht_nil;
 gdk_export const int int_nil;
-#ifdef NAN_CANNOT_BE_USED_AS_INITIALIZER
-/* Definition of NAN is seriously broken on Intel compiler (at least
- * in some versions), so we work around it. */
-union _flt_nil_t {
-       uint32_t l;
-       flt f;
-};
-gdk_export const union _flt_nil_t _flt_nil_;
-#define flt_nil (_flt_nil_.f)
-union _dbl_nil_t {
-       uint64_t l;
-       dbl d;
-};
-gdk_export const union _dbl_nil_t _dbl_nil_;
-#define dbl_nil (_dbl_nil_.d)
-#else
 gdk_export const flt flt_nil;
 gdk_export const dbl dbl_nil;
-#endif
 gdk_export const lng lng_nil;
 #ifdef HAVE_HGE
 gdk_export const hge hge_nil;
diff --git a/gdk/gdk_atoms.c b/gdk/gdk_atoms.c
--- a/gdk/gdk_atoms.c
+++ b/gdk/gdk_atoms.c
@@ -258,19 +258,8 @@ ATOMisdescendant(int tpe, int parent)
 const bte bte_nil = GDK_bte_min-1;
 const sht sht_nil = GDK_sht_min-1;
 const int int_nil = GDK_int_min-1;
-#ifdef NAN_CANNOT_BE_USED_AS_INITIALIZER
-/* Definition of NAN is seriously broken on Intel compiler (at least
- * in some versions), so we work around it. */
-const union _flt_nil_t _flt_nil_ = {
-       .l = UINT32_C(0x7FC00000)
-};
-const union _dbl_nil_t _dbl_nil_ = {
-       .l = UINT64_C(0x7FF8000000000000)
-};
-#else
 const flt flt_nil = NAN;
 const dbl dbl_nil = NAN;
-#endif
 const lng lng_nil = GDK_lng_min-1;
 #ifdef HAVE_HGE
 const hge hge_nil = GDK_hge_min-1;
diff --git a/gdk/gdk_batop.c b/gdk/gdk_batop.c
--- a/gdk/gdk_batop.c
+++ b/gdk/gdk_batop.c
@@ -2569,13 +2569,20 @@ do_sort(void *restrict h, void *restrict
        case TYPE_timestamp:
                assert(base == NULL);
                if (nilslast == reverse && (stable || n > 100))
-                       return GDKrsort(h, t, n, hs, ts, reverse, false);
+                       return GDKrsort(h, t, n, hs, ts, reverse, false, false);
                break;
        case TYPE_uuid:
        case TYPE_inet4:
+       case TYPE_inet6:
                assert(base == NULL);
                if (nilslast == reverse && (stable || n > 100))
-                       return GDKrsort(h, t, n, hs, ts, reverse, true);
+                       return GDKrsort(h, t, n, hs, ts, reverse, true, false);
+               break;
+       case TYPE_flt:
+       case TYPE_dbl:
+               assert(base == NULL);
+               if (nilslast == reverse && (stable || n > 100))
+                       return GDKrsort(h, t, n, hs, ts, reverse, false, true);
                break;
        default:
                break;
diff --git a/gdk/gdk_private.h b/gdk/gdk_private.h
--- a/gdk/gdk_private.h
+++ b/gdk/gdk_private.h
@@ -159,7 +159,7 @@ gdk_return GDKremovedir(int farmid, cons
 gdk_return GDKsave(int farmid, const char *nme, const char *ext, void *buf, 
size_t size, storage_t mode, bool dosync)
        __attribute__((__warn_unused_result__))
        __attribute__((__visibility__("hidden")));
-gdk_return GDKrsort(void *restrict h, void *restrict t, size_t n, size_t hs, 
size_t ts, bool reverse, bool isuuid)
+gdk_return GDKrsort(void *restrict h, void *restrict t, size_t n, size_t hs, 
size_t ts, bool reverse, bool bigendian, bool isfloat)
        __attribute__((__warn_unused_result__))
        __attribute__((__visibility__("hidden")));
 gdk_return GDKssort_rev(void *restrict h, void *restrict t, const void 
*restrict base, size_t n, int hs, int ts, int tpe)
diff --git a/gdk/gdk_rsort.c b/gdk/gdk_rsort.c
--- a/gdk/gdk_rsort.c
+++ b/gdk/gdk_rsort.c
@@ -18,7 +18,8 @@
 /* bigendian means value is in big-endian byte order, but also that it
  * is unsigned */
 gdk_return
-GDKrsort(void *restrict h, void *restrict t, size_t n, size_t hs, size_t ts, 
bool reverse, bool bigendian)
+GDKrsort(void *restrict h, void *restrict t, size_t n, size_t hs, size_t ts,
+        bool reverse, bool bigendian, bool isfloat)
 {
        allocator *ta = MT_thread_getallocator();
        allocator_state ta_state = ma_open(ta);
@@ -30,6 +31,7 @@ GDKrsort(void *restrict h, void *restric
        uint8_t *t2 = NULL;
        Heap tmph, tmpt;
 
+       assert(!isfloat || !bigendian); /* not both */
        if (counts == NULL) {
                ma_close(&ta_state);
                return GDK_FAIL;
@@ -61,6 +63,31 @@ GDKrsort(void *restrict h, void *restric
                ts = 0;
        }
 
+       if (isfloat) {
+               /* for floating point numbers, we can sort using the bit
+                * pattern, but negative numbers are then sorted in the
+                * reverse order; we therefore temporarily flip all bits
+                * for negative numbers and set the sign bit for
+                * positive ones; then sorting produces the correct
+                * order; at the end we undo the temporary change */
+               for (size_t i = 0, o = 0; i < n; i++, o += hs) {
+#ifndef WORDS_BIGENDIAN
+                       if (h1[o + hs - 1] & 0x80)
+                               for (size_t j = 0; j < hs; j++)
+                                       h1[o + j] = ~h1[o + j];
+                       else
+                               h1[o + hs - 1] |= 0x80;
+#else
+                       if (h1[o] & 0x80)
+                               for (size_t j = 0; j < hs; j++)
+                                       h1[o + j] = ~h1[o + j];
+                       else
+                               h1[o] |= 0x80;
+#endif
+               }
+       }
+
+       /* produce a histogram for each iteration */
 #ifndef WORDS_BIGENDIAN
        if (bigendian /* treat like big-endian */) {
 #endif
@@ -80,11 +107,21 @@ GDKrsort(void *restrict h, void *restric
                }
        }
 #endif
-       /* When sorting in ascending order, the negative numbers occupy
-        * the second half of the buckets in the last iteration; when
-        * sorting in descending order, the negative numbers occupy the
-        * first half.  In either case, at the end we need to put the
-        * second half first and the first half after. */
+
+       /* sort from least significant byte to most significant byte;
+        * each step is stable, so later reshuffles preserve the
+        * relative order established by previous iterations */
+
+       /* When sorting signed integers in ascending order, the negative
+        * numbers occupy the second half of the buckets in the last
+        * iteration; when sorting in descending order, the negative
+        * numbers occupy the first half.  In either case, at the end we
+        * need to put the second half first and the first half
+        * after.
+        * When sorting floating point numbers, negative numbers are
+        * dealt with, but nils are in the wrong place, similar to
+        * negative numbers in signed integers.  We use the same
+        * technique, but the details are slightly different. */
        size_t negpos = 0;
        for (size_t j = 0, k = hs - 1; j < hs; j++, k--) {
                size_t nb = counts[j][0] > 0;
@@ -102,8 +139,12 @@ GDKrsort(void *restrict h, void *restric
                        }
                }
                /* we're only interested in the position in the last
-                * iteration */
-               negpos = pos[NBUCKETS / 2 - reverse];
+                * iteration; for floating point, we're looking for nil
+                * which is encoded as sign bit (temporarily set to 1)
+                * followed by a size-dependent number of 1 bits (8 for
+                * float, 11 for double but in any case more than 7), so
+                * we're just looking at the last position */
+               negpos = isfloat ? pos[NBUCKETS - 1 - reverse] : pos[NBUCKETS / 
2 - reverse];
                if (nb == 1) {
                        /* no need to reshuffle data for this iteration:
                         * everything is in the same bucket */
@@ -137,15 +178,86 @@ GDKrsort(void *restrict h, void *restric
        }
        ma_close(&ta_state);
 
+
+       if (isfloat) {
+               /* undo the temporary change (see above) */
+               for (size_t i = 0, o = 0; i < n; i++, o += hs) {
+#ifndef WORDS_BIGENDIAN
+                       if ((h1[o + hs - 1] & 0x80) == 0)
+                               for (size_t j = 0; j < hs; j++)
+                                       h1[o + j] = ~h1[o + j];
+                       else
+                               h1[o + hs - 1] &= 0x7F;
+#else
+                       if ((h1[o] & 0x80) == 0)
+                               for (size_t j = 0; j < hs; j++)
+                                       h1[o + j] = ~h1[o + j];
+                       else
+                               h1[o] &= 0x7F;
+#endif
+               }
+               /* Deal with nils, we need to find the correct starting
+                * position (which depends on more than just the most
+                * significant byte.
+                * A nil is represented by a NaN, and a NaN is encoded
+                * by any sign bit (you can have positive and negative
+                * NaN), all bits of exponent set, and significand
+                * non-zero (zero represents infinity) */
+               if (reverse) {
+                       if (hs == 4) {
+                               /* float */
+                               while (negpos > 0) {
+                                       uint32_t v = ((uint32_t *) h1)[negpos - 
1];
+                                       if ((v & UINT32_C(0x7F800000)) == 
UINT32_C(0x7F800000) &&
+                                           (v & UINT32_C(0x7FFFFF)) != 0)
+                                               break;
+                                       negpos--;
+                               }
+                       } else {
+                               assert(hs == 8);
+                               /* double */
+                               while (negpos > 0) {
+                                       uint64_t v = ((uint64_t *) h1)[negpos - 
1];
+                                       if ((v & UINT64_C(0x7FF0000000000000)) 
== UINT64_C(0x7FF0000000000000) &&
+                                           (v & UINT64_C(0xFFFFFFFFFFFFF)) != 
0)
+                                               break;
+                                       negpos--;
+                               }
+                       }
+               } else {
+                       if (hs == 4) {
+                               /* float */
+                               while (negpos < n) {
+                                       uint32_t v = ((uint32_t *) h1)[negpos];
+                                       if ((v & UINT32_C(0x7F800000)) == 
UINT32_C(0x7F800000) &&
+                                           (v & UINT32_C(0x7FFFFF)) != 0)
+                                               break;
+                                       negpos++;
+                               }
+                       } else {
+                               assert(hs == 8);
+                               /* double */
+                               while (negpos < n) {
+                                       uint64_t v = ((uint64_t *) h1)[negpos];
+                                       if ((v & UINT64_C(0x7FF0000000000000)) 
== UINT64_C(0x7FF0000000000000) &&
+                                           (v & UINT64_C(0xFFFFFFFFFFFFF)) != 
0)
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to