Skip to content

Commit ec1c915

Browse files
committed
[libc][math][c23] Add logbf16 C23 math function
1 parent 1ecf747 commit ec1c915

File tree

12 files changed

+100
-5
lines changed

12 files changed

+100
-5
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
524524
libc.src.math.llogbf16
525525
libc.src.math.llrintf16
526526
libc.src.math.llroundf16
527+
libc.src.math.logbf16
527528
libc.src.math.lrintf16
528529
libc.src.math.lroundf16
529530
libc.src.math.nanf16

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
557557
libc.src.math.llogbf16
558558
libc.src.math.llrintf16
559559
libc.src.math.llroundf16
560+
libc.src.math.logbf16
560561
libc.src.math.lrintf16
561562
libc.src.math.lroundf16
562563
libc.src.math.nanf16

libc/docs/math/index.rst

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

libc/spec/stdc.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,7 @@ def StdC : StandardSpec<"stdc"> {
550550
FunctionSpec<"logb", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
551551
FunctionSpec<"logbf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
552552
FunctionSpec<"logbl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
553+
GuardedFunctionSpec<"logbf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
553554
GuardedFunctionSpec<"logbf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,
554555

555556
FunctionSpec<"modf", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoublePtr>]>,

libc/src/__support/FPUtil/ManipulationFunctions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ LIBC_INLINE constexpr T logb(T x) {
139139
return FPBits<T>::inf().get_val();
140140
}
141141

142-
DyadicFloat<FPBits<T>::STORAGE_LEN> normal(bits.get_val());
142+
DyadicFloat<cpp::max(FPBits<T>::STORAGE_LEN, 32)> normal(bits.get_val());
143143
return static_cast<T>(normal.get_unbiased_exponent());
144144
}
145145

libc/src/math/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ add_math_entrypoint_object(logf)
242242
add_math_entrypoint_object(logb)
243243
add_math_entrypoint_object(logbf)
244244
add_math_entrypoint_object(logbl)
245+
add_math_entrypoint_object(logbf16)
245246
add_math_entrypoint_object(logbf128)
246247

247248
add_math_entrypoint_object(llrint)

libc/src/math/generic/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1724,6 +1724,19 @@ add_entrypoint_object(
17241724
libc.src.__support.FPUtil.manipulation_functions
17251725
)
17261726

1727+
add_entrypoint_object(
1728+
logbf16
1729+
SRCS
1730+
logbf16.cpp
1731+
HDRS
1732+
../logbf16.h
1733+
COMPILE_OPTIONS
1734+
-O3
1735+
DEPENDS
1736+
libc.src.__support.macros.properties.types
1737+
libc.src.__support.FPUtil.manipulation_functions
1738+
)
1739+
17271740
add_entrypoint_object(
17281741
logbf128
17291742
SRCS
@@ -1733,6 +1746,7 @@ add_entrypoint_object(
17331746
COMPILE_OPTIONS
17341747
-O3
17351748
DEPENDS
1749+
libc.src.__support.macros.properties.types
17361750
libc.src.__support.FPUtil.manipulation_functions
17371751
)
17381752

libc/src/math/generic/logbf16.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//===-- Implementation of logbf16 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/logbf16.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, logbf16, (float16 x)) { return fputil::logb(x); }
16+
17+
} // namespace LIBC_NAMESPACE

libc/src/math/logbf16.h

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

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1586,8 +1586,11 @@ add_fp_unittest(
15861586
libc-math-smoke-tests
15871587
SRCS
15881588
logb_test.cpp
1589+
HDRS
1590+
LogbTest.h
15891591
DEPENDS
15901592
libc.src.math.logb
1593+
libc.src.__support.CPP.algorithm
15911594
libc.src.__support.FPUtil.manipulation_functions
15921595
)
15931596

@@ -1597,8 +1600,11 @@ add_fp_unittest(
15971600
libc-math-smoke-tests
15981601
SRCS
15991602
logbf_test.cpp
1603+
HDRS
1604+
LogbTest.h
16001605
DEPENDS
16011606
libc.src.math.logbf
1607+
libc.src.__support.CPP.algorithm
16021608
libc.src.__support.FPUtil.manipulation_functions
16031609
)
16041610

@@ -1612,6 +1618,21 @@ add_fp_unittest(
16121618
LogbTest.h
16131619
DEPENDS
16141620
libc.src.math.logbl
1621+
libc.src.__support.CPP.algorithm
1622+
libc.src.__support.FPUtil.manipulation_functions
1623+
)
1624+
1625+
add_fp_unittest(
1626+
logbf16_test
1627+
SUITE
1628+
libc-math-smoke-tests
1629+
SRCS
1630+
logbf16_test.cpp
1631+
HDRS
1632+
LogbTest.h
1633+
DEPENDS
1634+
libc.src.math.logbf16
1635+
libc.src.__support.CPP.algorithm
16151636
libc.src.__support.FPUtil.manipulation_functions
16161637
)
16171638

@@ -1621,8 +1642,11 @@ add_fp_unittest(
16211642
libc-math-smoke-tests
16221643
SRCS
16231644
logbf128_test.cpp
1645+
HDRS
1646+
LogbTest.h
16241647
DEPENDS
16251648
libc.src.math.logbf128
1649+
libc.src.__support.CPP.algorithm
16261650
libc.src.__support.FPUtil.manipulation_functions
16271651
)
16281652

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include "src/__support/CPP/algorithm.h"
910
#include "src/__support/FPUtil/ManipulationFunctions.h"
1011
#include "test/UnitTest/FEnvSafeTest.h"
1112
#include "test/UnitTest/FPMatcher.h"
@@ -69,9 +70,11 @@ class LogbTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
6970

7071
void testRange(LogbFunc func) {
7172
using StorageType = typename FPBits::StorageType;
72-
constexpr StorageType COUNT = 100'000;
73-
constexpr StorageType STEP = STORAGE_MAX / COUNT;
74-
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
73+
constexpr int COUNT = 100'000;
74+
constexpr StorageType STEP = LIBC_NAMESPACE::cpp::max(
75+
static_cast<StorageType>(STORAGE_MAX / COUNT), StorageType(1));
76+
StorageType v = 0;
77+
for (int i = 0; i <= COUNT; ++i, v += STEP) {
7578
FPBits x_bits = FPBits(v);
7679
if (x_bits.is_zero() || x_bits.is_inf_or_nan())
7780
continue;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//===-- Unittests for logbf16 ---------------------------------------------===//
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 "LogbTest.h"
10+
11+
#include "src/math/logbf16.h"
12+
13+
LIST_LOGB_TESTS(float16, LIBC_NAMESPACE::logbf16)

0 commit comments

Comments
 (0)