Skip to content

Commit 6b5ae14

Browse files
authored
[libc][math][c23] Add {fromfp,fromfpx,ufromfp,ufromfpx}f16 C23 math functions (#94254)
#93566
1 parent ae4f300 commit 6b5ae14

25 files changed

+543
-223
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,8 @@ if(LIBC_TYPES_HAS_FLOAT16)
502502
libc.src.math.ceilf16
503503
libc.src.math.fabsf16
504504
libc.src.math.floorf16
505+
libc.src.math.fromfpf16
506+
libc.src.math.fromfpxf16
505507
libc.src.math.llrintf16
506508
libc.src.math.llroundf16
507509
libc.src.math.lrintf16
@@ -511,6 +513,8 @@ if(LIBC_TYPES_HAS_FLOAT16)
511513
libc.src.math.roundf16
512514
libc.src.math.roundevenf16
513515
libc.src.math.truncf16
516+
libc.src.math.ufromfpf16
517+
libc.src.math.ufromfpxf16
514518
)
515519
endif()
516520

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,8 @@ if(LIBC_TYPES_HAS_FLOAT16)
535535
libc.src.math.ceilf16
536536
libc.src.math.fabsf16
537537
libc.src.math.floorf16
538+
libc.src.math.fromfpf16
539+
libc.src.math.fromfpxf16
538540
libc.src.math.llrintf16
539541
libc.src.math.llroundf16
540542
libc.src.math.lrintf16
@@ -544,6 +546,8 @@ if(LIBC_TYPES_HAS_FLOAT16)
544546
libc.src.math.roundf16
545547
libc.src.math.roundevenf16
546548
libc.src.math.truncf16
549+
libc.src.math.ufromfpf16
550+
libc.src.math.ufromfpxf16
547551
)
548552
endif()
549553

libc/docs/c23.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,10 @@ Additions:
5555
* powr*
5656
* rootn*
5757
* roundeven* |check|
58-
* fromfp*
59-
* ufromfp*
60-
* fromfpx*
58+
* fromfp* |check|
59+
* ufromfp* |check|
60+
* fromfpx* |check|
61+
* ufromfpx* |check|
6162
* nextup*
6263
* nextdown*
6364
* canonicalize*

libc/docs/math/index.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,9 @@ Basic Operations
162162
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
163163
| frexp | |check| | |check| | |check| | | |check| | 7.12.6.7 | F.10.3.7 |
164164
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
165-
| fromfp | |check| | |check| | |check| | | |check| | 7.12.9.10 | F.10.6.10 |
165+
| fromfp | |check| | |check| | |check| | |check| | |check| | 7.12.9.10 | F.10.6.10 |
166166
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
167-
| fromfpx | |check| | |check| | |check| | | |check| | 7.12.9.11 | F.10.6.11 |
167+
| fromfpx | |check| | |check| | |check| | |check| | |check| | 7.12.9.11 | F.10.6.11 |
168168
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
169169
| fsub | N/A | | | N/A | | 7.12.14.2 | F.10.11 |
170170
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
@@ -212,9 +212,9 @@ Basic Operations
212212
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
213213
| trunc | |check| | |check| | |check| | |check| | |check| | 7.12.9.9 | F.10.6.9 |
214214
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
215-
| ufromfp | |check| | |check| | |check| | | |check| | 7.12.9.10 | F.10.6.10 |
215+
| ufromfp | |check| | |check| | |check| | |check| | |check| | 7.12.9.10 | F.10.6.10 |
216216
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
217-
| ufromfpx | |check| | |check| | |check| | | |check| | 7.12.9.11 | F.10.6.11 |
217+
| ufromfpx | |check| | |check| | |check| | |check| | |check| | 7.12.9.11 | F.10.6.11 |
218218
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
219219

220220

libc/spec/stdc.td

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -476,22 +476,26 @@ def StdC : StandardSpec<"stdc"> {
476476
FunctionSpec<"fromfp", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>]>,
477477
FunctionSpec<"fromfpf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>]>,
478478
FunctionSpec<"fromfpl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>]>,
479-
GuardedFunctionSpec<"fromfpf128", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>], "LIBC_TYPES_HAS_FLOAT128">,
479+
GuardedFunctionSpec<"fromfpf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>], "LIBC_TYPES_HAS_FLOAT16">,
480+
GuardedFunctionSpec<"fromfpf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>], "LIBC_TYPES_HAS_FLOAT128">,
480481

