Skip to content

[libc][math][c23] Add {fromfp,fromfpx,ufromfp,ufromfpx}f16 C23 math functions #94254

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 4 commits into from
Jun 4, 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
4 changes: 4 additions & 0 deletions libc/config/linux/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,8 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.ceilf16
libc.src.math.fabsf16
libc.src.math.floorf16
libc.src.math.fromfpf16
libc.src.math.fromfpxf16
libc.src.math.llrintf16
libc.src.math.llroundf16
libc.src.math.lrintf16
Expand All @@ -511,6 +513,8 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.roundf16
libc.src.math.roundevenf16
libc.src.math.truncf16
libc.src.math.ufromfpf16
libc.src.math.ufromfpxf16
)
endif()

Expand Down
4 changes: 4 additions & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,8 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.ceilf16
libc.src.math.fabsf16
libc.src.math.floorf16
libc.src.math.fromfpf16
libc.src.math.fromfpxf16
libc.src.math.llrintf16
libc.src.math.llroundf16
libc.src.math.lrintf16
Expand All @@ -544,6 +546,8 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.roundf16
libc.src.math.roundevenf16
libc.src.math.truncf16
libc.src.math.ufromfpf16
libc.src.math.ufromfpxf16
)
endif()

Expand Down
7 changes: 4 additions & 3 deletions libc/docs/c23.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ Additions:
* powr*
* rootn*
* roundeven* |check|
* fromfp*
* ufromfp*
* fromfpx*
* fromfp* |check|
* ufromfp* |check|
* fromfpx* |check|
* ufromfpx* |check|
* nextup*
* nextdown*
* canonicalize*
Expand Down
8 changes: 4 additions & 4 deletions libc/docs/math/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,9 @@ Basic Operations
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| frexp | |check| | |check| | |check| | | |check| | 7.12.6.7 | F.10.3.7 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| fromfp | |check| | |check| | |check| | | |check| | 7.12.9.10 | F.10.6.10 |
| fromfp | |check| | |check| | |check| | |check| | |check| | 7.12.9.10 | F.10.6.10 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| fromfpx | |check| | |check| | |check| | | |check| | 7.12.9.11 | F.10.6.11 |
| fromfpx | |check| | |check| | |check| | |check| | |check| | 7.12.9.11 | F.10.6.11 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| fsub | N/A | | | N/A | | 7.12.14.2 | F.10.11 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
Expand Down Expand Up @@ -212,9 +212,9 @@ Basic Operations
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| 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 |
| ufromfp | |check| | |check| | |check| | |check| | |check| | 7.12.9.10 | F.10.6.10 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| ufromfpx | |check| | |check| | |check| | | |check| | 7.12.9.11 | F.10.6.11 |
| ufromfpx | |check| | |check| | |check| | |check| | |check| | 7.12.9.11 | F.10.6.11 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+


Expand Down
12 changes: 8 additions & 4 deletions libc/spec/stdc.td
Original file line number Diff line number Diff line change
Expand Up @@ -476,22 +476,26 @@ def StdC : StandardSpec<"stdc"> {
FunctionSpec<"fromfp", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>]>,
FunctionSpec<"fromfpf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>]>,
FunctionSpec<"fromfpl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>]>,
GuardedFunctionSpec<"fromfpf128", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>], "LIBC_TYPES_HAS_FLOAT128">,
GuardedFunctionSpec<"fromfpf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>], "LIBC_TYPES_HAS_FLOAT16">,
GuardedFunctionSpec<"fromfpf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>], "LIBC_TYPES_HAS_FLOAT128">,

FunctionSpec<"fromfpx", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>]>,
FunctionSpec<"fromfpxf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>]>,
FunctionSpec<"fromfpxl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>]>,
GuardedFunctionSpec<"fromfpxf128", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>], "LIBC_TYPES_HAS_FLOAT128">,
GuardedFunctionSpec<"fromfpxf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>], "LIBC_TYPES_HAS_FLOAT16">,
GuardedFunctionSpec<"fromfpxf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>], "LIBC_TYPES_HAS_FLOAT128">,

FunctionSpec<"ufromfp", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>]>,
FunctionSpec<"ufromfpf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>]>,
FunctionSpec<"ufromfpl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>]>,
GuardedFunctionSpec<"ufromfpf128", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>], "LIBC_TYPES_HAS_FLOAT128">,
GuardedFunctionSpec<"ufromfpf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>], "LIBC_TYPES_HAS_FLOAT16">,
GuardedFunctionSpec<"ufromfpf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>], "LIBC_TYPES_HAS_FLOAT128">,

