Skip to content

Commit ff1cc5b

Browse files
author
Job Henandez Lara
authored
[libc][math][c23] Add totalorderl function. (#102564)
1 parent 93fc459 commit ff1cc5b

File tree

15 files changed

+128
-13
lines changed

15 files changed

+128
-13
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,7 @@ set(TARGET_LIBM_ENTRYPOINTS
568568
libc.src.math.tanhf
569569
libc.src.math.totalorder
570570
libc.src.math.totalorderf
571+
libc.src.math.totalorderl
571572
libc.src.math.totalordermag
572573
libc.src.math.totalordermagf
573574
libc.src.math.totalordermagl

libc/config/linux/arm/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,7 @@ set(TARGET_LIBM_ENTRYPOINTS
396396
libc.src.math.tanhf
397397
libc.src.math.totalorder
398398
libc.src.math.totalorderf
399+
libc.src.math.totalorderl
399400
libc.src.math.totalordermag
400401
libc.src.math.totalordermagf
401402
libc.src.math.totalordermagl

libc/config/linux/riscv/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,7 @@ set(TARGET_LIBM_ENTRYPOINTS
571571
libc.src.math.tanhf
572572
libc.src.math.totalorder
573573
libc.src.math.totalorderf
574+
libc.src.math.totalorderl
574575
libc.src.math.totalordermag
575576
libc.src.math.totalordermagf
576577
libc.src.math.totalordermagl

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,7 @@ set(TARGET_LIBM_ENTRYPOINTS
571571
libc.src.math.tanhf
572572
libc.src.math.totalorder
573573
libc.src.math.totalorderf
574+
libc.src.math.totalorderl
574575
libc.src.math.totalordermag
575576
libc.src.math.totalordermagf
576577
libc.src.math.totalordermagl

libc/docs/math/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ Basic Operations
228228
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
229229
| setpayloadsig | |check| | |check| | |check| | |check| | |check| | F.10.13.3 | N/A |
230230
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
231-
| totalorder | |check| | |check| | | |check| | |check| | F.10.12.1 | N/A |
231+
| totalorder | |check| | |check| | |check| | |check| | |check| | F.10.12.1 | N/A |
232232
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
233233
| totalordermag | |check| | |check| | |check| | |check| | |check| | F.10.12.2 | N/A |
234234
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+

libc/newhdrgen/yaml/math.yaml

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1839,14 +1839,43 @@ functions:
18391839
- type: float128 *
18401840
- type: float128
18411841
guard: LIBC_TYPES_HAS_FLOAT128
1842+
- name: totalorder
1843+
standards:
1844+
- stdc
1845+
return_type: int
1846+
arguments:
1847+
- type: const double *
1848+
- type: const double *
1849+
- name: totalorderf
1850+
standards:
1851+
- stdc
1852+
return_type: int
1853+
arguments:
1854+
- type: const float *
1855+
- type: const float *
1856+
- name: totalorderl
1857+
standards:
1858+
- stdc
1859+
return_type: int
1860+
arguments:
1861+
- type: const long double *
1862+
- type: const long double *
18421863
- name: totalorderf16
18431864
standards:
18441865
- stdc
18451866
return_type: int
18461867
arguments:
1847-
- type: _Float16 *
1848-
- type: _Float16 *
1868+
- type: const _Float16 *
1869+
- type: const _Float16 *
18491870
guard: LIBC_TYPES_HAS_FLOAT16
1871+
- name: totalorderf128
1872+
standards:
1873+
- stdc
1874+
return_type: int
1875+
arguments:
1876+
- type: const float128 *
1877+
- type: const float128 *
1878+
guard: LIBC_TYPES_HAS_FLOAT128
18501879
- name: totalordermagf16
18511880
standards:
18521881
- stdc

libc/spec/spec.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ def Float16Ptr : PtrType<Float16Type>;
119119
def Float128Ptr : PtrType<Float128Type>;
120120
def UnsignedCharPtr : PtrType<UnsignedCharType>;
121121

122+
def ConstDoublePtr : ConstType<DoublePtr>;
123+
def ConstFloatPtr : ConstType<FloatPtr>;
124+
def ConstLongDoublePtr : ConstType<LongDoublePtr>;
125+
def ConstFloat16Ptr : ConstType<Float16Ptr>;
126+
def ConstFloat128Ptr : ConstType<Float128Ptr>;
127+
122128
def SigHandlerT : NamedType<"__sighandler_t">;
123129

124130
def TimeTType : NamedType<"time_t">;

libc/spec/stdc.td

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -743,10 +743,11 @@ def StdC : StandardSpec<"stdc"> {
743743

744744
FunctionSpec<"dsqrtl", RetValSpec<DoubleType>, [ArgSpec<LongDoubleType>]>,
745745

746-
FunctionSpec<"totalorder", RetValSpec<IntType>, [ArgSpec<DoublePtr>, ArgSpec<DoublePtr>]>,
747-
FunctionSpec<"totalorderf", RetValSpec<IntType>, [ArgSpec<FloatPtr>, ArgSpec<FloatPtr>]>,
748-
GuardedFunctionSpec<"totalorderf16", RetValSpec<IntType>, [ArgSpec<Float16Ptr>, ArgSpec<Float16Ptr>], "LIBC_TYPES_HAS_FLOAT16">,
749-
GuardedFunctionSpec<"totalorderf128", RetValSpec<IntType>, [ArgSpec<Float128Ptr>, ArgSpec<Float128Ptr>], "LIBC_TYPES_HAS_FLOAT128">,
746+
FunctionSpec<"totalorder", RetValSpec<IntType>, [ArgSpec<ConstDoublePtr>, ArgSpec<ConstDoublePtr>]>,
747+
FunctionSpec<"totalorderf", RetValSpec<IntType>, [ArgSpec<ConstFloatPtr>, ArgSpec<ConstFloatPtr>]>,
748+
FunctionSpec<"totalorderl", RetValSpec<IntType>, [ArgSpec<ConstLongDoublePtr>, ArgSpec<ConstLongDoublePtr>]>,
749+
GuardedFunctionSpec<"totalorderf16", RetValSpec<IntType>, [ArgSpec<ConstFloat16Ptr>, ArgSpec<ConstFloat16Ptr>], "LIBC_TYPES_HAS_FLOAT16">,
750+
GuardedFunctionSpec<"totalorderf128", RetValSpec<IntType>, [ArgSpec<ConstFloat128Ptr>, ArgSpec<ConstFloat128Ptr>], "LIBC_TYPES_HAS_FLOAT128">,
750751

751752
FunctionSpec<"totalordermag", RetValSpec<IntType>, [ArgSpec<DoublePtr>, ArgSpec<DoublePtr>]>,
752753
FunctionSpec<"totalordermagf", RetValSpec<IntType>, [ArgSpec<FloatPtr>, ArgSpec<FloatPtr>]>,

libc/src/__support/FPUtil/BasicOperations.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -321,12 +321,8 @@ totalorder(T x, T y) {
321321
StorageType x_u = x_bits.uintval();
322322
StorageType y_u = y_bits.uintval();
323323

324-
using signed_t = make_integral_or_big_int_signed_t<StorageType>;
325-
signed_t x_signed = static_cast<signed_t>(x_u);
326-
signed_t y_signed = static_cast<signed_t>(y_u);
327-
328-
bool both_neg = (x_u & y_u & FPBits::SIGN_MASK) != 0;
329-
return x_signed == y_signed || ((x_signed <= y_signed) != both_neg);
324+
bool has_neg = ((x_u | y_u) & FPBits::SIGN_MASK) != 0;
325+
return x_u == y_u || ((x_u < y_u) != has_neg);
330326
}
331327

332328
template <typename T>

libc/src/math/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@ add_math_entrypoint_object(lgamma_r)
479479

480480
add_math_entrypoint_object(totalorder)
481481
add_math_entrypoint_object(totalorderf)
482+
add_math_entrypoint_object(totalorderl)
482483
add_math_entrypoint_object(totalorderf16)
483484
add_math_entrypoint_object(totalorderf128)
484485

libc/src/math/generic/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4368,6 +4368,18 @@ add_entrypoint_object(
43684368
-O3
43694369
)
43704370

4371+
add_entrypoint_object(
4372+
totalorderl
4373+
SRCS
4374+
totalorderl.cpp
4375+
HDRS
4376+
../totalorderl.h
4377+
DEPENDS
4378+
libc.src.__support.FPUtil.basic_operations
4379+
COMPILE_OPTIONS
4380+
-O3
4381+
)
4382+
43714383
add_entrypoint_object(
43724384
totalorderf16
43734385
SRCS

libc/src/math/generic/totalorderl.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation of totalorderl 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/totalorderl.h"
10+
#include "src/__support/FPUtil/BasicOperations.h"
11+
#include "src/__support/common.h"
12+
#include "src/__support/macros/config.h"
13+
14+
namespace LIBC_NAMESPACE_DECL {
15+
16+
LLVM_LIBC_FUNCTION(int, totalorderl,
17+
(const long double *x, const long double *y)) {
18+
return static_cast<int>(fputil::totalorder(*x, *y));
19+
}
20+
21+
} // namespace LIBC_NAMESPACE_DECL

libc/src/math/totalorderl.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header for totalorderl -------------------*- 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_TOTALORDERL_H
10+
#define LLVM_LIBC_SRC_MATH_TOTALORDERL_H
11+
12+
#include "src/__support/macros/config.h"
13+
14+
namespace LIBC_NAMESPACE_DECL {
15+
16+
int totalorderl(const long double *x, const long double *y);
17+
18+
} // namespace LIBC_NAMESPACE_DECL
19+
20+
#endif // LLVM_LIBC_SRC_MATH_TOTALORDERL_H

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3837,6 +3837,18 @@ add_fp_unittest(
38373837
libc.src.math.totalorderf
38383838
)
38393839

3840+
add_fp_unittest(
3841+
totalorderl_test
3842+
SUITE
3843+
libc-math-smoke-tests
3844+
SRCS
3845+
totalorderl_test.cpp
3846+
HDRS
3847+
TotalOrderTest.h
3848+
DEPENDS
3849+
libc.src.math.totalorderl
3850+
)
3851+
38403852
add_fp_unittest(
38413853
totalorderf16_test
38423854
SUITE
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//===-- Unittests for totalorderl -----------------------------------------===//
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 "TotalOrderTest.h"
10+
11+
#include "src/math/totalorderl.h"
12+
13+
LIST_TOTALORDER_TESTS(long double, LIBC_NAMESPACE::totalorderl)

0 commit comments

Comments
 (0)