From 0c9eff16d91925308de9f0fc087edc22135283c7 Mon Sep 17 00:00:00 2001 From: Stefan Frederik Date: Fri, 17 Dec 2021 15:40:19 +0100 Subject: [PATCH] my_round() function instead of ROUND() macro --- src/actions.c | 13 +++++++++++-- src/callback.c | 24 ++++++++++++------------ src/move.c | 8 ++++---- src/save.c | 2 +- src/token.c | 2 +- src/xschem.h | 2 +- 6 files changed, 30 insertions(+), 21 deletions(-) diff --git a/src/actions.c b/src/actions.c index a2929ae6..735e3409 100644 --- a/src/actions.c +++ b/src/actions.c @@ -623,10 +623,12 @@ void enable_layers(void) { int i; char tmp[50]; + const char *en; xctx->n_active_layers = 0; for(i = 0; i< cadlayers; i++) { my_snprintf(tmp, S(tmp), "enable_layer(%d)",i); - if(tclgetvar(tmp)[0] == '0') xctx->enable_layer[i] = 0; + en = tclgetvar(tmp); + if(!en || en[0] == '0') xctx->enable_layer[i] = 0; else { xctx->enable_layer[i] = 1; if(i>=7) { @@ -2296,6 +2298,13 @@ int text_bbox(const char *str,double xscale, double yscale, return 1; } +/* does not exist in C89 */ +double my_round(double a) +{ + /* return 0.0 or -0.0 if a == 0.0 or -0.0 */ + return (a == 0.0) ? a : (a > 0.0) ? floor(a + 0.5) : ceil(a - 0.5); +} + int place_text(int draw_text, double mx, double my) { char *txt; @@ -2319,7 +2328,7 @@ int place_text(int draw_text, double mx, double my) dbg(1, "place_text(): hsize=%s vsize=%s\n",tclgetvar("hsize"), tclgetvar("vsize") ); txt = (char *)tclgetvar("retval"); - if(!strcmp(txt,"")) return 0; /* dont allocate text object if empty string given */ + if(!txt || !strcmp(txt,"")) return 0; /* dont allocate text object if empty string given */ xctx->push_undo(); check_text_storage(); t->txt_ptr=NULL; diff --git a/src/callback.c b/src/callback.c index 7108cccf..2655020b 100644 --- a/src/callback.c +++ b/src/callback.c @@ -220,8 +220,8 @@ int callback(const char *winpath, int event, int mx, int my, KeySym key, } xctx->mousex=X_TO_XSCHEM(mx); xctx->mousey=Y_TO_XSCHEM(my); - xctx->mousex_snap=ROUND(xctx->mousex / c_snap) * c_snap; - xctx->mousey_snap=ROUND(xctx->mousey / c_snap) * c_snap; + xctx->mousex_snap=my_round(xctx->mousex / c_snap) * c_snap; + xctx->mousey_snap=my_round(xctx->mousey / c_snap) * c_snap; my_snprintf(str, S(str), "mouse = %.16g %.16g - selected: %d path: %s", xctx->mousex_snap, xctx->mousey_snap, xctx->lastsel, xctx->sch_path[xctx->currsch] ); statusmsg(str,1); @@ -597,8 +597,8 @@ int callback(const char *winpath, int event, int mx, int my, KeySym key, if(xctx->semaphore >= 2) break; if(!(xctx->ui_state & STARTWIRE)){ find_closest_net_or_symbol_pin(xctx->mousex, xctx->mousey, &x, &y); - xctx->mx_double_save = ROUND(x / c_snap) * c_snap; - xctx->my_double_save = ROUND(y / c_snap) * c_snap; + xctx->mx_double_save = my_round(x / c_snap) * c_snap; + xctx->my_double_save = my_round(y / c_snap) * c_snap; new_wire(PLACE, x, y); } else { @@ -1432,18 +1432,18 @@ int callback(const char *winpath, int event, int mx, int my, KeySym key, clear_drawing(); for(i=0;i<5000; i++) { - w = (1+ROUND(rand()%1200/snap))*snap; - x1 = 40000+ROUND(rand()%30000/snap)*snap; - y1 = 40000+ROUND(rand()%30000/snap)*snap; + w = (1+my_round(rand()%1200/snap))*snap; + x1 = 40000+my_round(rand()%30000/snap)*snap; + y1 = 40000+my_round(rand()%30000/snap)*snap; x2=x1+w; y2=y1; ORDER(x1, y1, x2, y2); storeobject(-1, x1, y1, x2, y2 ,WIRE,0,0,NULL); } for(i=0;i<5000; i++) { - w = (1+ROUND(rand()%1200/snap))*snap; - x1 = 40000+ROUND(rand()%30000/snap)*snap; - y1 = 40000+ROUND(rand()%30000/snap)*snap; + w = (1+my_round(rand()%1200/snap))*snap; + x1 = 40000+my_round(rand()%30000/snap)*snap; + y1 = 40000+my_round(rand()%30000/snap)*snap; x2=x1; y2=y1+w; ORDER(x1, y1, x2, y2); @@ -1681,8 +1681,8 @@ int callback(const char *winpath, int event, int mx, int my, KeySym key, double x, y; find_closest_net_or_symbol_pin(xctx->mousex, xctx->mousey, &x, &y); - xctx->mx_double_save = ROUND(x / c_snap) * c_snap; - xctx->my_double_save = ROUND(y / c_snap) * c_snap; + xctx->mx_double_save = my_round(x / c_snap) * c_snap; + xctx->my_double_save = my_round(y / c_snap) * c_snap; new_wire(PLACE, x, y); xctx->ui_state &=~MENUSTARTSNAPWIRE; break; diff --git a/src/move.c b/src/move.c index c3a2118c..38073f23 100644 --- a/src/move.c +++ b/src/move.c @@ -418,12 +418,12 @@ void draw_selection(GC g, int interruptable) drawtemparc(g, ADD, xctx->rx1, xctx->ry1, fabs(xctx->arc[c][n].r+xctx->deltax), angle, xctx->arc[c][n].b); } else if(xctx->arc[c][n].sel==SELECTED3) { - angle = ROUND(fmod(atan2(-xctx->deltay, xctx->deltax)*180./XSCH_PI+xctx->arc[c][n].b, 360.)); + angle = my_round(fmod(atan2(-xctx->deltay, xctx->deltax)*180./XSCH_PI+xctx->arc[c][n].b, 360.)); if(angle<0.) angle +=360.; if(angle==0) angle=360.; drawtemparc(g, ADD, xctx->rx1, xctx->ry1, xctx->arc[c][n].r, xctx->arc[c][n].a, angle); } else if(xctx->arc[c][n].sel==SELECTED2) { - angle = ROUND(fmod(atan2(-xctx->deltay, xctx->deltax)*180./XSCH_PI+angle, 360.)); + angle = my_round(fmod(atan2(-xctx->deltay, xctx->deltax)*180./XSCH_PI+angle, 360.)); if(angle<0.) angle +=360.; drawtemparc(g, ADD, xctx->rx1, xctx->ry1, xctx->arc[c][n].r, angle, xctx->arc[c][n].b); } @@ -1324,13 +1324,13 @@ void move_objects(int what, int merge, double dx, double dy) if(xctx->arc[c][n].r+xctx->deltax) xctx->arc[c][n].r = fabs(xctx->arc[c][n].r+xctx->deltax); xctx->arc[c][n].a = angle; } else if(xctx->arc[c][n].sel == SELECTED2) { - angle = ROUND(fmod(atan2(-xctx->deltay, xctx->deltax)*180./XSCH_PI+angle, 360.)); + angle = my_round(fmod(atan2(-xctx->deltay, xctx->deltax)*180./XSCH_PI+angle, 360.)); if(angle<0.) angle +=360.; xctx->arc[c][n].x = xctx->rx1; xctx->arc[c][n].y = xctx->ry1; xctx->arc[c][n].a = angle; } else if(xctx->arc[c][n].sel==SELECTED3) { - angle = ROUND(fmod(atan2(-xctx->deltay, xctx->deltax)*180./XSCH_PI+xctx->arc[c][n].b, 360.)); + angle = my_round(fmod(atan2(-xctx->deltay, xctx->deltax)*180./XSCH_PI+xctx->arc[c][n].b, 360.)); if(angle<0.) angle +=360.; if(angle==0) angle=360.; xctx->arc[c][n].x = xctx->rx1; diff --git a/src/save.c b/src/save.c index a4fb3cc1..98a5527d 100644 --- a/src/save.c +++ b/src/save.c @@ -2436,7 +2436,7 @@ void descend_symbol(void) } /* 20111023 align selected object to current grid setting */ -#define SNAP_TO_GRID(a) (a=ROUND(( a)/c_snap)*c_snap ) +#define SNAP_TO_GRID(a) (a=my_round(( a)/c_snap)*c_snap ) void round_schematic_to_grid(double c_snap) { int i, c, n, p; diff --git a/src/token.c b/src/token.c index 7c3b6d20..90728bd3 100644 --- a/src/token.c +++ b/src/token.c @@ -413,7 +413,7 @@ const char *list_tokens(const char *s, int with_quotes) /* state machine that parses a string made up of = ... */ /* couples and returns the value of the given token */ /* if s==NULL or no match return empty string */ -/* NULL tok NOT ALLOWED !!!!!!!! */ +/* NULL tok is used with NULL s to free internal storage (destructor) */ /* never returns NULL... */ /* with_quotes: */ /* bit 0: */ diff --git a/src/xschem.h b/src/xschem.h index 620cda2e..b3d008a9 100644 --- a/src/xschem.h +++ b/src/xschem.h @@ -303,7 +303,6 @@ extern char win_temp_dir[PATH_MAX]; #define CLIP(x,a,b) ((x) < a ? (a) : (x) > b ? (b) : (x)) #define MINOR(a,b) ( (a) <= (b) ? (a) : (b) ) -#define ROUND(a) ((a) > 0.0 ? ceil((a) - 0.5) : floor((a) + 0.5)) #define IS_LABEL_SH_OR_PIN(type) (!(strcmp(type,"label") && strcmp(type,"ipin") && strcmp(type,"opin") && \ strcmp(type,"show_label") && strcmp(type,"iopin"))) @@ -1161,6 +1160,7 @@ extern void my_realloc(int id, void *ptr,size_t size); extern void *my_calloc(int id, size_t nmemb, size_t size); extern void my_free(int id, void *ptr); extern size_t my_strcat(int id, char **, const char *); +extern double my_round(double a); extern const char *subst_token(const char *s, const char *tok, const char *new_val); extern void new_prop_string(int i, const char *old_prop,int fast, int dis_uniq_names); extern void hash_name(char *token, int remove);