Skip to content

Commit 56ef6a2

Browse files
authored
[libc][math][c23] Add f16div{,l,f128} C23 math functions (#97054)
Part of #93566.
1 parent 534f6ed commit 56ef6a2

21 files changed

+331
-2
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,9 @@ if(LIBC_TYPES_HAS_FLOAT16)
507507
libc.src.math.canonicalizef16
508508
libc.src.math.ceilf16
509509
libc.src.math.copysignf16
510+
libc.src.math.f16div
510511
libc.src.math.f16divf
512+
libc.src.math.f16divl
511513
libc.src.math.f16fmaf
512514
libc.src.math.f16sqrtf
513515
libc.src.math.fabsf16
@@ -560,6 +562,13 @@ if(LIBC_TYPES_HAS_FLOAT16)
560562
libc.src.math.ufromfpf16
561563
libc.src.math.ufromfpxf16
562564
)
565+
566+
if(LIBC_TYPES_HAS_FLOAT128)
567+
list(APPEND TARGET_LIBM_ENTRYPOINTS
568+
# math.h C23 mixed _Float16 and _Float128 entrypoints
569+
libc.src.math.f16divf128
570+
)
571+
endif()
563572
endif()
564573

565574
if(LIBC_TYPES_HAS_FLOAT128)

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,9 @@ if(LIBC_TYPES_HAS_FLOAT16)
537537
libc.src.math.canonicalizef16
538538
libc.src.math.ceilf16
539539
libc.src.math.copysignf16
540+
libc.src.math.f16div
540541
libc.src.math.f16divf
542+
libc.src.math.f16divl
541543
libc.src.math.f16fma
542544
libc.src.math.f16fmaf
543545
libc.src.math.f16fmal
@@ -595,6 +597,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
595597
list(APPEND TARGET_LIBM_ENTRYPOINTS
596598
# math.h C23 mixed _Float16 and _Float128 entrypoints
597599
libc.src.math.f16fmaf128
600+
libc.src.math.f16divf128
598601
)
599602
endif()
600603
endif()

libc/docs/math/index.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ Basic Operations
124124
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
125125
| dsub | N/A | N/A | | N/A | | 7.12.14.2 | F.10.11 |
126126
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
127-
| f16div | |check| | | | N/A | | 7.12.14.4 | F.10.11 |
127+
| f16div | |check|\* | |check|\* | |check|\* | N/A | |check| | 7.12.14.4 | F.10.11 |
128128
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
129129
| f16fma | |check| | |check| | |check| | N/A | |check| | 7.12.14.5 | F.10.11 |
130130
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
@@ -350,6 +350,7 @@ Legends:
350350
tie-to-even).
351351
* x ULPs: largest errors recorded.
352352
* N/A: Not defined in the standard or will not be added.
353+
* \*: LLVM libc extension.
353354

354355
..
355356
TODO(lntue): Add a new page to discuss about the algorithms used in the

libc/spec/llvm_libc_ext.td

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,21 @@ def LLVMLibcExt : StandardSpec<"llvm_libc_ext"> {
5151
]
5252
>;
5353

54+
HeaderSpec Math = HeaderSpec<
55+
"math.h",
56+
[], // Macros
57+
[], // Types
58+
[], // Enumerations
59+
[
60+
GuardedFunctionSpec<"f16div", RetValSpec<Float16Type>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>], "LIBC_TYPES_HAS_FLOAT16">,
61+
GuardedFunctionSpec<"f16divf", RetValSpec<Float16Type>, [ArgSpec<FloatType>, ArgSpec<FloatType>], "LIBC_TYPES_HAS_FLOAT16">,
62+
GuardedFunctionSpec<"f16divl", RetValSpec<Float16Type>, [ArgSpec<LongDoubleType>, ArgSpec<LongDoubleType>], "LIBC_TYPES_HAS_FLOAT16">,
63+
]
64+
>;
65+
5466
let Headers = [
5567
Assert,
68+
Math,
5669
Sched,
5770
Strings,
5871
];

