Skip to content

Commit b6e41c1

Browse files
authored
[libc] Provide isnan, isnanf and isnanl functions (#96008)
While C99 defines type generic isnan macro, BSD provided isnan, isnanf and isnanl in prior C standards and existing code still relies on these.
1 parent 3e4adef commit b6e41c1

File tree

19 files changed

+190
-5
lines changed

19 files changed

+190
-5
lines changed

libc/config/baremetal/arm/entrypoints.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,9 @@ set(TARGET_LIBM_ENTRYPOINTS
314314
libc.src.math.ilogb
315315
libc.src.math.ilogbf
316316
libc.src.math.ilogbl
317+
libc.src.math.isnan
318+
libc.src.math.isnanf
319+
libc.src.math.isnanl
317320
libc.src.math.ldexp
318321
libc.src.math.ldexpf
319322
libc.src.math.ldexpl

libc/config/baremetal/riscv/entrypoints.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,9 @@ set(TARGET_LIBM_ENTRYPOINTS
309309
libc.src.math.ilogb
310310
libc.src.math.ilogbf
311311
libc.src.math.ilogbl
312+
libc.src.math.isnan
313+
libc.src.math.isnanf
314+
libc.src.math.isnanl
312315
libc.src.math.ldexp
313316
libc.src.math.ldexpf
314317
libc.src.math.ldexpl

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,9 @@ set(TARGET_LIBM_ENTRYPOINTS
424424
libc.src.math.ilogb
425425
libc.src.math.ilogbf
426426
libc.src.math.ilogbl
427+
libc.src.math.isnan
428+
libc.src.math.isnanf
429+
libc.src.math.isnanl
427430
libc.src.math.ldexp
428431
libc.src.math.ldexpf
429432
libc.src.math.ldexpl

libc/config/linux/riscv/entrypoints.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,9 @@ set(TARGET_LIBM_ENTRYPOINTS
426426
libc.src.math.ilogb
427427
libc.src.math.ilogbf
428428
libc.src.math.ilogbl
429+
libc.src.math.isnan
430+
libc.src.math.isnanf
431+
libc.src.math.isnanl
429432
libc.src.math.ldexp
430433
libc.src.math.ldexpf
431434
libc.src.math.ldexpl

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,9 @@ set(TARGET_LIBM_ENTRYPOINTS
450450
libc.src.math.ilogb
451451
libc.src.math.ilogbf
452452
libc.src.math.ilogbl
453+
libc.src.math.isnan
454+
libc.src.math.isnanf
455+
libc.src.math.isnanl
453456
libc.src.math.ldexp
454457
libc.src.math.ldexpf
455458
libc.src.math.ldexpl

libc/include/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ add_gen_header(
122122
.llvm_libc_common_h
123123
.llvm-libc-macros.float16_macros
124124
.llvm-libc-macros.math_macros
125+
.llvm-libc-macros.math_function_macros
125126
.llvm-libc-types.double_t
126127
.llvm-libc-types.float_t
127128
.llvm-libc-types.float128

libc/include/llvm-libc-macros/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ add_macro_header(
117117
.limits_macros
118118
)
119119

120+
add_macro_header(
121+
math_function_macros
122+
HDR
123+
math-function-macros.h
124+
)
125+
120126
add_macro_header(
121127
offsetof_macro
122128
HDR
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//===-- Definition of function macros from math.h -------------------------===//
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_MACROS_MATH_FUNCTION_MACROS_H
10+
#define LLVM_LIBC_MACROS_MATH_FUNCTION_MACROS_H
11+
12+
#define isfinite(x) __builtin_isfinite(x)
13+
#define isinf(x) __builtin_isinf(x)
14+
#define isnan(x) __builtin_isnan(x)
15+
16+
#endif // LLVM_LIBC_MACROS_MATH_FUNCTION_MACROS_H

libc/include/llvm-libc-macros/math-macros.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,4 @@
4949
#define math_errhandling (MATH_ERRNO | MATH_ERREXCEPT)
5050
#endif
5151

52-
// TODO: Move generic functional math macros to a separate header file.
53-
#define isfinite(x) __builtin_isfinite(x)
54-
#define isinf(x) __builtin_isinf(x)
55-
#define isnan(x) __builtin_isnan(x)
56-
5752
#endif // LLVM_LIBC_MACROS_MATH_MACROS_H

libc/include/math.h.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@
1717

1818
%%public_api()
1919

20+
21+
#include "llvm-libc-macros/math-function-macros.h"
22+
2023
#endif // LLVM_LIBC_MATH_H

libc/spec/bsd_ext.td

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
def BsdExtensions : StandardSpec<"BSDExtensions"> {
2+
HeaderSpec Math = HeaderSpec<
3+
"math.h",
4+
[], // Macros
5+
[], // Types
6+
[], // Enumerations
7+
[
8+
FunctionSpec<"isnan", RetValSpec<IntType>, [ArgSpec<DoubleType>]>,
9+
FunctionSpec<"isnanf", RetValSpec<IntType>, [ArgSpec<FloatType>]>,
10+
FunctionSpec<"isnanl", RetValSpec<IntType>, [ArgSpec<LongDoubleType>]>,
11+
]
12+
>;
13+
214
HeaderSpec String = HeaderSpec<
315
"string.h",
416
[], // Macros
@@ -67,6 +79,7 @@ def BsdExtensions : StandardSpec<"BSDExtensions"> {
6779
>;
6880

6981
let Headers = [
82+
Math,
7083
String,
7184
Strings,
7285
SysWait,

libc/src/math/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,10 @@ add_math_entrypoint_object(ilogbl)
245245
add_math_entrypoint_object(ilogbf16)
246246
add_math_entrypoint_object(ilogbf128)
247247

248+
add_math_entrypoint_object(isnan)
249+
add_math_entrypoint_object(isnanf)
250+
add_math_entrypoint_object(isnanl)
251+
248252
add_math_entrypoint_object(llogb)
249253
add_math_entrypoint_object(llogbf)
250254
add_math_entrypoint_object(llogbl)

libc/src/math/generic/CMakeLists.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2848,6 +2848,36 @@ add_entrypoint_object(
28482848
-O3
28492849
)
28502850

2851+
add_entrypoint_object(
2852+
isnan
2853+
SRCS
2854+
isnan.cpp
2855+
HDRS
2856+
../isnan.h
2857+
COMPILE_OPTIONS
2858+
-O3
2859+
)
2860+
2861+
add_entrypoint_object(
2862+
isnanf
2863+
SRCS
2864+
isnanf.cpp
2865+
HDRS
2866+
../isnanf.h
2867+
COMPILE_OPTIONS
2868+
-O3
2869+
)
2870+
2871+
add_entrypoint_object(
2872+
isnanl
2873+
SRCS
2874+
isnanl.cpp
2875+
HDRS
2876+
../isnanl.h
2877+
COMPILE_OPTIONS
2878+
-O3
2879+
)
2880+
28512881
add_entrypoint_object(
28522882
nan
28532883
SRCS

libc/src/math/generic/isnan.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//===-- Implementation of isnan 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/isnan.h"
10+
#include "src/__support/common.h"
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
LLVM_LIBC_FUNCTION(int, isnan, (double x)) { return __builtin_isnan(x); }
15+
16+
} // namespace LIBC_NAMESPACE

libc/src/math/generic/isnanf.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//===-- Implementation of isnanf 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/isnanf.h"
10+
#include "src/__support/common.h"
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
LLVM_LIBC_FUNCTION(int, isnanf, (float x)) { return __builtin_isnan(x); }
15+
16+
} // namespace LIBC_NAMESPACE

libc/src/math/generic/isnanl.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//===-- Implementation of isnanl 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/isnanl.h"
10+
#include "src/__support/common.h"
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
LLVM_LIBC_FUNCTION(int, isnanl, (long double x)) { return __builtin_isnan(x); }
15+
16+
} // namespace LIBC_NAMESPACE

libc/src/math/isnan.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation header for isnan -------------------------*- 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_ISNAN_H
10+
#define LLVM_LIBC_SRC_MATH_ISNAN_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
int isnan(double x);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_MATH_ISNAN_H

libc/src/math/isnanf.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation header for isnanf ------------------------*- 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_ISNANF_H
10+
#define LLVM_LIBC_SRC_MATH_ISNANF_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
int isnanf(float x);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_MATH_ISNANF_H

libc/src/math/isnanl.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation header for isnanl ------------------------*- 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_ISNANL_H
10+
#define LLVM_LIBC_SRC_MATH_ISNANL_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
int isnanl(long double x);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_MATH_ISNANL_H

0 commit comments

Comments
 (0)