Changeset: fe1fb3ce0c9b for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=fe1fb3ce0c9b
Added Files:
        sql/backends/monet5/sql_round_impl.h
Modified Files:
        sql/backends/monet5/Makefile.ag
        sql/backends/monet5/sql_round.c
Branch: Jan2014
Log Message:

Reduce code duplication by including a file multiple times.


diffs (truncated from 2630 to 300 lines):

diff --git a/sql/backends/monet5/Makefile.ag b/sql/backends/monet5/Makefile.ag
--- a/sql/backends/monet5/Makefile.ag
+++ b/sql/backends/monet5/Makefile.ag
@@ -52,7 +52,7 @@ lib__sql = {
                sql_cast_impl_down_from_int.h \
                sql_cast_impl_up_to_flt.h \
                sql_cast_impl_up_to_int.h \
-               sql_round.c sql_bat2time.c sql_fround.c sql_rdf.c
+               sql_round.c sql_round_impl.h sql_bat2time.c sql_fround.c 
sql_rdf.c
        LIBS = ../../server/libsqlserver \
                   ../../storage/libstore \
                   ../../storage/bat/libbatstore \
diff --git a/sql/backends/monet5/sql_round.c b/sql/backends/monet5/sql_round.c
--- a/sql/backends/monet5/sql_round.c
+++ b/sql/backends/monet5/sql_round.c
@@ -56,2141 +56,33 @@ static lng scales[20] = {
        LL_CONSTANT(1000000000000000000)
 };
 
-static inline bte
-bte_dec_round_body_nonil(bte v, bte r)
-{
-       bte add = r >> 1;
+#define CONCAT_2(a, b)         a##b
+#define CONCAT_3(a, b, c)      a##b##c
 
-       assert(v != bte_nil);
+#define NIL(t)                 CONCAT_2(t, _nil)
+#define TPE(t)                 CONCAT_2(TYPE_, t)
+#define GDKmin(t)              CONCAT_3(GDK_, t, _min)
+#define GDKmax(t)              CONCAT_3(GDK_, t, _max)
+#define FUN(a, b)              CONCAT_3(a, _, b)
 
-       if (v < 0)
-               add = -add;
-       v += add;
-       return v / r;
-}
+#define STRING(a)              #a
 
-static inline bte
-bte_dec_round_body(bte v, bte r)
-{
-       /* shortcut nil */
-       if (v == bte_nil) {
-               return bte_nil;
-       } else {
-               return bte_dec_round_body_nonil(v, r);
-       }
-}
+#define TYPE bte
+#include "sql_round_impl.h"
+#undef TYPE
 
-str
-bte_dec_round_wrap(bte *res, bte *v, bte *r)
-{
-       /* basic sanity checks */
-       assert(res && v && r);
-       *res = bte_dec_round_body(*v, *r);
-       return MAL_SUCCEED;
-}
+#define TYPE sht
+#include "sql_round_impl.h"
+#undef TYPE
 
-str
-bte_bat_dec_round_wrap(bat *_res, bat *_v, bte *r)
-{
-       BAT *res, *v;
-       bte *src, *dst;
-       BUN i, cnt;
-       bit nonil;              /* TRUE: we know there are no NIL (NULL) values 
*/
-       bit nil;                /* TRUE: we know there is at least one NIL 
(NULL) value */
+#define TYPE int
+#include "sql_round_impl.h"
+#undef TYPE
 
-       /* basic sanity checks */
-       assert(_res && _v && r);
+#define TYPE wrd
+#include "sql_round_impl.h"
+#undef TYPE
 
-       /* get argument BAT descriptor */
-       if ((v = BATdescriptor(*_v)) == NULL)
-               throw(MAL, "round", RUNTIME_OBJECT_MISSING);
-
-       /* more sanity checks */
-       if (!BAThdense(v)) {
-               BBPreleaseref(v->batCacheid);
-               throw(MAL, "round", "argument 1 must have a dense head");
-       }
-       if (v->ttype != TYPE_bte) {
-               BBPreleaseref(v->batCacheid);
-               throw(MAL, "round", "argument 1 must have a bte tail");
-       }
-       cnt = BATcount(v);
-
-       /* allocate result BAT */
-       res = BATnew(TYPE_void, TYPE_bte, cnt);
-       if (res == NULL) {
-               BBPreleaseref(v->batCacheid);
-               throw(MAL, "round", MAL_MALLOC_FAIL);
-       }
-
-       /* access columns as arrays */
-       src = (bte *) Tloc(v, BUNfirst(v));
-       dst = (bte *) Tloc(res, BUNfirst(res));
-
-       nil = FALSE;
-       nonil = TRUE;
-       if (v->T->nonil == TRUE) {
-               for (i = 0; i < cnt; i++)
-                       dst[i] = bte_dec_round_body_nonil(src[i], *r);
-       } else {
-               for (i = 0; i < cnt; i++) {
-                       if (src[i] == bte_nil) {
-                               nil = TRUE;
-                               nonil = FALSE;
-                               dst[i] = bte_nil;
-                       } else {
-                               dst[i] = bte_dec_round_body_nonil(src[i], *r);
-                       }
-               }
-       }
-
-       /* set result BAT properties */
-       BATsetcount(res, cnt);
-       /* result head is aligned with agument head */
-       ALIGNsetH(res, v);
-       /* hard to predict correct tail properties in general */
-       res->T->nonil = nonil;
-       res->T->nil = nil;
-       res->tdense = FALSE;
-       res->tsorted = v->tsorted;
-       BATkey(BATmirror(res), FALSE);
-
-       /* release argument BAT descriptors */
-       BBPreleaseref(v->batCacheid);
-
-       /* keep result */
-       BBPkeepref(*_res = res->batCacheid);
-
-       return MAL_SUCCEED;
-}
-
-static inline bte
-bte_round_body_nonil(bte v, int d, int s, bte r)
-{
-       bte res = bte_nil;
-
-       assert(v != bte_nil);
-
-       if (-r > d) {
-               res = 0;
-       } else if (r > 0 && r < s) {
-               int dff = s - r;
-               lng rnd = scales[dff] >> 1;
-               lng lres;
-               if (v > 0)
-                       lres = (((v + rnd) / scales[dff]) * scales[dff]);
-               else
-                       lres = (((v - rnd) / scales[dff]) * scales[dff]);
-               assert((lng) GDK_bte_min < lres && lres <= (lng) GDK_bte_max);
-               res = (bte) lres;
-       } else if (r <= 0 && -r + s > 0) {
-               int dff = -r + s;
-               lng rnd = scales[dff] >> 1;
-               lng lres;
-               if (v > 0)
-                       lres = (((v + rnd) / scales[dff]) * scales[dff]);
-               else
-                       lres = (((v - rnd) / scales[dff]) * scales[dff]);
-               assert((lng) GDK_bte_min < lres && lres <= (lng) GDK_bte_max);
-               res = (bte) lres;
-       } else {
-               res = v;
-       }
-       return res;
-}
-
-static inline bte
-bte_round_body(bte v, int d, int s, bte r)
-{
-       /* shortcut nil */
-       if (v == bte_nil) {
-               return bte_nil;
-       } else {
-               return bte_round_body_nonil(v, d, s, r);
-       }
-}
-
-str
-bte_round_wrap(bte *res, bte *v, int *d, int *s, bte *r)
-{
-       /* basic sanity checks */
-       assert(res && v && r && d && s);
-
-       *res = bte_round_body(*v, *d, *s, *r);
-       return MAL_SUCCEED;
-}
-
-str
-bte_bat_round_wrap(bat *_res, bat *_v, int *d, int *s, bte *r)
-{
-       BAT *res, *v;
-       bte *src, *dst;
-       BUN i, cnt;
-       bit nonil;              /* TRUE: we know there are no NIL (NULL) values 
*/
-       bit nil;                /* TRUE: we know there is at least one NIL 
(NULL) value */
-
-       /* basic sanity checks */
-       assert(_res && _v && r && d && s);
-
-       /* get argument BAT descriptor */
-       if ((v = BATdescriptor(*_v)) == NULL)
-               throw(MAL, "round", RUNTIME_OBJECT_MISSING);
-
-       /* more sanity checks */
-       if (!BAThdense(v)) {
-               BBPreleaseref(v->batCacheid);
-               throw(MAL, "round", "argument 1 must have a dense head");
-       }
-       if (v->ttype != TYPE_bte) {
-               BBPreleaseref(v->batCacheid);
-               throw(MAL, "round", "argument 1 must have a bte tail");
-       }
-       cnt = BATcount(v);
-
-       /* allocate result BAT */
-       res = BATnew(TYPE_void, TYPE_bte, cnt);
-       if (res == NULL) {
-               BBPreleaseref(v->batCacheid);
-               throw(MAL, "round", MAL_MALLOC_FAIL);
-       }
-
-       /* access columns as arrays */
-       src = (bte *) Tloc(v, BUNfirst(v));
-       dst = (bte *) Tloc(res, BUNfirst(res));
-
-       nil = FALSE;
-       nonil = TRUE;
-       if (v->T->nonil == TRUE) {
-               for (i = 0; i < cnt; i++)
-                       dst[i] = bte_round_body_nonil(src[i], *d, *s, *r);
-       } else {
-               for (i = 0; i < cnt; i++) {
-                       if (src[i] == bte_nil) {
-                               nil = TRUE;
-                               nonil = FALSE;
-                               dst[i] = bte_nil;
-                       } else {
-                               dst[i] = bte_round_body_nonil(src[i], *d, *s, 
*r);
-                       }
-               }
-       }
-
-       /* set result BAT properties */
-       BATsetcount(res, cnt);
-       /* result head is aligned with agument head */
-       ALIGNsetH(res, v);
-       /* hard to predict correct tail properties in general */
-       res->T->nonil = nonil;
-       res->T->nil = nil;
-       res->tdense = FALSE;
-       res->tsorted = v->tsorted;
-       BATkey(BATmirror(res), FALSE);
-
-       /* release argument BAT descriptors */
-       BBPreleaseref(v->batCacheid);
-
-       /* keep result */
-       BBPkeepref(*_res = res->batCacheid);
-
-       return MAL_SUCCEED;
-}
-
-str
-nil_2dec_bte(bte *res, void *val, int *d, int *sc)
-{
-       (void) val;
-       (void) d;
-       (void) sc;
-
-       *res = bte_nil;
-       return MAL_SUCCEED;
-}
-
-str
-str_2dec_bte(bte *res, str *val, int *d, int *sc)
-{
-       char *s = strip_extra_zeros(*val);
-       char *dot = strchr(s, '.');
-       int digits = _strlen(s) - 1;
-       int scale = digits - (int) (dot - s);
-       lng value = 0;
-
-       if (!dot) {
-               if (GDK_STRNIL(*val)) {
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to