Skip to content

Commit 637c370

Browse files
authored
[libc][math] Add C23 math function frexpf128. (#81337)
1 parent cc02e50 commit 637c370

File tree

15 files changed

+119
-45
lines changed

15 files changed

+119
-45
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ if(LIBC_COMPILER_HAS_FLOAT128)
386386
libc.src.math.floorf128
387387
libc.src.math.fmaxf128
388388
libc.src.math.fminf128
389+
libc.src.math.frexpf128
389390
libc.src.math.roundf128
390391
libc.src.math.sqrtf128
391392
libc.src.math.truncf128

libc/config/linux/riscv/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ if(LIBC_COMPILER_HAS_FLOAT128)
395395
libc.src.math.floorf128
396396
libc.src.math.fmaxf128
397397
libc.src.math.fminf128
398+
libc.src.math.frexpf128
398399
libc.src.math.roundf128
399400
libc.src.math.sqrtf128
400401
libc.src.math.truncf128

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ if(LIBC_COMPILER_HAS_FLOAT128)
414414
libc.src.math.floorf128
415415
libc.src.math.fmaxf128
416416
libc.src.math.fminf128
417+
libc.src.math.frexpf128
417418
libc.src.math.roundf128
418419
libc.src.math.sqrtf128
419420
libc.src.math.truncf128

libc/docs/math/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ Basic Operations
176176
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
177177
| frexpl | |check| | |check| | |check| | |check| | |check| | | | |check| | |check| | |check| | | |
178178
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
179+
| frexpf128 | |check| | |check| | | |check| | | | | | | | | |
180+
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
179181
| ilogb | |check| | |check| | |check| | |check| | |check| | | | |check| | |check| | |check| | | |
180182
+--------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
181183
| ilogbf | |check| | |check| | |check| | |check| | |check| | | | |check| | |check| | |check| | | |

libc/spec/stdc.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@ def StdC : StandardSpec<"stdc"> {
401401
FunctionSpec<"frexp", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<IntPtr>]>,
402402
FunctionSpec<"frexpf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<IntPtr>]>,
403403
FunctionSpec<"frexpl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<IntPtr>]>,
404+
GuardedFunctionSpec<"frexpf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>, ArgSpec<IntPtr>]], "LIBC_COMPILER_HAS_FLOAT128">,
404405

405406
FunctionSpec<"hypot", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
406407
FunctionSpec<"hypotf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>]>,

libc/src/math/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ add_math_entrypoint_object(fmodf)
137137
add_math_entrypoint_object(frexp)
138138
add_math_entrypoint_object(frexpf)
139139
add_math_entrypoint_object(frexpl)
140+
add_math_entrypoint_object(frexpf128)
140141

141142
add_math_entrypoint_object(hypot)
142143
add_math_entrypoint_object(hypotf)

libc/src/math/frexpf128.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header for frexpf128 ---------------------*- 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_MATH_FREXPF128_H
10+
#define LLVM_LIBC_SRC_MATH_FREXPF128_H
11+
12+
#include "src/__support/macros/properties/float.h"
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
float128 frexpf128(float128 x, int *exp);
17+
18+
} // namespace LIBC_NAMESPACE
19+
20+
#endif // LLVM_LIBC_SRC_MATH_FREXPF128_H

libc/src/math/generic/CMakeLists.txt

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -916,10 +916,10 @@ add_entrypoint_object(
916916
frexp.cpp
917917
HDRS
918918
../frexp.h
919+
COMPILE_OPTIONS
920+
-O3
919921
DEPENDS
920922
libc.src.__support.FPUtil.manipulation_functions
921-
COMPILE_OPTIONS
922-
-O2
923923
)
924924

