Skip to content

Commit 25b037b

Browse files
authored
[libc][math][c23] Add {ceil,floor,round,roundeven,trunc}f16 C23 math functions (#94001)
1 parent 2ee7f49 commit 25b037b

28 files changed

+443
-30
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,12 @@ set(TARGET_LIBM_ENTRYPOINTS
499499
if(LIBC_TYPES_HAS_FLOAT16)
500500
list(APPEND TARGET_LIBM_ENTRYPOINTS
501501
# math.h C23 _Float16 entrypoints
502+
libc.src.math.ceilf16
502503
libc.src.math.fabsf16
504+
libc.src.math.floorf16
505+
libc.src.math.roundf16
506+
libc.src.math.roundevenf16
507+
libc.src.math.truncf16
503508
)
504509
endif()
505510

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,12 @@ set(TARGET_LIBM_ENTRYPOINTS
532532
if(LIBC_TYPES_HAS_FLOAT16)
533533
list(APPEND TARGET_LIBM_ENTRYPOINTS
534534
# math.h C23 _Float16 entrypoints
535+
libc.src.math.ceilf16
535536
libc.src.math.fabsf16
537+
libc.src.math.floorf16
538+
libc.src.math.roundf16
539+
libc.src.math.roundevenf16
540+
libc.src.math.truncf16
536541
)
537542
endif()
538543

libc/docs/math/index.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ Basic Operations
108108
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
109109
| <Func> | <Func_f> (float) | <Func> (double) | <Func_l> (long double) | <Func_f16> (float16) | <Func_f128> (float128) | C23 Definition Section | C23 Error Handling Section |
110110
+==================+==================+=================+========================+======================+========================+========================+============================+
111-
| ceil | |check| | |check| | |check| | | |check| | 7.12.9.1 | F.10.6.1 |
111+
| ceil | |check| | |check| | |check| | |check| | |check| | 7.12.9.1 | F.10.6.1 |
112112
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
113113
| canonicalize | |check| | |check| | |check| | | |check| | 7.12.11.7 | F.10.8.7 |
114114
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
@@ -134,7 +134,7 @@ Basic Operations
134134
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
135135
| ffma | N/A | | | N/A | | 7.12.14.5 | F.10.11 |
136136
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
137-
| floor | |check| | |check| | |check| | | |check| | 7.12.9.2 | F.10.6.2 |
137+
| floor | |check| | |check| | |check| | |check| | |check| | 7.12.9.2 | F.10.6.2 |
138138
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
139139
| fmax | |check| | |check| | |check| | | |check| | 7.12.12.2 | F.10.9.2 |
140140
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
@@ -204,13 +204,13 @@ Basic Operations
204204
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
205205
| rint | |check| | |check| | |check| | | |check| | 7.12.9.4 | F.10.6.4 |
206206
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
207-
| round | |check| | |check| | |check| | | |check| | 7.12.9.6 | F.10.6.6 |
207+
| round | |check| | |check| | |check| | |check| | |check| | 7.12.9.6 | F.10.6.6 |
208208
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
209-
| roundeven | |check| | |check| | |check| | | |check| | 7.12.9.8 | F.10.6.8 |
209+
| roundeven | |check| | |check| | |check| | |check| | |check| | 7.12.9.8 | F.10.6.8 |
210210
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
211211
| scalbn | |check| | |check| | |check| | | |check| | 7.12.6.19 | F.10.3.19 |
212212
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
213-
| trunc | |check| | |check| | |check| | | |check| | 7.12.9.9 | F.10.6.9 |
213+
| trunc | |check| | |check| | |check| | |check| | |check| | 7.12.9.9 | F.10.6.9 |
214214
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
215215
| ufromfp | |check| | |check| | |check| | | |check| | 7.12.9.10 | F.10.6.10 |
216216
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+

libc/spec/stdc.td

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,7 @@ def StdC : StandardSpec<"stdc"> {
390390
FunctionSpec<"ceil", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
391391
FunctionSpec<"ceilf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
392392
FunctionSpec<"ceill", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
393+
GuardedFunctionSpec<"ceilf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
393394
GuardedFunctionSpec<"ceilf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,
394395

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

411413
FunctionSpec<"fmin", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
@@ -565,8 +567,15 @@ def StdC : StandardSpec<"stdc"> {
565567
FunctionSpec<"round", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
566568
FunctionSpec<"roundf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
567569
FunctionSpec<"roundl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
570+
GuardedFunctionSpec<"roundf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
568571
GuardedFunctionSpec<"roundf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,
569572

573+
FunctionSpec<"roundeven", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
574+
FunctionSpec<"roundevenf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
575+
FunctionSpec<"roundevenl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
576+
GuardedFunctionSpec<"roundevenf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
577+
GuardedFunctionSpec<"roundevenf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,
578+
570579
FunctionSpec<"lround", RetValSpec<LongType>, [ArgSpec<DoubleType>]>,
571580
FunctionSpec<"lroundf", RetValSpec<LongType>, [ArgSpec<FloatType>]>,
572581
FunctionSpec<"lroundl", RetValSpec<LongType>, [ArgSpec<LongDoubleType>]>,
@@ -600,6 +609,7 @@ def StdC : StandardSpec<"stdc"> {
600609
FunctionSpec<"trunc", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
601610
FunctionSpec<"truncf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
602611
FunctionSpec<"truncl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
612+
GuardedFunctionSpec<"truncf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
603613
GuardedFunctionSpec<"truncf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,
604614

605615
FunctionSpec<"nearbyint", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,

libc/src/__support/FPUtil/NearestIntegerOperations.h

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ namespace fputil {
2222

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

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

4546
int trim_size = FPBits<T>::FRACTION_LEN - exponent;
46-
bits.set_mantissa((bits.get_mantissa() >> trim_size) << trim_size);
47+
StorageType trunc_mantissa =
48+
static_cast<StorageType>((bits.get_mantissa() >> trim_size) << trim_size);
49+
bits.set_mantissa(trunc_mantissa);
4750
return bits.get_val();
4851
}
4952

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

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

7377
uint32_t trim_size = FPBits<T>::FRACTION_LEN - exponent;
74-
bits.set_mantissa((bits.get_mantissa() >> trim_size) << trim_size);
78+
StorageType trunc_mantissa =
79+
static_cast<StorageType>((bits.get_mantissa() >> trim_size) << trim_size);
80+
bits.set_mantissa(trunc_mantissa);
7581
T trunc_value = bits.get_val();
7682

7783
// If x is already an integer, return it.
@@ -124,7 +130,9 @@ LIBC_INLINE T round(T x) {
124130
uint32_t trim_size = FPBits<T>::FRACTION_LEN - exponent;
125131
bool half_bit_set =
126132
bool(bits.get_mantissa() & (StorageType(1) << (trim_size - 1)));
127-
bits.set_mantissa((bits.get_mantissa() >> trim_size) << trim_size);
133+
StorageType trunc_mantissa =
134+
static_cast<StorageType>((bits.get_mantissa() >> trim_size) << trim_size);
135+
bits.set_mantissa(trunc_mantissa);
128136
T trunc_value = bits.get_val();
129137

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

182190
uint32_t trim_size = FPBits<T>::FRACTION_LEN - exponent;
183191
FPBits<T> new_bits = bits;
184-
new_bits.set_mantissa((bits.get_mantissa() >> trim_size) << trim_size);
192+
StorageType trunc_mantissa =
193+
static_cast<StorageType>((bits.get_mantissa() >> trim_size) << trim_size);
194+
new_bits.set_mantissa(trunc_mantissa);
185195
T trunc_value = new_bits.get_val();
186196

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

191201
StorageType trim_value =
192202
bits.get_mantissa() & ((StorageType(1) << trim_size) - 1);
193-
StorageType half_value = (StorageType(1) << (trim_size - 1));
203+
StorageType half_value =
204+
static_cast<StorageType>((StorageType(1) << (trim_size - 1)));
194205
// If exponent is 0, trimSize will be equal to the mantissa width, and
195206
// truncIsOdd` will not be correct. So, we handle it as a special case
196207
// below.

libc/src/math/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ add_math_entrypoint_object(canonicalizel)
6767
add_math_entrypoint_object(ceil)
6868
add_math_entrypoint_object(ceilf)
6969
add_math_entrypoint_object(ceill)
70+
add_math_entrypoint_object(ceilf16)
7071
add_math_entrypoint_object(ceilf128)
7172

7273
add_math_entrypoint_object(copysign)
@@ -110,6 +111,7 @@ add_math_entrypoint_object(fdimf128)
110111
add_math_entrypoint_object(floor)
111112
add_math_entrypoint_object(floorf)
112113
add_math_entrypoint_object(floorl)
114+
add_math_entrypoint_object(floorf16)
113115
add_math_entrypoint_object(floorf128)
114116

115117
add_math_entrypoint_object(fma)
@@ -293,11 +295,13 @@ add_math_entrypoint_object(rintf128)
293295
add_math_entrypoint_object(round)
294296
add_math_entrypoint_object(roundf)
295297
add_math_entrypoint_object(roundl)
298+
add_math_entrypoint_object(roundf16)
296299
add_math_entrypoint_object(roundf128)
297300

298301
add_math_entrypoint_object(roundeven)
299302
add_math_entrypoint_object(roundevenf)
300303
add_math_entrypoint_object(roundevenl)
304+
add_math_entrypoint_object(roundevenf16)
301305
add_math_entrypoint_object(roundevenf128)
302306

303307
add_math_entrypoint_object(scalbn)
@@ -331,6 +335,7 @@ add_math_entrypoint_object(tgammaf)
331335
add_math_entrypoint_object(trunc)
332336
add_math_entrypoint_object(truncf)
333337
add_math_entrypoint_object(truncl)
338+
add_math_entrypoint_object(truncf16)
334339
add_math_entrypoint_object(truncf128)
335340

336341
add_math_entrypoint_object(ufromfp)

libc/src/math/ceilf16.h

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

libc/src/math/floorf16.h

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

libc/src/math/generic/CMakeLists.txt

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,19 @@ add_entrypoint_object(
8282
libc.src.__support.FPUtil.nearest_integer_operations
8383
)
8484

85+
add_entrypoint_object(
86+
ceilf16
87+
SRCS
88+
ceilf16.cpp
89+
HDRS
90+
../ceilf16.h
91+
COMPILE_OPTIONS
92+
-O3
93+
DEPENDS
94+
libc.src.__support.macros.properties.types
95+
libc.src.__support.FPUtil.nearest_integer_operations
96+
)
97+
8598
add_entrypoint_object(
8699
ceilf128
87100
SRCS
@@ -303,6 +316,19 @@ add_entrypoint_object(
303316
libc.src.__support.FPUtil.nearest_integer_operations
304317
)
305318

319+
add_entrypoint_object(
320+
truncf16
321+
SRCS
322+
truncf16.cpp
323+
HDRS
324+
../truncf16.h
325+
COMPILE_OPTIONS
326+
-O3
327+
DEPENDS
328+
libc.src.__support.macros.properties.types
329+
libc.src.__support.FPUtil.nearest_integer_operations
330+
)
331+
306332
add_entrypoint_object(
307333
truncf128
308334
SRCS
@@ -352,6 +378,19 @@ add_entrypoint_object(
352378
libc.src.__support.FPUtil.nearest_integer_operations
353379
)
354380

381+
add_entrypoint_object(
382+
floorf16
383+
SRCS
384+
floorf16.cpp
385+
HDRS
386+
../floorf16.h
387+
COMPILE_OPTIONS
388+
-O3
389+
DEPENDS
390+
libc.src.__support.macros.properties.types
391+
libc.src.__support.FPUtil.nearest_integer_operations
392+
)
393+
355394
add_entrypoint_object(
356395
floorf128
357396
SRCS
@@ -401,6 +440,19 @@ add_entrypoint_object(
401440
libc.src.__support.FPUtil.nearest_integer_operations
402441
)
403442

443+
add_entrypoint_object(
444+
roundf16
445+
SRCS
446+
roundf16.cpp
447+
HDRS
448+
../roundf16.h
449+
COMPILE_OPTIONS
450+
-O3
451+
DEPENDS
452+
libc.src.__support.macros.properties.types
453+
libc.src.__support.FPUtil.nearest_integer_operations
454+
)
455+
404456
add_entrypoint_object(
405457
roundf128
406458
SRCS
@@ -450,6 +502,19 @@ add_entrypoint_object(
450502
libc.src.__support.FPUtil.nearest_integer_operations
451503
)
452504

505+
add_entrypoint_object(
506+
roundevenf16
507+
SRCS
508+
roundevenf16.cpp
509+
HDRS
510+
../roundevenf16.h
511+
COMPILE_OPTIONS
512+
-O3
513+
DEPENDS
514+
libc.src.__support.macros.properties.types
515+
libc.src.__support.FPUtil.nearest_integer_operations
516+
)
517+
453518
add_entrypoint_object(
454519
roundevenf128
455520
SRCS

libc/src/math/generic/ceilf16.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//===-- Implementation of ceilf16 function --------------------------------===//
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+
#include "src/math/ceilf16.h"
10+
#include "src/__support/FPUtil/NearestIntegerOperations.h"
11+
#include "src/__support/common.h"
12+
13+
namespace LIBC_NAMESPACE {
14+
15+
LLVM_LIBC_FUNCTION(float16, ceilf16, (float16 x)) { return fputil::ceil(x); }
16+
17+
} // namespace LIBC_NAMESPACE

libc/src/math/generic/floorf16.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//===-- Implementation of floorf16 function -------------------------------===//
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+
#include "src/math/floorf16.h"
10+
#include "src/__support/FPUtil/NearestIntegerOperations.h"
11+
#include "src/__support/common.h"
12+
13+
namespace LIBC_NAMESPACE {
14+
15+
LLVM_LIBC_FUNCTION(float16, floorf16, (float16 x)) { return fputil::floor(x); }
16+
17+
} // namespace LIBC_NAMESPACE

0 commit comments

Comments
 (0)