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.
This commit is contained in:
Ben Harris
2023-02-18 18:52:21 +00:00
parent 26c7f3aa28
commit e8ac0381f9
11 changed files with 32 additions and 32 deletions

10
cube.c
View File

@ -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;
}

View File

@ -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;

6
emcc.c
View File

@ -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. */

2
flip.c
View File

@ -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);

View File

@ -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;

View File

@ -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;
}

12
net.c
View File

@ -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);

View File

@ -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 {

View File

@ -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

View File

@ -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);

View File

@ -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;
}