Changeset: ad775409c2ce for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/ad775409c2ce
Added Files:
        common/utils/mswap.h
Modified Files:
        common/stream/rw.c
        common/stream/stream_internal.h
        common/utils/CMakeLists.txt
        common/utils/copybinary_support.h
        sql/backends/monet5/sql_result.c
        sql/backends/monet5/vaults/parquet/pqc_thrift.h
Branch: default
Log Message:

Provide a single definition of byteswap operation for various integer sizes.
The new definition is in the include file mswap.h.


diffs (truncated from 473 to 300 lines):

diff --git a/common/stream/rw.c b/common/stream/rw.c
--- a/common/stream/rw.c
+++ b/common/stream/rw.c
@@ -11,6 +11,7 @@
 #include "monetdb_config.h"
 #include "stream.h"
 #include "stream_internal.h"
+#include "mswap.h"
 
 
 
@@ -55,7 +56,7 @@ mnstr_readSht(stream *restrict s, int16_
        switch (s->read(s, val, sizeof(*val), 1)) {
        case 1:
                if (s->swapbytes)
-                       *val = short_int_SWAP(*val);
+                       *val = (int16_t) swap_int16((uint16_t) *val);
                return 1;
        case 0:
                return 0;
@@ -71,7 +72,7 @@ mnstr_writeSht(stream *s, int16_t val)
                return 0;
        assert(s->binary);
        if (s->swapbytes)
-               val = short_int_SWAP(val);
+               val = (int16_t) swap_int16((uint16_t) val);
        return s->write(s, &val, sizeof(val), 1) == 1;
 }
 
@@ -84,7 +85,7 @@ mnstr_readInt(stream *restrict s, int *r
        switch (s->read(s, val, sizeof(*val), 1)) {
        case 1:
                if (s->swapbytes)
-                       *val = normal_int_SWAP(*val);
+                       *val = (int32_t) swap_int32((uint32_t) *val);
                return 1;
        case 0:
                return 0;
@@ -100,7 +101,7 @@ mnstr_writeInt(stream *s, int val)
                return 0;
        assert(s->binary);
        if (s->swapbytes)
-               val = normal_int_SWAP(val);
+               val = (int32_t) swap_int32((uint32_t) val);
        return s->write(s, &val, sizeof(val), 1) == 1;
 }
 
@@ -136,7 +137,7 @@ mnstr_readLng(stream *restrict s, int64_
        switch (s->read(s, val, sizeof(*val), 1)) {
        case 1:
                if (s->swapbytes)
-                       *val = long_int_SWAP(*val);
+                       *val = (int64_t) swap_int64((uint64_t) *val);
                return 1;
        case 0:
                return 0;
@@ -152,7 +153,7 @@ mnstr_writeLng(stream *s, int64_t val)
                return 0;
        assert(s->binary);
        if (s->swapbytes)
-               val = long_int_SWAP(val);
+               val = (int64_t) swap_int64((uint64_t) val);
        return s->write(s, &val, sizeof(val), 1) == 1;
 }
 
@@ -185,7 +186,7 @@ mnstr_readHge(stream *restrict s, int128
        switch (s->read(s, val, sizeof(*val), 1)) {
        case 1:
                if (s->swapbytes)
-                       *val = huge_int_SWAP(*val);
+                       *val = (int128_t) swap_int128((uint128_t) *val);
                return 1;
        case 0:
                return 0;
@@ -201,7 +202,7 @@ mnstr_writeHge(stream *s, int128_t val)
                return 0;
        assert(s->binary);
        if (s->swapbytes)
-               val = huge_int_SWAP(val);
+               val = (int128_t) swap_int128((uint128_t) val);
        return s->write(s, &val, sizeof(val), 1) == 1;
 }
 #endif
@@ -242,7 +243,7 @@ mnstr_readShtArray(stream *restrict s, i
        }
        if (s->swapbytes) {
                for (size_t i = 0; i < cnt; i++, val++)
-                       *val = short_int_SWAP(*val);
+                       *val = (int16_t) swap_int16((uint16_t) *val);
        }
        return 1;
 }
@@ -275,7 +276,7 @@ mnstr_readIntArray(stream *restrict s, i
        }
        if (s->swapbytes) {
                for (size_t i = 0; i < cnt; i++, val++)
-                       *val = normal_int_SWAP(*val);
+                       *val = (int32_t) swap_int32((uint32_t) *val);
        }
        return 1;
 }
@@ -308,7 +309,7 @@ mnstr_readLngArray(stream *restrict s, i
        }
        if (s->swapbytes) {
                for (size_t i = 0; i < cnt; i++, val++)
-                       *val = long_int_SWAP(*val);
+                       *val = (int64_t) swap_int64((uint64_t) *val);
        }
        return 1;
 }
@@ -342,7 +343,7 @@ mnstr_readHgeArray(stream *restrict s, i
        }
        if (s->swapbytes) {
                for (size_t i = 0; i < cnt; i++, val++)
-                       *val = huge_int_SWAP(*val);
+                       *val = (int128_t) swap_int128((uint128_t) *val);
        }
        return 1;
 }
