Skip to content

Commit

Permalink
[libc][math][c23] Add f16div{,l,f128} C23 math functions (llvm#97054)
Browse files Browse the repository at this point in the history
Part of llvm#93566.
  • Loading branch information
overmighty authored and lravenclaw committed Jul 3, 2024
1 parent ba248f2 commit 8bb33d9
Show file tree
Hide file tree
Showing 21 changed files with 331 additions and 2 deletions.
9 changes: 9 additions & 0 deletions libc/config/linux/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,9 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.canonicalizef16
libc.src.math.ceilf16
libc.src.math.copysignf16
libc.src.math.f16div
libc.src.math.f16divf
libc.src.math.f16divl
libc.src.math.f16fmaf
libc.src.math.f16sqrtf
libc.src.math.fabsf16
Expand Down Expand Up @@ -560,6 +562,13 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.ufromfpf16
libc.src.math.ufromfpxf16
)

if(LIBC_TYPES_HAS_FLOAT128)
list(APPEND TARGET_LIBM_ENTRYPOINTS
# math.h C23 mixed _Float16 and _Float128 entrypoints
libc.src.math.f16divf128
)
endif()
endif()

if(LIBC_TYPES_HAS_FLOAT128)
Expand Down
3 changes: 3 additions & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,9 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.canonicalizef16
libc.src.math.ceilf16
libc.src.math.copysignf16
libc.src.math.f16div
libc.src.math.f16divf
libc.src.math.f16divl
libc.src.math.f16fma
libc.src.math.f16fmaf
libc.src.math.f16fmal
Expand Down Expand Up @@ -595,6 +597,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
list(APPEND TARGET_LIBM_ENTRYPOINTS
# math.h C23 mixed _Float16 and _Float128 entrypoints
libc.src.math.f16fmaf128
libc.src.math.f16divf128
)
endif()
endif()
Expand Down
3 changes: 2 additions & 1 deletion libc/docs/math/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ Basic Operations
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| dsub | N/A | N/A | | N/A | | 7.12.14.2 | F.10.11 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| f16div | |check| | | | N/A | | 7.12.14.4 | F.10.11 |
| f16div | |check|\* | |check|\* | |check|\* | N/A | |check| | 7.12.14.4 | F.10.11 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| f16fma | |check| | |check| | |check| | N/A | |check| | 7.12.14.5 | F.10.11 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
Expand Down Expand Up @@ -350,6 +350,7 @@ Legends:
tie-to-even).
* x ULPs: largest errors recorded.
* N/A: Not defined in the standard or will not be added.
* \*: LLVM libc extension.

..
TODO(lntue): Add a new page to discuss about the algorithms used in the
Expand Down
13 changes: 13 additions & 0 deletions libc/spec/llvm_libc_ext.td
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,21 @@ def LLVMLibcExt : StandardSpec<"llvm_libc_ext"> {
]
>;

HeaderSpec Math = HeaderSpec<
"math.h",
[], // Macros
[], // Types
[], // Enumerations
[
GuardedFunctionSpec<"f16div", RetValSpec<Float16Type>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>], "LIBC_TYPES_HAS_FLOAT16">,
GuardedFunctionSpec<"f16divf", RetValSpec<Float16Type>, [ArgSpec<FloatType>, ArgSpec<FloatType>], "LIBC_TYPES_HAS_FLOAT16">,
GuardedFunctionSpec<"f16divl", RetValSpec<Float16Type>, [ArgSpec<LongDoubleType>, ArgSpec<LongDoubleType>], "LIBC_TYPES_HAS_FLOAT16">,
]
>;

let Headers = [
Assert,
Math,
Sched,
Strings,
];
Expand Down
2 changes: 1 addition & 1 deletion libc/spec/stdc.td
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ def StdC : StandardSpec<"stdc"> {

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

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

GuardedFunctionSpec<"f16sqrtf", RetValSpec<Float16Type>, [ArgSpec<FloatType>], "LIBC_TYPES_HAS_FLOAT16">,
]
Expand Down
3 changes: 3 additions & 0 deletions libc/src/math/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ add_math_entrypoint_object(exp10f)
add_math_entrypoint_object(expm1)
add_math_entrypoint_object(expm1f)

add_math_entrypoint_object(f16div)
add_math_entrypoint_object(f16divf)
add_math_entrypoint_object(f16divl)
add_math_entrypoint_object(f16divf128)

add_math_entrypoint_object(f16fma)
add_math_entrypoint_object(f16fmaf)
Expand Down
20 changes: 20 additions & 0 deletions libc/src/math/f16div.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation header for f16div ------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_MATH_F16DIV_H
#define LLVM_LIBC_SRC_MATH_F16DIV_H

#include "src/__support/macros/properties/types.h"