libc/spec/stdc.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ def StdC : StandardSpec<"stdc"> {
729729

730730
GuardedFunctionSpec<"setpayloadsigf16", RetValSpec<IntType>, [ArgSpec<Float16Ptr>, ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
731731

732-
GuardedFunctionSpec<"f16divf", RetValSpec<Float16Type>, [ArgSpec<FloatType>, ArgSpec<FloatType>], "LIBC_TYPES_HAS_FLOAT16">,
732+
GuardedFunctionSpec<"f16divf128", RetValSpec<Float16Type>, [ArgSpec<Float128Type>, ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT16_AND_FLOAT128">,
733733

734734
GuardedFunctionSpec<"f16sqrtf", RetValSpec<Float16Type>, [ArgSpec<FloatType>], "LIBC_TYPES_HAS_FLOAT16">,
735735
]

libc/src/math/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,10 @@ add_math_entrypoint_object(exp10f)
9999
add_math_entrypoint_object(expm1)
100100
add_math_entrypoint_object(expm1f)
101101

102+
add_math_entrypoint_object(f16div)
102103
add_math_entrypoint_object(f16divf)
104+
add_math_entrypoint_object(f16divl)
105+
add_math_entrypoint_object(f16divf128)
103106

104107
add_math_entrypoint_object(f16fma)
105108
add_math_entrypoint_object(f16fmaf)

libc/src/math/f16div.h

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

libc/src/math/f16divf128.h

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

libc/src/math/f16divl.h

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

libc/src/math/generic/CMakeLists.txt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3776,6 +3776,19 @@ add_entrypoint_object(
37763776
-O3
37773777
)
37783778

3779+
add_entrypoint_object(
3780+
f16div
3781+
SRCS
3782+
f16div.cpp
3783+
HDRS
3784+
../f16div.h
3785+
DEPENDS
3786+
libc.src.__support.macros.properties.types
3787+
libc.src.__support.FPUtil.generic.div
3788+
COMPILE_OPTIONS
3789+
-O3
3790+
)
3791+
37793792
add_entrypoint_object(
37803793
f16divf
37813794
SRCS
@@ -3789,6 +3802,32 @@ add_entrypoint_object(
37893802
-O3
37903803
)
37913804

3805+
add_entrypoint_object(
3806+
f16divl
3807+
SRCS
3808+
f16divl.cpp
3809+
HDRS
3810+
../f16divl.h
3811+
DEPENDS
3812+
libc.src.__support.macros.properties.types
3813+
libc.src.__support.FPUtil.generic.div
3814+
COMPILE_OPTIONS
3815+
-O3
3816+
)
3817+
3818+
add_entrypoint_object(
3819+
f16divf128
3820+
SRCS
3821+
f16divf128.cpp
3822+
HDRS
3823+
../f16divf128.h
3824+
DEPENDS
3825+
libc.src.__support.macros.properties.types
3826+
libc.src.__support.FPUtil.generic.div
3827+
COMPILE_OPTIONS
3828+
-O3
3829+
)
3830+
37923831
add_entrypoint_object(
37933832
f16fma
37943833
SRCS

libc/src/math/generic/f16div.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===-- Implementation of f16div 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/f16div.h"
10+
#include "src/__support/FPUtil/generic/div.h"
11+
#include "src/__support/common.h"
12+
13+
namespace LIBC_NAMESPACE {
14+
15+
LLVM_LIBC_FUNCTION(float16, f16div, (double x, double y)) {
16+
return fputil::generic::div<float16>(x, y);
17+
}
18+
19+
} // namespace LIBC_NAMESPACE

libc/src/math/generic/f16divf128.cpp

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

libc/src/math/generic/f16divl.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===-- Implementation of f16divl 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/f16divl.h"
10+
#include "src/__support/FPUtil/generic/div.h"
11+
#include "src/__support/common.h"
12+
13+
namespace LIBC_NAMESPACE {
14+
15+
LLVM_LIBC_FUNCTION(float16, f16divl, (long double x, long double y)) {
16+
return fputil::generic::div<float16>(x, y);
17+
}
18+
19+
} // namespace LIBC_NAMESPACE

libc/test/src/math/CMakeLists.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1902,6 +1902,19 @@ add_fp_unittest(
19021902
libc.src.__support.FPUtil.fp_bits
19031903
)
19041904

1905+
add_fp_unittest(
1906+
f16div_test
1907+
NEED_MPFR
1908+
SUITE
1909+
libc-math-unittests
1910+
SRCS
1911+
f16div_test.cpp
1912+
HDRS
1913+
DivTest.h
1914+
DEPENDS
1915+
libc.src.math.f16div
1916+
)
1917+
19051918
add_fp_unittest(
19061919
f16divf_test
19071920
NEED_MPFR
@@ -1915,6 +1928,19 @@ add_fp_unittest(
19151928
libc.src.math.f16divf
19161929
)
19171930

1931+
add_fp_unittest(
1932+
f16divl_test
1933+
NEED_MPFR
1934+
SUITE
1935+
libc-math-unittests
1936+
SRCS
1937+
f16divl_test.cpp
1938+
HDRS
1939+
DivTest.h
1940+
DEPENDS
1941+
libc.src.math.f16divl
1942+
)
1943+
19181944
add_fp_unittest(
19191945
f16fma_test
19201946
NEED_MPFR

libc/test/src/math/f16div_test.cpp

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

libc/test/src/math/f16divl_test.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//===-- Unittests for f16divl ---------------------------------------------===//
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 "DivTest.h"
10+
11+
#include "src/math/f16divl.h"
12+
13+
LIST_DIV_TESTS(float16, long double, LIBC_NAMESPACE::f16divl)

0 commit comments

Comments
 (0)