Skip to content

Commit

Permalink
Eliminate warnings reported by Cppcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
jserv committed Jul 21, 2024
1 parent 3d80857 commit 545945c
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 74 deletions.
16 changes: 6 additions & 10 deletions src/fixed.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,15 @@

twin_fixed_t twin_fixed_sqrt(twin_fixed_t a)
{
twin_fixed_t max, min, mid;
twin_fixed_t sqr;
twin_fixed_t max = a, min = 0;

max = a;
min = 0;
while (max > min) {
mid = (max + min) >> 1;
twin_fixed_t mid = (max + min) >> 1;
if (mid >= 181 * TWIN_FIXED_ONE) {
max = mid - 1;
continue;
}
sqr = twin_fixed_mul(mid, mid);
twin_fixed_t sqr = twin_fixed_mul(mid, mid);
if (sqr == a)
return mid;
if (sqr < a)
Expand All @@ -36,13 +33,12 @@ twin_fixed_t twin_fixed_sqrt(twin_fixed_t a)

twin_sfixed_t _twin_sfixed_sqrt(twin_sfixed_t as)
{
twin_dfixed_t max = as, min = 0, mid;
twin_dfixed_t max = as, min = 0;
twin_dfixed_t a = twin_sfixed_to_dfixed(as);
twin_dfixed_t sqr;

while (max > min) {
mid = (max + min) >> 1;
sqr = mid * mid;
twin_dfixed_t mid = (max + min) >> 1;
twin_dfixed_t sqr = mid * mid;
if (sqr == a)
return (twin_sfixed_t) mid;
if (sqr < a)
Expand Down
2 changes: 1 addition & 1 deletion src/font.c
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,6 @@ void twin_path_ucs4(twin_path_t *path, twin_ucs4_t ucs4)
twin_path_t *pen = NULL;
twin_fixed_t width;
twin_text_info_t info;
signed char op;

_twin_text_compute_info(path, font, &info);
if (info.snap)
Expand All @@ -346,6 +345,7 @@ void twin_path_ucs4(twin_path_t *path, twin_ucs4_t ucs4)

x1 = y1 = 0;
for (;;) {
signed char op;
switch ((op = *g++)) {
case 'm':
x1 = FX(*g++, &info);
Expand Down
33 changes: 15 additions & 18 deletions src/hull.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* All rights reserved.
*/

#include <stdbool.h>
#include <stdlib.h>

#include "twin_private.h"
Expand All @@ -16,7 +17,7 @@ typedef struct twin_slope {
typedef struct _twin_hull {
twin_spoint_t point;
twin_slope_t slope;
int discard;
bool discard;
} twin_hull_t;

static void _twin_slope_init(twin_slope_t *slope,
Expand All @@ -29,23 +30,21 @@ static void _twin_slope_init(twin_slope_t *slope,

static twin_hull_t *_twin_hull_create(twin_path_t *path, int *nhull)
{
int i, j;
int n = path->npoints;
twin_spoint_t *p = path->points;
twin_hull_t *hull;
int e;

e = 0;
for (i = 1; i < n; i++)
int e = 0;
for (int i = 1; i < n; i++)
if (p[i].y < p[e].y || (p[i].y == p[e].y && p[i].x < p[e].x))
e = i;

hull = malloc(n * sizeof(twin_hull_t));
if (hull == NULL)
if (!hull)
return NULL;
*nhull = n;

for (i = 0; i < n; i++) {
for (int i = 0, j; i < n; i++) {
/* place extremum first in array */
if (i == 0)
j = e;
Expand All @@ -59,9 +58,9 @@ static twin_hull_t *_twin_hull_create(twin_path_t *path, int *nhull)

/* Discard all points coincident with the extremal point */
if (i != 0 && hull[i].slope.dx == 0 && hull[i].slope.dy == 0)
hull[i].discard = 1;
hull[i].discard = true;
else
hull[i].discard = 0;
hull[i].discard = false;
}

return hull;
Expand Down Expand Up @@ -102,9 +101,8 @@ static int _twin_hull_vertex_compare(const void *av, const void *bv)
{
twin_hull_t *a = (twin_hull_t *) av;
twin_hull_t *b = (twin_hull_t *) bv;
int ret;

ret = _twin_slope_compare(&a->slope, &b->slope);
int ret = _twin_slope_compare(&a->slope, &b->slope);

/* In the case of two vertices with identical slope from the
extremal point discard the nearer point. */
Expand All @@ -116,10 +114,10 @@ static int _twin_hull_vertex_compare(const void *av, const void *bv)
b_dist = ((twin_dfixed_t) b->slope.dx * b->slope.dx +
(twin_dfixed_t) b->slope.dy * b->slope.dy);
if (a_dist < b_dist) {
a->discard = 1;
a->discard = true;
ret = -1;
} else {
b->discard = 1;
b->discard = true;
ret = 1;
}
}
Expand Down Expand Up @@ -154,12 +152,11 @@ static int _twin_hull_next_valid(twin_hull_t *hull, int num_hull, int index)

static void _twin_hull_eliminate_concave(twin_hull_t *hull, int num_hull)
{
int i, j, k;
twin_slope_t slope_ij, slope_jk;

i = 0;
j = _twin_hull_next_valid(hull, num_hull, i);
k = _twin_hull_next_valid(hull, num_hull, j);
int i = 0;
int j = _twin_hull_next_valid(hull, num_hull, i);
int k = _twin_hull_next_valid(hull, num_hull, j);

do {
_twin_slope_init(&slope_ij, &hull[i].point, &hull[j].point);
Expand All @@ -169,7 +166,7 @@ static void _twin_hull_eliminate_concave(twin_hull_t *hull, int num_hull)
if (_twin_slope_compare(&slope_ij, &slope_jk) >= 0) {
if (i == k)
break;
hull[j].discard = 1;
hull[j].discard = true;
j = i;
i = _twin_hull_prev_valid(hull, num_hull, j);
} else {
Expand Down
26 changes: 9 additions & 17 deletions src/path.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,42 +193,34 @@ void twin_path_arc(twin_path_t *path,
twin_angle_t extent)
{
twin_matrix_t save = twin_path_current_matrix(path);
twin_fixed_t max_radius;
int32_t sides;
int32_t n;
twin_angle_t a;
twin_angle_t first, last, step, inc;
twin_angle_t epsilon;

twin_path_translate(path, x, y);
twin_path_scale(path, x_radius, y_radius);

max_radius = _twin_matrix_max_radius(&path->state.matrix);
sides = max_radius / twin_sfixed_to_fixed(TWIN_SFIXED_TOLERANCE);
twin_fixed_t max_radius = _twin_matrix_max_radius(&path->state.matrix);
int32_t sides = max_radius / twin_sfixed_to_fixed(TWIN_SFIXED_TOLERANCE);
if (sides > 1024)
sides = 1024;

n = 2;
int32_t n = 2;
while ((1 << n) < sides)
n++;

sides = (1 << n);

step = TWIN_ANGLE_360 >> n;
inc = step;
epsilon = 1;
twin_angle_t step = TWIN_ANGLE_360 >> n;
twin_angle_t inc = step;
twin_angle_t epsilon = 1;
if (extent < 0) {
inc = -inc;
epsilon = -1;
}

first = (start + inc - epsilon) & ~(step - 1);
last = (start + extent - inc + epsilon) & ~(step - 1);
twin_angle_t first = (start + inc - epsilon) & ~(step - 1);
twin_angle_t last = (start + extent - inc + epsilon) & ~(step - 1);

if (first != start)
twin_path_draw(path, twin_cos(start), twin_sin(start));

for (a = first; a != last; a += inc)
for (twin_angle_t a = first; a != last; a += inc)
twin_path_draw(path, twin_cos(a), twin_sin(a));

if (last != start + extent)
Expand Down
41 changes: 15 additions & 26 deletions src/poly.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,11 @@ static int _twin_edge_build(twin_spoint_t *vertices,
twin_sfixed_t dy,
twin_sfixed_t top_y)
{
int v, nv;
int tv, bv;
int e;
twin_sfixed_t y;

e = 0;
for (v = 0; v < nvertices; v++) {
nv = v + 1;
int e = 0;
for (int v = 0; v < nvertices; v++) {
int nv = v + 1;
if (nv == nvertices)
nv = 0;

Expand All @@ -91,7 +88,7 @@ static int _twin_edge_build(twin_spoint_t *vertices,
}

/* snap top to first grid point in pixmap */
y = _twin_sfixed_grid_ceil(vertices[tv].y + dy);
twin_sfixed_t y = _twin_sfixed_grid_ceil(vertices[tv].y + dy);
if (y < TWIN_POLY_START + top_y)
y = TWIN_POLY_START + top_y;

Expand Down Expand Up @@ -233,14 +230,11 @@ static void _twin_edge_fill(twin_pixmap_t *pixmap,
int nedges)
{
twin_edge_t *active, *a, *n, **prev;
int e;
twin_sfixed_t y;
twin_sfixed_t x0 = 0;
int w;

qsort(edges, nedges, sizeof(twin_edge_t), _edge_compare_y);
e = 0;
y = edges[0].top;
int e = 0;
twin_sfixed_t y = edges[0].top;
active = 0;
for (;;) {
/* add in new edges */
Expand All @@ -253,14 +247,13 @@ static void _twin_edge_fill(twin_pixmap_t *pixmap,
}

/* walk this y value marking coverage */
w = 0;
int w = 0;
for (a = active; a; a = a->next) {
if (w == 0)
x0 = a->x;
w += a->winding;
if (w == 0) {
if (w == 0)
_span_fill(pixmap, y, x0, a->x);
}
}

/* step down, clipping to pixmap */
Expand Down Expand Up @@ -303,19 +296,14 @@ void twin_fill_path(twin_pixmap_t *pixmap,
twin_coord_t dx,
twin_coord_t dy)
{
twin_edge_t *edges;
int nedges, n;
int nalloc;
int s;
int p;
twin_sfixed_t sdx = twin_int_to_sfixed(dx + pixmap->origin_x);
twin_sfixed_t sdy = twin_int_to_sfixed(dy + pixmap->origin_y);

nalloc = path->npoints + path->nsublen + 1;
edges = malloc(sizeof(twin_edge_t) * nalloc);
p = 0;
nedges = 0;
for (s = 0; s <= path->nsublen; s++) {
int nalloc = path->npoints + path->nsublen + 1;
twin_edge_t *edges = malloc(sizeof(twin_edge_t) * nalloc);
int p = 0;
int nedges = 0;
for (int s = 0; s <= path->nsublen; s++) {
int sublen;
int npoints;

Expand All @@ -325,7 +313,8 @@ void twin_fill_path(twin_pixmap_t *pixmap,
sublen = path->sublen[s];
npoints = sublen - p;
if (npoints > 1) {
n = _twin_edge_build(path->points + p, npoints, edges + nedges, sdx,
int n =
_twin_edge_build(path->points + p, npoints, edges + nedges, sdx,
sdy, twin_int_to_sfixed(pixmap->clip.top));
p = sublen;
nedges += n;
Expand Down
3 changes: 1 addition & 2 deletions src/timeout.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ void _twin_run_timeout(void)
{
twin_time_t now = twin_now();
twin_timeout_t *timeout;
twin_timeout_t *first;
twin_time_t delay;

first = (twin_timeout_t *) _twin_queue_set_order(&head);
twin_timeout_t *first = (twin_timeout_t *) _twin_queue_set_order(&head);
for (timeout = first; timeout && twin_time_compare(now, >=, timeout->time);
timeout = (twin_timeout_t *) timeout->queue.order) {
delay = (*timeout->proc)(now, timeout->closure);
Expand Down

0 comments on commit 545945c

Please sign in to comment.