Changeset: ce173120a1cf for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=ce173120a1cf
Modified Files:
        common/utils/copybinary.h
        sql/backends/monet5/sql.c
Branch: copybinary
Log Message:

Byteswap on big endian platforms

so the files can always be little endian


diffs (83 lines):

diff --git a/common/utils/copybinary.h b/common/utils/copybinary.h
--- a/common/utils/copybinary.h
+++ b/common/utils/copybinary.h
@@ -1,3 +1,6 @@
+#ifndef COPYBINARY_H
+#define COPYBINARY_H
+
 #include "monetdb_config.h"
 
 typedef struct {
@@ -18,3 +21,42 @@ typedef struct {
        copy_binary_time time;
        copy_binary_date date;
 } copy_binary_timestamp; // natural size: 96, natural alignment: 32
+
+#ifdef _MSC_VER
+    // use intrinsic functions on Windows
+    #define COPY_BINARY_BYTESWAP16(s)  (_byteswap_ushort((uint16_t) (s)))
+    #define COPY_BINARY_BYTESWAP32(i)  (_byteswap_ulong((uint32_t) (i)))
+#else
+    // this code sequence is recognized by gcc and clang
+    #define COPY_BINARY_BYTESWAP16(u) ( \
+              ((uint8_t)(u) << 8) \
+            | ((uint8_t)(u>>8)) \
+        )
+    #define COPY_BINARY_BYTESWAP32(u) ( \
+              ((uint8_t)(u) << 24) \
+            | ((uint8_t)(u>>8) << 16) \
+            | ((uint8_t)(u>>16) << 8) \
+            | ((uint8_t)(u>>24)) \
+        )
+#endif
+
+#ifdef WORDS_BIGENDIAN
+       #define COPY_BINARY_CONVERT_DATE_ENDIAN(d) \
+               do { (d).year = COPY_BINARY_BYTESWAP16((d).year); } while (0)
+       #define COPY_BINARY_CONVERT_TIME_ENDIAN(t) \
+               do { (t).ms = COPY_BINARY_BYTESWAP32((t).ms); } while (0)
+#else
+       // still refer to the fields to provoke error message when used 
incorrectly
+       #define COPY_BINARY_CONVERT_DATE_ENDIAN(d) \
+               do { (void)(d).year;  } while (0)
+       #define COPY_BINARY_CONVERT_TIME_ENDIAN(t) \
+               do { (void)(t).ms; } while (0)
+#endif
+
+#define COPY_BINARY_CONVERT_TIMESTAMP_ENDIAN(ts) \
+    do { \
+        COPY_BINARY_CONVERT_DATE_ENDIAN((ts).date); \
+        COPY_BINARY_CONVERT_TIME_ENDIAN((ts).time); \
+    } while (0)
+
+#endif
diff --git a/sql/backends/monet5/sql.c b/sql/backends/monet5/sql.c
--- a/sql/backends/monet5/sql.c
+++ b/sql/backends/monet5/sql.c
@@ -3347,6 +3347,8 @@ convert_timestamp(void *cookie, size_t i
        timestamp *dst = (timestamp*) internal;
        assert(internal_size == sizeof(*dst));
 
+       COPY_BINARY_CONVERT_TIMESTAMP_ENDIAN(*src);
+
        date dt = date_create(src->date.year, src->date.month, src->date.day);
        daytime tm = daytime_create(src->time.hours, src->time.minutes, 
src->time.seconds, src->time.ms);
        timestamp value = timestamp_create(dt, tm);
@@ -3364,6 +3366,8 @@ convert_date(void *cookie, size_t intern
        date *dst = (date*) internal;
        assert(internal_size == sizeof(*dst));
 
+       COPY_BINARY_CONVERT_DATE_ENDIAN(*src);
+
        date value = date_create(src->year, src->month, src->day);
 
        (void)cookie;
@@ -3379,6 +3383,8 @@ convert_time(void *cookie, size_t intern
        timestamp *dst = (timestamp*) internal;
        assert(internal_size == sizeof(*dst));
 
+       COPY_BINARY_CONVERT_TIME_ENDIAN(*src);
+
        daytime value = daytime_create(src->hours, src->minutes, src->seconds, 
src->ms);
 
        (void)cookie;
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to