Skip to content

Commit ba03740

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

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

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
#include <stdint.h>
2020

21-
template <typename T>
21+
template <typename T, typename U = int>
2222
class LdExpTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
2323
using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>;
2424
using NormalFloat = LIBC_NAMESPACE::fputil::NormalFloat<T>;
@@ -34,17 +34,28 @@ class LdExpTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
3434
static constexpr StorageType MANTISSA = NormalFloat::ONE + 0x123;
3535

3636
public:
37-
typedef T (*LdExpFunc)(T, int);
37+
typedef T (*LdExpFunc)(T, U);
3838

3939
void testSpecialNumbers(LdExpFunc func) {
40-
int exp_array[5] = {-INT_MAX - 1, -10, 0, 10, INT_MAX};
40+
int exp_array[5] = {INT_MIN, -10, 0, 10, INT_MAX};
4141
for (int exp : exp_array) {
4242
ASSERT_FP_EQ(zero, func(zero, exp));
4343
ASSERT_FP_EQ(neg_zero, func(neg_zero, exp));
4444
ASSERT_FP_EQ(inf, func(inf, exp));
4545
ASSERT_FP_EQ(neg_inf, func(neg_inf, exp));
4646
ASSERT_FP_EQ(nan, func(nan, exp));
4747
}
48+
49+
if constexpr (sizeof(U) < sizeof(long) || sizeof(long) == sizeof(int))
50+
return;
51+
long long_exp_array[4] = {LONG_MIN, INT_MIN - 1L, INT_MAX + 1L, LONG_MAX};
52+
for (long exp : long_exp_array) {
53+
ASSERT_FP_EQ(zero, func(zero, exp));
54+
ASSERT_FP_EQ(neg_zero, func(neg_zero, exp));
55+
ASSERT_FP_EQ(inf, func(inf, exp));
56+
ASSERT_FP_EQ(neg_inf, func(neg_inf, exp));
57+
ASSERT_FP_EQ(nan, func(nan, exp));
58+
}
4859
}
4960

5061
void testPowersOfTwo(LdExpFunc func) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
#include "LdExpTest.h"
1313
#include "test/UnitTest/Test.h"
1414

15-
#define LIST_SCALBN_TESTS(T, func) \
16-
using LlvmLibcScalbnTest = LdExpTestTemplate<T>; \
15+
#define LIST_SCALBN_TESTS(T, U, func) \
16+
using LlvmLibcScalbnTest = LdExpTestTemplate<T, U>; \
1717
TEST_F(LlvmLibcScalbnTest, SpecialNumbers) { testSpecialNumbers(&func); } \
1818
TEST_F(LlvmLibcScalbnTest, PowersOfTwo) { testPowersOfTwo(&func); } \
1919
TEST_F(LlvmLibcScalbnTest, OverFlow) { testOverflow(&func); } \
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//===-- Unittests for scalblnf16 ------------------------------------------===//
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 "ScalbnTest.h"
10+
11+
#include "src/math/scalblnf16.h"
12+
13+
LIST_SCALBN_TESTS(float16, long, LIBC_NAMESPACE::scalblnf16)

libc/test/src/math/smoke/scalbn_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010

1111
#include "src/math/scalbn.h"
1212

13-
LIST_SCALBN_TESTS(double, LIBC_NAMESPACE::scalbn)
13+
LIST_SCALBN_TESTS(double, int, LIBC_NAMESPACE::scalbn)

libc/test/src/math/smoke/scalbnf128_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010

1111
#include "src/math/scalbnf128.h"
1212

13-
LIST_SCALBN_TESTS(float128, LIBC_NAMESPACE::scalbnf128)
13+
LIST_SCALBN_TESTS(float128, int, LIBC_NAMESPACE::scalbnf128)

libc/test/src/math/smoke/scalbnf16_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010

1111
#include "src/math/scalbnf16.h"
1212

13-
LIST_SCALBN_TESTS(float16, LIBC_NAMESPACE::scalbnf16)
13+
LIST_SCALBN_TESTS(float16, int, LIBC_NAMESPACE::scalbnf16)

libc/test/src/math/smoke/scalbnf_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010

1111
#include "src/math/scalbnf.h"
1212

13-
LIST_SCALBN_TESTS(float, LIBC_NAMESPACE::scalbnf)
13+
LIST_SCALBN_TESTS(float, int, LIBC_NAMESPACE::scalbnf)

libc/test/src/math/smoke/scalbnl_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010

1111
#include "src/math/scalbnl.h"
1212

13-
LIST_SCALBN_TESTS(long double, LIBC_NAMESPACE::scalbnl)
13+
LIST_SCALBN_TESTS(long double, int, LIBC_NAMESPACE::scalbnl)

0 commit comments

Comments
 (0)