Changeset: 625055e430ba for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=625055e430ba
Modified Files:
sql/backends/monet5/sql_round.c
sql/backends/monet5/sql_round_impl.h
Branch: default
Log Message:
Remove (near) duplicate code.
diffs (truncated from 523 to 300 lines):
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
@@ -110,6 +110,8 @@ static lng scales[19] = {
#define STRING(a) #a
+#define BIG int /* a larger type */
+
#define TYPE bte
#include "sql_round_impl.h"
#undef TYPE
@@ -118,6 +120,9 @@ static lng scales[19] = {
#include "sql_round_impl.h"
#undef TYPE
+#undef BIG
+#define BIG lng
+
#define TYPE int
#include "sql_round_impl.h"
#undef TYPE
@@ -131,437 +136,9 @@ static lng scales[19] = {
#undef TYPE
#ifdef HAVE_HGE
-static inline hge
-hge_dec_round_body_nonil(hge v, hge r)
-{
- hge add = r >> 1;
-
- assert(v != hge_nil);
-
- if (v < 0)
- add = -add;
- v += add;
- return v / r;
-}
-
-static inline hge
-hge_dec_round_body(hge v, hge r)
-{
- /* shortcut nil */
- if (v == hge_nil) {
- return hge_nil;
- } else {
- return hge_dec_round_body_nonil(v, r);
- }
-}
-
-str
-hge_dec_round_wrap(hge *res, hge *v, hge *r)
-{
- /* basic sanity checks */
- assert(res && v && r);
-
- *res = hge_dec_round_body(*v, *r);
- return MAL_SUCCEED;
-}
-
-str
-hge_bat_dec_round_wrap(bat *_res, bat *_v, hge *r)
-{
- BAT *res, *v;
- hge *src, *dst;
- BUN i, cnt;
- int nonil; /* TRUE: we know there are no NIL (NULL) values
*/
-
- /* basic sanity checks */
- assert(_res && _v && r);
-
- /* 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_hge) {
- BBPreleaseref(v->batCacheid);
- throw(MAL, "round", "argument 1 must have a hge tail");
- }
- cnt = BATcount(v);
-
- /* allocate result BAT */
- res = BATnew(TYPE_void, TYPE_hge, cnt, TRANSIENT);
- if (res == NULL) {
- BBPreleaseref(v->batCacheid);
- throw(MAL, "round", MAL_MALLOC_FAIL);
- }
-
- /* access columns as arrays */
- src = (hge *) Tloc(v, BUNfirst(v));
- dst = (hge *) Tloc(res, BUNfirst(res));
-
- nonil = TRUE;
- if (v->T->nonil == TRUE) {
- for (i = 0; i < cnt; i++)
- dst[i] = hge_dec_round_body_nonil(src[i], *r);
- } else {
- for (i = 0; i < cnt; i++) {
- if (src[i] == hge_nil) {
- nonil = FALSE;
- dst[i] = hge_nil;
- } else {
- dst[i] = hge_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 = !nonil;
- res->tdense = FALSE;
- res->tsorted = v->tsorted;
- res->trevsorted = v->trevsorted;
- BATkey(BATmirror(res), FALSE);
-
- /* release argument BAT descriptors */
- BBPreleaseref(v->batCacheid);
-
- /* keep result */
- BBPkeepref(*_res = res->batCacheid);
-
- return MAL_SUCCEED;
-}
-
-static inline hge
-hge_round_body_nonil(hge v, int d, int s, int r)
-{
- hge res = hge_nil;
-
- assert(v != hge_nil);
-
- if (-r > d) {
- res = 0;
- } else if (r > 0 && r < s) {
- int dff = s - r;
- hge rnd = scales[dff] >> 1;
- hge hres;
- if (v > 0)
- hres = (((v + rnd) / scales[dff]) * scales[dff]);
- else
- hres = (((v - rnd) / scales[dff]) * scales[dff]);
- assert((hge) GDK_hge_min < hres && hres <= (hge) GDK_hge_max);
- res = (hge) hres;
- } else if (r <= 0 && -r + s > 0) {
- int dff = -r + s;
- hge rnd = scales[dff] >> 1;
- hge hres;
- if (v > 0)
- hres = (((v + rnd) / scales[dff]) * scales[dff]);
- else
- hres = (((v - rnd) / scales[dff]) * scales[dff]);
- assert((hge) GDK_hge_min < hres && hres <= (hge) GDK_hge_max);
- res = (hge) hres;
- } else {
- res = v;
- }
- return res;
-}
-
-static inline hge
-hge_round_body(hge v, int d, int s, int r)
-{
- /* shortcut nil */
- if (v == hge_nil) {
- return hge_nil;
- } else {
- return hge_round_body_nonil(v, d, s, r);
- }
-}
-
-str
-hge_round_wrap(hge *res, hge *v, int *d, int *s, bte *r)
-{
- /* basic sanity checks */
- assert(res && v && r && d && s);
-
- *res = hge_round_body(*v, *d, *s, *r);
- return MAL_SUCCEED;
-}
-
-str
-hge_bat_round_wrap(bat *_res, bat *_v, int *d, int *s, bte *r)
-{
- BAT *res, *v;
- hge *src, *dst;
- BUN i, cnt;
- int nonil; /* TRUE: we know there are no NIL (NULL) values
*/
-
- /* 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_hge) {
- BBPreleaseref(v->batCacheid);
- throw(MAL, "round", "argument 1 must have a hge tail");
- }
- cnt = BATcount(v);
-
- /* allocate result BAT */
- res = BATnew(TYPE_void, TYPE_hge, cnt, TRANSIENT);
- if (res == NULL) {
- BBPreleaseref(v->batCacheid);
- throw(MAL, "round", MAL_MALLOC_FAIL);
- }
-
- /* access columns as arrays */
- src = (hge *) Tloc(v, BUNfirst(v));
- dst = (hge *) Tloc(res, BUNfirst(res));
-
- nonil = TRUE;
- if (v->T->nonil == TRUE) {
- for (i = 0; i < cnt; i++)
- dst[i] = hge_round_body_nonil(src[i], *d, *s, *r);
- } else {
- for (i = 0; i < cnt; i++) {
- if (src[i] == hge_nil) {
- nonil = FALSE;
- dst[i] = hge_nil;
- } else {
- dst[i] = hge_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 = !nonil;
- res->tdense = FALSE;
- res->tsorted = v->tsorted;
- res->trevsorted = v->trevsorted;
- BATkey(BATmirror(res), FALSE);
-
- /* release argument BAT descriptors */
- BBPreleaseref(v->batCacheid);
-
- /* keep result */
- BBPkeepref(*_res = res->batCacheid);
-
- return MAL_SUCCEED;
-}
-
-str
-nil_2dec_hge(hge *res, void *val, int *d, int *sc)
-{
- (void) val;
- (void) d;
- (void) sc;
-
- *res = hge_nil;
- return MAL_SUCCEED;
-}
-
-str
-str_2dec_hge(hge *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);
- hge value = 0;
-
- if (!dot) {
- if (GDK_STRNIL(*val)) {
- *res = hge_nil;
- return MAL_SUCCEED;
- } else {
- scale = 0;
- }
- } else { /* we have a dot in the string */
- digits--;
- }
-
- value = decimal_from_str(s);
- if (*s == '+' || *s == '-')
- digits--;
- if (scale < *sc) {
- /* the current scale is too small, increase it by adding 0's */
- int dff = *sc - scale; /* CANNOT be 0! */
-
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list