[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Guile-commits] 01/01: Fix a corner case with empty arrays in (array-for
From: |
Daniel Llorens |
Subject: |
[Guile-commits] 01/01: Fix a corner case with empty arrays in (array-for-each-cell) |
Date: |
Thu, 21 Apr 2016 16:06:41 +0000 |
lloda pushed a commit to branch lloda-array-support
in repository guile.
commit ec974895c66e3461fbac4e61d709addbc218e58b
Author: Daniel Llorens <address@hidden>
Date: Thu Apr 21 17:38:49 2016 +0200
Fix a corner case with empty arrays in (array-for-each-cell)
* libguile/array-map.c (scm_array_for_each_cell): Bail out early if any
of the sizes is zero. Pack ais at the end of the fake stack.
* test-suite/tests/array-map.test: Add regression test.
---
libguile/array-map.c | 93 ++++++++++++++++++++++++---------------
test-suite/tests/array-map.test | 14 +++++-
2 files changed, 70 insertions(+), 37 deletions(-)
diff --git a/libguile/array-map.c b/libguile/array-map.c
index 0bbc095..2911239 100644
--- a/libguile/array-map.c
+++ b/libguile/array-map.c
@@ -683,14 +683,15 @@ SCM_DEFINE (scm_array_for_each_cell,
"array-for-each-cell", 2, 0, 1,
stack_size += N*sizeof (SCM);
stack_size += N*sizeof (scm_t_array_dim *);
stack_size += N*sizeof (int);
- stack_size += frank*sizeof (ssize_t);
+ stack_size += frank*sizeof (ssize_t);
stack_size += N*sizeof (SCM);
stack_size += N*sizeof (SCM *);
stack_size += frank*sizeof (ssize_t);
- stack_size += frank*sizeof (int);
+ stack_size += frank*sizeof (int);
stack_size += N*sizeof (size_t);
+ stack_size += N*sizeof (scm_t_array_dim *);
char * stack = scm_gc_malloc_pointerless (stack_size, "stack");
#define AFIC_ALLOC_ADVANCE(stack, count, type, name) \
@@ -702,14 +703,15 @@ SCM_DEFINE (scm_array_for_each_cell,
"array-for-each-cell", 2, 0, 1,
AFIC_ALLOC_ADVANCE (stack, N, SCM, args_);
AFIC_ALLOC_ADVANCE (stack, N, scm_t_array_dim *, as);
AFIC_ALLOC_ADVANCE (stack, N, int, rank);
- AFIC_ALLOC_ADVANCE (stack, frank, ssize_t, s);
+ AFIC_ALLOC_ADVANCE (stack, frank, ssize_t, s);
AFIC_ALLOC_ADVANCE (stack, N, SCM, ai);
AFIC_ALLOC_ADVANCE (stack, N, SCM *, dargs);
AFIC_ALLOC_ADVANCE (stack, frank, ssize_t, i);
- AFIC_ALLOC_ADVANCE (stack, frank, int, order);
- AFIC_ALLOC_ADVANCE(stack, N, size_t, base);
+ AFIC_ALLOC_ADVANCE (stack, frank, int, order);
+ AFIC_ALLOC_ADVANCE (stack, N, size_t, base);
+ AFIC_ALLOC_ADVANCE (stack, N, scm_t_array_dim *, ais);
assert((stack0+stack_size==stack) && "internal error");
#undef AFIC_ALLOC_ADVANCE
@@ -727,23 +729,36 @@ SCM_DEFINE (scm_array_for_each_cell,
"array-for-each-cell", 2, 0, 1,
msg = "bad frame rank";
} else
{
- for (int n=0; n!=N; ++n) {
- if (rank[n]<frank) {
- msg = "frame too large for arguments";
- goto check_msg;
- }
- for (int k=0; k!=frank; ++k) {
- if (as[n][k].lbnd!=0) {
- msg = "non-zero base index is not supported";
- goto check_msg;
- }
- if (as[0][k].ubnd!=as[n][k].ubnd) {
- msg = "mismatched frames";
- goto check_msg;
- }
- s[k] = as[n][k].ubnd + 1;
+ for (int n=0; n!=N; ++n)
+ {
+ if (rank[n]<frank)
+ {
+ msg = "frame too large for arguments";
+ goto check_msg;
+ }
+ for (int k=0; k!=frank; ++k)
+ {
+ if (as[n][k].lbnd!=0)
+ {
+ msg = "non-zero base index is not supported";
+ goto check_msg;
+ }
+ if (as[0][k].ubnd!=as[n][k].ubnd)
+ {
+ msg = "mismatched frames";
+ goto check_msg;
+ }
+ s[k] = as[n][k].ubnd + 1;
+
+ // this check is needed if the array cannot be entirely
+ // unrolled, because the step loop will be run before
+ // checking the dimensions of the frame.
+ if (s[k]==0)
+ {
+ goto end;
+ }
+ }
}
- }
}
check_msg: ;
if (msg!=NULL)
@@ -754,7 +769,6 @@ SCM_DEFINE (scm_array_for_each_cell, "array-for-each-cell",
2, 0, 1,
scm_misc_error("array-for-each-cell", msg, scm_cons_star(frame_rank,
args));
}
// prepare moving cells.
- scm_t_array_dim * ais[N];
for (int n=0; n!=N; ++n)
{
ai[n] = scm_i_make_array(rank[n]-frank);
@@ -762,19 +776,21 @@ SCM_DEFINE (scm_array_for_each_cell,
"array-for-each-cell", 2, 0, 1,
// FIXME scm_array_handle_base (ah+n) should be in Guile
SCM_I_ARRAY_SET_BASE (ai[n], ah[n].base);
ais[n] = SCM_I_ARRAY_DIMS(ai[n]);
- for (int k=frank; k!=rank[n]; ++k) {
- ais[n][k-frank] = as[n][k];
- }
+ for (int k=frank; k!=rank[n]; ++k)
+ {
+ ais[n][k-frank] = as[n][k];
+ }
}
// prepare rest list for callee.
SCM dargs_ = SCM_EOL;
{
SCM *p = &dargs_;
- for (int n=0; n<N; ++n) {
- *p = scm_cons (SCM_UNSPECIFIED, SCM_EOL);
- dargs[n] = SCM_CARLOC (*p);
- p = SCM_CDRLOC (*p);
- }
+ for (int n=0; n<N; ++n)
+ {
+ *p = scm_cons (SCM_UNSPECIFIED, SCM_EOL);
+ dargs[n] = SCM_CARLOC (*p);
+ p = SCM_CDRLOC (*p);
+ }
}
// special case for rank 0.
if (frank==0)
@@ -801,11 +817,13 @@ SCM_DEFINE (scm_array_for_each_cell,
"array-for-each-cell", 2, 0, 1,
int ocd = 1;
for (; ocd<frank; step *= s[order[ocd]], ++ocd)
{
- for (int n=0; n!=N; ++n) {
- if (step*as[n][order[0]].inc!=as[n][order[ocd]].inc) {
- goto ocd_reached;
+ for (int n=0; n!=N; ++n)
+ {
+ if (step*as[n][order[0]].inc!=as[n][order[ocd]].inc)
+ {
+ goto ocd_reached;
+ }
}
- }
}
ocd_reached: ;
// rank loop.
@@ -838,7 +856,8 @@ SCM_DEFINE (scm_array_for_each_cell, "array-for-each-cell",
2, 0, 1,
if (k==frank)
{
goto end;
- } else if (i[order[k]]<s[order[k]]-1)
+ }
+ else if (i[order[k]]<s[order[k]]-1)
{
++i[order[k]];
for (int n=0; n<N; ++n)
@@ -846,7 +865,9 @@ SCM_DEFINE (scm_array_for_each_cell, "array-for-each-cell",
2, 0, 1,
base[n] += as[n][order[k]].inc;
}
break;
- } else {
+ }
+ else
+ {
i[order[k]] = 0;
for (int n=0; n<N; ++n)
{
diff --git a/test-suite/tests/array-map.test b/test-suite/tests/array-map.test
index f5487ba..cefe7b7 100644
--- a/test-suite/tests/array-map.test
+++ b/test-suite/tests/array-map.test
@@ -525,4 +525,16 @@
(let* ((x (list->typed-array 'f64 2 '((9 1) (7 8))))
(y (f64vector 99 99)))
(array-for-each-cell 1 (lambda (y x) (array-set! y (- (array-ref x 0)
(array-ref x 1)))) y x)
- y)))
+ y))
+
+ (pass-if-equal "regression: zero-sized frame loop without unrolling"
+ 99
+ (let* ((x 99)
+ (o (make-array 0. 0 3 2)))
+ (array-for-each-cell 2
+ (lambda (o a0 a1)
+ (set! x 0))
+ o
+ (make-shared-array (make-array 1. 0 1) (const '(0 0)) 0 3)
+ (make-array 2. 0 3))
+ x)))