925925
add_entrypoint_object(
@@ -928,10 +928,10 @@ add_entrypoint_object(
928928
frexpf.cpp
929929
HDRS
930930
../frexpf.h
931+
COMPILE_OPTIONS
932+
-O3
931933
DEPENDS
932934
libc.src.__support.FPUtil.manipulation_functions
933-
COMPILE_OPTIONS
934-
-O2
935935
)
936936

937937
add_entrypoint_object(
@@ -940,10 +940,23 @@ add_entrypoint_object(
940940
frexpl.cpp
941941
HDRS
942942
../frexpl.h
943+
COMPILE_OPTIONS
944+
-O3
943945
DEPENDS
944946
libc.src.__support.FPUtil.manipulation_functions
947+
)
948+
949+
add_entrypoint_object(
950+
frexpf128
951+
SRCS
952+
frexpf128.cpp
953+
HDRS
954+
../frexpf128.h
945955
COMPILE_OPTIONS
946-
-O2
956+
-O3
957+
DEPENDS
958+
libc.src.__support.macros.properties.float
959+
libc.src.__support.FPUtil.manipulation_functions
947960
)
948961

949962
add_entrypoint_object(

libc/src/math/generic/frexpf128.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===-- Implementation of frexpf128 function ------------------------------===//
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+
#include "src/math/frexpf128.h"
10+
#include "src/__support/FPUtil/ManipulationFunctions.h"
11+
#include "src/__support/common.h"
12+
13+
namespace LIBC_NAMESPACE {
14+
15+
LLVM_LIBC_FUNCTION(float128, frexpf128, (float128 x, int *exp)) {
16+
return fputil::frexp(x, *exp);
17+
}
18+
19+
} // namespace LIBC_NAMESPACE

libc/test/src/math/smoke/CMakeLists.txt

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -779,9 +779,7 @@ add_fp_unittest(
779779
HDRS
780780
FrexpTest.h
781781
DEPENDS
782-
libc.include.math
783782
libc.src.math.frexp
784-
libc.src.__support.FPUtil.basic_operations
785783
)
786784

787785
add_fp_unittest(
@@ -793,9 +791,7 @@ add_fp_unittest(
793791
HDRS
794792
FrexpTest.h
795793
DEPENDS
796-
libc.include.math
797794
libc.src.math.frexpf
798-
libc.src.__support.FPUtil.basic_operations
799795
)
800796

801797
add_fp_unittest(
@@ -807,9 +803,19 @@ add_fp_unittest(
807803
HDRS
808804
FrexpTest.h
809805
DEPENDS
810-
libc.include.math
811806
libc.src.math.frexpl
812-
libc.src.__support.FPUtil.basic_operations
807+
)
808+
809+
add_fp_unittest(
810+
frexpf128_test
811+
SUITE
812+
libc-math-smoke-tests
813+
SRCS
814+
frexpf128_test.cpp
815+
HDRS
816+
FrexpTest.h
817+
DEPENDS
818+
libc.src.math.frexpf128
813819
)
814820

815821
# FIXME: These tests are currently broken for NVPTX.

libc/test/src/math/smoke/FrexpTest.h

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,81 +10,76 @@
1010
#include "test/UnitTest/FPMatcher.h"
1111
#include "test/UnitTest/Test.h"
1212

13-
#include <math.h>
14-
1513
template <typename T> class FrexpTest : public LIBC_NAMESPACE::testing::Test {
1614

1715
DECLARE_SPECIAL_CONSTANTS(T)
1816

19-
static constexpr StorageType HIDDEN_BIT =
20-
StorageType(1) << LIBC_NAMESPACE::fputil::FPBits<T>::FRACTION_LEN;
21-
2217
public:
2318
typedef T (*FrexpFunc)(T, int *);
2419

2520
void testSpecialNumbers(FrexpFunc func) {
2621
int exponent;
27-
ASSERT_FP_EQ(aNaN, func(aNaN, &exponent));
28-
ASSERT_FP_EQ(inf, func(inf, &exponent));
29-
ASSERT_FP_EQ(neg_inf, func(neg_inf, &exponent));
22+
EXPECT_FP_EQ_ALL_ROUNDING(aNaN, func(aNaN, &exponent));
23+
EXPECT_FP_EQ_ALL_ROUNDING(inf, func(inf, &exponent));
24+
EXPECT_FP_EQ_ALL_ROUNDING(neg_inf, func(neg_inf, &exponent));
3025

31-
ASSERT_FP_EQ(0.0, func(0.0, &exponent));
32-
ASSERT_EQ(exponent, 0);
26+
EXPECT_FP_EQ_ALL_ROUNDING(0.0, func(0.0, &exponent));
27+
EXPECT_EQ(exponent, 0);
3328

34-
ASSERT_FP_EQ(-0.0, func(-0.0, &exponent));
35-
ASSERT_EQ(exponent, 0);
29+
EXPECT_FP_EQ_ALL_ROUNDING(-0.0, func(-0.0, &exponent));
30+
EXPECT_EQ(exponent, 0);
3631
}
3732

3833
void testPowersOfTwo(FrexpFunc func) {
3934
int exponent;
4035

41-
EXPECT_FP_EQ(T(0.5), func(T(1.0), &exponent));
36+
EXPECT_FP_EQ_ALL_ROUNDING(T(0.5), func(T(1.0), &exponent));
4237
EXPECT_EQ(exponent, 1);
43-
EXPECT_FP_EQ(T(-0.5), func(T(-1.0), &exponent));
38+
EXPECT_FP_EQ_ALL_ROUNDING(T(-0.5), func(T(-1.0), &exponent));
4439
EXPECT_EQ(exponent, 1);
4540

46-
EXPECT_FP_EQ(T(0.5), func(T(2.0), &exponent));
41+
EXPECT_FP_EQ_ALL_ROUNDING(T(0.5), func(T(2.0), &exponent));
4742
EXPECT_EQ(exponent, 2);
48-
EXPECT_FP_EQ(T(-0.5), func(T(-2.0), &exponent));
43+
EXPECT_FP_EQ_ALL_ROUNDING(T(-0.5), func(T(-2.0), &exponent));
4944
EXPECT_EQ(exponent, 2);
5045

51-
EXPECT_FP_EQ(T(0.5), func(T(4.0), &exponent));
46+
EXPECT_FP_EQ_ALL_ROUNDING(T(0.5), func(T(4.0), &exponent));
5247
EXPECT_EQ(exponent, 3);
53-
EXPECT_FP_EQ(T(-0.5), func(T(-4.0), &exponent));
48+
EXPECT_FP_EQ_ALL_ROUNDING(T(-0.5), func(T(-4.0), &exponent));
5449
EXPECT_EQ(exponent, 3);
5550

56-
EXPECT_FP_EQ(T(0.5), func(T(8.0), &exponent));
51+
EXPECT_FP_EQ_ALL_ROUNDING(T(0.5), func(T(8.0), &exponent));
5752
EXPECT_EQ(exponent, 4);
58-
EXPECT_FP_EQ(T(-0.5), func(T(-8.0), &exponent));
53+
EXPECT_FP_EQ_ALL_ROUNDING(T(-0.5), func(T(-8.0), &exponent));
5954
EXPECT_EQ(exponent, 4);
6055

61-
EXPECT_FP_EQ(T(0.5), func(T(16.0), &exponent));
56+
EXPECT_FP_EQ_ALL_ROUNDING(T(0.5), func(T(16.0), &exponent));
6257
EXPECT_EQ(exponent, 5);
63-
EXPECT_FP_EQ(T(-0.5), func(T(-16.0), &exponent));
58+
EXPECT_FP_EQ_ALL_ROUNDING(T(-0.5), func(T(-16.0), &exponent));
6459
EXPECT_EQ(exponent, 5);
6560

66-
EXPECT_FP_EQ(T(0.5), func(T(32.0), &exponent));
61+
EXPECT_FP_EQ_ALL_ROUNDING(T(0.5), func(T(32.0), &exponent));
6762
EXPECT_EQ(exponent, 6);
68-
EXPECT_FP_EQ(T(-0.5), func(T(-32.0), &exponent));
63+
EXPECT_FP_EQ_ALL_ROUNDING(T(-0.5), func(T(-32.0), &exponent));
6964
EXPECT_EQ(exponent, 6);
7065
}
7166

7267
void testSomeIntegers(FrexpFunc func) {
7368
int exponent;
7469

75-
EXPECT_FP_EQ(T(0.75), func(T(24.0), &exponent));
70+
EXPECT_FP_EQ_ALL_ROUNDING(T(0.75), func(T(24.0), &exponent));
7671
EXPECT_EQ(exponent, 5);
77-
EXPECT_FP_EQ(T(-0.75), func(T(-24.0), &exponent));
72+
EXPECT_FP_EQ_ALL_ROUNDING(T(-0.75), func(T(-24.0), &exponent));
7873
EXPECT_EQ(exponent, 5);
7974

80-
EXPECT_FP_EQ(T(0.625), func(T(40.0), &exponent));
75+
EXPECT_FP_EQ_ALL_ROUNDING(T(0.625), func(T(40.0), &exponent));
8176
EXPECT_EQ(exponent, 6);
82-
EXPECT_FP_EQ(T(-0.625), func(T(-40.0), &exponent));
77+
EXPECT_FP_EQ_ALL_ROUNDING(T(-0.625), func(T(-40.0), &exponent));
8378
EXPECT_EQ(exponent, 6);
8479

85-
EXPECT_FP_EQ(T(0.78125), func(T(800.0), &exponent));
80+
EXPECT_FP_EQ_ALL_ROUNDING(T(0.78125), func(T(800.0), &exponent));
8681
EXPECT_EQ(exponent, 10);
87-
EXPECT_FP_EQ(T(-0.78125), func(T(-800.0), &exponent));
82+
EXPECT_FP_EQ_ALL_ROUNDING(T(-0.78125), func(T(-800.0), &exponent));
8883
EXPECT_EQ(exponent, 10);
8984
}
9085
};
@@ -93,4 +88,5 @@ template <typename T> class FrexpTest : public LIBC_NAMESPACE::testing::Test {
9388
using LlvmLibcFrexpTest = FrexpTest<T>; \
9489
TEST_F(LlvmLibcFrexpTest, SpecialNumbers) { testSpecialNumbers(&func); } \
9590
TEST_F(LlvmLibcFrexpTest, PowersOfTwo) { testPowersOfTwo(&func); } \
96-
TEST_F(LlvmLibcFrexpTest, SomeIntegers) { testSomeIntegers(&func); }
91+
TEST_F(LlvmLibcFrexpTest, SomeIntegers) { testSomeIntegers(&func); } \
92+
static_assert(true, "Require semicolon.")

libc/test/src/math/smoke/frexp_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010

1111
#include "src/math/frexp.h"
1212

13-
LIST_FREXP_TESTS(double, LIBC_NAMESPACE::frexp)
13+
LIST_FREXP_TESTS(double, LIBC_NAMESPACE::frexp);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//===-- Unittests for frexpf128 -------------------------------------------===//
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+
#include "FrexpTest.h"
10+
11+
#include "src/math/frexpf128.h"
12+
13+
LIST_FREXP_TESTS(float128, LIBC_NAMESPACE::frexpf128);

libc/test/src/math/smoke/frexpf_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010

1111
#include "src/math/frexpf.h"
1212

13-
LIST_FREXP_TESTS(float, LIBC_NAMESPACE::frexpf)
13+
LIST_FREXP_TESTS(float, LIBC_NAMESPACE::frexpf);

libc/test/src/math/smoke/frexpl_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010

1111
#include "src/math/frexpl.h"
1212

13-
LIST_FREXP_TESTS(long double, LIBC_NAMESPACE::frexpl)
13+
LIST_FREXP_TESTS(long double, LIBC_NAMESPACE::frexpl);

0 commit comments

Comments
 (0)