Skip to content

Commit 10cd96d

Browse files
authored
[libc][math][c23] Add {frexp,ilogb,llogb,logb,modf}f16 C23 math functions (#94758)
Part of #93566.
1 parent a1c29df commit 10cd96d

31 files changed

+480
-36
lines changed

libc/config/linux/aarch64/entrypoints.txt

+5
Original file line numberDiff line numberDiff line change
@@ -516,12 +516,17 @@ if(LIBC_TYPES_HAS_FLOAT16)
516516
libc.src.math.fminimum_magf16
517517
libc.src.math.fminimum_mag_numf16
518518
libc.src.math.fminimum_numf16
519+
libc.src.math.frexpf16
519520
libc.src.math.fromfpf16
520521
libc.src.math.fromfpxf16
522+
libc.src.math.ilogbf16
523+
libc.src.math.llogbf16
521524
libc.src.math.llrintf16
522525
libc.src.math.llroundf16
526+
libc.src.math.logbf16
523527
libc.src.math.lrintf16
524528
libc.src.math.lroundf16
529+
libc.src.math.modff16
525530
libc.src.math.nanf16
526531
libc.src.math.nearbyintf16
527532
libc.src.math.nextafterf16

libc/config/linux/x86_64/entrypoints.txt

+5
Original file line numberDiff line numberDiff line change
@@ -549,12 +549,17 @@ if(LIBC_TYPES_HAS_FLOAT16)
549549
libc.src.math.fminimum_mag_numf16
550550
libc.src.math.fminimum_numf16
551551
libc.src.math.fmodf16
552+
libc.src.math.frexpf16
552553
libc.src.math.fromfpf16
553554
libc.src.math.fromfpxf16
555+
libc.src.math.ilogbf16
556+
libc.src.math.llogbf16
554557
libc.src.math.llrintf16
555558
libc.src.math.llroundf16
559+
libc.src.math.logbf16
556560
libc.src.math.lrintf16
557561
libc.src.math.lroundf16
562+
libc.src.math.modff16
558563
libc.src.math.nanf16
559564
libc.src.math.nearbyintf16
560565
libc.src.math.nextafterf16

libc/docs/c23.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Additions:
5050
* issignaling
5151
* issubnormal
5252
* iszero
53-
* llogb*
53+
* llogb* |check|
5454
* pown*
5555
* powr*
5656
* rootn*

libc/docs/math/index.rst

+5-5
Original file line numberDiff line numberDiff line change
@@ -160,31 +160,31 @@ Basic Operations
160160
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
161161
| fmul | N/A | |check| | | N/A | | 7.12.14.3 | F.10.11 |
162162
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
163-
| frexp | |check| | |check| | |check| | | |check| | 7.12.6.7 | F.10.3.7 |
163+
| frexp | |check| | |check| | |check| | |check| | |check| | 7.12.6.7 | F.10.3.7 |
164164
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
165165
| fromfp | |check| | |check| | |check| | |check| | |check| | 7.12.9.10 | F.10.6.10 |
166166
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
167167
| 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
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
171-
| ilogb | |check| | |check| | |check| | | |check| | 7.12.6.8 | F.10.3.8 |
171+
| ilogb | |check| | |check| | |check| | |check| | |check| | 7.12.6.8 | F.10.3.8 |
172172
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
173173
| ldexp | |check| | |check| | |check| | | |check| | 7.12.6.9 | F.10.3.9 |
174174
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
175-
| llogb | |check| | |check| | |check| | | |check| | 7.12.6.10 | F.10.3.10 |
175+
| llogb | |check| | |check| | |check| | |check| | |check| | 7.12.6.10 | F.10.3.10 |
176176
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
177177
| llrint | |check| | |check| | |check| | |check| | |check| | 7.12.9.5 | F.10.6.5 |
178178
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
179179
| llround | |check| | |check| | |check| | |check| | |check| | 7.12.9.7 | F.10.6.7 |
180180
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
181-
| logb | |check| | |check| | |check| | | |check| | 7.12.6.17 | F.10.3.17 |
181+
| logb | |check| | |check| | |check| | |check| | |check| | 7.12.6.17 | F.10.3.17 |
182182
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
183183
| lrint | |check| | |check| | |check| | |check| | |check| | 7.12.9.5 | F.10.6.5 |
184184
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
185185
| lround | |check| | |check| | |check| | |check| | |check| | 7.12.9.7 | F.10.6.7 |
186186
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
187-
| modf | |check| | |check| | |check| | | |check| | 7.12.6.18 | F.10.3.18 |
187+
| modf | |check| | |check| | |check| | |check| | |check| | 7.12.6.18 | F.10.3.18 |
188188
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
189189
| nan | |check| | |check| | |check| | |check| | |check| | 7.12.11.2 | F.10.8.2 |
190190
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+

libc/spec/spec.td

+1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ def IntPtr : PtrType<IntType>;
115115
def RestrictedIntPtr : RestrictedPtrType<IntType>;
116116
def FloatPtr : PtrType<FloatType>;
117117
def DoublePtr : PtrType<DoubleType>;
118+
def Float16Ptr : PtrType<Float16Type>;
118119
def Float128Ptr : PtrType<Float128Type>;
119120
def UnsignedCharPtr : PtrType<UnsignedCharType>;
120121

libc/spec/stdc.td

+5
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,7 @@ def StdC : StandardSpec<"stdc"> {
487487
FunctionSpec<"frexp", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<IntPtr>]>,
488488
FunctionSpec<"frexpf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<IntPtr>]>,
489489
FunctionSpec<"frexpl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<IntPtr>]>,
490+
GuardedFunctionSpec<"frexpf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>, ArgSpec<IntPtr>], "LIBC_TYPES_HAS_FLOAT16">,
490491
GuardedFunctionSpec<"frexpf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>, ArgSpec<IntPtr>], "LIBC_TYPES_HAS_FLOAT128">,
491492

492493
FunctionSpec<"fromfp", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>]>,
@@ -519,11 +520,13 @@ def StdC : StandardSpec<"stdc"> {
519520
FunctionSpec<"ilogb", RetValSpec<IntType>, [ArgSpec<DoubleType>]>,
520521
FunctionSpec<"ilogbf", RetValSpec<IntType>, [ArgSpec<FloatType>]>,
521522
FunctionSpec<"ilogbl", RetValSpec<IntType>, [ArgSpec<LongDoubleType>]>,
523+
GuardedFunctionSpec<"ilogbf16", RetValSpec<IntType>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
522524
GuardedFunctionSpec<"ilogbf128", RetValSpec<IntType>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,
523525

524526
FunctionSpec<"llogb", RetValSpec<LongType>, [ArgSpec<DoubleType>]>,
525527
FunctionSpec<"llogbf", RetValSpec<LongType>, [ArgSpec<FloatType>]>,
526528
FunctionSpec<"llogbl", RetValSpec<LongType>, [ArgSpec<LongDoubleType>]>,
529+
GuardedFunctionSpec<"llogbf16", RetValSpec<LongType>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
527530
GuardedFunctionSpec<"llogbf128", RetValSpec<LongType>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,
528531

529532
FunctionSpec<"ldexp", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<IntType>]>,
@@ -546,11 +549,13 @@ def StdC : StandardSpec<"stdc"> {
546549
FunctionSpec<"logb", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
547550
FunctionSpec<"logbf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
548551
FunctionSpec<"logbl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
552+
GuardedFunctionSpec<"logbf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
549553
GuardedFunctionSpec<"logbf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,
550554

551555
FunctionSpec<"modf", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoublePtr>]>,
552556
FunctionSpec<"modff", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatPtr>]>,
553557
FunctionSpec<"modfl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<LongDoublePtr>]>,
558+
GuardedFunctionSpec<"modff16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>, ArgSpec<Float16Ptr>], "LIBC_TYPES_HAS_FLOAT16">,
554559
GuardedFunctionSpec<"modff128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>, ArgSpec<Float128Ptr>], "LIBC_TYPES_HAS_FLOAT128">,
555560

