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

0 commit comments

Comments
 (0)