Skip to content

Commit 0386f2b

Browse files
stffrdhrnjeremybennett
authored andcommitted
or32: Fix overflow detection on -O2 compilation
The check for overflow assumes that the compiler will allow us to detect when a value overflows to a negative number. When running GCC with -O2 and above this overflow cannot be detected. The C standard explains that overflow behavior is undefined meaning this expected overflow causing a negative number cannot be guaranteed and it is not. Multiple overflow flag tests were failing. Fix the detection buy just using the compiler builtins for detecting overflow conditions.
1 parent b735ef8 commit 0386f2b

File tree

1 file changed

+7
-31
lines changed

1 file changed

+7
-31
lines changed

cpu/or32/insnset.c

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,15 @@
3030
INSTRUCTION (l_add) {
3131
orreg_t temp1, temp2, temp3;
3232
int8_t temp4;
33-
33+
3434
temp2 = (orreg_t)PARAM2;
3535
temp3 = (orreg_t)PARAM1;
3636
temp1 = temp2 + temp3;
3737
SET_PARAM0 (temp1);
3838

3939
/* Set overflow if two negative values gave a positive sum, or if two
4040
positive values gave a negative sum. Otherwise clear it */
41-
if ((((long int) temp2 < 0) &&
42-
((long int) temp3 < 0) &&
43-
((long int) temp1 >= 0)) ||
44-
(((long int) temp2 >= 0) &&
45-
((long int) temp3 >= 0) &&
46-
((long int) temp1 < 0)))
41+
if (__builtin_add_overflow_p (temp2, temp3, temp1))
4742
{
4843
cpu_state.sprs[SPR_SR] |= SPR_SR_OV;
4944
}
@@ -119,12 +114,8 @@ INSTRUCTION (l_addc) {
119114
positive values gave a negative sum. Otherwise clear it. There are no
120115
corner cases with the extra bit carried in (unlike the carry flag - see
121116
below). */
122-
if ((((long int) temp2 < 0) &&
123-
((long int) temp3 < 0) &&
124-
((long int) temp1 >= 0)) ||
125-
(((long int) temp2 >= 0) &&
126-
((long int) temp3 >= 0) &&
127-
((long int) temp1 < 0)))
117+
if (((temp2 < 0) && (temp3 < 0) && (temp1 >= 0)) ||
118+
((temp2 >= 0) && (temp3 >= 0) && (temp1 < 0)))
128119
{
129120
cpu_state.sprs[SPR_SR] |= SPR_SR_OV;
130121
}
@@ -336,12 +327,7 @@ INSTRUCTION (l_sub) {
336327
/* Set overflow if a negative value minus a positive value gave a positive
337328
sum, or if a positive value minus a negative value gave a negative
338329
sum. Otherwise clear it */
339-
if ((((long int) temp2 < 0) &&
340-
((long int) temp3 >= 0) &&
341-
((long int) temp1 >= 0)) ||
342-
(((long int) temp2 >= 0) &&
343-
((long int) temp3 < 0) &&
344-
((long int) temp1 < 0)))
330+
if (__builtin_sub_overflow_p (temp2, temp3, temp1))
345331
{
346332
cpu_state.sprs[SPR_SR] |= SPR_SR_OV;
347333
}
@@ -974,12 +960,7 @@ INSTRUCTION (l_mac) {
974960

975961
/* Set overflow if two negative values gave a positive sum, or if two
976962
positive values gave a negative sum. Otherwise clear it */
977-
if (((acc < 0) &&
978-
(prod < 0) &&
979-
(tmp >= 0)) ||
980-
((acc >= 0) &&
981-
(prod >= 0) &&
982-
(tmp < 0)))
963+
if (__builtin_add_overflow_p (acc, prod, tmp))
983964
{
984965
cpu_state.sprs[SPR_SR] |= SPR_SR_OV;
985966
}
@@ -1042,12 +1023,7 @@ INSTRUCTION (l_msb) {
10421023
/* Set overflow if a negative value minus a positive value gave a positive
10431024
sum, or if a positive value minus a negative value gave a negative
10441025
sum. Otherwise clear it */
1045-
if (((acc < 0) &&
1046-
(prod >= 0) &&
1047-
(tmp >= 0)) ||
1048-
((acc >= 0) &&
1049-
(prod < 0) &&
1050-
(tmp < 0)))
1026+
if (__builtin_sub_overflow_p (acc, prod, tmp))
10511027
{
10521028
cpu_state.sprs[SPR_SR] |= SPR_SR_OV;
10531029
}

0 commit comments

Comments
 (0)