556561
FunctionSpec<"cos", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,

libc/src/__support/FPUtil/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ add_header_library(
217217
.nearest_integer_operations
218218
.normal_float
219219
libc.hdr.math_macros
220+
libc.src.__support.CPP.algorithm
220221
libc.src.__support.CPP.bit
221222
libc.src.__support.CPP.limits
222223
libc.src.__support.CPP.type_traits

libc/src/__support/FPUtil/ManipulationFunctions.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "rounding_mode.h"
1717

1818
#include "hdr/math_macros.h"
19+
#include "src/__support/CPP/algorithm.h"
1920
#include "src/__support/CPP/bit.h"
2021
#include "src/__support/CPP/limits.h" // INT_MAX, INT_MIN
2122
#include "src/__support/CPP/type_traits.h"
@@ -102,7 +103,7 @@ intlogb(U x) {
102103
return IntLogbConstants<T>::T_MAX;
103104
}
104105

105-
DyadicFloat<FPBits<U>::STORAGE_LEN> normal(bits.get_val());
106+
DyadicFloat<cpp::max(FPBits<U>::STORAGE_LEN, 32)> normal(bits.get_val());
106107
int exponent = normal.get_unbiased_exponent();
107108
// The C standard does not specify the return value when an exponent is
108109
// out of int range. However, XSI conformance required that INT_MAX or
@@ -138,7 +139,7 @@ LIBC_INLINE constexpr T logb(T x) {
138139
return FPBits<T>::inf().get_val();
139140
}
140141

141-
DyadicFloat<FPBits<T>::STORAGE_LEN> normal(bits.get_val());
142+
DyadicFloat<cpp::max(FPBits<T>::STORAGE_LEN, 32)> normal(bits.get_val());
142143
return static_cast<T>(normal.get_unbiased_exponent());
143144
}
144145

