Skip to content

Commit e880ee2

Browse files
committed
[libc][math][c23] Add fmodf16 C23 math function
1 parent dd1cd02 commit e880ee2

File tree

13 files changed

+118
-29
lines changed

13 files changed

+118
-29
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.fmodf16
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
@@ -548,6 +548,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
548548
libc.src.math.fminimum_magf16
549549
libc.src.math.fminimum_mag_numf16
550550
libc.src.math.fminimum_numf16
551+
libc.src.math.fmodf16
551552
libc.src.math.fromfpf16
552553
libc.src.math.fromfpxf16
553554
libc.src.math.llrintf16

libc/docs/math/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ Basic Operations
156156
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
157157
| fminimum_num | |check| | |check| | |check| | |check| | |check| | 7.12.12.9 | F.10.9.5 |
158158
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
159-
| fmod | |check| | |check| | |check| | | |check| | 7.12.10.1 | F.10.7.1 |
159+
| fmod | |check| | |check| | |check| | |check| | |check| | 7.12.10.1 | F.10.7.1 |
160160
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
161161
| fmul | N/A | | | N/A | | 7.12.14.3 | F.10.11 |
162162
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+

libc/spec/stdc.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,7 @@ def StdC : StandardSpec<"stdc"> {
478478
FunctionSpec<"fmod", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
479479
FunctionSpec<"fmodf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>]>,
480480
FunctionSpec<"fmodl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<LongDoubleType>]>,
481+
GuardedFunctionSpec<"fmodf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>, ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
481482
GuardedFunctionSpec<"fmodf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>, ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,
482483

483484
FunctionSpec<"frexp", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<IntPtr>]>,

libc/src/__support/FPUtil/FPBits.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ struct FPRepImpl : public FPRepSem<fp_type, RetT> {
744744
if (LIBC_LIKELY(ep >= 0)) {
745745
// Implicit number bit will be removed by mask
746746
result.set_significand(number);
747-
result.set_biased_exponent(ep + 1);
747+
result.set_biased_exponent(static_cast<StorageType>(ep + 1));
748748
} else {
749749
result.set_significand(number >> -ep);
750750
}

libc/src/__support/FPUtil/generic/FMod.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,9 @@ class FMod {
210210
e_x - e_y <= int(FPB::EXP_LEN))) {
211211
StorageType m_x = sx.get_explicit_mantissa();
212212
StorageType m_y = sy.get_explicit_mantissa();
213-
StorageType d = (e_x == e_y) ? (m_x - m_y) : (m_x << (e_x - e_y)) % m_y;
213+
StorageType d = (e_x == e_y)
214+
? (m_x - m_y)
215+
: static_cast<StorageType>(m_x << (e_x - e_y)) % m_y;
214216
if (d == 0)
215217
return FPB::zero();
216218
// iy - 1 because of "zero power" for number with power 1

libc/src/math/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ add_math_entrypoint_object(fminimum_mag_numf128)
183183
add_math_entrypoint_object(fmod)
184184
add_math_entrypoint_object(fmodf)
185185
add_math_entrypoint_object(fmodl)
186+
add_math_entrypoint_object(fmodf16)
186187
add_math_entrypoint_object(fmodf128)
187188

188189
add_math_entrypoint_object(frexp)

libc/src/math/fmodf16.h

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

libc/src/math/generic/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2887,6 +2887,19 @@ add_entrypoint_object(
28872887
-O3
28882888
)
28892889

2890+
add_entrypoint_object(
2891+
fmodf16
2892+
SRCS
2893+
fmodf16.cpp
2894+
HDRS
2895+
../fmodf16.h
2896+
DEPENDS
2897+
libc.src.__support.macros.properties.types
2898+
libc.src.__support.FPUtil.generic.fmod
2899+
COMPILE_OPTIONS
2900+
-O3
2901+
)
2902+
28902903
add_entrypoint_object(
28912904
fmodf128
28922905
SRCS

libc/src/math/generic/fmodf16.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===-- Implementation of fmodf16 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/fmodf16.h"
10+
#include "src/__support/FPUtil/generic/FMod.h"
11+
#include "src/__support/common.h"
12+
13+
namespace LIBC_NAMESPACE {
14+
15+
LLVM_LIBC_FUNCTION(float16, fmodf16, (float16 x, float16 y)) {
16+
return fputil::generic::FMod<float16>::eval(x, y);
17+
}
18+
19+
} // namespace LIBC_NAMESPACE

0 commit comments

Comments
 (0)