From e8ac0381f976f2dfd70fbe52e0ec59c1a8da9df6 Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Sat, 18 Feb 2023 18:52:21 +0000 Subject: [PATCH] Convert a lot of floating-point constants to single precision For reasons now lost to history, Puzzles generally uses single-precision floating point. However, C floating-point constants are by default double-precision, and if they're then operated on along with a single-precision variable the value of the variable gets promoted to double precision, then the operation gets done, and then often the result gets converted back to single precision again. This is obviously silly, so I've used Clang's "-Wdouble-promotion" to find instances of this and mark the constants as single-precision as well. This is a bit awkward for PI, which ends up with a cast. Maybe there should be a PIF, or maybe PI should just be single-precision. This doesn't eliminate all warnings from -Wdouble-promotion. Some of the others might merit fixing but adding explicit casts to double just to shut the compiler up would be going too far, I feel. --- cube.c | 10 +++++----- drawing.c | 8 ++++---- emcc.c | 6 +++--- flip.c | 2 +- galaxies.c | 8 ++++---- inertia.c | 2 +- net.c | 12 ++++++------ netslide.c | 4 ++-- samegame.c | 2 +- twiddle.c | 8 ++++---- undead.c | 2 +- 11 files changed, 32 insertions(+), 32 deletions(-) diff --git a/cube.c b/cube.c index 99f392a..69a9d35 100644 --- a/cube.c +++ b/cube.c @@ -171,7 +171,7 @@ enum { LEFT, RIGHT, UP, DOWN, UP_LEFT, UP_RIGHT, DOWN_LEFT, DOWN_RIGHT }; (ra)[0] = rx; (ra)[1] = ry; (ra)[2] = rz; \ } while (0) -#define APPROXEQ(x,y) ( SQ(x-y) < 0.1 ) +#define APPROXEQ(x,y) ( SQ(x-y) < 0.1F ) struct grid_square { float x, y; @@ -787,7 +787,7 @@ static bool align_poly(const struct solid *solid, struct grid_square *sq, dist += SQ(solid->vertices[i*3+1] * flip - sq->points[j*2+1] + sq->y); dist += SQ(solid->vertices[i*3+2] - zmin); - if (dist < 0.1) { + if (dist < 0.1F) { matches++; index = i; } @@ -837,7 +837,7 @@ static struct solid *transform_poly(const struct solid *solid, bool flip, */ vx = ret->vertices[key1*3+0] - ret->vertices[key0*3+0]; vy = ret->vertices[key1*3+1] - ret->vertices[key0*3+1]; - assert(APPROXEQ(vx*vx + vy*vy, 1.0)); + assert(APPROXEQ(vx*vx + vy*vy, 1.0F)); vmatrix[0] = vx; vmatrix[3] = vy; vmatrix[6] = 0; vmatrix[1] = -vy; vmatrix[4] = vx; vmatrix[7] = 0; @@ -1091,11 +1091,11 @@ static int find_move_dest(const game_state *from, int direction, for (j = 0; j < from->grid->squares[i].npoints; j++) { dist = (SQ(from->grid->squares[i].points[j*2] - points[0]) + SQ(from->grid->squares[i].points[j*2+1] - points[1])); - if (dist < 0.1) + if (dist < 0.1F) dkey[match++] = j; dist = (SQ(from->grid->squares[i].points[j*2] - points[2]) + SQ(from->grid->squares[i].points[j*2+1] - points[3])); - if (dist < 0.1) + if (dist < 0.1F) dkey[match++] = j; } diff --git a/drawing.c b/drawing.c index 3584936..5fb8a31 100644 --- a/drawing.c +++ b/drawing.c @@ -90,8 +90,8 @@ void draw_line(drawing *dr, int x1, int y1, int x2, int y2, int colour) void draw_thick_line(drawing *dr, float thickness, float x1, float y1, float x2, float y2, int colour) { - if (thickness < 1.0) - thickness = 1.0; + if (thickness < 1.0F) + thickness = 1.0F; if (dr->api->draw_thick_line) { dr->api->draw_thick_line(dr->handle, thickness, x1, y1, x2, y2, colour); @@ -101,8 +101,8 @@ void draw_thick_line(drawing *dr, float thickness, * polygon rendering uses integer coordinates. */ float len = sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1)); - float tvhatx = (x2 - x1)/len * (thickness/2 - 0.2); - float tvhaty = (y2 - y1)/len * (thickness/2 - 0.2); + float tvhatx = (x2 - x1)/len * (thickness/2 - 0.2F); + float tvhaty = (y2 - y1)/len * (thickness/2 - 0.2F); int p[8]; p[0] = x1 - tvhaty; diff --git a/emcc.c b/emcc.c index d634b7c..8037be4 100644 --- a/emcc.c +++ b/emcc.c @@ -1045,9 +1045,9 @@ int main(int argc, char **argv) for (i = 0; i < ncolours; i++) { char col[40]; sprintf(col, "#%02x%02x%02x", - (unsigned)(0.5 + 255 * colours[i*3+0]), - (unsigned)(0.5 + 255 * colours[i*3+1]), - (unsigned)(0.5 + 255 * colours[i*3+2])); + (unsigned)(0.5F + 255 * colours[i*3+0]), + (unsigned)(0.5F + 255 * colours[i*3+1]), + (unsigned)(0.5F + 255 * colours[i*3+2])); colour_strings[i] = dupstr(col); } /* Put the background colour in a CSS variable. */ diff --git a/flip.c b/flip.c index cc80b09..466a3af 100644 --- a/flip.c +++ b/flip.c @@ -1160,7 +1160,7 @@ static void draw_tile(drawing *dr, game_drawstate *ds, const game_state *state, coords[7] = by + TILE_SIZE - (int)((float)TILE_SIZE * animtime); colour = (tile & 1 ? COL_WRONG : COL_RIGHT); - if (animtime < 0.5) + if (animtime < 0.5F) colour = COL_WRONG + COL_RIGHT - colour; draw_polygon(dr, coords, 4, colour, COL_GRID); diff --git a/galaxies.c b/galaxies.c index a789349..73356fb 100644 --- a/galaxies.c +++ b/galaxies.c @@ -2503,8 +2503,8 @@ static char *interpret_move(const game_state *state, game_ui *ui, int px, py; space *sp; - px = 2*FROMCOORD((float)x) + 0.5; - py = 2*FROMCOORD((float)y) + 0.5; + px = 2*FROMCOORD((float)x) + 0.5F; + py = 2*FROMCOORD((float)y) + 0.5F; if (button == 'C' || button == 'c') return dupstr("C"); @@ -2618,8 +2618,8 @@ static char *interpret_move(const game_state *state, game_ui *ui, ui->cur_visible = false; - px = (int)(2*FROMCOORD((float)x) + 0.5); - py = (int)(2*FROMCOORD((float)y) + 0.5); + px = (int)(2*FROMCOORD((float)x) + 0.5F); + py = (int)(2*FROMCOORD((float)y) + 0.5F); dot = NULL; diff --git a/inertia.c b/inertia.c index b47d58e..ed50c7a 100644 --- a/inertia.c +++ b/inertia.c @@ -1605,7 +1605,7 @@ static char *interpret_move(const game_state *state, game_ui *ui, * end up the right way round. */ angle = atan2(dx, -dy); - angle = (angle + (PI/8)) / (PI/4); + angle = (angle + (float)(PI/8)) / (float)(PI/4); assert(angle > -16.0F); dir = (int)(angle + 16.0F) & 7; } diff --git a/net.c b/net.c index 9ac2270..76273c2 100644 --- a/net.c +++ b/net.c @@ -2606,8 +2606,8 @@ static void draw_wires(drawing *dr, int cx, int cy, int radius, for (i = 0; i < npoints; i++) { rotated_coords(&xf, &yf, matrix, cx, cy, fpoints[2*i], fpoints[2*i+1]); - points[2*i] = 0.5 + xf; - points[2*i+1] = 0.5 + yf; + points[2*i] = 0.5F + xf; + points[2*i+1] = 0.5F + yf; } draw_polygon(dr, points, npoints, colour, colour); @@ -2747,8 +2747,8 @@ static void draw_tile(drawing *dr, game_drawstate *ds, int x, int y, * rotated by an arbitrary angle about that centre point. */ if (tile & TILE_ROTATING) { - matrix[0] = (float)cos(angle * PI / 180.0); - matrix[2] = (float)sin(angle * PI / 180.0); + matrix[0] = (float)cos(angle * (float)PI / 180.0F); + matrix[2] = (float)sin(angle * (float)PI / 180.0F); } else { matrix[0] = 1.0F; matrix[2] = 0.0F; @@ -2787,8 +2787,8 @@ static void draw_tile(drawing *dr, game_drawstate *ds, int x, int y, float x, y; rotated_coords(&x, &y, matrix, cx, cy, boxr * points[i], boxr * points[i+1]); - points[i] = x + 0.5; - points[i+1] = y + 0.5; + points[i] = x + 0.5F; + points[i+1] = y + 0.5F; } draw_polygon(dr, points, 4, col, COL_WIRE); diff --git a/netslide.c b/netslide.c index be83b7c..9305906 100644 --- a/netslide.c +++ b/netslide.c @@ -1492,7 +1492,7 @@ static void draw_tile(drawing *dr, game_drawstate *ds, const game_state *state, vx = (dy ? 1 : 0); vy = (dx ? 1 : 0); - if (xshift == 0.0 && yshift == 0.0 && (tile & dir)) { + if (xshift == 0.0F && yshift == 0.0F && (tile & dir)) { /* * If we are fully connected to the other tile, we must * draw right across the tile border. (We can use our @@ -1696,7 +1696,7 @@ static void game_redraw(drawing *dr, game_drawstate *ds, /* * Draw any tile which differs from the way it was last drawn. */ - if (xshift != 0.0 || yshift != 0.0) { + if (xshift != 0.0F || yshift != 0.0F) { active = compute_active(state, state->last_move_row, state->last_move_col); } else { diff --git a/samegame.c b/samegame.c index 121ed7d..57ce4b8 100644 --- a/samegame.c +++ b/samegame.c @@ -1554,7 +1554,7 @@ static void game_redraw(drawing *dr, game_drawstate *ds, ds->started = true; } - if (flashtime > 0.0) { + if (flashtime > 0.0F) { int frame = (int)(flashtime / FLASH_FRAME); bgcolour = (frame % 2 ? COL_LOWLIGHT : COL_HIGHLIGHT); } else diff --git a/twiddle.c b/twiddle.c index 8c565a0..a8ed114 100644 --- a/twiddle.c +++ b/twiddle.c @@ -884,8 +884,8 @@ static void rotate(int *xy, struct rotation *rot) xf2 = rot->c * xf + rot->s * yf; yf2 = - rot->s * xf + rot->c * yf; - xy[0] = (int)(xf2 + rot->ox + 0.5); /* round to nearest */ - xy[1] = (int)(yf2 + rot->oy + 0.5); /* round to nearest */ + xy[0] = (int)(xf2 + rot->ox + 0.5F); /* round to nearest */ + xy[1] = (int)(yf2 + rot->oy + 0.5F); /* round to nearest */ } } @@ -1072,7 +1072,7 @@ static int highlight_colour(float angle) COL_LOWLIGHT, }; - return colours[(int)((angle + 2*PI) / (PI/16)) & 31]; + return colours[(int)((angle + 2*(float)PI) / ((float)PI/16)) & 31]; } static float game_anim_length_real(const game_state *oldstate, @@ -1196,7 +1196,7 @@ static void game_redraw(drawing *dr, game_drawstate *ds, rot->cw = rot->ch = TILE_SIZE * state->n; rot->ox = rot->cx + rot->cw/2; rot->oy = rot->cy + rot->ch/2; - angle = (float)((-PI/2 * lastr) * (1.0 - animtime / anim_max)); + angle = ((-(float)PI/2 * lastr) * (1.0F - animtime / anim_max)); rot->c = (float)cos(angle); rot->s = (float)sin(angle); diff --git a/undead.c b/undead.c index 78abf28..1c52c27 100644 --- a/undead.c +++ b/undead.c @@ -1031,7 +1031,7 @@ static char *new_game_desc(const game_params *params, random_state *rs, /* Monsters / Mirrors ratio should be balanced */ ratio = (float)new->common->num_total / (float)(new->common->params.w * new->common->params.h); - if (ratio < 0.48 || ratio > 0.78) { + if (ratio < 0.48F || ratio > 0.78F) { free_game(new); continue; }