diff --git a/common/stream/stream_internal.h b/common/stream/stream_internal.h
--- a/common/stream/stream_internal.h
+++ b/common/stream/stream_internal.h
@@ -90,55 +90,6 @@
 #define UTF8BOM                "\xEF\xBB\xBF"  /* UTF-8 encoding of Unicode 
BOM */
 #define UTF8BOMLENGTH  3               /* length of above */
 
-#ifdef _MSC_VER
-/* use intrinsic functions on Windows */
-#define short_int_SWAP(s)      ((int16_t) _byteswap_ushort((uint16_t) (s)))
-/* on Windows, long is the same size as int */
-#define normal_int_SWAP(i)     ((int) _byteswap_ulong((unsigned long) (i)))
-#define long_int_SWAP(l)       ((int64_t) _byteswap_uint64((unsigned __int64) 
(l)))
-#else
-#define short_int_SWAP(s)                              \
-       ((int16_t) (((0x00ff & (uint16_t) (s)) << 8) |  \
-                   ((0xff00 & (uint16_t) (s)) >> 8)))
-
-#define normal_int_SWAP(i)                                             \
-       ((int) (((((unsigned) 0xff <<  0) & (unsigned) (i)) << 24) |    \
-               ((((unsigned) 0xff <<  8) & (unsigned) (i)) <<  8) |    \
-               ((((unsigned) 0xff << 16) & (unsigned) (i)) >>  8) |    \
-               ((((unsigned) 0xff << 24) & (unsigned) (i)) >> 24)))
-
-#define long_int_SWAP(l)                                               \
-       ((int64_t) (((((uint64_t) 0xff <<  0) & (uint64_t) (l)) << 56) | \
-                   ((((uint64_t) 0xff <<  8) & (uint64_t) (l)) << 40) | \
-                   ((((uint64_t) 0xff << 16) & (uint64_t) (l)) << 24) | \
-                   ((((uint64_t) 0xff << 24) & (uint64_t) (l)) <<  8) | \
-                   ((((uint64_t) 0xff << 32) & (uint64_t) (l)) >>  8) | \
-                   ((((uint64_t) 0xff << 40) & (uint64_t) (l)) >> 24) | \
-                   ((((uint64_t) 0xff << 48) & (uint64_t) (l)) >> 40) | \
-                   ((((uint64_t) 0xff << 56) & (uint64_t) (l)) >> 56)))
-#endif
-
-#ifdef HAVE_HGE
-#define huge_int_SWAP(h)                                               \
-       ((int128_t) (((((uint128_t) 0xff <<   0) & (uint128_t) (h)) << 120) | \
-                    ((((uint128_t) 0xff <<   8) & (uint128_t) (h)) << 104) | \
-                    ((((uint128_t) 0xff <<  16) & (uint128_t) (h)) <<  88) | \
-                    ((((uint128_t) 0xff <<  24) & (uint128_t) (h)) <<  72) | \
-                    ((((uint128_t) 0xff <<  32) & (uint128_t) (h)) <<  56) | \
-                    ((((uint128_t) 0xff <<  40) & (uint128_t) (h)) <<  40) | \
-                    ((((uint128_t) 0xff <<  48) & (uint128_t) (h)) <<  24) | \
-                    ((((uint128_t) 0xff <<  56) & (uint128_t) (h)) <<   8) | \
-                    ((((uint128_t) 0xff <<  64) & (uint128_t) (h)) >>   8) | \
-                    ((((uint128_t) 0xff <<  72) & (uint128_t) (h)) >>  24) | \
-                    ((((uint128_t) 0xff <<  80) & (uint128_t) (h)) >>  40) | \
-                    ((((uint128_t) 0xff <<  88) & (uint128_t) (h)) >>  56) | \
-                    ((((uint128_t) 0xff <<  96) & (uint128_t) (h)) >>  72) | \
-                    ((((uint128_t) 0xff << 104) & (uint128_t) (h)) >>  88) | \
-                    ((((uint128_t) 0xff << 112) & (uint128_t) (h)) >> 104) | \
-                    ((((uint128_t) 0xff << 120) & (uint128_t) (h)) >> 120)))
-#endif
-
-
 struct stream {
        char *name;             /* name of the stream */
        struct stream *inner; /* if this stream is a wrapper around another one 
*/
diff --git a/common/utils/CMakeLists.txt b/common/utils/CMakeLists.txt
--- a/common/utils/CMakeLists.txt
+++ b/common/utils/CMakeLists.txt
@@ -22,6 +22,7 @@ target_sources(mutils
   mprompt.h
   msabaoth.c msabaoth.h
   mstring.c
+  mswap.h
   mutf8.c mutf8.h
   mutils.c mutils.h
   muuid.c muuid.h
diff --git a/common/utils/copybinary_support.h 
b/common/utils/copybinary_support.h
--- a/common/utils/copybinary_support.h
+++ b/common/utils/copybinary_support.h
@@ -13,6 +13,7 @@
 
 #include <stdint.h>
 #include "copybinary.h"
+#include "mswap.h"
 
 // According to Godbolt, these code sequences are recognized by
 // GCC at least back to 6.2 and Clang at least back to 6.0.0.
@@ -25,87 +26,11 @@
 //    uint16_t swapped = copy_binary_byteswap16(value);
 
 
-#ifdef _MSC_VER
-
-static inline uint16_t
-copy_binary_byteswap16(uint16_t value)
-{
-       return _byteswap_ushort(value);
-}
-
-static inline uint32_t
-copy_binary_byteswap32(uint32_t value)
-{
-       return _byteswap_ulong(value);
-}
-
-static inline uint64_t
-copy_binary_byteswap64(uint64_t value)
-{
-       return _byteswap_uint64(value);
-}
-
-#else
-
-static inline uint16_t
-copy_binary_byteswap16(uint16_t value)
-{
-       return
-               ((value & (UINT16_C(0xFF) << 8)) >>  8) |
-               ((value & (UINT16_C(0xFF) << 0)) <<  8)
-               ;
-}
-
-static inline uint32_t
-copy_binary_byteswap32(uint32_t value)
-{
-       return
-               ((value & (UINT32_C(0xFF) << 24)) >> 24) |
-               ((value & (UINT32_C(0xFF) << 16)) >>  8) |
-               ((value & (UINT32_C(0xFF) <<  8)) <<  8) |
-               ((value & (UINT32_C(0xFF) <<  0)) << 24)
-               ;
-}
-
-static inline uint64_t
-copy_binary_byteswap64(uint64_t value)
-{
-       return
-               ((value & (UINT64_C(0xFF) << 56)) >> 56) |
-               ((value & (UINT64_C(0xFF) << 48)) >> 40) |
-               ((value & (UINT64_C(0xFF) << 40)) >> 24) |
-               ((value & (UINT64_C(0xFF) << 32)) >>  8) |
-               ((value & (UINT64_C(0xFF) << 24)) <<  8) |
-               ((value & (UINT64_C(0xFF) << 16)) << 24) |
-               ((value & (UINT64_C(0xFF) <<  8)) << 40) |
-               ((value & (UINT64_C(0xFF) <<  0)) << 56)
-               ;
-}
-
-#endif
-
+#define copy_binary_byteswap16(v)      swap_int16(v)
+#define copy_binary_byteswap32(v)      swap_int32(v)
+#define copy_binary_byteswap64(v)      swap_int64(v)
 #ifdef HAVE_HGE
-static inline uint128_t
-copy_binary_byteswap128(uint128_t value)
-{
-       return
-               ((value & ((uint128_t) 0xFF << 120)) >> 120) |
-               ((value & ((uint128_t) 0xFF << 112)) >> 104) |
-               ((value & ((uint128_t) 0xFF << 104)) >>  88) |
-               ((value & ((uint128_t) 0xFF <<  96)) >>  72) |
-               ((value & ((uint128_t) 0xFF <<  88)) >>  56) |
-               ((value & ((uint128_t) 0xFF <<  80)) >>  40) |
-               ((value & ((uint128_t) 0xFF <<  72)) >>  24) |
-               ((value & ((uint128_t) 0xFF <<  64)) >>   8) |
-               ((value & ((uint128_t) 0xFF <<  56)) <<   8) |
-               ((value & ((uint128_t) 0xFF <<  48)) <<  24) |
-               ((value & ((uint128_t) 0xFF <<  40)) <<  40) |
-               ((value & ((uint128_t) 0xFF <<  32)) <<  56) |
-               ((value & ((uint128_t) 0xFF <<  24)) <<  72) |
-               ((value & ((uint128_t) 0xFF <<  16)) <<  88) |
-               ((value & ((uint128_t) 0xFF <<   8)) << 104) |
-               ((value & ((uint128_t) 0xFF <<   0)) << 120);
-}
+#define copy_binary_byteswap128(v)     swap_int128(v)
 #endif
 
 static inline copy_binary_date
diff --git a/common/utils/mswap.h b/common/utils/mswap.h
new file mode 100644
--- /dev/null
+++ b/common/utils/mswap.h
@@ -0,0 +1,84 @@
+/*
+ * SPDX-License-Identifier: MPL-2.0
+ *
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to