481482
FunctionSpec<"fromfpx", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>]>,
482483
FunctionSpec<"fromfpxf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>]>,
483484
FunctionSpec<"fromfpxl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>]>,
484-
GuardedFunctionSpec<"fromfpxf128", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>], "LIBC_TYPES_HAS_FLOAT128">,
485+
GuardedFunctionSpec<"fromfpxf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>], "LIBC_TYPES_HAS_FLOAT16">,
486+
GuardedFunctionSpec<"fromfpxf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>], "LIBC_TYPES_HAS_FLOAT128">,
485487

486488
FunctionSpec<"ufromfp", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>]>,
487489
FunctionSpec<"ufromfpf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>]>,
488490
FunctionSpec<"ufromfpl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>]>,
489-
GuardedFunctionSpec<"ufromfpf128", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>], "LIBC_TYPES_HAS_FLOAT128">,
491+
GuardedFunctionSpec<"ufromfpf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>], "LIBC_TYPES_HAS_FLOAT16">,
492+
GuardedFunctionSpec<"ufromfpf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>], "LIBC_TYPES_HAS_FLOAT128">,
490493

491494
FunctionSpec<"ufromfpx", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>]>,
492495
FunctionSpec<"ufromfpxf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>]>,
493496
FunctionSpec<"ufromfpxl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>]>,
494-
GuardedFunctionSpec<"ufromfpxf128", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>], "LIBC_TYPES_HAS_FLOAT128">,
497+
GuardedFunctionSpec<"ufromfpxf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>], "LIBC_TYPES_HAS_FLOAT16">,
498+
GuardedFunctionSpec<"ufromfpxf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>], "LIBC_TYPES_HAS_FLOAT128">,
495499

496500
FunctionSpec<"hypot", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
497501
FunctionSpec<"hypotf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>]>,