namespace LIBC_NAMESPACE {

float16 f16div(double x, double y);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_MATH_F16DIV_H
20 changes: 20 additions & 0 deletions libc/src/math/f16divf128.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation header for f16divf128 --------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_MATH_F16DIVF128_H
#define LLVM_LIBC_SRC_MATH_F16DIVF128_H

#include "src/__support/macros/properties/types.h"

namespace LIBC_NAMESPACE {

float16 f16divf128(float128 x, float128 y);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_MATH_F16DIVF128_H
20 changes: 20 additions & 0 deletions libc/src/math/f16divl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation header for f16divl -----------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_MATH_F16DIVL_H
#define LLVM_LIBC_SRC_MATH_F16DIVL_H

#include "src/__support/macros/properties/types.h"

namespace LIBC_NAMESPACE {

float16 f16divl(long double x, long double y);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_MATH_F16DIVL_H
39 changes: 39 additions & 0 deletions libc/src/math/generic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3776,6 +3776,19 @@ add_entrypoint_object(
-O3
)

add_entrypoint_object(
f16div
SRCS
f16div.cpp
HDRS
../f16div.h
DEPENDS
libc.src.__support.macros.properties.types
libc.src.__support.FPUtil.generic.div
COMPILE_OPTIONS
-O3
)

add_entrypoint_object(
f16divf
SRCS
Expand All @@ -3789,6 +3802,32 @@ add_entrypoint_object(
-O3
)

add_entrypoint_object(
f16divl
SRCS
f16divl.cpp
HDRS
../f16divl.h
DEPENDS
libc.src.__support.macros.properties.types
libc.src.__support.FPUtil.generic.div
COMPILE_OPTIONS
-O3
)

add_entrypoint_object(
f16divf128
SRCS
f16divf128.cpp
HDRS
../f16divf128.h
DEPENDS
libc.src.__support.macros.properties.types
libc.src.__support.FPUtil.generic.div
COMPILE_OPTIONS
-O3
)

add_entrypoint_object(
f16fma
SRCS
Expand Down
19 changes: 19 additions & 0 deletions libc/src/math/generic/f16div.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//===-- Implementation of f16div function ---------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/math/f16div.h"
#include "src/__support/FPUtil/generic/div.h"
#include "src/__support/common.h"

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(float16, f16div, (double x, double y)) {
return fputil::generic::div<float16>(x, y);
}

} // namespace LIBC_NAMESPACE
19 changes: 19 additions & 0 deletions libc/src/math/generic/f16divf128.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//===-- Implementation of f16divf128 function -----------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/math/f16divf128.h"
#include "src/__support/FPUtil/generic/div.h"
#include "src/__support/common.h"

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(float16, f16divf128, (float128 x, float128 y)) {
return fputil::generic::div<float16>(x, y);
}

} // namespace LIBC_NAMESPACE
19 changes: 19 additions & 0 deletions libc/src/math/generic/f16divl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//===-- Implementation of f16divl function --------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/math/f16divl.h"
#include "src/__support/FPUtil/generic/div.h"
#include "src/__support/common.h"

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(float16, f16divl, (long double x, long double y)) {
return fputil::generic::div<float16>(x, y);
}

} // namespace LIBC_NAMESPACE
26 changes: 26 additions & 0 deletions libc/test/src/math/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1902,6 +1902,19 @@ add_fp_unittest(
libc.src.__support.FPUtil.fp_bits
)

add_fp_unittest(
f16div_test
NEED_MPFR
SUITE
libc-math-unittests
SRCS
f16div_test.cpp
HDRS
DivTest.h
DEPENDS
libc.src.math.f16div
)

add_fp_unittest(
f16divf_test
NEED_MPFR
Expand All @@ -1915,6 +1928,19 @@ add_fp_unittest(
libc.src.math.f16divf
)

add_fp_unittest(
f16divl_test
NEED_MPFR
SUITE
libc-math-unittests
SRCS
f16divl_test.cpp
HDRS
DivTest.h
DEPENDS
libc.src.math.f16divl
)

add_fp_unittest(
f16fma_test
NEED_MPFR
Expand Down
13 changes: 13 additions & 0 deletions libc/test/src/math/f16div_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//===-- Unittests for f16div ----------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "DivTest.h"

#include "src/math/f16div.h"

LIST_DIV_TESTS(float16, double, LIBC_NAMESPACE::f16div)
13 changes: 13 additions & 0 deletions libc/test/src/math/f16divl_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//===-- Unittests for f16divl ---------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "DivTest.h"

#include "src/math/f16divl.h"

LIST_DIV_TESTS(float16, long double, LIBC_NAMESPACE::f16divl)
Loading

0 comments on commit 8bb33d9

Please sign in to comment.