Skip to content

Commit 9ce03e9

Browse files
committed
fixed a couple bugs
1 parent 5a1594c commit 9ce03e9

File tree

6 files changed

+59
-55
lines changed

6 files changed

+59
-55
lines changed

libc/test/src/complex/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ add_libc_test(
1010
libc.src.complex.cargf
1111
LINK_LIBRARIES
1212
LibcFPTestHelpers
13+
libcMPCWrapper
1314
)
1415

1516
add_libc_test(

libc/test/src/complex/CargTest.h

Lines changed: 0 additions & 35 deletions
This file was deleted.

libc/test/src/complex/cargf_test.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,17 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "CargTest.h"
10-
119
#include "src/complex/cargf.h"
1210

13-
LIST_CARG_TESTS(_Complex float, float, LIBC_NAMESPACE::cargf)
11+
#include "test/UnitTest/FPMatcher.h"
12+
#include "test/UnitTest/Test.h"
13+
#include "utils/MPCWrapper/MPCUtils.h"
14+
15+
using LlvmLibcCargTest = LIBC_NAMESPACE::testing::FPTest<float>;
16+
17+
namespace mpc = LIBC_NAMESPACE::testing::mpc;
18+
19+
TEST_F(LlvmLibcCargTest, RandomFloats) {
20+
_Complex float test1 = 5.0 + 10.0i;
21+
EXPECT_MPC_MATCH_DEFAULT(mpc::Operation::Carg, test1, LIBC_NAMESPACE::cargf(test1), 0.5);
22+
}

libc/utils/MPCWrapper/MPCUtils.cpp

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -135,22 +135,26 @@ class MPCNumber {
135135
mpc_set(value, other.value, mpc_rounding);
136136
}
137137

138-
MPCNumber &operator=(const MPCNumber &rhs) {
138+
MPCNumber& operator=(const MPCNumber &rhs) {
139139
mpc_real_precision = rhs.mpc_real_precision;
140140
mpc_imag_precision = rhs.mpc_imag_precision;
141141
mpc_rounding = rhs.mpc_rounding;
142142
mpc_set(value, rhs.value, mpc_rounding);
143143
return *this;
144144
}
145145

146-
MPCNumber(const mpc_t &x, unsigned int r_p, unsigned int i_p, mpc_rnd_t rnd)
146+
MPCNumber(const mpc_t x, unsigned int r_p, unsigned int i_p, mpc_rnd_t rnd)
147147
: mpc_real_precision(r_p), mpc_imag_precision(i_p), mpc_rounding(rnd) {
148148
mpc_init3(value, mpc_real_precision, mpc_imag_precision);
149149
mpc_set(value, x, mpc_rounding);
150150
}
151151

152152
~MPCNumber() { mpc_clear(value); }
153153

154+
void getValue(mpc_t val) const {
155+
mpc_set(val, value, mpc_rounding);
156+
}
157+
154158
MPCNumber carg() const {
155159
mpfr_t res;
156160
mpfr_init2(res, this->mpc_real_precision);
@@ -187,10 +191,12 @@ bool compare_unary_operation_single_output_same_type(Operation op,
187191
MPCNumber mpc_result;
188192
mpc_result = unary_operation(op, input, precision, rounding);
189193
mpfr_t real, imag;
190-
mpc_real(real, mpc_result.value, rounding.Rrnd);
191-
mpc_imag(imag, mpc_result.value, rounding.Irnd);
192-
MPFRNumber mpfr_real(real, precision, rounding.Rrnd);
193-
MPFRNumber mpfr_imag(imag, precision, rounding.Irnd);
194+
mpc_t mpc_result_val;
195+
mpc_result.getValue(mpc_result_val);
196+
mpc_real(real, mpc_result_val, get_mpfr_rounding_mode(rounding.Rrnd));
197+
mpc_imag(imag, mpc_result_val, get_mpfr_rounding_mode(rounding.Irnd));
198+
mpfr::MPFRNumber mpfr_real(real, precision, rounding.Rrnd);
199+
mpfr::MPFRNumber mpfr_imag(imag, precision, rounding.Irnd);
194200
double ulp_real = mpfr_real.ulp(
195201
(cpp::bit_cast<MPCComplex<get_real_t<InputType>>>(libc_result)).real);
196202
double ulp_imag = mpfr_imag.ulp(
@@ -210,17 +216,19 @@ bool compare_unary_operation_single_output_different_type(
210216
unsigned int precision = get_precision<get_real_t<InputType>>(ulp_tolerance);
211217
MPCNumber mpc_result;
212218
mpc_result = unary_operation(op, input, precision, rounding);
219+
mpc_t mpc_result_val;
220+
mpc_result.getValue(mpc_result_val);
213221
mpfr_t real;
214-
mpc_real(real, mpc_result.value, rounding.Rrnd);
215-
MPFRNumber mpfr_real(real, precision, rounding.Rrnd);
222+
mpc_real(real, mpc_result_val, get_mpfr_rounding_mode(rounding.Rrnd));
223+
mpfr::MPFRNumber mpfr_real(real, precision, rounding.Rrnd);
216224
double ulp_real = mpfr_real.ulp(libc_result);
217225
return (ulp_real <= ulp_tolerance);
218226
}
219227

220228
template bool compare_unary_operation_single_output_different_type(
221-
Operation, _Complex float, _Complex float, double, MPCRoundingMode);
229+
Operation, _Complex float, float, double, MPCRoundingMode);
222230
template bool compare_unary_operation_single_output_different_type(
223-
Operation, _Complex double, _Complex double, double, MPCRoundingMode);
231+
Operation, _Complex double, double, double, MPCRoundingMode);
224232
} // namespace internal
225233

226234
} // namespace mpc

libc/utils/MPCWrapper/MPCUtils.h

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ using LIBC_NAMESPACE::fputil::testing::RoundingMode;
6666
struct MPCRoundingMode {
6767
RoundingMode Rrnd;
6868
RoundingMode Irnd;
69+
70+
MPCRoundingMode(RoundingMode r, RoundingMode i) : Rrnd(r), Irnd(i) {}
6971
};
7072

7173
template <typename T> struct BinaryInput {
@@ -80,7 +82,13 @@ template <typename T> struct BinaryInput {
8082
namespace internal {
8183

8284
template <typename InputType, typename OutputType>
83-
bool compare_unary_operation_single_output(Operation op, InputType input,
85+
bool compare_unary_operation_single_output_same_type(Operation op, InputType input,
86+
OutputType libc_output,
87+
double ulp_tolerance,
88+
MPCRoundingMode rounding);
89+
90+
template <typename InputType, typename OutputType>
91+
bool compare_unary_operation_single_output_different_type(Operation op, InputType input,
8492
OutputType libc_output,
8593
double ulp_tolerance,
8694
MPCRoundingMode rounding);
@@ -93,7 +101,13 @@ bool compare_binary_operation_one_output(Operation op,
93101
MPCRoundingMode rounding);
94102

95103
template <typename InputType, typename OutputType>
96-
void explain_unary_operation_single_output_error(Operation op, InputType input,
104+
void explain_unary_operation_single_output_same_type_error(Operation op, InputType input,
105+
OutputType match_value,
106+
double ulp_tolerance,
107+
MPCRoundingMode rounding);
108+
109+
template <typename InputType, typename OutputType>
110+
void explain_unary_operation_single_output_different_type_error(Operation op, InputType input,
97111
OutputType match_value,
98112
double ulp_tolerance,
99113
MPCRoundingMode rounding);
@@ -220,15 +234,15 @@ get_mpc_matcher(InputType input, [[maybe_unused]] OutputType output,
220234
match_value, \
221235
LIBC_NAMESPACE::testing::mpc::get_mpc_matcher<op>( \
222236
input, match_value, ulp_tolerance, \
223-
MPCRoundingMode{ \
237+
LIBC_NAMESPACE::testing::mpc::MPCRoundingMode{ \
224238
LIBC_NAMESPACE::fputil::testing::RoundingMode::Nearest, \
225239
LIBC_NAMESPACE::fputil::testing::RoundingMode::Nearest}))
226240

227241
#define EXPECT_MPC_MATCH_ROUNDING(op, input, match_value, ulp_tolerance, \
228242
Rrounding, Irounding) \
229243
EXPECT_THAT(match_value, LIBC_NAMESPACE::testing::mpc::get_mpc_matcher<op>( \
230244
input, match_value, ulp_tolerance, \
231-
MPCRoundingMode{Rrounding, Irounding}))
245+
LIBC_NAMESPACE::testing::mpc::MPCRoundingMode{Rrounding, Irounding}))
232246

233247
#define EXPECT_MPC_MATCH_ALL_ROUNDING_HELPER( \
234248
i, j, op, input, match_value, ulp_tolerance, Rrounding, Irounding) \
@@ -260,23 +274,23 @@ get_mpc_matcher(InputType input, [[maybe_unused]] OutputType output,
260274
Rrounding, Irounding) \
261275
LIBC_NAMESPACE::testing::mpc::get_mpc_matcher<op>( \
262276
input, match_value, ulp_tolerance, \
263-
MPCRoundingMode{Rrounding, Irounding}) \
277+
LIBC_NAMESPACE::testing::mpc::MPCRoundingMode{Rrounding, Irounding}) \
264278
.match(match_value)
265279

266280
#define ASSERT_MPC_MATCH_DEFAULT(op, input, match_value, ulp_tolerance) \
267281
ASSERT_THAT( \
268282
match_value, \
269283
LIBC_NAMESPACE::testing::mpc::get_mpc_matcher<op>( \
270284
input, match_value, ulp_tolerance, \
271-
MPCRoundingMode{ \
285+
LIBC_NAMESPACE::testing::mpc::MPCRoundingMode{ \
272286
LIBC_NAMESPACE::fputil::testing::RoundingMode::Nearest, \
273287
LIBC_NAMESPACE::fputil::testing::RoundingMode::Nearest}))
274288

275289
#define ASSERT_MPC_MATCH_ROUNDING(op, input, match_value, ulp_tolerance, \
276290
Rrounding, Irounding) \
277291
ASSERT_THAT(match_value, LIBC_NAMESPACE::testing::mpc::get_mpc_matcher<op>( \
278292
input, match_value, ulp_tolerance, \
279-
MPCRoundingMode{Rrounding, Irounding}))
293+
LIBC_NAMESPACE::testing::mpc::MPCRoundingMode{Rrounding, Irounding}))
280294

281295
#define ASSERT_MPC_MATCH_ALL_ROUNDING_HELPER( \
282296
i, j, op, input, match_value, ulp_tolerance, Rrounding, Irounding) \

libc/utils/MPFRWrapper/MPFRUtils.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,13 @@ class MPFRNumber {
188188
mpfr_set(value, other.value, mpfr_rounding);
189189
}
190190

191+
MPFRNumber(const mpfr_t& x, unsigned int precision, RoundingMode rounding)
192+
: mpfr_precision(precision),
193+
mpfr_rounding(get_mpfr_rounding_mode(rounding)) {
194+
mpfr_init2(value, mpfr_precision);
195+
mpfr_set(value, x, mpfr_rounding);
196+
}
197+
191198
~MPFRNumber() { mpfr_clear(value); }
192199

193200
MPFRNumber &operator=(const MPFRNumber &rhs) {

0 commit comments

Comments
 (0)