libc/src/__support/FPUtil/NearestIntegerOperations.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,8 @@ fromfp(T x, int rnd, unsigned int width) {
282282
if (width - 1 > FPBits<T>::EXP_BIAS)
283283
return rounded_value;
284284

285-
StorageType range_exp = width - 1U + FPBits<T>::EXP_BIAS;
285+
StorageType range_exp =
286+
static_cast<StorageType>(width - 1 + FPBits<T>::EXP_BIAS);
286287
// rounded_value < -2^(width - 1)
287288
T range_min =
288289
FPBits<T>::create_value(Sign::NEG, range_exp, EXPLICIT_BIT).get_val();
@@ -311,7 +312,7 @@ fromfp(T x, int rnd, unsigned int width) {
311312
if (width > FPBits<T>::EXP_BIAS)
312313
return rounded_value;
313314

314-
StorageType range_exp = width + FPBits<T>::EXP_BIAS;
315+
StorageType range_exp = static_cast<StorageType>(width + FPBits<T>::EXP_BIAS);
315316
// rounded_value > 2^width - 1
316317
T range_max =
317318
FPBits<T>::create_value(Sign::POS, range_exp, EXPLICIT_BIT).get_val() -

libc/src/math/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,13 @@ add_math_entrypoint_object(frexpf128)
180180
add_math_entrypoint_object(fromfp)
181181
add_math_entrypoint_object(fromfpf)
182182
add_math_entrypoint_object(fromfpl)
183+
add_math_entrypoint_object(fromfpf16)
183184
add_math_entrypoint_object(fromfpf128)
184185

185186
add_math_entrypoint_object(fromfpx)
186187
add_math_entrypoint_object(fromfpxf)
187188
add_math_entrypoint_object(fromfpxl)
189+
add_math_entrypoint_object(fromfpxf16)
188190
add_math_entrypoint_object(fromfpxf128)
189191

190192
add_math_entrypoint_object(hypot)
@@ -347,9 +349,11 @@ add_math_entrypoint_object(truncf128)
347349
add_math_entrypoint_object(ufromfp)
348350
add_math_entrypoint_object(ufromfpf)
349351
add_math_entrypoint_object(ufromfpl)
352+
add_math_entrypoint_object(ufromfpf16)
350353
add_math_entrypoint_object(ufromfpf128)
351354

352355
add_math_entrypoint_object(ufromfpx)
353356
add_math_entrypoint_object(ufromfpxf)
354357
add_math_entrypoint_object(ufromfpxl)
358+
add_math_entrypoint_object(ufromfpxf16)
355359
add_math_entrypoint_object(ufromfpxf128)

libc/src/math/fromfpf16.h

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

libc/src/math/fromfpxf16.h

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

libc/src/math/generic/CMakeLists.txt

Lines changed: 68 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2686,7 +2686,7 @@ add_entrypoint_object(
26862686
HDRS
26872687
../fromfp.h
26882688
DEPENDS
2689-
libc.src.__support.FPUtil.nearest_integer
2689+
libc.src.__support.FPUtil.nearest_integer_operations
26902690
COMPILE_OPTIONS
26912691
-O3
26922692
)
@@ -2698,7 +2698,7 @@ add_entrypoint_object(
26982698
HDRS
26992699
../fromfpf.h
27002700
DEPENDS
2701-
libc.src.__support.FPUtil.nearest_integer
2701+
libc.src.__support.FPUtil.nearest_integer_operations
27022702
COMPILE_OPTIONS
27032703
-O3
27042704
)
@@ -2710,7 +2710,20 @@ add_entrypoint_object(
27102710
HDRS
27112711
../fromfpl.h
27122712
DEPENDS
2713-
libc.src.__support.FPUtil.nearest_integer
2713+
libc.src.__support.FPUtil.nearest_integer_operations
2714+
COMPILE_OPTIONS
2715+
-O3
2716+
)
2717+
2718+
add_entrypoint_object(
2719+
fromfpf16
2720+
SRCS
2721+
fromfpf16.cpp
2722+
HDRS
2723+
../fromfpf16.h
2724+
DEPENDS
2725+
libc.src.__support.macros.properties.types
2726+
libc.src.__support.FPUtil.nearest_integer_operations
27142727
COMPILE_OPTIONS
27152728
-O3
27162729
)
@@ -2723,7 +2736,7 @@ add_entrypoint_object(
27232736
../fromfpf128.h
27242737
DEPENDS
27252738
libc.src.__support.macros.properties.types
2726-
libc.src.__support.FPUtil.nearest_integer
2739+
libc.src.__support.FPUtil.nearest_integer_operations
27272740
COMPILE_OPTIONS
27282741
-O3
27292742
)
@@ -2735,7 +2748,7 @@ add_entrypoint_object(
27352748
HDRS
27362749
../fromfpx.h
27372750
DEPENDS
2738-
libc.src.__support.FPUtil.nearest_integer
2751+
libc.src.__support.FPUtil.nearest_integer_operations
27392752
COMPILE_OPTIONS
27402753
-O3
27412754
)
@@ -2747,7 +2760,7 @@ add_entrypoint_object(
27472760
HDRS
27482761
../fromfpxf.h
27492762
DEPENDS
2750-
libc.src.__support.FPUtil.nearest_integer
2763+
libc.src.__support.FPUtil.nearest_integer_operations
27512764
COMPILE_OPTIONS
27522765
-O3
27532766
)
@@ -2759,7 +2772,20 @@ add_entrypoint_object(
27592772
HDRS
27602773
../fromfpxl.h
27612774
DEPENDS
2762-
libc.src.__support.FPUtil.nearest_integer
2775+
libc.src.__support.FPUtil.nearest_integer_operations
2776+
COMPILE_OPTIONS
2777+
-O3
2778+
)
2779+
2780+
add_entrypoint_object(
2781+
fromfpxf16
2782+
SRCS
2783+
fromfpxf16.cpp
2784+
HDRS
2785+
../fromfpxf16.h
2786+
DEPENDS
2787+
libc.src.__support.macros.properties.types
2788+
libc.src.__support.FPUtil.nearest_integer_operations
27632789
COMPILE_OPTIONS
27642790
-O3
27652791
)
@@ -2772,7 +2798,7 @@ add_entrypoint_object(
27722798
../fromfpxf128.h
27732799
DEPENDS
27742800
libc.src.__support.macros.properties.types
2775-
libc.src.__support.FPUtil.nearest_integer
2801+
libc.src.__support.FPUtil.nearest_integer_operations
27762802
COMPILE_OPTIONS
27772803
-O3
27782804
)
@@ -2784,7 +2810,7 @@ add_entrypoint_object(
27842810
HDRS
27852811
../ufromfp.h
27862812
DEPENDS
2787-
libc.src.__support.FPUtil.nearest_integer
2813+
libc.src.__support.FPUtil.nearest_integer_operations
27882814
COMPILE_OPTIONS
27892815
-O3
27902816
)
@@ -2796,7 +2822,7 @@ add_entrypoint_object(
27962822
HDRS
27972823
../ufromfpf.h
27982824
DEPENDS
2799-
libc.src.__support.FPUtil.nearest_integer
2825+
libc.src.__support.FPUtil.nearest_integer_operations
28002826
COMPILE_OPTIONS
28012827
-O3
28022828
)
@@ -2808,7 +2834,20 @@ add_entrypoint_object(
28082834
HDRS
28092835
../ufromfpl.h
28102836
DEPENDS
2811-
libc.src.__support.FPUtil.nearest_integer
2837+
libc.src.__support.FPUtil.nearest_integer_operations
2838+
COMPILE_OPTIONS
2839+
-O3
2840+
)
2841+
2842+
add_entrypoint_object(
2843+
ufromfpf16
2844+
SRCS
2845+
ufromfpf16.cpp
2846+
HDRS
2847+
../ufromfpf16.h
2848+
DEPENDS
2849+
libc.src.__support.macros.properties.types
2850+
libc.src.__support.FPUtil.nearest_integer_operations
28122851
COMPILE_OPTIONS
28132852
-O3
28142853
)
@@ -2821,7 +2860,7 @@ add_entrypoint_object(
28212860
../ufromfpf128.h
28222861
DEPENDS
28232862
libc.src.__support.macros.properties.types
2824-
libc.src.__support.FPUtil.nearest_integer
2863+
libc.src.__support.FPUtil.nearest_integer_operations
28252864
COMPILE_OPTIONS
28262865
-O3
28272866
)
@@ -2833,7 +2872,7 @@ add_entrypoint_object(
28332872
HDRS
28342873
../ufromfpx.h
28352874
DEPENDS
2836-
libc.src.__support.FPUtil.nearest_integer
2875+
libc.src.__support.FPUtil.nearest_integer_operations
28372876
COMPILE_OPTIONS
28382877
-O3
28392878
)
@@ -2845,7 +2884,7 @@ add_entrypoint_object(
28452884
HDRS
28462885
../ufromfpxf.h
28472886
DEPENDS
2848-
libc.src.__support.FPUtil.nearest_integer
2887+
libc.src.__support.FPUtil.nearest_integer_operations
28492888
COMPILE_OPTIONS
28502889
-O3
28512890
)
@@ -2857,7 +2896,20 @@ add_entrypoint_object(
28572896
HDRS
28582897
../ufromfpxl.h
28592898
DEPENDS
2860-
libc.src.__support.FPUtil.nearest_integer
2899+
libc.src.__support.FPUtil.nearest_integer_operations
2900+
COMPILE_OPTIONS
2901+
-O3
2902+
)
2903+
2904+
add_entrypoint_object(
2905+
ufromfpxf16
2906+
SRCS
2907+
ufromfpxf16.cpp
2908+
HDRS
2909+
../ufromfpxf16.h
2910+
DEPENDS
2911+
libc.src.__support.macros.properties.types
2912+
libc.src.__support.FPUtil.nearest_integer_operations
28612913
COMPILE_OPTIONS
28622914
-O3
28632915
)
@@ -2870,7 +2922,7 @@ add_entrypoint_object(
28702922
../ufromfpxf128.h
28712923
DEPENDS
28722924
libc.src.__support.macros.properties.types
2873-
libc.src.__support.FPUtil.nearest_integer
2925+
libc.src.__support.FPUtil.nearest_integer_operations
28742926
COMPILE_OPTIONS
28752927
-O3
28762928
)

0 commit comments

Comments
 (0)