Skip to content

Commit 2d784b1

Browse files
authored
[libc][math] Implement iscanonical[f|l] as a libc math function (#110565)
This PR implements the iscanonical function as part of the libc math library. The addition of this function is crucial for completing the implementation of remaining math macros, as referenced in #109201
1 parent da4b972 commit 2d784b1

24 files changed

+472
-0
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,9 @@ set(TARGET_LIBM_ENTRYPOINTS
476476
libc.src.math.ilogb
477477
libc.src.math.ilogbf
478478
libc.src.math.ilogbl
479+
libc.src.math.iscanonical
480+
libc.src.math.iscanonicalf
481+
libc.src.math.iscanonicall
479482
libc.src.math.isnan
480483
libc.src.math.isnanf
481484
libc.src.math.isnanl
@@ -640,6 +643,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
640643
libc.src.math.fromfpxf16
641644
libc.src.math.getpayloadf16
642645
libc.src.math.ilogbf16
646+
libc.src.math.iscanonicalf16
643647
libc.src.math.issignalingf16
644648
libc.src.math.ldexpf16
645649
libc.src.math.llogbf16
@@ -724,6 +728,7 @@ if(LIBC_TYPES_HAS_FLOAT128)
724728
libc.src.math.fsubf128
725729
libc.src.math.getpayloadf128
726730
libc.src.math.ilogbf128
731+
libc.src.math.iscanonicalf128
727732
libc.src.math.issignalingf128
728733
libc.src.math.ldexpf128
729734
libc.src.math.llogbf128

libc/config/linux/riscv/entrypoints.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,9 @@ set(TARGET_LIBM_ENTRYPOINTS
479479
libc.src.math.ilogb
480480
libc.src.math.ilogbf
481481
libc.src.math.ilogbl
482+
libc.src.math.iscanonical
483+
libc.src.math.iscanonicalf
484+
libc.src.math.iscanonicall
482485
libc.src.math.isnan
483486
libc.src.math.isnanf
484487
libc.src.math.isnanl
@@ -630,6 +633,7 @@ if(LIBC_TYPES_HAS_FLOAT128)
630633
libc.src.math.fsubf128
631634
libc.src.math.getpayloadf128
632635
libc.src.math.ilogbf128
636+
libc.src.math.iscanonicalf128
633637
libc.src.math.issignalingf128
634638
libc.src.math.ldexpf128
635639
libc.src.math.llogbf128

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,9 @@ set(TARGET_LIBM_ENTRYPOINTS
479479
libc.src.math.ilogb
480480
libc.src.math.ilogbf
481481
libc.src.math.ilogbl
482+
libc.src.math.iscanonical
483+
libc.src.math.iscanonicalf
484+
libc.src.math.iscanonicall
482485
libc.src.math.isnan
483486
libc.src.math.isnanf
484487
libc.src.math.isnanl
@@ -640,6 +643,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
640643
libc.src.math.fromfpxf16
641644
libc.src.math.getpayloadf16
642645
libc.src.math.ilogbf16
646+
libc.src.math.iscanonicalf16
643647
libc.src.math.issignalingf16
644648
libc.src.math.ldexpf16
645649
libc.src.math.llogbf16
@@ -721,6 +725,7 @@ if(LIBC_TYPES_HAS_FLOAT128)
721725
libc.src.math.fsubf128
722726
libc.src.math.getpayloadf128
723727
libc.src.math.ilogbf128
728+
libc.src.math.iscanonicalf128
724729
libc.src.math.issignalingf128
725730
libc.src.math.ldexpf128
726731
libc.src.math.llogbf128

libc/docs/math/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ Basic Operations
182182
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
183183
| ilogb | |check| | |check| | |check| | |check| | |check| | 7.12.6.8 | F.10.3.8 |
184184
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
185+
| iscanonical | |check| | |check| | |check| | |check| | |check| | 7.12.3.2 | N/A |
186+
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
185187
| issignaling | |check| | |check| | |check| | |check| | |check| | 7.12.3.8 | N/A |
186188
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
187189
| ldexp | |check| | |check| | |check| | |check| | |check| | 7.12.6.9 | F.10.3.9 |

libc/spec/stdc.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,12 @@ def StdC : StandardSpec<"stdc"> {
837837
GuardedFunctionSpec<"canonicalizef16", RetValSpec<IntType>, [ArgSpec<Float16Type>, ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
838838
GuardedFunctionSpec<"canonicalizef128", RetValSpec<IntType>, [ArgSpec<Float128Type>, ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,
839839

840+
FunctionSpec<"iscanonical", RetValSpec<IntType>, [ArgSpec<DoubleType>]>,
841+
FunctionSpec<"iscanonicalf", RetValSpec<IntType>, [ArgSpec<FloatType>]>,
842+
FunctionSpec<"iscanonicall", RetValSpec<IntType>, [ArgSpec<LongDoubleType>]>,
843+
GuardedFunctionSpec<"iscanonicalf16", RetValSpec<IntType>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
844+
GuardedFunctionSpec<"iscanonicalf128", RetValSpec<IntType>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,
845+
840846
FunctionSpec<"dsqrtl", RetValSpec<DoubleType>, [ArgSpec<LongDoubleType>]>,
841847

842848
FunctionSpec<"totalorder", RetValSpec<IntType>, [ArgSpec<ConstDoublePtr>, ArgSpec<ConstDoublePtr>]>,

libc/src/math/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ add_math_entrypoint_object(canonicalizel)
6666
add_math_entrypoint_object(canonicalizef16)
6767
add_math_entrypoint_object(canonicalizef128)
6868

69+
add_math_entrypoint_object(iscanonical)
70+
add_math_entrypoint_object(iscanonicalf)
71+
add_math_entrypoint_object(iscanonicall)
72+
add_math_entrypoint_object(iscanonicalf16)
73+
add_math_entrypoint_object(iscanonicalf128)
74+
6975
add_math_entrypoint_object(cbrt)
7076
add_math_entrypoint_object(cbrtf)
7177

libc/src/math/generic/CMakeLists.txt

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,60 @@ add_entrypoint_object(
6060
libc.src.__support.FPUtil.basic_operations
6161
)
6262

63+
add_entrypoint_object(
64+
iscanonical
65+
SRCS
66+
iscanonical.cpp
67+
HDRS
68+
../iscanonical.h
69+
COMPILE_OPTIONS
70+
-O3
71+
)
72+
73+
add_entrypoint_object(
74+
iscanonicalf
75+
SRCS
76+
iscanonicalf.cpp
77+
HDRS
78+
../iscanonicalf.h
79+
COMPILE_OPTIONS
80+
-O3
81+
)
82+
83+
add_entrypoint_object(
84+
iscanonicall
85+
SRCS
86+
iscanonicall.cpp
87+
HDRS
88+
../iscanonicall.h
89+
COMPILE_OPTIONS
90+
-O3
91+
)
92+
93+
add_entrypoint_object(
94+
iscanonicalf16
95+
SRCS
96+
iscanonicalf16.cpp
97+
HDRS
98+
../iscanonicalf16.h
99+
COMPILE_OPTIONS
100+
-O3
101+
DEPENDS
102+
libc.src.__support.macros.properties.types
103+
)
104+
105+
add_entrypoint_object(
106+
iscanonicalf128
107+
SRCS
108+
iscanonicalf128.cpp
109+
HDRS
110+
../iscanonicalf128.h
111+
COMPILE_OPTIONS
112+
-O3
113+
DEPENDS
114+
libc.src.__support.macros.properties.types
115+
)
116+
63117
add_entrypoint_object(
64118
ceil
65119
SRCS

libc/src/math/generic/iscanonical.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation of iscanonical 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/iscanonical.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, iscanonical, (double x)) {
17+
double temp;
18+
return static_cast<int>(fputil::canonicalize(temp, x) == 0);
19+
}
20+
21+
} // namespace LIBC_NAMESPACE_DECL
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation of iscanonicalf 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/iscanonicalf.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, iscanonicalf, (float x)) {
17+
float temp;
18+
return static_cast<int>(fputil::canonicalize(temp, x) == 0);
19+
}
20+
21+
} // namespace LIBC_NAMESPACE_DECL
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation of iscanonicalf128 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/iscanonicalf128.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, iscanonicalf128, (float128 x)) {
17+
float128 temp;
18+
return static_cast<int>(fputil::canonicalize(temp, x) == 0);
19+
}
20+
21+
} // namespace LIBC_NAMESPACE_DECL
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation of iscanonicalf16 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/iscanonicalf16.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, iscanonicalf16, (float16 x)) {
17+
float16 temp;
18+
return static_cast<int>(fputil::canonicalize(temp, x) == 0);
19+
}
20+
21+
} // namespace LIBC_NAMESPACE_DECL
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation of iscanonicall 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/iscanonicall.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, iscanonicall, (long double x)) {
17+
long double temp;
18+
return static_cast<int>(fputil::canonicalize(temp, x) == 0);
19+
}
20+
21+
} // namespace LIBC_NAMESPACE_DECL

libc/src/math/iscanonical.h

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

libc/src/math/iscanonicalf.h

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

libc/src/math/iscanonicalf128.h

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

libc/src/math/iscanonicalf16.h

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

libc/src/math/iscanonicall.h

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

0 commit comments

Comments
 (0)