Skip to content

Commit ba03740

Browse files
committed
[libc][math][c23] Add scalblnf16 C23 math function
1 parent f772f7e commit ba03740

File tree

18 files changed

+118
-12
lines changed

18 files changed

+118
-12
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
533533
libc.src.math.rintf16
534534
libc.src.math.roundf16
535535
libc.src.math.roundevenf16
536+
libc.src.math.scalblnf16
536537
libc.src.math.scalbnf16
537538
libc.src.math.truncf16
538539
libc.src.math.ufromfpf16

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
562562
libc.src.math.rintf16
563563
libc.src.math.roundf16
564564
libc.src.math.roundevenf16
565+
libc.src.math.scalblnf16
565566
libc.src.math.scalbnf16
566567
libc.src.math.truncf16
567568
libc.src.math.ufromfpf16

libc/docs/math/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ Basic Operations
208208
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
209209
| roundeven | |check| | |check| | |check| | |check| | |check| | 7.12.9.8 | F.10.6.8 |
210210
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
211+
| scalbln | | | | |check| | | 7.12.6.19 | F.10.3.19 |
212+
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
211213
| scalbn | |check| | |check| | |check| | |check| | |check| | 7.12.6.19 | F.10.3.19 |
212214
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
213215
| trunc | |check| | |check| | |check| | |check| | |check| | 7.12.9.9 | F.10.6.9 |

libc/spec/stdc.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,8 @@ def StdC : StandardSpec<"stdc"> {
683683
FunctionSpec<"asinhf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
684684
FunctionSpec<"atanhf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
685685

686+
GuardedFunctionSpec<"scalblnf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>, ArgSpec<LongType>], "LIBC_TYPES_HAS_FLOAT16">,
687+
686688
FunctionSpec<"scalbn", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<IntType>]>,
687689
FunctionSpec<"scalbnf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<IntType>]>,
688690
FunctionSpec<"scalbnl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<IntType>]>,

libc/src/__support/FPUtil/ManipulationFunctions.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ LIBC_INLINE constexpr T logb(T x) {
143143
}
144144

145145
template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
146-
LIBC_INLINE constexpr T ldexp(T x, int exp) {
146+
LIBC_INLINE constexpr T ldexp(T x, long exp) {
147147
FPBits<T> bits(x);
148148
if (LIBC_UNLIKELY((exp == 0) || bits.is_zero() || bits.is_inf_or_nan()))
149149
return x;
@@ -186,7 +186,9 @@ LIBC_INLINE constexpr T ldexp(T x, int exp) {
186186

187187
// For all other values, NormalFloat to T conversion handles it the right way.
188188
DyadicFloat<cpp::max(FPBits<T>::STORAGE_LEN, 32)> normal(bits.get_val());
189-
normal.exponent += exp;
189+
// Make sure that exp fits into an int when not taking the fast paths above.
190+
static_assert(EXP_LIMIT <= INT_MAX && -EXP_LIMIT >= INT_MIN);
191+
normal.exponent += static_cast<int>(exp);
190192
return static_cast<T>(normal);
191193
}
192194

libc/src/math/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,8 @@ add_math_entrypoint_object(roundevenl)
330330
add_math_entrypoint_object(roundevenf16)
331331
add_math_entrypoint_object(roundevenf128)
332332

333+
add_math_entrypoint_object(scalblnf16)
334+
333335
add_math_entrypoint_object(scalbn)
334336
add_math_entrypoint_object(scalbnf)
335337
add_math_entrypoint_object(scalbnl)

libc/src/math/generic/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3368,6 +3368,20 @@ add_entrypoint_object(
33683368
libc.src.__support.macros.optimization
33693369
)
33703370

3371+
add_entrypoint_object(
3372+
scalblnf16
3373+
SRCS
3374+
scalblnf16.cpp
3375+
HDRS
3376+
../scalblnf16.h
3377+
DEPENDS
3378+
libc.hdr.float_macros
3379+
libc.src.__support.macros.properties.types
3380+
libc.src.__support.FPUtil.manipulation_functions
3381+
COMPILE_OPTIONS
3382+
-O3
3383+
)
3384+
33713385
add_entrypoint_object(
33723386
scalbn
33733387
SRCS

libc/src/math/generic/scalblnf16.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===-- Implementation of scalblnf16 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/scalblnf16.h"
10+
#include "src/__support/FPUtil/ManipulationFunctions.h"
11+
#include "src/__support/common.h"
12+
13+
#include "hdr/float_macros.h"
14+
15+
#if FLT_RADIX != 2
16+
#error "FLT_RADIX != 2 is not supported."
17+
#endif
18+
19+
namespace LIBC_NAMESPACE {
20+
21+
LLVM_LIBC_FUNCTION(float16, scalblnf16, (float16 x, long n)) {
22+
return fputil::ldexp(x, n);
23+
}
24+
25+
} // namespace LIBC_NAMESPACE

libc/src/math/scalblnf16.h

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

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3306,6 +3306,19 @@ add_fp_unittest(
33063306
libc.src.__support.FPUtil.fp_bits
33073307
)
33083308

3309+
add_fp_unittest(
3310+
scalblnf16_test
3311+
SUITE
3312+
libc-math-smoke-tests
3313+
SRCS
3314+
scalblnf16_test.cpp
3315+
HDRS
3316+
ScalbnTest.h
3317+
DEPENDS
3318+
libc.src.math.scalblnf16
3319+
libc.src.__support.FPUtil.fp_bits
3320+
)
3321+
33093322
add_fp_unittest(
33103323
scalbn_test
33113324
SUITE

0 commit comments

Comments
 (0)