llvm-project/clang/test/CodeGen/complex-math-mixed.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

147 lines
4.3 KiB
C
Raw Normal View History

[Clang] [Sema] Fix bug in `_Complex float`+`int` arithmetic (#83063) C23 6.3.1.8 ‘Usual arithmetic conversions’ p1 states (emphasis mine): > Otherwise, if the corresponding real type of either operand is `float`, the other operand is converted, *without change of type domain*, to a type whose corresponding real type is `float`. ‘type domain’ here refers to `_Complex` vs real (i.e. non-`_Complex`); there is another clause that states the same for `double`. Consider the following code: ```c++ _Complex float f; int x; f / x; ``` After talking this over with @AaronBallman, we came to the conclusion that `x` should be converted to `float` and *not* `_Complex float` (that is, we should perform a division of `_Complex float / float`, and *not* `_Complex float / _Complex float`; the same also applies to `-+*`). This was already being done correctly for cases where `x` was already a `float`; it’s just mixed `_Complex float`+`int` operations that currently suffer from this problem. This pr removes the extra `FloatingRealToComplex` conversion that we were erroneously inserting and adds some tests to make sure we’re actually doing `_Complex float / float` and not `_Complex float / _Complex float` (and analogously for `double` and `-+*`). The only exception here is `float / _Complex float`, which calls a library function (`__divsc3`) that takes 4 `float`s, so we end up having to convert the `float` to a `_Complex float` after all (and analogously for `double`); I don’t believe there is a way around this. Lastly, we were also missing tests for `_Complex` arithmetic at compile time, so this adds some tests for that as well.
2024-03-13 17:39:23 +01:00
// RUN: %clang_cc1 %s -O0 -emit-llvm -triple x86_64-unknown-unknown -o - | FileCheck %s --check-prefix=X86
// RUN: %clang_cc1 %s -O0 -triple x86_64-unknown-unknown -ast-dump | FileCheck %s --check-prefix=AST
[Clang] [Sema] Fix bug in `_Complex float`+`int` arithmetic (#83063) C23 6.3.1.8 ‘Usual arithmetic conversions’ p1 states (emphasis mine): > Otherwise, if the corresponding real type of either operand is `float`, the other operand is converted, *without change of type domain*, to a type whose corresponding real type is `float`. ‘type domain’ here refers to `_Complex` vs real (i.e. non-`_Complex`); there is another clause that states the same for `double`. Consider the following code: ```c++ _Complex float f; int x; f / x; ``` After talking this over with @AaronBallman, we came to the conclusion that `x` should be converted to `float` and *not* `_Complex float` (that is, we should perform a division of `_Complex float / float`, and *not* `_Complex float / _Complex float`; the same also applies to `-+*`). This was already being done correctly for cases where `x` was already a `float`; it’s just mixed `_Complex float`+`int` operations that currently suffer from this problem. This pr removes the extra `FloatingRealToComplex` conversion that we were erroneously inserting and adds some tests to make sure we’re actually doing `_Complex float / float` and not `_Complex float / _Complex float` (and analogously for `double` and `-+*`). The only exception here is `float / _Complex float`, which calls a library function (`__divsc3`) that takes 4 `float`s, so we end up having to convert the `float` to a `_Complex float` after all (and analogously for `double`); I don’t believe there is a way around this. Lastly, we were also missing tests for `_Complex` arithmetic at compile time, so this adds some tests for that as well.
2024-03-13 17:39:23 +01:00
// Check that for 'F _Complex + int' (F = real floating-point type), we emit an
// implicit cast from 'int' to 'F', but NOT to 'F _Complex' (i.e. that we do
// 'F _Complex + F', NOT 'F _Complex + F _Complex'), and likewise for -/*.
// AST-NOT: FloatingRealToComplex
float _Complex add_float_ci(float _Complex a, int b) {
// X86-LABEL: @add_float_ci
// X86: [[I:%.*]] = sitofp i32 {{%.*}} to float
// X86: fadd float {{.*}}, [[I]]
// X86-NOT: fadd
return a + b;
}
float _Complex add_float_ic(int a, float _Complex b) {
// X86-LABEL: @add_float_ic
// X86: [[I:%.*]] = sitofp i32 {{%.*}} to float
// X86: fadd float [[I]]
// X86-NOT: fadd
return a + b;
}
float _Complex sub_float_ci(float _Complex a, int b) {
// X86-LABEL: @sub_float_ci
// X86: [[I:%.*]] = sitofp i32 {{%.*}} to float
// X86: fsub float {{.*}}, [[I]]
// X86-NOT: fsub
return a - b;
}
float _Complex sub_float_ic(int a, float _Complex b) {
// X86-LABEL: @sub_float_ic
// X86: [[I:%.*]] = sitofp i32 {{%.*}} to float
// X86: fsub float [[I]]
// X86: fneg
// X86-NOT: fsub
return a - b;
}
float _Complex mul_float_ci(float _Complex a, int b) {
// X86-LABEL: @mul_float_ci
// X86: [[I:%.*]] = sitofp i32 {{%.*}} to float
// X86: fmul float {{.*}}, [[I]]
// X86: fmul float {{.*}}, [[I]]
// X86-NOT: fmul
return a * b;
}
float _Complex mul_float_ic(int a, float _Complex b) {
// X86-LABEL: @mul_float_ic
// X86: [[I:%.*]] = sitofp i32 {{%.*}} to float
// X86: fmul float [[I]]
// X86: fmul float [[I]]
// X86-NOT: fmul
return a * b;
}
float _Complex div_float_ci(float _Complex a, int b) {
// X86-LABEL: @div_float_ci
// X86: [[I:%.*]] = sitofp i32 {{%.*}} to float
// X86: fdiv float {{.*}}, [[I]]
// X86: fdiv float {{.*}}, [[I]]
// X86-NOT: @__divsc3
return a / b;
}
// There is no good way of doing this w/o converting the 'int' to a complex
// number, so we expect complex division here.
float _Complex div_float_ic(int a, float _Complex b) {
// X86-LABEL: @div_float_ic
// X86: [[I:%.*]] = sitofp i32 {{%.*}} to float
// X86: call {{.*}} @__divsc3(float {{.*}} [[I]], float noundef 0.{{0+}}e+00, float {{.*}}, float {{.*}})
return a / b;
}
double _Complex add_double_ci(double _Complex a, int b) {
// X86-LABEL: @add_double_ci
// X86: [[I:%.*]] = sitofp i32 {{%.*}} to double
// X86: fadd double {{.*}}, [[I]]
// X86-NOT: fadd
return a + b;
}
double _Complex add_double_ic(int a, double _Complex b) {
// X86-LABEL: @add_double_ic
// X86: [[I:%.*]] = sitofp i32 {{%.*}} to double
// X86: fadd double [[I]]
// X86-NOT: fadd
return a + b;
}
double _Complex sub_double_ci(double _Complex a, int b) {
// X86-LABEL: @sub_double_ci
// X86: [[I:%.*]] = sitofp i32 {{%.*}} to double
// X86: fsub double {{.*}}, [[I]]
// X86-NOT: fsub
return a - b;
}
double _Complex sub_double_ic(int a, double _Complex b) {
// X86-LABEL: @sub_double_ic
// X86: [[I:%.*]] = sitofp i32 {{%.*}} to double
// X86: fsub double [[I]]
// X86: fneg
// X86-NOT: fsub
return a - b;
}
double _Complex mul_double_ci(double _Complex a, int b) {
// X86-LABEL: @mul_double_ci
// X86: [[I:%.*]] = sitofp i32 {{%.*}} to double
// X86: fmul double {{.*}}, [[I]]
// X86: fmul double {{.*}}, [[I]]
// X86-NOT: fmul
return a * b;
}
double _Complex mul_double_ic(int a, double _Complex b) {
// X86-LABEL: @mul_double_ic
// X86: [[I:%.*]] = sitofp i32 {{%.*}} to double
// X86: fmul double [[I]]
// X86: fmul double [[I]]
// X86-NOT: fmul
return a * b;
}
double _Complex div_double_ci(double _Complex a, int b) {
// X86-LABEL: @div_double_ci
// X86: [[I:%.*]] = sitofp i32 {{%.*}} to double
// X86: fdiv double {{.*}}, [[I]]
// X86: fdiv double {{.*}}, [[I]]
// X86-NOT: @__divdc3
return a / b;
}
// There is no good way of doing this w/o converting the 'int' to a complex
// number, so we expect complex division here.
double _Complex div_double_ic(int a, double _Complex b) {
// X86-LABEL: @div_double_ic
// X86: [[I:%.*]] = sitofp i32 {{%.*}} to double
// X86: call {{.*}} @__divdc3(double {{.*}} [[I]], double noundef 0.{{0+}}e+00, double {{.*}}, double {{.*}})
return a / b;
}