Skip to content

Commit 7f37b34

Browse files
authored
[libc][complex] Testing infra for MPC (#121261)
This PR aims to add the groundwork to test the precision of libc complex functions against MPC. I took `cargf` as a test to verify that the infra works fine.
1 parent 7109f52 commit 7f37b34

31 files changed

+1714
-906
lines changed

libc/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ if(LIBC_TARGET_OS_IS_GPU)
262262
endif()
263263

264264
include(LLVMLibCCheckMPFR)
265+
include(LLVMLibCCheckMPC)
265266

266267
if(LLVM_LIBC_CLANG_TIDY)
267268
set(LLVM_LIBC_ENABLE_LINTING ON)
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
if(LIBC_TESTS_CAN_USE_MPFR)
2+
set(LLVM_LIBC_MPC_INSTALL_PATH "" CACHE PATH "Path to where MPC is installed (e.g. C:/src/install or ~/src/install)")
3+
4+
if(LLVM_LIBC_MPC_INSTALL_PATH)
5+
set(LIBC_TESTS_CAN_USE_MPC TRUE)
6+
elseif(LIBC_TARGET_OS_IS_GPU OR LLVM_LIBC_FULL_BUILD)
7+
# In full build mode, the MPC library should be built using our own facilities,
8+
# which is currently not possible.
9+
set(LIBC_TESTS_CAN_USE_MPC FALSE)
10+
else()
11+
try_compile(
12+
LIBC_TESTS_CAN_USE_MPC
13+
${CMAKE_CURRENT_BINARY_DIR}
14+
SOURCES
15+
${LIBC_SOURCE_DIR}/utils/MPCWrapper/check_mpc.cpp
16+
COMPILE_DEFINITIONS
17+
${LIBC_COMPILE_OPTIONS_DEFAULT}
18+
LINK_LIBRARIES
19+
-lmpc -lmpfr -lgmp -latomic
20+
)
21+
endif()
22+
endif()

libc/src/__support/CMakeLists.txt

+10-2
Original file line numberDiff line numberDiff line change
@@ -243,12 +243,20 @@ add_header_library(
243243
HDRS
244244
complex_type.h
245245
DEPENDS
246-
libc.src.__support.CPP.bit
247-
libc.src.__support.FPUtil.fp_bits
248246
libc.src.__support.macros.properties.types
249247
libc.src.__support.macros.properties.complex_types
250248
)
251249

250+
add_header_library(
251+
complex_basic_ops
252+
HDRS
253+
complex_basic_ops.h
254+
DEPENDS
255+
.complex_type
256+
libc.src.__support.CPP.bit
257+
libc.src.__support.FPUtil.fp_bits
258+
)
259+
252260
add_header_library(
253261
integer_operations
254262
HDRS

libc/src/__support/CPP/type_traits.h

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "src/__support/CPP/type_traits/is_array.h"
2727
#include "src/__support/CPP/type_traits/is_base_of.h"
2828
#include "src/__support/CPP/type_traits/is_class.h"
29+
#include "src/__support/CPP/type_traits/is_complex.h"
2930
#include "src/__support/CPP/type_traits/is_const.h"
3031
#include "src/__support/CPP/type_traits/is_constant_evaluated.h"
3132
#include "src/__support/CPP/type_traits/is_convertible.h"
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//===-- complex basic operations --------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC___SUPPORT_COMPLEX_BASIC_OPERATIONS_H
10+
#define LLVM_LIBC_SRC___SUPPORT_COMPLEX_BASIC_OPERATIONS_H
11+
12+
#include "complex_type.h"
13+
#include "src/__support/CPP/bit.h"
14+
#include "src/__support/FPUtil/FPBits.h"
15+
16+
namespace LIBC_NAMESPACE_DECL {
17+
18+
template <typename T> LIBC_INLINE constexpr T conjugate(T c) {
19+
Complex<make_real_t<T>> c_c = cpp::bit_cast<Complex<make_real_t<T>>>(c);
20+
c_c.imag = -c_c.imag;
21+
return cpp::bit_cast<T>(c_c);
22+
}
23+
24+
template <typename T> LIBC_INLINE constexpr T project(T c) {
25+
using real_t = make_real_t<T>;
26+
Complex<real_t> c_c = cpp::bit_cast<Complex<real_t>>(c);
27+
if (fputil::FPBits<real_t>(c_c.real).is_inf() ||
28+
fputil::FPBits<real_t>(c_c.imag).is_inf())
29+
return cpp::bit_cast<T>(
30+
Complex<real_t>{(fputil::FPBits<real_t>::inf(Sign::POS).get_val()),
31+
static_cast<real_t>(c_c.imag > 0 ? 0.0 : -0.0)});
32+
return c;
33+
}
34+
35+
} // namespace LIBC_NAMESPACE_DECL
36+
#endif // LLVM_LIBC_SRC___SUPPORT_COMPLEX_BASIC_OPERATIONS_H

libc/src/__support/complex_type.h

-21
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
#ifndef LLVM_LIBC_SRC___SUPPORT_COMPLEX_TYPE_H
1010
#define LLVM_LIBC_SRC___SUPPORT_COMPLEX_TYPE_H
1111

12-
#include "src/__support/CPP/bit.h"
13-
#include "src/__support/FPUtil/FPBits.h"
1412
#include "src/__support/macros/config.h"
1513
#include "src/__support/macros/properties/complex_types.h"
1614
#include "src/__support/macros/properties/types.h"
@@ -71,24 +69,5 @@ template <> struct make_real<cfloat128> {
7169

7270
template <typename T> using make_real_t = typename make_real<T>::type;
7371

74-
template <typename T> LIBC_INLINE constexpr T conjugate(T c) {
75-
Complex<make_real_t<T>> c_c = cpp::bit_cast<Complex<make_real_t<T>>>(c);
76-
c_c.imag = -c_c.imag;
77-
return cpp::bit_cast<T>(c_c);
78-
}
79-
80-
template <typename T> LIBC_INLINE constexpr T project(T c) {
81-
using real_t = make_real_t<T>;
82-
Complex<real_t> c_c = cpp::bit_cast<Complex<real_t>>(c);
83-
if (fputil::FPBits<real_t>(c_c.real).is_inf() ||
84-
fputil::FPBits<real_t>(c_c.imag).is_inf()) {
85-
return cpp::bit_cast<T>(
86-
Complex<real_t>{(fputil::FPBits<real_t>::inf(Sign::POS).get_val()),
87-
static_cast<real_t>(c_c.imag > 0 ? 0.0 : -0.0)});
88-
} else {
89-
return c;
90-
}
91-
}
92-
9372
} // namespace LIBC_NAMESPACE_DECL
9473
#endif // LLVM_LIBC_SRC___SUPPORT_COMPLEX_TYPE_H

libc/src/complex/generic/CMakeLists.txt

+10-10
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ add_entrypoint_object(
77
COMPILE_OPTIONS
88
${libc_opt_high_flag}
99
DEPENDS
10-
libc.src.__support.complex_type
10+
libc.src.__support.complex_basic_ops
1111
)
1212

1313
add_entrypoint_object(
@@ -19,7 +19,7 @@ add_entrypoint_object(
1919
COMPILE_OPTIONS
2020
${libc_opt_high_flag}
2121
DEPENDS
22-
libc.src.__support.complex_type
22+
libc.src.__support.complex_basic_ops
2323
)
2424

2525
add_entrypoint_object(
@@ -31,7 +31,7 @@ add_entrypoint_object(
3131
COMPILE_OPTIONS
3232
${libc_opt_high_flag}
3333
DEPENDS
34-
libc.src.__support.complex_type
34+
libc.src.__support.complex_basic_ops
3535
)
3636

3737
add_entrypoint_object(
@@ -43,7 +43,7 @@ add_entrypoint_object(
4343
COMPILE_OPTIONS
4444
${libc_opt_high_flag}
4545
DEPENDS
46-
libc.src.__support.complex_type
46+
libc.src.__support.complex_basic_ops
4747
libc.src.__support.macros.properties.types
4848
libc.src.__support.macros.properties.complex_types
4949
)
@@ -57,7 +57,7 @@ add_entrypoint_object(
5757
COMPILE_OPTIONS
5858
${libc_opt_high_flag}
5959
DEPENDS
60-
libc.src.__support.complex_type
60+
libc.src.__support.complex_basic_ops
6161
libc.src.__support.macros.properties.types
6262
libc.src.__support.macros.properties.complex_types
6363
)
@@ -71,7 +71,7 @@ add_entrypoint_object(
7171
COMPILE_OPTIONS
7272
${libc_opt_high_flag}
7373
DEPENDS
74-
libc.src.__support.complex_type
74+
libc.src.__support.complex_basic_ops
7575
)
7676

7777
add_entrypoint_object(
@@ -83,7 +83,7 @@ add_entrypoint_object(
8383
COMPILE_OPTIONS
8484
${libc_opt_high_flag}
8585
DEPENDS
86-
libc.src.__support.complex_type
86+
libc.src.__support.complex_basic_ops
8787
)
8888

8989
add_entrypoint_object(
@@ -95,7 +95,7 @@ add_entrypoint_object(
9595
COMPILE_OPTIONS
9696
${libc_opt_high_flag}
9797
DEPENDS
98-
libc.src.__support.complex_type
98+
libc.src.__support.complex_basic_ops
9999
)
100100

101101
add_entrypoint_object(
@@ -107,7 +107,7 @@ add_entrypoint_object(
107107
COMPILE_OPTIONS
108108
${libc_opt_high_flag}
109109
DEPENDS
110-
libc.src.__support.complex_type
110+
libc.src.__support.complex_basic_ops
111111
libc.src.__support.macros.properties.types
112112
libc.src.__support.macros.properties.complex_types
113113
)
@@ -121,7 +121,7 @@ add_entrypoint_object(
121121
COMPILE_OPTIONS
122122
${libc_opt_high_flag}
123123
DEPENDS
124-
libc.src.__support.complex_type
124+
libc.src.__support.complex_basic_ops
125125
libc.src.__support.macros.properties.types
126126
libc.src.__support.macros.properties.complex_types
127127
)

libc/src/complex/generic/conj.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
#include "src/complex/conj.h"
1010
#include "src/__support/common.h"
11-
#include "src/__support/complex_type.h"
11+
#include "src/__support/complex_basic_ops.h"
1212

1313
namespace LIBC_NAMESPACE_DECL {
1414

libc/src/complex/generic/conjf.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
#include "src/complex/conjf.h"
1010
#include "src/__support/common.h"
11-
#include "src/__support/complex_type.h"
11+
#include "src/__support/complex_basic_ops.h"
1212

1313
namespace LIBC_NAMESPACE_DECL {
1414

libc/src/complex/generic/conjf128.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
#include "src/complex/conjf128.h"
1010
#include "src/__support/common.h"
11-
#include "src/__support/complex_type.h"
11+
#include "src/__support/complex_basic_ops.h"
1212

1313
namespace LIBC_NAMESPACE_DECL {
1414

libc/src/complex/generic/conjf16.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
#include "src/complex/conjf16.h"
1010
#include "src/__support/common.h"
11-
#include "src/__support/complex_type.h"
11+
#include "src/__support/complex_basic_ops.h"
1212

1313
namespace LIBC_NAMESPACE_DECL {
1414

libc/src/complex/generic/conjl.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
#include "src/complex/conjl.h"
1010
#include "src/__support/common.h"
11-
#include "src/__support/complex_type.h"
11+
#include "src/__support/complex_basic_ops.h"
1212

1313
namespace LIBC_NAMESPACE_DECL {
1414

libc/src/complex/generic/cproj.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
#include "src/complex/cproj.h"
1010
#include "src/__support/common.h"
11-
#include "src/__support/complex_type.h"
11+
#include "src/__support/complex_basic_ops.h"
1212

1313
namespace LIBC_NAMESPACE_DECL {
1414

libc/src/complex/generic/cprojf.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
#include "src/complex/cprojf.h"
1010
#include "src/__support/common.h"
11-
#include "src/__support/complex_type.h"
11+
#include "src/__support/complex_basic_ops.h"
1212

1313
namespace LIBC_NAMESPACE_DECL {
1414

libc/src/complex/generic/cprojf128.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
#include "src/complex/cprojf128.h"
1010
#include "src/__support/common.h"
11-
#include "src/__support/complex_type.h"
11+
#include "src/__support/complex_basic_ops.h"
1212

1313
namespace LIBC_NAMESPACE_DECL {
1414

libc/src/complex/generic/cprojf16.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
#include "src/complex/cprojf16.h"
1010
#include "src/__support/common.h"
11-
#include "src/__support/complex_type.h"
11+
#include "src/__support/complex_basic_ops.h"
1212

1313
namespace LIBC_NAMESPACE_DECL {
1414

libc/src/complex/generic/cprojl.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
#include "src/complex/cprojl.h"
1010
#include "src/__support/common.h"
11-
#include "src/__support/complex_type.h"
11+
#include "src/__support/complex_basic_ops.h"
1212

1313
namespace LIBC_NAMESPACE_DECL {
1414

libc/test/UnitTest/FPMatcher.h

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
#include "src/__support/CPP/array.h"
1313
#include "src/__support/CPP/type_traits.h"
14-
#include "src/__support/CPP/type_traits/is_complex.h"
1514
#include "src/__support/FPUtil/FEnvImpl.h"
1615
#include "src/__support/FPUtil/FPBits.h"
1716
#include "src/__support/FPUtil/fpbits_str.h"

libc/test/src/CMakeLists.txt

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
function(add_fp_unittest name)
22
cmake_parse_arguments(
33
"MATH_UNITTEST"
4-
"NEED_MPFR;UNIT_TEST_ONLY;HERMETIC_TEST_ONLY" # Optional arguments
4+
"NEED_MPFR;NEED_MPC;UNIT_TEST_ONLY;HERMETIC_TEST_ONLY" # Optional arguments
55
"" # Single value arguments
66
"LINK_LIBRARIES;DEPENDS" # Multi-value arguments
77
${ARGN}
88
)
99

10+
if(MATH_UNITTEST_NEED_MPC)
11+
set(MATH_UNITTEST_NEED_MPFR TRUE)
12+
if(NOT LIBC_TESTS_CAN_USE_MPC)
13+
message(VERBOSE "Complex test ${name} will be skipped as MPC library is not available.")
14+
return()
15+
endif()
16+
list(APPEND MATH_UNITTEST_LINK_LIBRARIES libcMPCWrapper)
17+
endif()
18+
1019
if(MATH_UNITTEST_NEED_MPFR)
1120
if(NOT LIBC_TESTS_CAN_USE_MPFR)
1221
message(VERBOSE "Math test ${name} will be skipped as MPFR library is not available.")

0 commit comments

Comments
 (0)