Skip to content

Commit

Permalink
[libc][math][c23] Add fdimf16 C23 math function (#94354)
Browse files Browse the repository at this point in the history
  • Loading branch information
overmighty authored Jun 5, 2024
1 parent 07b8990 commit c537f35
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 10 deletions.
1 change: 1 addition & 0 deletions libc/config/linux/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.ceilf16
libc.src.math.copysignf16
libc.src.math.fabsf16
libc.src.math.fdimf16
libc.src.math.floorf16
libc.src.math.fromfpf16
libc.src.math.fromfpxf16
Expand Down
1 change: 1 addition & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.ceilf16
libc.src.math.copysignf16
libc.src.math.fabsf16
libc.src.math.fdimf16
libc.src.math.floorf16
libc.src.math.fromfpf16
libc.src.math.fromfpxf16
Expand Down
2 changes: 1 addition & 1 deletion libc/docs/math/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ Basic Operations
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| fadd | N/A | | | N/A | | 7.12.14.1 | F.10.11 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| fdim | |check| | |check| | |check| | | |check| | 7.12.12.1 | F.10.9.1 |
| fdim | |check| | |check| | |check| | |check| | |check| | 7.12.12.1 | F.10.9.1 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| fdiv | N/A | | | N/A | | 7.12.14.4 | F.10.11 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
Expand Down
1 change: 1 addition & 0 deletions libc/spec/stdc.td
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ def StdC : StandardSpec<"stdc"> {
FunctionSpec<"fdim", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
FunctionSpec<"fdimf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>]>,
FunctionSpec<"fdiml", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<LongDoubleType>]>,
GuardedFunctionSpec<"fdimf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>, ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
GuardedFunctionSpec<"fdimf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>, ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,

FunctionSpec<"floor", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
Expand Down
1 change: 1 addition & 0 deletions libc/src/math/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ add_math_entrypoint_object(fabsf128)
add_math_entrypoint_object(fdim)
add_math_entrypoint_object(fdimf)
add_math_entrypoint_object(fdiml)
add_math_entrypoint_object(fdimf16)
add_math_entrypoint_object(fdimf128)

add_math_entrypoint_object(floor)
Expand Down
20 changes: 20 additions & 0 deletions libc/src/math/fdimf16.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation header for fdimf16 -----------------------*- 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_FDIMF16_H
#define LLVM_LIBC_SRC_MATH_FDIMF16_H

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

namespace LIBC_NAMESPACE {

float16 fdimf16(float16 x, float16 y);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_MATH_FDIMF16_H
13 changes: 13 additions & 0 deletions libc/src/math/generic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2397,6 +2397,19 @@ add_entrypoint_object(
libc.src.__support.FPUtil.basic_operations
)

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

add_entrypoint_object(
fdimf128
SRCS
Expand Down
19 changes: 19 additions & 0 deletions libc/src/math/generic/fdimf16.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//===-- Implementation of fdimf16 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/fdimf16.h"
#include "src/__support/FPUtil/BasicOperations.h"
#include "src/__support/common.h"

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(float16, fdimf16, (float16 x, float16 y)) {
return fputil::fdim(x, y);
}

} // namespace LIBC_NAMESPACE
22 changes: 18 additions & 4 deletions libc/test/src/math/smoke/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1635,7 +1635,7 @@ add_fp_unittest(
FDimTest.h
DEPENDS
libc.src.math.fdimf
libc.src.__support.FPUtil.basic_operations
libc.src.__support.CPP.algorithm
libc.src.__support.FPUtil.fp_bits
)

Expand All @@ -1649,7 +1649,7 @@ add_fp_unittest(
FDimTest.h
DEPENDS
libc.src.math.fdim
libc.src.__support.FPUtil.basic_operations
libc.src.__support.CPP.algorithm
libc.src.__support.FPUtil.fp_bits
)

Expand All @@ -1663,7 +1663,21 @@ add_fp_unittest(
FDimTest.h
DEPENDS
libc.src.math.fdiml
libc.src.__support.FPUtil.basic_operations
libc.src.__support.CPP.algorithm
libc.src.__support.FPUtil.fp_bits
)

add_fp_unittest(
fdimf16_test
SUITE
libc-math-smoke-tests
SRCS
fdimf16_test.cpp
HDRS
FDimTest.h
DEPENDS
libc.src.math.fdimf16
libc.src.__support.CPP.algorithm
libc.src.__support.FPUtil.fp_bits
)

Expand All @@ -1677,7 +1691,7 @@ add_fp_unittest(
FDimTest.h
DEPENDS
libc.src.math.fdimf128
libc.src.__support.FPUtil.basic_operations
libc.src.__support.CPP.algorithm
libc.src.__support.FPUtil.fp_bits
)

Expand Down
11 changes: 6 additions & 5 deletions libc/test/src/math/smoke/FDimTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
//===---------------------------------------------------------------------===//

#include "src/__support/FPUtil/BasicOperations.h"
#include "src/__support/CPP/algorithm.h"
#include "src/__support/FPUtil/FPBits.h"
#include "test/UnitTest/FEnvSafeTest.h"
#include "test/UnitTest/FPMatcher.h"
Expand Down Expand Up @@ -61,10 +61,11 @@ class FDimTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
void test_in_range(FuncPtr func) {
constexpr StorageType STORAGE_MAX =
LIBC_NAMESPACE::cpp::numeric_limits<StorageType>::max();
constexpr StorageType COUNT = 100'001;
constexpr StorageType STEP = STORAGE_MAX / COUNT;
for (StorageType i = 0, v = 0, w = STORAGE_MAX; i <= COUNT;
++i, v += STEP, w -= STEP) {
constexpr int COUNT = 100'001;
constexpr StorageType STEP = LIBC_NAMESPACE::cpp::max(
static_cast<StorageType>(STORAGE_MAX / COUNT), StorageType(1));
StorageType v = 0, w = STORAGE_MAX;
for (int i = 0; i <= COUNT; ++i, v += STEP, w -= STEP) {
FPBits xbits(v), ybits(w);
if (xbits.is_inf_or_nan())
continue;
Expand Down
13 changes: 13 additions & 0 deletions libc/test/src/math/smoke/fdimf16_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//===-- Unittests for fdimf16 ---------------------------------------------===//
//
// 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 "FDimTest.h"

#include "src/math/fdimf16.h"

LIST_FDIM_TESTS(float16, LIBC_NAMESPACE::fdimf16);

0 comments on commit c537f35

Please sign in to comment.