FunctionSpec<"ufromfpx", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>]>,
FunctionSpec<"ufromfpxf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>]>,
FunctionSpec<"ufromfpxl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>]>,
GuardedFunctionSpec<"ufromfpxf128", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>], "LIBC_TYPES_HAS_FLOAT128">,
GuardedFunctionSpec<"ufromfpxf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>], "LIBC_TYPES_HAS_FLOAT16">,
GuardedFunctionSpec<"ufromfpxf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>], "LIBC_TYPES_HAS_FLOAT128">,

FunctionSpec<"hypot", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
FunctionSpec<"hypotf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>]>,
Expand Down
5 changes: 3 additions & 2 deletions libc/src/__support/FPUtil/NearestIntegerOperations.h
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,8 @@ fromfp(T x, int rnd, unsigned int width) {
if (width - 1 > FPBits<T>::EXP_BIAS)
return rounded_value;

StorageType range_exp = width - 1U + FPBits<T>::EXP_BIAS;
StorageType range_exp =
static_cast<StorageType>(width - 1 + FPBits<T>::EXP_BIAS);
// rounded_value < -2^(width - 1)
T range_min =
FPBits<T>::create_value(Sign::NEG, range_exp, EXPLICIT_BIT).get_val();
Expand Down Expand Up @@ -311,7 +312,7 @@ fromfp(T x, int rnd, unsigned int width) {
if (width > FPBits<T>::EXP_BIAS)
return rounded_value;

StorageType range_exp = width + FPBits<T>::EXP_BIAS;
StorageType range_exp = static_cast<StorageType>(width + FPBits<T>::EXP_BIAS);
// rounded_value > 2^width - 1
T range_max =
FPBits<T>::create_value(Sign::POS, range_exp, EXPLICIT_BIT).get_val() -
Expand Down
4 changes: 4 additions & 0 deletions libc/src/math/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,13 @@ add_math_entrypoint_object(frexpf128)
add_math_entrypoint_object(fromfp)
add_math_entrypoint_object(fromfpf)
add_math_entrypoint_object(fromfpl)
add_math_entrypoint_object(fromfpf16)
add_math_entrypoint_object(fromfpf128)

add_math_entrypoint_object(fromfpx)
add_math_entrypoint_object(fromfpxf)
add_math_entrypoint_object(fromfpxl)
add_math_entrypoint_object(fromfpxf16)
add_math_entrypoint_object(fromfpxf128)

add_math_entrypoint_object(hypot)
Expand Down Expand Up @@ -347,9 +349,11 @@ add_math_entrypoint_object(truncf128)
add_math_entrypoint_object(ufromfp)
add_math_entrypoint_object(ufromfpf)
add_math_entrypoint_object(ufromfpl)
add_math_entrypoint_object(ufromfpf16)
add_math_entrypoint_object(ufromfpf128)

add_math_entrypoint_object(ufromfpx)
add_math_entrypoint_object(ufromfpxf)
add_math_entrypoint_object(ufromfpxl)
add_math_entrypoint_object(ufromfpxf16)
add_math_entrypoint_object(ufromfpxf128)
20 changes: 20 additions & 0 deletions libc/src/math/fromfpf16.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation header for fromfpf16 ---------------------*- 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_FROMFPF16_H
#define LLVM_LIBC_SRC_MATH_FROMFPF16_H

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

namespace LIBC_NAMESPACE {

float16 fromfpf16(float16 x, int rnd, unsigned int width);

} // namespace LIBC_NAMESPACE

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

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

namespace LIBC_NAMESPACE {

float16 fromfpxf16(float16 x, int rnd, unsigned int width);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_MATH_FROMFPXF16_H
84 changes: 68 additions & 16 deletions libc/src/math/generic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2686,7 +2686,7 @@ add_entrypoint_object(
HDRS
../fromfp.h
DEPENDS
libc.src.__support.FPUtil.nearest_integer
libc.src.__support.FPUtil.nearest_integer_operations
COMPILE_OPTIONS
-O3
)
Expand All @@ -2698,7 +2698,7 @@ add_entrypoint_object(
HDRS
../fromfpf.h
DEPENDS
libc.src.__support.FPUtil.nearest_integer
libc.src.__support.FPUtil.nearest_integer_operations
COMPILE_OPTIONS
-O3
)
Expand All @@ -2710,7 +2710,20 @@ add_entrypoint_object(
HDRS
../fromfpl.h
DEPENDS
libc.src.__support.FPUtil.nearest_integer
libc.src.__support.FPUtil.nearest_integer_operations
COMPILE_OPTIONS
-O3
)

add_entrypoint_object(
fromfpf16
SRCS
fromfpf16.cpp
HDRS
../fromfpf16.h
DEPENDS
libc.src.__support.macros.properties.types
libc.src.__support.FPUtil.nearest_integer_operations
COMPILE_OPTIONS
-O3
)
Expand All @@ -2723,7 +2736,7 @@ add_entrypoint_object(
../fromfpf128.h
DEPENDS
libc.src.__support.macros.properties.types
libc.src.__support.FPUtil.nearest_integer
libc.src.__support.FPUtil.nearest_integer_operations
COMPILE_OPTIONS
-O3
)
Expand All @@ -2735,7 +2748,7 @@ add_entrypoint_object(
HDRS
../fromfpx.h
DEPENDS
libc.src.__support.FPUtil.nearest_integer
libc.src.__support.FPUtil.nearest_integer_operations
COMPILE_OPTIONS
-O3
)
Expand All @@ -2747,7 +2760,7 @@ add_entrypoint_object(
HDRS
../fromfpxf.h
DEPENDS
libc.src.__support.FPUtil.nearest_integer
libc.src.__support.FPUtil.nearest_integer_operations
COMPILE_OPTIONS
-O3
)
Expand All @@ -2759,7 +2772,20 @@ add_entrypoint_object(
HDRS
../fromfpxl.h
DEPENDS
libc.src.__support.FPUtil.nearest_integer
libc.src.__support.FPUtil.nearest_integer_operations
COMPILE_OPTIONS
-O3
)

add_entrypoint_object(
fromfpxf16
SRCS
fromfpxf16.cpp
HDRS
../fromfpxf16.h
DEPENDS
libc.src.__support.macros.properties.types
libc.src.__support.FPUtil.nearest_integer_operations
COMPILE_OPTIONS
-O3
)
Expand All @@ -2772,7 +2798,7 @@ add_entrypoint_object(
../fromfpxf128.h
DEPENDS
libc.src.__support.macros.properties.types
libc.src.__support.FPUtil.nearest_integer
libc.src.__support.FPUtil.nearest_integer_operations
COMPILE_OPTIONS
-O3
)
Expand All @@ -2784,7 +2810,7 @@ add_entrypoint_object(
HDRS
../ufromfp.h
DEPENDS
libc.src.__support.FPUtil.nearest_integer
libc.src.__support.FPUtil.nearest_integer_operations
COMPILE_OPTIONS
-O3
)
Expand All @@ -2796,7 +2822,7 @@ add_entrypoint_object(
HDRS
../ufromfpf.h
DEPENDS
libc.src.__support.FPUtil.nearest_integer
libc.src.__support.FPUtil.nearest_integer_operations
COMPILE_OPTIONS
-O3
)
Expand All @@ -2808,7 +2834,20 @@ add_entrypoint_object(
HDRS
../ufromfpl.h
DEPENDS
libc.src.__support.FPUtil.nearest_integer
libc.src.__support.FPUtil.nearest_integer_operations
COMPILE_OPTIONS
-O3
)

add_entrypoint_object(
ufromfpf16
SRCS
ufromfpf16.cpp
HDRS
../ufromfpf16.h
DEPENDS
libc.src.__support.macros.properties.types
libc.src.__support.FPUtil.nearest_integer_operations
COMPILE_OPTIONS
-O3
)
Expand All @@ -2821,7 +2860,7 @@ add_entrypoint_object(
../ufromfpf128.h
DEPENDS
libc.src.__support.macros.properties.types
libc.src.__support.FPUtil.nearest_integer
libc.src.__support.FPUtil.nearest_integer_operations
COMPILE_OPTIONS
-O3
)
Expand All @@ -2833,7 +2872,7 @@ add_entrypoint_object(
HDRS
../ufromfpx.h
DEPENDS
libc.src.__support.FPUtil.nearest_integer
libc.src.__support.FPUtil.nearest_integer_operations
COMPILE_OPTIONS
-O3
)
Expand All @@ -2845,7 +2884,7 @@ add_entrypoint_object(
HDRS
../ufromfpxf.h
DEPENDS
libc.src.__support.FPUtil.nearest_integer
libc.src.__support.FPUtil.nearest_integer_operations
COMPILE_OPTIONS
-O3
)
Expand All @@ -2857,7 +2896,20 @@ add_entrypoint_object(
HDRS
../ufromfpxl.h
DEPENDS
libc.src.__support.FPUtil.nearest_integer
libc.src.__support.FPUtil.nearest_integer_operations
COMPILE_OPTIONS
-O3
)

add_entrypoint_object(
ufromfpxf16
SRCS
ufromfpxf16.cpp
HDRS
../ufromfpxf16.h
DEPENDS
libc.src.__support.macros.properties.types
libc.src.__support.FPUtil.nearest_integer_operations
COMPILE_OPTIONS
-O3
)
Expand All @@ -2870,7 +2922,7 @@ add_entrypoint_object(
../ufromfpxf128.h
DEPENDS
libc.src.__support.macros.properties.types
libc.src.__support.FPUtil.nearest_integer
libc.src.__support.FPUtil.nearest_integer_operations
COMPILE_OPTIONS
-O3
)
Expand Down
Loading