Skip to content

Commit 44b3eac

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

File tree

12 files changed

+89
-6
lines changed

12 files changed

+89
-6
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
515515
libc.src.math.fminimum_magf16
516516
libc.src.math.fminimum_mag_numf16
517517
libc.src.math.fminimum_numf16
518+
libc.src.math.frexpf16
518519
libc.src.math.fromfpf16
519520
libc.src.math.fromfpxf16
520521
libc.src.math.llrintf16

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
547547
libc.src.math.fminimum_magf16
548548
libc.src.math.fminimum_mag_numf16
549549
libc.src.math.fminimum_numf16
550+
libc.src.math.frexpf16
550551
libc.src.math.fromfpf16
551552
libc.src.math.fromfpxf16
552553
libc.src.math.llrintf16

libc/docs/math/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ Basic Operations
160160
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
161161
| fmul | N/A | | | N/A | | 7.12.14.3 | F.10.11 |
162162
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
163-
| frexp | |check| | |check| | |check| | | |check| | 7.12.6.7 | F.10.3.7 |
163+
| frexp | |check| | |check| | |check| | |check| | |check| | 7.12.6.7 | F.10.3.7 |
164164
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
165165
| fromfp | |check| | |check| | |check| | |check| | |check| | 7.12.9.10 | F.10.6.10 |
166166
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+

libc/spec/stdc.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,7 @@ def StdC : StandardSpec<"stdc"> {
483483
FunctionSpec<"frexp", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<IntPtr>]>,
484484
FunctionSpec<"frexpf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<IntPtr>]>,
485485
FunctionSpec<"frexpl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<IntPtr>]>,
486+
GuardedFunctionSpec<"frexpf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>, ArgSpec<IntPtr>], "LIBC_TYPES_HAS_FLOAT16">,
486487
GuardedFunctionSpec<"frexpf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>, ArgSpec<IntPtr>], "LIBC_TYPES_HAS_FLOAT128">,
487488

488489
FunctionSpec<"fromfp", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<IntType>, ArgSpec<UnsignedIntType>]>,

libc/src/__support/FPUtil/NormalFloat.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,11 @@ template <typename T> struct NormalFloat {
110110
if (shift <= FPBits<T>::FRACTION_LEN + 1) {
111111
// Generate a subnormal number. Might lead to loss of precision.
112112
// We round to nearest and round halfway cases to even.
113-
const StorageType shift_out_mask = (StorageType(1) << shift) - 1;
113+
const StorageType shift_out_mask =
114+
static_cast<StorageType>(StorageType(1) << shift) - 1;
114115
const StorageType shift_out_value = mantissa & shift_out_mask;
115-
const StorageType halfway_value = StorageType(1) << (shift - 1);
116+
const StorageType halfway_value =
117+
static_cast<StorageType>(StorageType(1) << (shift - 1));
116118
result.set_biased_exponent(0);
117119
result.set_mantissa(mantissa >> shift);
118120
StorageType new_mantissa = result.get_mantissa();
@@ -135,7 +137,8 @@ template <typename T> struct NormalFloat {
135137
}
136138
}
137139

138-
result.set_biased_exponent(exponent + FPBits<T>::EXP_BIAS);
140+
result.set_biased_exponent(
141+
static_cast<StorageType>(exponent + FPBits<T>::EXP_BIAS));
139142
result.set_mantissa(mantissa);
140143
return result.get_val();
141144
}
@@ -155,7 +158,7 @@ template <typename T> struct NormalFloat {
155158
// Normalize subnormal numbers.
156159
if (bits.is_subnormal()) {
157160
unsigned shift = evaluate_normalization_shift(bits.get_mantissa());
158-
mantissa = StorageType(bits.get_mantissa()) << shift;
161+
mantissa = static_cast<StorageType>(bits.get_mantissa() << shift);
159162
exponent = 1 - FPBits<T>::EXP_BIAS - shift;
160163
} else {
161164
exponent = bits.get_biased_exponent() - FPBits<T>::EXP_BIAS;

libc/src/math/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ add_math_entrypoint_object(fmodf128)
188188
add_math_entrypoint_object(frexp)
189189
add_math_entrypoint_object(frexpf)
190190
add_math_entrypoint_object(frexpl)
191+
add_math_entrypoint_object(frexpf16)
191192
add_math_entrypoint_object(frexpf128)
192193

193194
add_math_entrypoint_object(fromfp)

libc/src/math/frexpf16.h

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

libc/src/math/generic/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,6 +1301,19 @@ add_entrypoint_object(
13011301
libc.src.__support.FPUtil.manipulation_functions
13021302
)
13031303

1304+
add_entrypoint_object(
1305+
frexpf16
1306+
SRCS
1307+
frexpf16.cpp
1308+
HDRS
1309+
../frexpf16.h
1310+
COMPILE_OPTIONS
1311+
-O3
1312+
DEPENDS
1313+
libc.src.__support.macros.properties.types
1314+
libc.src.__support.FPUtil.manipulation_functions
1315+
)
1316+
13041317
add_entrypoint_object(
13051318
frexpf128
13061319
SRCS

libc/src/math/generic/frexpf16.cpp

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

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,18 @@ add_fp_unittest(
10911091
libc.src.math.frexpl
10921092
)
10931093

1094+
add_fp_unittest(
1095+
frexpf16_test
1096+
SUITE
1097+
libc-math-smoke-tests
1098+
SRCS
1099+
frexpf16_test.cpp
1100+
HDRS
1101+
FrexpTest.h
1102+
DEPENDS
1103+
libc.src.math.frexpf16
1104+
)
1105+
10941106
add_fp_unittest(
10951107
frexpf128_test
10961108
SUITE

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

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

9-
#include "src/__support/FPUtil/BasicOperations.h"
109
#include "test/UnitTest/FEnvSafeTest.h"
1110
#include "test/UnitTest/FPMatcher.h"
1211
#include "test/UnitTest/Test.h"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//===-- Unittests for frexpf16 --------------------------------------------===//
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 "FrexpTest.h"
10+
11+
#include "src/math/frexpf16.h"
12+
13+
LIST_FREXP_TESTS(float16, LIBC_NAMESPACE::frexpf16);

0 commit comments

Comments
 (0)