Skip to content

Commit

Permalink
Fix arithmetic conversion to work for complex. References #8, Referen…
Browse files Browse the repository at this point in the history
…ces #9
  • Loading branch information
michael-schwarz committed Jan 16, 2020
1 parent 65bf016 commit 1d71297
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
31 changes: 19 additions & 12 deletions src/frontc/cabs2cil.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1254,19 +1254,26 @@ let defaultArgumentPromotion (t : typ) : typ = (* c.f. ISO 6.5.2.2:6 *)
let arithmeticConversion (* c.f. ISO 6.3.1.8 *)
(t1: typ)
(t2: typ) : typ =
let resultingFType fkind1 t1 fkind2 t2 =
(* t1 and t2 are the original types before unrollType, so TNamed is preserved if possible *)
let isComplex f = f = FComplexFloat || f = FComplexDouble || f = FComplexLongDouble in
match fkind1, fkind2 with
| FComplexLongDouble, _ -> t1
| _, FComplexLongDouble -> t2
| FLongDouble, other -> if isComplex other then TFloat(FComplexLongDouble, []) else t1
| other, FLongDouble -> if isComplex other then TFloat(FComplexLongDouble, []) else t2
| FComplexDouble, other -> t1
| other, FComplexDouble -> t2
| FDouble, other -> if isComplex other then TFloat(FComplexDouble, []) else t1
| other, FDouble -> if isComplex other then TFloat(FComplexDouble, []) else t2
| FComplexFloat, other -> t1
| other, FComplexFloat -> t2
| FFloat, FFloat -> t1
in
match unrollType t1, unrollType t2 with
TFloat(FLongDouble, _), _ -> t1
| TFloat(FComplexLongDouble, _), _ -> t1
| _, TFloat(FLongDouble, _) -> t2
| _, TFloat(FComplexLongDouble, _) -> t2
| TFloat(FDouble, _), _ -> t1
| TFloat(FComplexDouble, _), _ -> t1
| _, TFloat (FDouble, _) -> t2
| _, TFloat (FComplexDouble, _) -> t2
| TFloat(FFloat, _), _ -> t1
| TFloat(FComplexFloat, _), _ -> t1
| _, TFloat (FFloat, _) -> t2
| _, TFloat (FComplexFloat, _) -> t2
| TFloat(fkind1, _), TFloat(fkind2, _) -> resultingFType fkind1 t1 fkind2 t2
| TFloat(_, _), _ -> t1
| _, TFloat(_, _) -> t2
| _, _ -> begin
let t1' = integralPromotion t1 in
let t2' = integralPromotion t2 in
Expand Down
6 changes: 5 additions & 1 deletion test/small1/c99-complex.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ int main(void)
{
double complex x1 = 1.0iF + 0.5;

if(sizeof(double complex) != sizeof(1.0iF + 0.5)) {
E(1);
}

double d = creal(x1);

if(d != 0.5)
E(1);
E(2);

double complex z1 = 1.0iF + 1;
printf("I * I = %.1f%+.1fi\n", creal(z1), cimag(z1));
Expand Down

0 comments on commit 1d71297

Please sign in to comment.