Skip to content

[libc][math][c23] Add {ceil,floor,round,roundeven,trunc}f16 C23 math functions #94001

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions libc/config/linux/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,12 @@ set(TARGET_LIBM_ENTRYPOINTS
if(LIBC_TYPES_HAS_FLOAT16)
list(APPEND TARGET_LIBM_ENTRYPOINTS
# math.h C23 _Float16 entrypoints
libc.src.math.ceilf16
libc.src.math.fabsf16
libc.src.math.floorf16
libc.src.math.roundf16
libc.src.math.roundevenf16
libc.src.math.truncf16
)
endif()

Expand Down
5 changes: 5 additions & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,12 @@ set(TARGET_LIBM_ENTRYPOINTS
if(LIBC_TYPES_HAS_FLOAT16)
list(APPEND TARGET_LIBM_ENTRYPOINTS
# math.h C23 _Float16 entrypoints
libc.src.math.ceilf16
libc.src.math.fabsf16
libc.src.math.floorf16
libc.src.math.roundf16
libc.src.math.roundevenf16
libc.src.math.truncf16
)
endif()

Expand Down
10 changes: 5 additions & 5 deletions libc/docs/math/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ Basic Operations
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| <Func> | <Func_f> (float) | <Func> (double) | <Func_l> (long double) | <Func_f16> (float16) | <Func_f128> (float128) | C23 Definition Section | C23 Error Handling Section |
+==================+==================+=================+========================+======================+========================+========================+============================+
| ceil | |check| | |check| | |check| | | |check| | 7.12.9.1 | F.10.6.1 |
| ceil | |check| | |check| | |check| | |check| | |check| | 7.12.9.1 | F.10.6.1 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| canonicalize | |check| | |check| | |check| | | |check| | 7.12.11.7 | F.10.8.7 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
Expand All @@ -134,7 +134,7 @@ Basic Operations
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| ffma | N/A | | | N/A | | 7.12.14.5 | F.10.11 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| floor | |check| | |check| | |check| | | |check| | 7.12.9.2 | F.10.6.2 |
| floor | |check| | |check| | |check| | |check| | |check| | 7.12.9.2 | F.10.6.2 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| fmax | |check| | |check| | |check| | | |check| | 7.12.12.2 | F.10.9.2 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
Expand Down Expand Up @@ -204,13 +204,13 @@ Basic Operations
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| rint | |check| | |check| | |check| | | |check| | 7.12.9.4 | F.10.6.4 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| round | |check| | |check| | |check| | | |check| | 7.12.9.6 | F.10.6.6 |
| round | |check| | |check| | |check| | |check| | |check| | 7.12.9.6 | F.10.6.6 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| roundeven | |check| | |check| | |check| | | |check| | 7.12.9.8 | F.10.6.8 |
| roundeven | |check| | |check| | |check| | |check| | |check| | 7.12.9.8 | F.10.6.8 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| scalbn | |check| | |check| | |check| | | |check| | 7.12.6.19 | F.10.3.19 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| trunc | |check| | |check| | |check| | | |check| | 7.12.9.9 | F.10.6.9 |
| trunc | |check| | |check| | |check| | |check| | |check| | 7.12.9.9 | F.10.6.9 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| ufromfp | |check| | |check| | |check| | | |check| | 7.12.9.10 | F.10.6.10 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
Expand Down
10 changes: 10 additions & 0 deletions libc/spec/stdc.td
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ def StdC : StandardSpec<"stdc"> {
FunctionSpec<"ceil", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
FunctionSpec<"ceilf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
FunctionSpec<"ceill", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
GuardedFunctionSpec<"ceilf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
GuardedFunctionSpec<"ceilf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,

FunctionSpec<"fabs", RetValSpec<DoubleType>, [ArgSpec<DoubleType>], [ConstAttr]>,
Expand All @@ -406,6 +407,7 @@ def StdC : StandardSpec<"stdc"> {
FunctionSpec<"floor", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
FunctionSpec<"floorf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
FunctionSpec<"floorl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
GuardedFunctionSpec<"floorf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
GuardedFunctionSpec<"floorf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,

FunctionSpec<"fmin", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
Expand Down Expand Up @@ -565,8 +567,15 @@ def StdC : StandardSpec<"stdc"> {
FunctionSpec<"round", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
FunctionSpec<"roundf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
FunctionSpec<"roundl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
GuardedFunctionSpec<"roundf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
GuardedFunctionSpec<"roundf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,

FunctionSpec<"roundeven", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
FunctionSpec<"roundevenf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
FunctionSpec<"roundevenl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
GuardedFunctionSpec<"roundevenf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
GuardedFunctionSpec<"roundevenf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,

FunctionSpec<"lround", RetValSpec<LongType>, [ArgSpec<DoubleType>]>,
FunctionSpec<"lroundf", RetValSpec<LongType>, [ArgSpec<FloatType>]>,
FunctionSpec<"lroundl", RetValSpec<LongType>, [ArgSpec<LongDoubleType>]>,
Expand Down Expand Up @@ -600,6 +609,7 @@ def StdC : StandardSpec<"stdc"> {
FunctionSpec<"trunc", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
FunctionSpec<"truncf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
FunctionSpec<"truncl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
GuardedFunctionSpec<"truncf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
GuardedFunctionSpec<"truncf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,

FunctionSpec<"nearbyint", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
Expand Down
21 changes: 16 additions & 5 deletions libc/src/__support/FPUtil/NearestIntegerOperations.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace fputil {

template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
LIBC_INLINE T trunc(T x) {
using StorageType = typename FPBits<T>::StorageType;
FPBits<T> bits(x);

// If x is infinity or NaN, return it.
Expand All @@ -43,12 +44,15 @@ LIBC_INLINE T trunc(T x) {
return FPBits<T>::zero(bits.sign()).get_val();

int trim_size = FPBits<T>::FRACTION_LEN - exponent;
bits.set_mantissa((bits.get_mantissa() >> trim_size) << trim_size);
StorageType trunc_mantissa =
static_cast<StorageType>((bits.get_mantissa() >> trim_size) << trim_size);
bits.set_mantissa(trunc_mantissa);
Comment on lines +47 to +49
Copy link
Member Author

@overmighty overmighty May 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mantissa truncation is done multiple times in the file, so it could be extracted into a function, but doing it now would conflict with #93986.

return bits.get_val();
}

template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
LIBC_INLINE T ceil(T x) {
using StorageType = typename FPBits<T>::StorageType;
FPBits<T> bits(x);

// If x is infinity NaN or zero, return it.
Expand All @@ -71,7 +75,9 @@ LIBC_INLINE T ceil(T x) {
}

uint32_t trim_size = FPBits<T>::FRACTION_LEN - exponent;
bits.set_mantissa((bits.get_mantissa() >> trim_size) << trim_size);
StorageType trunc_mantissa =
static_cast<StorageType>((bits.get_mantissa() >> trim_size) << trim_size);
bits.set_mantissa(trunc_mantissa);
T trunc_value = bits.get_val();

// If x is already an integer, return it.
Expand Down Expand Up @@ -124,7 +130,9 @@ LIBC_INLINE T round(T x) {
uint32_t trim_size = FPBits<T>::FRACTION_LEN - exponent;
bool half_bit_set =
bool(bits.get_mantissa() & (StorageType(1) << (trim_size - 1)));
bits.set_mantissa((bits.get_mantissa() >> trim_size) << trim_size);
StorageType trunc_mantissa =
static_cast<StorageType>((bits.get_mantissa() >> trim_size) << trim_size);
bits.set_mantissa(trunc_mantissa);
T trunc_value = bits.get_val();

// If x is already an integer, return it.
Expand Down Expand Up @@ -181,7 +189,9 @@ round_using_specific_rounding_mode(T x, int rnd) {

uint32_t trim_size = FPBits<T>::FRACTION_LEN - exponent;
FPBits<T> new_bits = bits;
new_bits.set_mantissa((bits.get_mantissa() >> trim_size) << trim_size);
StorageType trunc_mantissa =
static_cast<StorageType>((bits.get_mantissa() >> trim_size) << trim_size);
new_bits.set_mantissa(trunc_mantissa);
T trunc_value = new_bits.get_val();

// If x is already an integer, return it.
Expand All @@ -190,7 +200,8 @@ round_using_specific_rounding_mode(T x, int rnd) {

StorageType trim_value =
bits.get_mantissa() & ((StorageType(1) << trim_size) - 1);
StorageType half_value = (StorageType(1) << (trim_size - 1));
StorageType half_value =
static_cast<StorageType>((StorageType(1) << (trim_size - 1)));
// If exponent is 0, trimSize will be equal to the mantissa width, and
// truncIsOdd` will not be correct. So, we handle it as a special case
// below.
Expand Down
5 changes: 5 additions & 0 deletions libc/src/math/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ add_math_entrypoint_object(canonicalizel)
add_math_entrypoint_object(ceil)
add_math_entrypoint_object(ceilf)
add_math_entrypoint_object(ceill)
add_math_entrypoint_object(ceilf16)
add_math_entrypoint_object(ceilf128)

add_math_entrypoint_object(copysign)
Expand Down Expand Up @@ -110,6 +111,7 @@ add_math_entrypoint_object(fdimf128)
add_math_entrypoint_object(floor)
add_math_entrypoint_object(floorf)
add_math_entrypoint_object(floorl)
add_math_entrypoint_object(floorf16)
add_math_entrypoint_object(floorf128)

add_math_entrypoint_object(fma)
Expand Down Expand Up @@ -293,11 +295,13 @@ add_math_entrypoint_object(rintf128)
add_math_entrypoint_object(round)
add_math_entrypoint_object(roundf)
add_math_entrypoint_object(roundl)
add_math_entrypoint_object(roundf16)
add_math_entrypoint_object(roundf128)

add_math_entrypoint_object(roundeven)
add_math_entrypoint_object(roundevenf)
add_math_entrypoint_object(roundevenl)
add_math_entrypoint_object(roundevenf16)
add_math_entrypoint_object(roundevenf128)

add_math_entrypoint_object(scalbn)
Expand Down Expand Up @@ -331,6 +335,7 @@ add_math_entrypoint_object(tgammaf)
add_math_entrypoint_object(trunc)
add_math_entrypoint_object(truncf)
add_math_entrypoint_object(truncl)
add_math_entrypoint_object(truncf16)
add_math_entrypoint_object(truncf128)

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

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

namespace LIBC_NAMESPACE {

float16 ceilf16(float16 x);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_MATH_CEILF16_H
20 changes: 20 additions & 0 deletions libc/src/math/floorf16.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation header for floorf16 ----------------------*- 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_FLOORF16_H
#define LLVM_LIBC_SRC_MATH_FLOORF16_H

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

namespace LIBC_NAMESPACE {

float16 floorf16(float16 x);

} // namespace LIBC_NAMESPACE

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

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

add_entrypoint_object(
ceilf128
SRCS
Expand Down Expand Up @@ -303,6 +316,19 @@ add_entrypoint_object(
libc.src.__support.FPUtil.nearest_integer_operations
)

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

add_entrypoint_object(
truncf128
SRCS
Expand Down Expand Up @@ -352,6 +378,19 @@ add_entrypoint_object(
libc.src.__support.FPUtil.nearest_integer_operations
)

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

add_entrypoint_object(
floorf128
SRCS
Expand Down Expand Up @@ -401,6 +440,19 @@ add_entrypoint_object(
libc.src.__support.FPUtil.nearest_integer_operations
)

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

add_entrypoint_object(
roundf128
SRCS
Expand Down Expand Up @@ -450,6 +502,19 @@ add_entrypoint_object(
libc.src.__support.FPUtil.nearest_integer_operations
)

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

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

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(float16, ceilf16, (float16 x)) { return fputil::ceil(x); }

} // namespace LIBC_NAMESPACE
17 changes: 17 additions & 0 deletions libc/src/math/generic/floorf16.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//===-- Implementation of floorf16 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/floorf16.h"
#include "src/__support/FPUtil/NearestIntegerOperations.h"
#include "src/__support/common.h"

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(float16, floorf16, (float16 x)) { return fputil::floor(x); }

} // namespace LIBC_NAMESPACE
Loading
Loading