Changeset: a92fc79afad4 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=a92fc79afad4
Added Files:
        sql/backends/monet5/UDF/udf_impl.h
Modified Files:
        sql/backends/monet5/UDF/Makefile.ag
        sql/backends/monet5/UDF/udf.c
Branch: default
Log Message:

UDF: use multiply included skeleton file for type-expansion
rather than lengthy C macro;
this improved readability and debugability of the code


diffs (278 lines):

diff --git a/sql/backends/monet5/UDF/Makefile.ag 
b/sql/backends/monet5/UDF/Makefile.ag
--- a/sql/backends/monet5/UDF/Makefile.ag
+++ b/sql/backends/monet5/UDF/Makefile.ag
@@ -33,7 +33,7 @@ INCLUDES = .. \
 lib__udf = {
        MODULE
        DIR = libdir/monetdb5
-       SOURCES = udf.c udf.h
+       SOURCES = udf.c udf.h udf_impl.h
        LIBS = ../../../../monetdb5/tools/libmonetdb5 \
                   ../../../../gdk/libbat
 }
diff --git a/sql/backends/monet5/UDF/udf.c b/sql/backends/monet5/UDF/udf.c
--- a/sql/backends/monet5/UDF/udf.c
+++ b/sql/backends/monet5/UDF/udf.c
@@ -176,93 +176,35 @@ UDFBATreverse(bat *ret, bat *bid)
 
 
 
-/* scalar fuse */
+/* fuse */
 
