Skip to content

Commit f5dcfb9

Browse files
authored
[libc][math][c23] Add {totalorder,totalordermag}f16 C23 math functions (#95014)
Part of #93566.
1 parent b746bab commit f5dcfb9

File tree

18 files changed

+480
-4
lines changed

18 files changed

+480
-4
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,8 @@ if(LIBC_TYPES_HAS_FLOAT16)
541541
libc.src.math.rintf16
542542
libc.src.math.roundf16
543543
libc.src.math.roundevenf16
544+
libc.src.math.totalorderf16
545+
libc.src.math.totalordermagf16
544546
libc.src.math.truncf16
545547
libc.src.math.ufromfpf16
546548
libc.src.math.ufromfpxf16

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,8 @@ if(LIBC_TYPES_HAS_FLOAT16)
571571
libc.src.math.rintf16
572572
libc.src.math.roundf16
573573
libc.src.math.roundevenf16
574+
libc.src.math.totalorderf16
575+
libc.src.math.totalordermagf16
574576
libc.src.math.truncf16
575577
libc.src.math.ufromfpf16
576578
libc.src.math.ufromfpxf16

libc/docs/c23.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ Additions:
4242
* rsqrt*
4343
* __STDC_IEC_60559_DFP__ functions (_Decimal32, _Decimal64, _Decimal128)
4444
* compoundn*
45-
* totalorder*
46-
* totalordermag*
45+
* totalorder* |check|
46+
* totalordermag* |check|
4747
* getpayload*
4848
* setpayload*
4949
* iscannonical

libc/docs/math/index.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,10 @@ Basic Operations
210210
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
211211
| scalbn | |check| | |check| | |check| | | |check| | 7.12.6.19 | F.10.3.19 |
212212
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
213+
| totalorder | | | | |check| | | F.10.12.1 | N/A |
214+
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
215+
| totalordermag | | | | |check| | | F.10.12.2 | N/A |
216+
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
213217
| trunc | |check| | |check| | |check| | |check| | |check| | 7.12.9.9 | F.10.6.9 |
214218
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
215219
| ufromfp | |check| | |check| | |check| | |check| | |check| | 7.12.9.10 | F.10.6.10 |

libc/spec/stdc.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,10 @@ def StdC : StandardSpec<"stdc"> {
710710
FunctionSpec<"canonicalizel", RetValSpec<IntType>, [ArgSpec<LongDoubleType>, ArgSpec<LongDoubleType>]>,
711711
GuardedFunctionSpec<"canonicalizef16", RetValSpec<IntType>, [ArgSpec<Float16Type>, ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
712712
GuardedFunctionSpec<"canonicalizef128", RetValSpec<IntType>, [ArgSpec<Float128Type>, ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,
713+
714+
GuardedFunctionSpec<"totalorderf16", RetValSpec<IntType>, [ArgSpec<Float16Ptr>, ArgSpec<Float16Ptr>], "LIBC_TYPES_HAS_FLOAT16">,
715+
716+
GuardedFunctionSpec<"totalordermagf16", RetValSpec<IntType>, [ArgSpec<Float16Ptr>, ArgSpec<Float16Ptr>], "LIBC_TYPES_HAS_FLOAT16">,
713717
]
714718
>;
715719

libc/src/__support/FPUtil/BasicOperations.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,31 @@ LIBC_INLINE int canonicalize(T &cx, const T &x) {
240240
return 0;
241241
}
242242

243+
template <typename T>
244+
LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<T>, bool>
245+
totalorder(T x, T y) {
246+
using FPBits = FPBits<T>;
247+
FPBits x_bits(x);
248+
FPBits y_bits(y);
249+
250+
using StorageType = typename FPBits::StorageType;
251+
StorageType x_u = x_bits.uintval();
252+
StorageType y_u = y_bits.uintval();
253+
254+
using signed_t = cpp::make_signed_t<StorageType>;
255+
signed_t x_signed = static_cast<signed_t>(x_u);
256+
signed_t y_signed = static_cast<signed_t>(y_u);
257+
258+
bool both_neg = (x_u & y_u & FPBits::SIGN_MASK) != 0;
259+
return x_signed == y_signed || ((x_signed <= y_signed) != both_neg);
260+
}
261+
262+
template <typename T>
263+
LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<T>, bool>
264+
totalordermag(T x, T y) {
265+
return FPBits<T>(x).abs().uintval() <= FPBits<T>(y).abs().uintval();
266+
}
267+
243268
} // namespace fputil
244269
} // namespace LIBC_NAMESPACE
245270

libc/src/math/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,10 @@ add_math_entrypoint_object(tanhf)
369369
add_math_entrypoint_object(tgamma)
370370
add_math_entrypoint_object(tgammaf)
371371

372+
add_math_entrypoint_object(totalorderf16)
373+
374+
add_math_entrypoint_object(totalordermagf16)
375+
372376
add_math_entrypoint_object(trunc)
373377
add_math_entrypoint_object(truncf)
374378
add_math_entrypoint_object(truncl)

libc/src/math/generic/CMakeLists.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3577,3 +3577,27 @@ add_entrypoint_object(
35773577
COMPILE_OPTIONS
35783578
-O3
35793579
)
3580+
3581+
add_entrypoint_object(
3582+
totalorderf16
3583+
SRCS
3584+
totalorderf16.cpp
3585+
HDRS
3586+
../totalorderf16.h
3587+
DEPENDS
3588+
libc.src.__support.FPUtil.basic_operations
3589+
COMPILE_OPTIONS
3590+
-O3
3591+
)
3592+
3593+
add_entrypoint_object(
3594+
totalordermagf16
3595+
SRCS
3596+
totalordermagf16.cpp
3597+
HDRS
3598+
../totalordermagf16.h
3599+
DEPENDS
3600+
libc.src.__support.FPUtil.basic_operations
3601+
COMPILE_OPTIONS
3602+
-O3
3603+
)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===-- Implementation of totalorderf16 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/totalorderf16.h"
10+
#include "src/__support/FPUtil/BasicOperations.h"
11+
#include "src/__support/common.h"
12+
13+
namespace LIBC_NAMESPACE {
14+
15+
LLVM_LIBC_FUNCTION(int, totalorderf16, (const float16 *x, const float16 *y)) {
16+
return static_cast<int>(fputil::totalorder(*x, *y));
17+
}
18+
19+
} // namespace LIBC_NAMESPACE
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation of totalordermagf16 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/totalordermagf16.h"
10+
#include "src/__support/FPUtil/BasicOperations.h"
11+
#include "src/__support/common.h"
12+
13+
namespace LIBC_NAMESPACE {
14+
15+
LLVM_LIBC_FUNCTION(int, totalordermagf16,
16+
(const float16 *x, const float16 *y)) {
17+
return static_cast<int>(fputil::totalordermag(*x, *y));
18+
}
19+
20+
} // namespace LIBC_NAMESPACE

0 commit comments

Comments
 (0)