Skip to content

Commit e55d975

Browse files
committed
[libc][math][c23] Add ilogbf16 C23 math function
1 parent 44b3eac commit e55d975

File tree

13 files changed

+96
-6
lines changed

13 files changed

+96
-6
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
518518
libc.src.math.frexpf16
519519
libc.src.math.fromfpf16
520520
libc.src.math.fromfpxf16
521+
libc.src.math.ilogbf16
521522
libc.src.math.llrintf16
522523
libc.src.math.llroundf16
523524
libc.src.math.lrintf16

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
550550
libc.src.math.frexpf16
551551
libc.src.math.fromfpf16
552552
libc.src.math.fromfpxf16
553+
libc.src.math.ilogbf16
553554
libc.src.math.llrintf16
554555
libc.src.math.llroundf16
555556
libc.src.math.lrintf16

libc/docs/math/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ Basic Operations
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
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+

libc/spec/stdc.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,7 @@ def StdC : StandardSpec<"stdc"> {
516516
FunctionSpec<"ilogb", RetValSpec<IntType>, [ArgSpec<DoubleType>]>,
517517
FunctionSpec<"ilogbf", RetValSpec<IntType>, [ArgSpec<FloatType>]>,
518518
FunctionSpec<"ilogbl", RetValSpec<IntType>, [ArgSpec<LongDoubleType>]>,
519+
GuardedFunctionSpec<"ilogbf16", RetValSpec<IntType>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
519520
GuardedFunctionSpec<"ilogbf128", RetValSpec<IntType>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,
520521

521522
FunctionSpec<"llogb", RetValSpec<LongType>, [ArgSpec<DoubleType>]>,

libc/src/__support/FPUtil/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
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

Lines changed: 2 additions & 1 deletion
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

libc/src/math/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ add_math_entrypoint_object(hypotf)
209209
add_math_entrypoint_object(ilogb)
210210
add_math_entrypoint_object(ilogbf)
211211
add_math_entrypoint_object(ilogbl)
212+
add_math_entrypoint_object(ilogbf16)
212213
add_math_entrypoint_object(ilogbf128)
213214

214215
add_math_entrypoint_object(llogb)

libc/src/math/generic/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,6 +1363,19 @@ add_entrypoint_object(
13631363
libc.src.__support.FPUtil.manipulation_functions
13641364
)
13651365

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+
13661379
add_entrypoint_object(
13671380
ilogbf128
13681381
SRCS

libc/src/math/generic/ilogbf16.cpp

Lines changed: 19 additions & 0 deletions
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

libc/src/math/ilogbf16.h

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

libc/test/src/math/smoke/CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1400,6 +1400,21 @@ add_fp_unittest(
14001400
libc.src.__support.FPUtil.manipulation_functions
14011401
)
14021402

1403+
add_fp_unittest(
1404+
ilogbf16_test
1405+
SUITE
1406+
libc-math-smoke-tests
1407+
SRCS
1408+
ilogbf16_test.cpp
1409+
HDRS
1410+
ILogbTest.h
1411+
DEPENDS
1412+
libc.src.math.ilogbf16
1413+
libc.src.__support.CPP.limits
1414+
libc.src.__support.FPUtil.fp_bits
1415+
libc.src.__support.FPUtil.manipulation_functions
1416+
)
1417+
14031418
add_fp_unittest(
14041419
ilogbf128_test
14051420
SUITE

libc/test/src/math/smoke/ILogbTest.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,10 @@ class LlvmLibcILogbTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
7676
void test_subnormal_range(Func func) {
7777
constexpr StorageType MIN_SUBNORMAL = FPBits::min_subnormal().uintval();
7878
constexpr StorageType MAX_SUBNORMAL = FPBits::max_subnormal().uintval();
79-
constexpr StorageType COUNT = 10'001;
80-
constexpr StorageType STEP = (MAX_SUBNORMAL - MIN_SUBNORMAL) / COUNT;
79+
constexpr int COUNT = 10'001;
80+
constexpr StorageType STEP = LIBC_NAMESPACE::cpp::max(
81+
static_cast<StorageType>((MAX_SUBNORMAL - MIN_SUBNORMAL) / COUNT),
82+
StorageType(1));
8183
for (StorageType v = MIN_SUBNORMAL; v <= MAX_SUBNORMAL; v += STEP) {
8284
FPBits x_bits = FPBits(v);
8385
if (x_bits.is_zero() || x_bits.is_inf_or_nan())
@@ -94,8 +96,10 @@ class LlvmLibcILogbTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
9496
void test_normal_range(Func func) {
9597
constexpr StorageType MIN_NORMAL = FPBits::min_normal().uintval();
9698
constexpr StorageType MAX_NORMAL = FPBits::max_normal().uintval();
97-
constexpr StorageType COUNT = 10'001;
98-
constexpr StorageType STEP = (MAX_NORMAL - MIN_NORMAL) / COUNT;
99+
constexpr int COUNT = 10'001;
100+
constexpr StorageType STEP = LIBC_NAMESPACE::cpp::max(
101+
static_cast<StorageType>((MAX_NORMAL - MIN_NORMAL) / COUNT),
102+
StorageType(1));
99103
for (StorageType v = MIN_NORMAL; v <= MAX_NORMAL; v += STEP) {
100104
FPBits x_bits = FPBits(v);
101105
if (x_bits.is_zero() || x_bits.is_inf_or_nan())
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//===-- Unittests for ilogbf16 --------------------------------------------===//
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 "ILogbTest.h"
10+
11+
#include "src/math/ilogbf16.h"
12+
13+
LIST_INTLOGB_TESTS(int, float16, LIBC_NAMESPACE::ilogbf16);

0 commit comments

Comments
 (0)