Skip to content

Commit dce8941

Browse files
committed
Add template function and a few tests
1 parent 99bde32 commit dce8941

File tree

9 files changed

+29
-11
lines changed

9 files changed

+29
-11
lines changed

libc/src/__support/complex_type.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,13 @@ template <typename T> struct Complex {
1616
T real;
1717
T imag;
1818
};
19+
20+
template <typename T, typename U>
21+
T conjugate(T c) {
22+
Complex<U> c_c = cpp::bit_cast<Complex<U>>(c);
23+
c_c.imag = -c_c.imag;
24+
return cpp::bit_cast<T>(c_c);
25+
}
26+
1927
} // namespace LIBC_NAMESPACE_DECL
2028
#endif // LLVM_LIBC_SRC___SUPPORT_COMPLEX_TYPE_H

libc/src/complex/generic/conj.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
namespace LIBC_NAMESPACE_DECL {
1515

1616
LLVM_LIBC_FUNCTION(_Complex double, conj, (_Complex double x)) {
17-
Complex<double> x_c = cpp::bit_cast<Complex<double>>(x);
18-
return (x_c.real - x_c.imag * 1.0i);
17+
return conjugate<_Complex double, double>(x);
1918
}
2019

2120
} // namespace LIBC_NAMESPACE_DECL

libc/src/complex/generic/conjf.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
namespace LIBC_NAMESPACE_DECL {
1515

1616
LLVM_LIBC_FUNCTION(_Complex float, conjf, (_Complex float x)) {
17-
Complex<float> x_c = cpp::bit_cast<Complex<float>>(x);
18-
return (x_c.real - x_c.imag * (_Complex float)1.0i);
17+
return conjugate<_Complex float, float>(x);
1918
}
2019

2120
} // namespace LIBC_NAMESPACE_DECL

libc/src/complex/generic/conjf128.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
namespace LIBC_NAMESPACE_DECL {
1717

1818
LLVM_LIBC_FUNCTION(cfloat128, conjf128, (cfloat128 x)) {
19-
Complex<float128> x_c = cpp::bit_cast<Complex<float128>>(x);
20-
return (x_c.real - x_c.imag * (cfloat128)1.0i);
19+
return conjugate<cfloat128, float128>(x);
2120
}
2221

2322
} // namespace LIBC_NAMESPACE_DECL

libc/src/complex/generic/conjf16.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
namespace LIBC_NAMESPACE_DECL {
1717

1818
LLVM_LIBC_FUNCTION(cfloat16, conjf16, (cfloat16 x)) {
19-
Complex<float16> x_c = cpp::bit_cast<Complex<float16>>(x);
20-
return (x_c.real - x_c.imag * (cfloat16)1.0i);
19+
return conjugate<cfloat16, float16>(x);
2120
}
2221

2322
} // namespace LIBC_NAMESPACE_DECL

libc/src/complex/generic/conjl.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
namespace LIBC_NAMESPACE_DECL {
1515

1616
LLVM_LIBC_FUNCTION(_Complex long double, conjl, (_Complex long double x)) {
17-
Complex<long double> x_c = cpp::bit_cast<Complex<long double>>(x);
18-
return (x_c.real - x_c.imag * (_Complex long double)1.0i);
17+
return conjugate<_Complex long double, long double>(x);
1918
}
2019

2120
} // namespace LIBC_NAMESPACE_DECL

libc/test/src/complex/CImagTest.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ class CImagTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
4242
EXPECT_FP_EQ(func(CFPT(-0.0 + 0.0i)), 0.0);
4343
EXPECT_FP_EQ(func(CFPT(0.0 - 0.0i)), -0.0);
4444
EXPECT_FP_EQ(func(CFPT(-0.0 - 0.0i)), -0.0);
45+
EXPECT_FP_EQ(func(CFPT(0.0)), 0.0);
46+
EXPECT_FP_EQ(func(CFPT(-0.0)), 0.0);
47+
EXPECT_FP_EQ(func(CFPT(0.0i)), 0.0);
48+
EXPECT_FP_EQ(func(CFPT(-0.0i)), -0.0);
4549
}
4650

4751
void testRoundedNumbers(CImagFunc func) {

libc/test/src/complex/CRealTest.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ class CRealTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
4141
EXPECT_FP_EQ(func(CFPT(-0.0 + 0.0i)), 0.0);
4242
EXPECT_FP_EQ(func(CFPT(0.0 - 0.0i)), 0.0);
4343
EXPECT_FP_EQ(func(CFPT(-0.0 - 0.0i)), -0.0);
44+
EXPECT_FP_EQ(func(CFPT(0.0)), 0.0);
45+
EXPECT_FP_EQ(func(CFPT(-0.0)), -0.0);
46+
EXPECT_FP_EQ(func(CFPT(0.0i)), 0.0);
47+
EXPECT_FP_EQ(func(CFPT(-0.0i)), -0.0);
4448
}
4549

4650
void testRoundedNumbers(CRealFunc func) {

libc/test/src/complex/ConjTest.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,17 @@ class ConjTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
7070
CFPT(121.121 - zero * 1.0i));
7171
EXPECT_CFP_EQ(func(CFPT(0.0 - 0.0i)), CFPT(0.0 + 0.0i));
7272
EXPECT_CFP_EQ(func(CFPT(0.0 + 0.0i)), CFPT(0.0 - 0.0i));
73-
EXPECT_CFP_EQ(func(CFPT(-0.0 - 0.0i)), CFPT(-0.0 + 0.0i));
73+
// This test passes because the conjugate of -0.0 - 0.0i is -0.0 + 0.0i
74+
// but -0.0 + 0.0i is actually CMPLX(-0.0, 0.0) + CMPLX(0.0, 0.0) = 0.0 + 0.0i
75+
// so to represent -0.0 + 0.0i, we just write -0.0
76+
EXPECT_CFP_EQ(func(CFPT(-0.0 - 0.0i)), CFPT(-0.0));
7477
// This test passes because -0.0 + 0.0i is actually
7578
// CMPLX(-0.0, 0.0) + CMPLX(0.0, 0.0) = CMPLX(-0.0 + 0.0, 0.0) = 0.0 + 0.0i
7679
EXPECT_CFP_EQ(func(CFPT(-0.0 + 0.0i)), CFPT(0.0 - 0.0i));
80+
EXPECT_CFP_EQ(func(CFPT(0.0)), CFPT(0.0 - 0.0i));
81+
EXPECT_CFP_EQ(func(CFPT(-0.0)), CFPT(-0.0 - 0.0i));
82+
EXPECT_CFP_EQ(func(CFPT(0.0i)), CFPT(0.0 - 0.0i));
83+
EXPECT_CFP_EQ(func(CFPT(-0.0i)), CFPT(-0.0));
7784
}
7885

7986
void testRoundedNumbers(ConjFunc func) {

0 commit comments

Comments
 (0)