Skip to content

Commit d69e5ce

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 f00f11b commit d69e5ce

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
@@ -261,6 +261,9 @@ set(TARGET_LIBM_ENTRYPOINTS
261261
libc.src.math.ilogb
262262
libc.src.math.ilogbf
263263
libc.src.math.ilogbl
264+
libc.src.math.isnan
265+
libc.src.math.isnanf
266+
libc.src.math.isnanl
264267
libc.src.math.ldexp
265268
libc.src.math.ldexpf
266269
libc.src.math.ldexpl

libc/config/baremetal/riscv/entrypoints.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,9 @@ set(TARGET_LIBM_ENTRYPOINTS
260260
libc.src.math.ilogb
261261
libc.src.math.ilogbf
262262
libc.src.math.ilogbl
263+
libc.src.math.isnan
264+
libc.src.math.isnanf
265+
libc.src.math.isnanl
263266
libc.src.math.ldexp
264267
libc.src.math.ldexpf
265268
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
@@ -219,6 +219,10 @@ add_math_entrypoint_object(ilogbl)
219219
add_math_entrypoint_object(ilogbf16)
220220
add_math_entrypoint_object(ilogbf128)
221221

222+
add_math_entrypoint_object(isnan)
223+
add_math_entrypoint_object(isnanf)
224+
add_math_entrypoint_object(isnanl)
225+
222226
add_math_entrypoint_object(llogb)
223227
add_math_entrypoint_object(llogbf)
224228
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
@@ -2684,6 +2684,36 @@ add_entrypoint_object(
26842684
-O3
26852685
)
26862686

2687+
add_entrypoint_object(
2688+
isnan
2689+
SRCS
2690+
isnan.cpp
2691+
HDRS
2692+
../isnan.h
2693+
COMPILE_OPTIONS
2694+
-O3
2695+
)
2696+
2697+
add_entrypoint_object(
2698+
isnanf
2699+
SRCS
2700+
isnanf.cpp
2701+
HDRS
2702+
../isnanf.h
2703+
COMPILE_OPTIONS
2704+
-O3
2705+
)
2706+
2707+
add_entrypoint_object(
2708+
isnanl
2709+
SRCS
2710+
isnanl.cpp
2711+
HDRS
2712+
../isnanl.h
2713+
COMPILE_OPTIONS
2714+
-O3
2715+
)
2716+
26872717
add_entrypoint_object(
26882718
nan
26892719
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)