Skip to content

Commit 12189f3

Browse files
committed
r/src/Rmoocore.c (matrix_transpose_double): Faster transpose.
1 parent 377f345 commit 12189f3

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

r/src/Rmoocore.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -261,19 +261,23 @@ static inline void
261261
matrix_transpose_double(double * restrict dst, const double * restrict src,
262262
const size_t nrows, const size_t ncols)
263263
{
264-
ASSUME(nrows < SIZE_MAX/2 && ncols < SIZE_MAX/2);
265-
if (nrows <= 0 || ncols <= 0)
264+
// Make sure that nrows * (ncols + 1) < SIZE_MAX
265+
ASSUME(nrows < SIZE_MAX / (ncols+1) && (ncols+1) < SIZE_MAX / nrows);
266+
if (nrows == 0 || ncols == 0)
266267
return;
267268

268269
const size_t len_1 = (nrows * ncols) - 1;
269270
size_t i = 0, j = 0;
270271
for (; j <= len_1; i++, j += nrows)
271272
dst[j] = src[i];
272273

273-
for (; i <= len_1; i++, j += nrows) {
274-
if (j > len_1) j -= len_1;
275-
dst[j] = src[i];
276-
}
274+
// FIXME: https://gcc.gnu.org/PR125293
275+
j -= len_1;
276+
while (i <= len_1) {
277+
for (; j <= len_1; i++, j += nrows)
278+
dst[j] = src[i];
279+
j -= len_1;
280+
}
277281
}
278282

279283
SEXP

0 commit comments

Comments
 (0)