Skip to content

Commit 8536340

Browse files
committed
[libc] Provide isnan, isnanf and isnanl functions
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 8d23719 commit 8536340

File tree

13 files changed

+163
-1
lines changed

13 files changed

+163
-1
lines changed

libc/config/baremetal/arm/entrypoints.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,9 @@ set(TARGET_LIBM_ENTRYPOINTS
273273
libc.src.math.ilogb
274274
libc.src.math.ilogbf
275275
libc.src.math.ilogbl
276+
libc.src.math.isnan
277+
libc.src.math.isnanf
278+
libc.src.math.isnanl
276279
libc.src.math.ldexp
277280
libc.src.math.ldexpf
278281
libc.src.math.ldexpl

libc/config/baremetal/riscv/entrypoints.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,9 @@ set(TARGET_LIBM_ENTRYPOINTS
268268
libc.src.math.ilogb
269269
libc.src.math.ilogbf
270270
libc.src.math.ilogbl
271+
libc.src.math.isnan
272+
libc.src.math.isnanf
273+
libc.src.math.isnanl
271274
libc.src.math.ldexp
272275
libc.src.math.ldexpf
273276
libc.src.math.ldexpl

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,5 @@
5454
// TODO: Move generic functional math macros to a separate header file.
5555
#define isfinite(x) __builtin_isfinite(x)
5656
#define isinf(x) __builtin_isinf(x)
57-
#define isnan(x) __builtin_isnan(x)
5857

5958
#endif // LLVM_LIBC_MACROS_MATH_MACROS_H

libc/include/math.h.def

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

1818
%%public_api()
1919

20+
#define isnan(x) __builtin_isnan(x)
21+
2022
#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
@@ -223,6 +223,10 @@ add_math_entrypoint_object(ilogbl)
223223
add_math_entrypoint_object(ilogbf16)
224224
add_math_entrypoint_object(ilogbf128)
225225

226+
add_math_entrypoint_object(isnan)
227+
add_math_entrypoint_object(isnanf)
228+
add_math_entrypoint_object(isnanl)
229+
226230
add_math_entrypoint_object(llogb)
227231
add_math_entrypoint_object(llogbf)
228232
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
@@ -2791,6 +2791,36 @@ add_entrypoint_object(
27912791
-O3
27922792
)
27932793

2794+
add_entrypoint_object(
2795+
isnan
2796+
SRCS
2797+
isnan.cpp
2798+
HDRS
2799+
../isnan.h
2800+
COMPILE_OPTIONS
2801+
-O3
2802+
)
2803+
2804+
add_entrypoint_object(
2805+
isnanf
2806+
SRCS
2807+
isnanf.cpp
2808+
HDRS
2809+
../isnanf.h
2810+
COMPILE_OPTIONS
2811+
-O3
2812+
)
2813+
2814+
add_entrypoint_object(
2815+
isnanl
2816+
SRCS
2817+
isnanl.cpp
2818+
HDRS
2819+
../isnanl.h
2820+
COMPILE_OPTIONS
2821+
-O3
2822+
)
2823+
27942824
add_entrypoint_object(
27952825
nan
27962826
SRCS

libc/src/math/generic/isnan.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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)) {
15+
return __builtin_isnan(x);
16+
}
17+
18+
} // namespace LIBC_NAMESPACE

libc/src/math/generic/isnanf.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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)) {
15+
return __builtin_isnan(x);
16+
}
17+
18+
} // namespace LIBC_NAMESPACE

libc/src/math/generic/isnanl.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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)) {
15+
return __builtin_isnan(x);
16+
}
17+
18+
} // 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)