-/* use C macro to conveniently define type-specific functions */
-#define UDFfuse_scalar_impl(in,uin,out,shift)                          \
-/* fuse two (shift-byte) in values into one (2*shift-byte) out value */        
\
-/* actual implementation */                                            \
-static str                                                             \
-UDFfuse_##in##_##out##_ ( out *ret , in one , in two )                 \
-{                                                                      \
-       /* assert calling sanity */                                     \
-       assert(ret != NULL);                                            \
-                                                                       \
-       if (one == in##_nil || two == in##_nil)                         \
-               /* NULL/nil in => NULL/nil out */                       \
-               *ret = out##_nil;                                       \
-       else                                                            \
-               /* do the work; watch out for sign bits */              \
-               *ret = ( ((out)((uin)one)) << shift ) | ((uin)two);     \
-                                                                       \
-       return MAL_SUCCEED;                                             \
-}                                                                      \
-/* MAL wrapper */                                                      \
-str                                                                    \
-UDFfuse_##in##_##out ( out *ret , in *one , in *two )                  \
-{                                                                      \
-       /* assert calling sanity */                                     \
-       assert(ret != NULL && one != NULL && two != NULL);              \
-                                                                       \
-       return UDFfuse_##in##_##out##_ ( ret, *one, *two );             \
-}
+/* instantiate type-specific functions */
 
-/* call C macro to define type-specific functions */
-UDFfuse_scalar_impl(bte, unsigned char,  sht,  8)
-UDFfuse_scalar_impl(sht, unsigned short, int, 16)
-UDFfuse_scalar_impl(int, unsigned int,   lng, 32)
+#define UI bte
+#define UU unsigned char
+#define UO sht
+#include "udf_impl.h"
+#undef UI
+#undef UU
+#undef UO
 
+#define UI sht
+#define UU unsigned short
+#define UO int
+#include "udf_impl.h"
+#undef UI
+#undef UU
+#undef UO
+
+#define UI int
+#define UU unsigned int
+#define UO lng
+#include "udf_impl.h"
+#undef UI
+#undef UU
+#undef UO
 
 /* BAT fuse */
-/*
- * Type-expanded optimized version,
- * accessing value arrays directly.
- */
-
-/* type-specific core algorithm */
-/* use C macro to conveniently define type-specific functions */
-#define UDFBATfuse_impl(in,uin,out,shift)                              \
-static str                                                             \
-UDFBATfuse_##in##_##out ( BAT *bres, BAT *bone, BAT *btwo, BUN n,      \
-                            bit *two_tail_sorted_unsigned,             \
-                            bit *two_tail_revsorted_unsigned )         \
-{                                                                      \
-       in *one = NULL, *two = NULL;                                    \
-       out *res = NULL;                                                \
-       BUN i;                                                          \
-                                                                       \
-       /* assert calling sanity */                                     \
-       assert(bres != NULL && bone != NULL && btwo != NULL);           \
-       assert(BATcapacity(bres) >= n);                                 \
-       assert(BATcount(bone) >= n && BATcount(btwo) >= n);             \
-       assert(bone->ttype == TYPE_##in && btwo->ttype == TYPE_##in);   \
-       assert(bres->ttype == TYPE_##out);                              \
-                                                                       \
-       /* get direct access to the tail arrays */                      \
-       one = (in *) Tloc(bone, BUNfirst(bone));                        \
-       two = (in *) Tloc(btwo, BUNfirst(btwo));                        \
-       res = (out*) Tloc(bres, BUNfirst(bres));                        \
-       /* iterate over all values/tuples and do the work */            \
-       for (i = 0; i < n; i++)                                         \
-               if (one[i] == in##_nil || two[i] == in##_nil)           \
-                       /* NULL/nil in => NULL/nil out */               \
-                       res[i] = out##_nil;                             \
-               else                                                    \
-                       /* do the work; watch out for sign bits */      \
-                       res[i] = ((out) (uin) one[i] << shift) | (uin) two[i]; \
-                                                                       \
-       *two_tail_sorted_unsigned =                                     \
-               BATtordered(btwo) && (two[0] >= 0 || two[n-1] < 0);     \
-       *two_tail_revsorted_unsigned =                                  \
-               BATtrevordered(btwo) && (two[0] < 0 || two[n-1] >= 0);  \
-                                                                       \
-       return MAL_SUCCEED;                                             \
-}
-
-/* call C macro to define type-specific functions */
-UDFBATfuse_impl(bte, unsigned char,  sht,  8)
-UDFBATfuse_impl(sht, unsigned short, int, 16)
-UDFBATfuse_impl(int, unsigned int,   lng, 32)
 
 /* actual implementation */
 static str
@@ -322,13 +264,16 @@ UDFBATfuse_(BAT **ret, BAT *bone, BAT *b
        /* call type-specific core algorithm */
        switch (bone->ttype) {
        case TYPE_bte:
-               msg = UDFBATfuse_bte_sht ( bres, bone, btwo, n, 
&two_tail_sorted_unsigned, &two_tail_revsorted_unsigned );
+               msg = UDFBATfuse_bte_sht ( bres, bone, btwo, n,
+                       &two_tail_sorted_unsigned, &two_tail_revsorted_unsigned 
);
                break;
        case TYPE_sht:
-               msg = UDFBATfuse_sht_int ( bres, bone, btwo, n, 
&two_tail_sorted_unsigned, &two_tail_revsorted_unsigned );
+               msg = UDFBATfuse_sht_int ( bres, bone, btwo, n,
+                       &two_tail_sorted_unsigned, &two_tail_revsorted_unsigned 
);
                break;
        case TYPE_int:
-               msg = UDFBATfuse_int_lng ( bres, bone, btwo, n, 
&two_tail_sorted_unsigned, &two_tail_revsorted_unsigned );
+               msg = UDFBATfuse_int_lng ( bres, bone, btwo, n,
+                       &two_tail_sorted_unsigned, &two_tail_revsorted_unsigned 
);
                break;
        default:
                BBPreleaseref(bres->batCacheid);
diff --git a/sql/backends/monet5/UDF/udf_impl.h 
b/sql/backends/monet5/UDF/udf_impl.h
new file mode 100644
--- /dev/null
+++ b/sql/backends/monet5/UDF/udf_impl.h
@@ -0,0 +1,120 @@
+/*
+ * The contents of this file are subject to the MonetDB Public License
+ * Version 1.1 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ * http://www.monetdb.org/Legal/MonetDBLicense
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific language governing rights and limitations
+ * under the License.
+ *
+ * The Original Code is the MonetDB Database System.
+ *
+ * The Initial Developer of the Original Code is CWI.
+ * Portions created by CWI are Copyright (C) 1997-July 2008 CWI.
+ * Copyright August 2008-2012 MonetDB B.V.
+ * All Rights Reserved.
+ */
+
+/* This file is included multiple times.  We expect the tokens UI, UU, UO, US
+ * to be defined by the including file, and we expect that the
+ * combination (UI,UU,UO,US) is unique to each inclusion. */
+
+
+/* ! ENSURE THAT THESE LOCAL MACROS ARE UNDEFINED AT THE END OF THIS FILE ! */
+
+/* concatenate two or five tokens */
+#define U_CONCAT_2(a,b)       a##b
+#define U_CONCAT_5(a,b,c,d,e) a##b##c##d##e
+
+/* function names, *_nil & TYPE_* macros */
+#define UF(p,i,o,s) U_CONCAT_5(p,i,_,o,s)
+#define UN(t)       U_CONCAT_2(t,_nil)
+#define UT(t)       U_CONCAT_2(TYPE_,t)
+
+
+/* scalar fuse */
+
+/* fuse two (shift-byte) in values into one (2*shift-byte) out value */
+/* actual implementation */
+static str
+UF(UDFfuse_,UI,UO,_) ( UO *ret , UI one , UI two )
+{
+        int shift = sizeof(UI) * 8;
+
+       /* assert calling sanity */
+       assert(ret != NULL);
+
+       if (one == UN(UI) || two == UN(UI))
+               /* NULL/nil in => NULL/nil out */
+               *ret = UN(UO);
+       else
+               /* do the work; watch out for sign bits */
+               *ret = ((UO) (UU) one << shift) | (UU) two;
+
+       return MAL_SUCCEED;
+}
+/* MAL wrapper */
+str
+UF(UDFfuse_,UI,UO,) ( UO *ret , UI *one , UI *two )
+{
+       /* assert calling sanity */
+       assert(ret != NULL && one != NULL && two != NULL);
+
+       return UF(UDFfuse_,UI,UO,_) ( ret, *one, *two );
+}
+
+/* BAT fuse */
+/*
+ * TYPE-expanded optimized version,
+ * accessing value arrays directly.
+ */
+
+/* type-specific core algorithm */
+static str
+UF(UDFBATfuse_,UI,UO,)  ( BAT *bres, BAT *bone, BAT *btwo, BUN n,
+                       bit *two_tail_sorted_unsigned,
+                       bit *two_tail_revsorted_unsigned )
+{
+       UI *one = NULL, *two = NULL;
+       UO *res = NULL;
+       BUN i;
+        int shift = sizeof(UI) * 8;
+
+       /* assert calling sanity */
+       assert(bres != NULL && bone != NULL && btwo != NULL);
+       assert(BATcapacity(bres) >= n);
+       assert(BATcount(bone) >= n && BATcount(btwo) >= n);
+       assert(bone->ttype == UT(UI) && btwo->ttype == UT(UI));
+       assert(bres->ttype == UT(UO));
+
+       /* get direct access to the tail arrays */
+       one = (UI*) Tloc(bone, BUNfirst(bone));
+       two = (UI*) Tloc(btwo, BUNfirst(btwo));
+       res = (UO*) Tloc(bres, BUNfirst(bres));
+       /* iterate over all values/tuples and do the work */
+       for (i = 0; i < n; i++)
+               if (one[i] == UN(UI) || two[i] == UN(UI))
+                       /* NULL/nil in => NULL/nil out */
+                       res[i] = UN(UO);
+               else
+                       /* do the work; watch out for sign bits */
+                       res[i] = ((UO) (UU) one[i] << shift) | (UU) two[i];
+
+       *two_tail_sorted_unsigned =
+               BATtordered(btwo) && (two[0] >= 0 || two[n-1] < 0);
+       *two_tail_revsorted_unsigned =
+               BATtrevordered(btwo) && (two[0] < 0 || two[n-1] >= 0);
+
+       return MAL_SUCCEED;
+}
+
+
+/* undo local defines */
+#undef UT
+#undef UN
+#undef UF
+#undef U_CONCAT_5
+#undef U_CONCAT_2
+
_______________________________________________
Checkin-list mailing list
[email protected]
http://mail.monetdb.org/mailman/listinfo/checkin-list

Reply via email to