libc/src/__support/FPUtil/NormalFloat.h

+7-4
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,11 @@ template <typename T> struct NormalFloat {
110110
if (shift <= FPBits<T>::FRACTION_LEN + 1) {
111111
// Generate a subnormal number. Might lead to loss of precision.
112112
// We round to nearest and round halfway cases to even.
113-
const StorageType shift_out_mask = (StorageType(1) << shift) - 1;
113+
const StorageType shift_out_mask =
114+
static_cast<StorageType>(StorageType(1) << shift) - 1;
114115
const StorageType shift_out_value = mantissa & shift_out_mask;
115-
const StorageType halfway_value = StorageType(1) << (shift - 1);
116+
const StorageType halfway_value =
117+
static_cast<StorageType>(StorageType(1) << (shift - 1));
116118
result.set_biased_exponent(0);
117119
result.set_mantissa(mantissa >> shift);
118120
StorageType new_mantissa = result.get_mantissa();
@@ -135,7 +137,8 @@ template <typename T> struct NormalFloat {
135137
}
136138
}
137139

138-
result.set_biased_exponent(exponent + FPBits<T>::EXP_BIAS);
140+
result.set_biased_exponent(
141+
static_cast<StorageType>(exponent + FPBits<T>::EXP_BIAS));
139142
result.set_mantissa(mantissa);
140143
return result.get_val();
141144
}
@@ -155,7 +158,7 @@ template <typename T> struct NormalFloat {
155158
// Normalize subnormal numbers.
156159
if (bits.is_subnormal()) {
157160
unsigned shift = evaluate_normalization_shift(bits.get_mantissa());
158-
mantissa = StorageType(bits.get_mantissa()) << shift;
161+
mantissa = static_cast<StorageType>(bits.get_mantissa() << shift);
159162
exponent = 1 - FPBits<T>::EXP_BIAS - shift;
160163
} else {
161164
exponent = bits.get_biased_exponent() - FPBits<T>::EXP_BIAS;

libc/src/math/CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ add_math_entrypoint_object(fmodf128)
191191
add_math_entrypoint_object(frexp)
192192
add_math_entrypoint_object(frexpf)
193193
add_math_entrypoint_object(frexpl)
194+
add_math_entrypoint_object(frexpf16)
194195
add_math_entrypoint_object(frexpf128)
195196

196197
add_math_entrypoint_object(fromfp)
@@ -211,11 +212,13 @@ add_math_entrypoint_object(hypotf)
211212
add_math_entrypoint_object(ilogb)
212213
add_math_entrypoint_object(ilogbf)
213214
add_math_entrypoint_object(ilogbl)
215+
add_math_entrypoint_object(ilogbf16)
214216
add_math_entrypoint_object(ilogbf128)
215217

216218
add_math_entrypoint_object(llogb)
217219
add_math_entrypoint_object(llogbf)
218220
add_math_entrypoint_object(llogbl)
221+
add_math_entrypoint_object(llogbf16)
219222
add_math_entrypoint_object(llogbf128)
220223

221224
add_math_entrypoint_object(ldexp)
@@ -238,6 +241,7 @@ add_math_entrypoint_object(logf)
238241
add_math_entrypoint_object(logb)
239242
add_math_entrypoint_object(logbf)
240243
add_math_entrypoint_object(logbl)
244+
add_math_entrypoint_object(logbf16)
241245
add_math_entrypoint_object(logbf128)
242246

243247
add_math_entrypoint_object(llrint)
@@ -267,6 +271,7 @@ add_math_entrypoint_object(lroundf128)
267271
add_math_entrypoint_object(modf)
268272
add_math_entrypoint_object(modff)
269273
add_math_entrypoint_object(modfl)
274+
add_math_entrypoint_object(modff16)
270275
add_math_entrypoint_object(modff128)
271276

272277
add_math_entrypoint_object(nan)

libc/src/math/frexpf16.h

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

libc/src/math/generic/CMakeLists.txt

+66
Original file line numberDiff line numberDiff line change
@@ -1301,6 +1301,19 @@ add_entrypoint_object(
13011301
libc.src.__support.FPUtil.manipulation_functions
13021302
)
13031303

1304+
add_entrypoint_object(
1305+
frexpf16
1306+
SRCS
1307+
frexpf16.cpp
1308+
HDRS
1309+
../frexpf16.h
1310+
COMPILE_OPTIONS
1311+
-O3
1312+
DEPENDS
1313+
libc.src.__support.macros.properties.types
1314+
libc.src.__support.FPUtil.manipulation_functions
1315+
)
1316+
13041317
add_entrypoint_object(
13051318
frexpf128
13061319
SRCS
@@ -1350,6 +1363,19 @@ add_entrypoint_object(
13501363
libc.src.__support.FPUtil.manipulation_functions
13511364
)
13521365

1366+
add_entrypoint_object(
1367+
ilogbf16
1368+
SRCS
1369+
ilogbf16.cpp
1370+
HDRS
1371+
../ilogbf16.h
1372+
COMPILE_OPTIONS
1373+
-O3
1374+
DEPENDS
1375+
libc.src.__support.macros.properties.types
1376+
libc.src.__support.FPUtil.manipulation_functions
1377+
)
1378+
13531379
add_entrypoint_object(
13541380
ilogbf128
13551381
SRCS
@@ -1399,6 +1425,19 @@ add_entrypoint_object(
13991425
libc.src.__support.FPUtil.manipulation_functions
14001426
)
14011427

1428+
add_entrypoint_object(
1429+
llogbf16
1430+
SRCS
1431+
llogbf16.cpp
1432+
HDRS
1433+
../llogbf16.h
1434+
COMPILE_OPTIONS
1435+
-O3
1436+
DEPENDS
1437+
libc.src.__support.macros.properties.types
1438+
libc.src.__support.FPUtil.manipulation_functions
1439+
)
1440+
14021441
add_entrypoint_object(
14031442
llogbf128
14041443
SRCS
@@ -1672,6 +1711,19 @@ add_entrypoint_object(
16721711
libc.src.__support.FPUtil.manipulation_functions
16731712
)
16741713

1714+
add_entrypoint_object(
1715+
logbf16
1716+
SRCS
1717+
logbf16.cpp
1718+
HDRS
1719+
../logbf16.h
1720+
COMPILE_OPTIONS
1721+
-O3
1722+
DEPENDS
1723+
libc.src.__support.macros.properties.types
1724+
libc.src.__support.FPUtil.manipulation_functions
1725+
)
1726+
16751727
add_entrypoint_object(
16761728
logbf128
16771729
SRCS
@@ -1681,6 +1733,7 @@ add_entrypoint_object(
16811733
COMPILE_OPTIONS
16821734
-O3
16831735
DEPENDS
1736+
libc.src.__support.macros.properties.types
16841737
libc.src.__support.FPUtil.manipulation_functions
16851738
)
16861739

@@ -1720,6 +1773,19 @@ add_entrypoint_object(
17201773
-O3
17211774
)
17221775

1776+
add_entrypoint_object(
1777+
modff16
1778+
SRCS
1779+
modff16.cpp
1780+
HDRS
1781+
../modff16.h
1782+
DEPENDS
1783+
libc.src.__support.macros.properties.types
1784+
libc.src.__support.FPUtil.manipulation_functions
1785+
COMPILE_OPTIONS
1786+
-O3
1787+
)
1788+
17231789
add_entrypoint_object(
17241790
modff128
17251791
SRCS

libc/src/math/generic/frexpf16.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===-- Implementation of frexpf16 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/frexpf16.h"
10+
#include "src/__support/FPUtil/ManipulationFunctions.h"
11+
#include "src/__support/common.h"
12+
13+
namespace LIBC_NAMESPACE {
14+
15+
LLVM_LIBC_FUNCTION(float16, frexpf16, (float16 x, int *exp)) {
16+
return fputil::frexp(x, *exp);
17+
}
18+
19+
} // namespace LIBC_NAMESPACE

libc/src/math/generic/ilogbf16.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===-- Implementation of ilogbf16 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/ilogbf16.h"
10+
#include "src/__support/FPUtil/ManipulationFunctions.h"
11+
#include "src/__support/common.h"
12+
13+
namespace LIBC_NAMESPACE {
14+
15+
LLVM_LIBC_FUNCTION(int, ilogbf16, (float16 x)) {
16+
return fputil::intlogb<int>(x);
17+
}
18+
19+
} // namespace LIBC_NAMESPACE

0 commit comments

Comments
 (0)