Skip to content

Commit dd9b028

Browse files
committed
Fix issue #763 MINLOC intrinsic incorrect return result
MINLOC returns last minimum rather than first as if BACK was specified as .TRUE.
1 parent fd240b9 commit dd9b028

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

tools/flang1/flang1exe/semutil2.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8521,10 +8521,10 @@ eval_merge(ACL *arg, DTYPE dtype)
85218521
return result;
85228522
}
85238523

8524-
/* Compare two constant ACLs. Return x > y or x < y depending on want_greater.
8524+
/* Compare two constant ACLs. Return x > y or x < y depending on want_max.
85258525
*/
85268526
static bool
8527-
cmp_acl(DTYPE dtype, ACL *x, ACL *y, bool want_greater, bool back)
8527+
cmp_acl(DTYPE dtype, ACL *x, ACL *y, bool want_max, bool back)
85288528
{
85298529
int cmp;
85308530
switch (DTY(dtype)) {
@@ -8535,10 +8535,12 @@ cmp_acl(DTYPE dtype, ACL *x, ACL *y, bool want_greater, bool back)
85358535
case TY_BINT:
85368536
case TY_SINT:
85378537
case TY_INT:
8538-
if (back && want_greater) {
8539-
cmp = x->conval >= y->conval ? 1 : -1;
8538+
if (x->conval == y->conval) {
8539+
cmp = 0;
8540+
} else if (x->conval > y->conval) {
8541+
cmp = 1;
85408542
} else {
8541-
cmp = x->conval > y->conval ? 1 : -1;
8543+
cmp = -1;
85428544
}
85438545
break;
85448546
case TY_REAL:
@@ -8553,9 +8555,9 @@ cmp_acl(DTYPE dtype, ACL *x, ACL *y, bool want_greater, bool back)
85538555
return false;
85548556
}
85558557
if (back) {
8556-
return want_greater ? cmp >= 0 : cmp <= 0;
8558+
return want_max ? cmp >= 0 : cmp <= 0;
85578559
} else {
8558-
return want_greater ? cmp > 0 : cmp < 0;
8560+
return want_max ? cmp > 0 : cmp < 0;
85598561
}
85608562
}
85618563

@@ -8730,6 +8732,7 @@ do_eval_minval_or_maxval(INDEX *index, DTYPE elem_dt, DTYPE loc_dt, ACL *elems,
87308732
if (!want_val) {
87318733
for (i = 0; i < locs_size; i++) {
87328734
ACL *elem = GET_ACL(15);
8735+
BZERO(elem, ACL, 1);
87338736
elem->id = AC_CONST;
87348737
elem->dtype = loc_dt;
87358738
elem->is_const = true;

tools/flang2/flang2exe/dinit.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2672,9 +2672,9 @@ eval_max(CONST *arg, DTYPE dtype)
26722672
return rslt;
26732673
}
26742674

2675-
/* Compare two constant CONSTs. Return x > y or x < y depending on want_greater. */
2675+
/* Compare two constant CONSTs. Return x > y or x < y depending on want_max. */
26762676
static bool
2677-
cmp_acl(DTYPE dtype, CONST *x, CONST *y, bool want_greater, bool back)
2677+
cmp_acl(DTYPE dtype, CONST *x, CONST *y, bool want_max, bool back)
26782678
{
26792679
int cmp;
26802680
switch (DTY(dtype)) {
@@ -2685,11 +2685,13 @@ cmp_acl(DTYPE dtype, CONST *x, CONST *y, bool want_greater, bool back)
26852685
case TY_BINT:
26862686
case TY_SINT:
26872687
case TY_INT:
2688-
if (back && want_greater) {
2689-
cmp = x->u1.conval >= y->u1.conval ? 1 : -1;
2688+
if (x->u1.conval == y->u1.conval) {
2689+
cmp = 0;
2690+
} else if (x->u1.conval > y->u1.conval) {
2691+
cmp = 1;
26902692
} else {
2691-
cmp = x->u1.conval > y->u1.conval ? 1 : -1;
2692-
}
2693+
cmp = -1;
2694+
}
26932695
break;
26942696
case TY_REAL:
26952697
cmp = xfcmp(x->u1.conval, y->u1.conval);
@@ -2703,9 +2705,9 @@ cmp_acl(DTYPE dtype, CONST *x, CONST *y, bool want_greater, bool back)
27032705
return false;
27042706
}
27052707
if (back) {
2706-
return want_greater ? cmp >= 0 : cmp <= 0;
2708+
return want_max ? cmp >= 0 : cmp <= 0;
27072709
} else {
2708-
return want_greater ? cmp > 0 : cmp < 0;
2710+
return want_max ? cmp > 0 : cmp < 0;
27092711
}
27102712
}
27112713

@@ -3061,6 +3063,7 @@ do_eval_minval_or_maxval(INDEX *index, DTYPE elem_dt, DTYPE loc_dt,
30613063
if (!want_val) {
30623064
for (i = 0; i < locs_size; i++) {
30633065
CONST *elem = (CONST *)getitem(4, sizeof(CONST));
3066+
BZERO(elem, CONST, 1);
30643067
elem->id = AC_CONST;
30653068
elem->dtype = loc_dt;
30663069
elem->u1.conval = locs[i];

0 commit comments

Comments
 (0)