Save the original value when doing postfix ++/-- on fp types.

The old code would add 1 and then subtract 1, which does not necessarily give the original value (e.g. if it is much less than 1).
This commit is contained in:
Stephen Heumann 2021-03-09 18:54:31 -06:00
parent db7a0a995d
commit 031af54112
2 changed files with 22 additions and 16 deletions

View File

@ -2955,9 +2955,10 @@ var
{do real or bool inc or dec} {do real or bool inc or dec}
LoadScalar(tree^.id); {load the value} LoadScalar(tree^.id); {load the value}
if pc_l in [pc_lli,pc_lld] then if pc_l in [pc_lli,pc_lld] then
if iType^.cType = ctBool then begin if iType^.cType in [ctBool,ctFloat,ctDouble,ctLongDouble,
t1 := GetTemp(cgWordSize); ctComp] then begin
Gen2t(pc_cop, t1, 0, cgWord); t1 := GetTemp(ord(iType^.size));
Gen2t(pc_cop, t1, 0, iType^.baseType);
end; {if} end; {if}
tp := baseType; tp := baseType;
expressionType := iType; expressionType := iType;
@ -2971,11 +2972,12 @@ var
end; {case} end; {case}
{correct the value for postfix ops} {correct the value for postfix ops}
if pc_l in [pc_lli,pc_lld] then if pc_l in [pc_lli,pc_lld] then
if iType^.cType = ctBool then begin if iType^.cType in [ctBool,ctFloat,ctDouble,ctLongDouble,
Gen0t(pc_pop, cgWord); ctComp] then begin
Gen2t(pc_lod, t1, 0, cgWord); Gen0t(pc_pop, iType^.baseType);
Gen0t(pc_bno, cgWord); Gen2t(pc_lod, t1, 0, iType^.baseType);
FreeTemp(t1, cgWordSize); Gen0t(pc_bno, iType^.baseType);
FreeTemp(t1, ord(iType^.size));
end {if} end {if}
else else
IncOrDec(pc_l = pc_lld); IncOrDec(pc_l = pc_lld);
@ -3068,9 +3070,10 @@ var
else else
Gen1t(pc_ind, 0, tp); Gen1t(pc_ind, 0, tp);
if pc_l in [pc_lli,pc_lld] then if pc_l in [pc_lli,pc_lld] then
if expressionType^.cType = ctBool then begin if expressionType^.cType in [ctBool,ctFloat,ctDouble,ctLongDouble,
t1 := GetTemp(cgWordSize); ctComp] then begin
Gen2t(pc_cop, t1, 0, cgWord); t1 := GetTemp(ord(expressionType^.size));
Gen2t(pc_cop, t1, 0, expressionType^.baseType);
end; {if} end; {if}
IncOrDec(pc_l in [pc_lli,pc_lil]); {do the ++ or --} IncOrDec(pc_l in [pc_lli,pc_lil]); {do the ++ or --}
if isBitField then {copy the value} if isBitField then {copy the value}
@ -3079,11 +3082,12 @@ var
Gen0t(pc_cpi, tp); Gen0t(pc_cpi, tp);
Gen0t(pc_bno, tp); Gen0t(pc_bno, tp);
if pc_l in [pc_lli,pc_lld] then {correct the value for postfix ops} if pc_l in [pc_lli,pc_lld] then {correct the value for postfix ops}
if expressionType^.cType = ctBool then begin if expressionType^.cType in [ctBool,ctFloat,ctDouble,ctLongDouble,
Gen0t(pc_pop, cgWord); ctComp] then begin
Gen2t(pc_lod, t1, 0, cgWord); Gen0t(pc_pop, expressionType^.baseType);
Gen0t(pc_bno, cgWord); Gen2t(pc_lod, t1, 0, expressionType^.baseType);
FreeTemp(t1, cgWordSize); Gen0t(pc_bno, expressionType^.baseType);
FreeTemp(t1, ord(expressionType^.size));
end {if} end {if}
else else
IncOrDec(pc_l = pc_lld); IncOrDec(pc_l = pc_lld);

View File

@ -1186,6 +1186,8 @@ int foo(int[42]);
152. sizeof did not give the right values for certain expressions of floating-point and character types, including expressions cast to those types. 152. sizeof did not give the right values for certain expressions of floating-point and character types, including expressions cast to those types.
153. The postfix ++ and -- operators might not return exactly the original value when applied to floating-point variables.
-- Bugs from C 2.1.0 that have been fixed ----------------------------------- -- Bugs from C 2.1.0 that have been fixed -----------------------------------
1. In some situations, fread() reread the first 1K or so of the file. 1. In some situations, fread() reread the first 1K or so of the file.