Skip to content

Commit 63bf518

Browse files
committed
libchafa: Fix bug caused by floating-point imprecision
This would manifest as a row occasionally being skipped on i386 only with certain batch size/unit combinations. Fixes #292 (GitHub).
1 parent a8e341b commit 63bf518

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

chafa/internal/chafa-batch.c

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ chafa_process_batches (gpointer ctx, GFunc batch_func, GFunc post_func, gint n_r
8282
gint n_threads;
8383
gint n_units;
8484
gfloat units_per_batch;
85-
gfloat ofs [2] = { .0f, .0f };
85+
gfloat next_unit_ofs_f = .0f;
86+
gint unit_ofs [2] = { 0, 0 };
8687
gint i;
8788

8889
g_assert (n_batches >= 1);
@@ -96,6 +97,7 @@ chafa_process_batches (gpointer ctx, GFunc batch_func, GFunc post_func, gint n_r
9697

9798
n_units = (n_rows + batch_unit - 1) / batch_unit;
9899
units_per_batch = (gfloat) n_units / (gfloat) n_batches;
100+
units_per_batch = MAX (units_per_batch, 1.0f);
99101

100102
batches = g_new0 (ChafaBatchInfo, n_batches);
101103

@@ -116,23 +118,18 @@ chafa_process_batches (gpointer ctx, GFunc batch_func, GFunc post_func, gint n_r
116118
ChafaBatchInfo *batch;
117119
gint row_ofs [2];
118120

119-
row_ofs [0] = ofs [0];
120-
121121
do
122122
{
123-
ofs [1] += units_per_batch;
124-
row_ofs [1] = ofs [1];
123+
next_unit_ofs_f += units_per_batch;
124+
unit_ofs [1] = next_unit_ofs_f;
125125
}
126-
while (row_ofs [0] == row_ofs [1]);
126+
while (unit_ofs [0] == unit_ofs [1]);
127127

128-
row_ofs [0] *= batch_unit;
129-
row_ofs [1] *= batch_unit;
128+
row_ofs [0] = unit_ofs [0] * batch_unit;
129+
row_ofs [1] = unit_ofs [1] * batch_unit;
130130

131-
if (row_ofs [1] > n_rows || i == n_batches - 1)
132-
{
133-
ofs [1] = n_rows + 0.5;
131+
if (i == n_batches - 1 || row_ofs [1] > n_rows)
134132
row_ofs [1] = n_rows;
135-
}
136133

137134
if (row_ofs [0] >= row_ofs [1])
138135
{
@@ -159,7 +156,7 @@ chafa_process_batches (gpointer ctx, GFunc batch_func, GFunc post_func, gint n_r
159156
batch_func (batch, ctx);
160157
}
161158

162-
ofs [0] = ofs [1];
159+
unit_ofs [0] = unit_ofs [1];
163160
}
164161

165162
if (n_threads >= 2)

0 commit comments

Comments
 (0)