Skip to content

Commit ac802a3

Browse files
authored
[libc][math] Implement issignaling macro. (#109615)
#109201
1 parent 26e0b50 commit ac802a3

File tree

7 files changed

+176
-0
lines changed

7 files changed

+176
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,9 @@
2020
__builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL, FP_ZERO, x)
2121
#define isnormal(x) __builtin_isnormal(x)
2222
#define issubnormal(x) (fpclassify(x) == FP_SUBNORMAL)
23+
#if (defined(__clang__) && __clang_major__ >= 18) || \
24+
(defined(__GNUC__) && __GNUC__ >= 13)
25+
#define issignaling(x) __builtin_issignaling(x)
26+
#endif
2327

2428
#endif // LLVM_LIBC_MACROS_MATH_FUNCTION_MACROS_H

libc/test/include/CMakeLists.txt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,36 @@ add_libc_test(
8181
libc.include.llvm-libc-macros.stdckdint_macros
8282
)
8383

84+
add_libc_test(
85+
issignaling_test
86+
SUITE
87+
libc_include_tests
88+
SRCS
89+
issignaling_test.cpp
90+
DEPENDS
91+
libc.include.llvm-libc-macros.math_function_macros
92+
)
93+
94+
add_libc_test(
95+
issignalingf_test
96+
SUITE
97+
libc_include_tests
98+
SRCS
99+
issignalingf_test.cpp
100+
DEPENDS
101+
libc.include.llvm-libc-macros.math_function_macros
102+
)
103+
104+
add_libc_test(
105+
issignalingl_test
106+
SUITE
107+
libc_include_tests
108+
SRCS
109+
issignalingl_test.cpp
110+
DEPENDS
111+
libc.include.llvm-libc-macros.math_function_macros
112+
)
113+
84114
add_libc_test(
85115
issubnormal_test
86116
SUITE
@@ -366,6 +396,21 @@ add_libc_test(
366396
libc.include.llvm-libc-macros.math_function_macros
367397
)
368398

399+
add_libc_test(
400+
issignaling_c_test
401+
C_TEST
402+
UNIT_TEST_ONLY
403+
SUITE
404+
libc_include_tests
405+
SRCS
406+
issignaling_test.c
407+
COMPILE_OPTIONS
408+
-Wall
409+
-Werror
410+
DEPENDS
411+
libc.include.llvm-libc-macros.math_function_macros
412+
)
413+
369414
add_libc_test(
370415
isinf_c_test
371416
C_TEST

libc/test/include/IsSignalingTest.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//===-- Utility class to test the issignaling macro ------------*- 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_TEST_INCLUDE_MATH_ISSIGNALING_H
10+
#define LLVM_LIBC_TEST_INCLUDE_MATH_ISSIGNALING_H
11+
12+
#include "test/UnitTest/FPMatcher.h"
13+
#include "test/UnitTest/Test.h"
14+
15+
#include "include/llvm-libc-macros/math-function-macros.h"
16+
17+
template <typename T>
18+
class IsSignalingTest : public LIBC_NAMESPACE::testing::Test {
19+
DECLARE_SPECIAL_CONSTANTS(T)
20+
21+
public:
22+
typedef int (*IsSignalingFunc)(T);
23+
24+
void testSpecialNumbers(IsSignalingFunc func) {
25+
EXPECT_EQ(func(aNaN), 0);
26+
EXPECT_EQ(func(neg_aNaN), 0);
27+
EXPECT_EQ(func(sNaN), 1);
28+
EXPECT_EQ(func(neg_sNaN), 1);
29+
EXPECT_EQ(func(inf), 0);
30+
EXPECT_EQ(func(neg_inf), 0);
31+
EXPECT_EQ(func(min_normal), 0);
32+
EXPECT_EQ(func(max_normal), 0);
33+
EXPECT_EQ(func(neg_max_normal), 0);
34+
EXPECT_EQ(func(min_denormal), 0);
35+
EXPECT_EQ(func(neg_min_denormal), 0);
36+
EXPECT_EQ(func(max_denormal), 0);
37+
EXPECT_EQ(func(zero), 0);
38+
EXPECT_EQ(func(neg_zero), 0);
39+
}
40+
};
41+
42+
#define LIST_ISSIGNALING_TESTS(T, func) \
43+
using LlvmLibcIsSignalingTest = IsSignalingTest<T>; \
44+
TEST_F(LlvmLibcIsSignalingTest, SpecialNumbers) { \
45+
auto issignaling_func = [](T x) { return func(x); }; \
46+
testSpecialNumbers(issignaling_func); \
47+
}
48+
49+
#endif // LLVM_LIBC_TEST_INCLUDE_MATH_ISSIGNALING_H

libc/test/include/issignaling_test.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===-- Unittests for issignaling macro -----------------------------------===//
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+
// SPDSList-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
#include "include/llvm-libc-macros/math-function-macros.h"
9+
10+
#include <assert.h>
11+
12+
// TODO: enable the test unconditionally when issignaling macro is fixed for
13+
// older compiler
14+
int main(void) {
15+
#ifdef issignaling
16+
assert(issignaling(__builtin_nans("")) == 1);
17+
assert(issignaling(__builtin_nansf("")) == 1);
18+
assert(issignaling(__builtin_nansl("")) == 1);
19+
assert(issignaling(1.819f) == 0);
20+
assert(issignaling(-1.726) == 0);
21+
assert(issignaling(1.426L) == 0);
22+
#endif
23+
return 0;
24+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Unittest for issignaling[d] macro ---------------------------------===//
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+
// SPDSList-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "IsSignalingTest.h"
10+
#include "include/llvm-libc-macros/math-function-macros.h"
11+
12+
// TODO: enable the test unconditionally when issignaling macro is fixed for
13+
// older compiler
14+
#ifdef issignaling
15+
LIST_ISSIGNALING_TESTS(double, issignaling)
16+
#else
17+
int main() { return 0; }
18+
#endif
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Unittest for issignaling[f] macro ---------------------------------===//
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+
// SPDSList-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "IsSignalingTest.h"
10+
#include "include/llvm-libc-macros/math-function-macros.h"
11+
12+
// TODO: enable the test unconditionally when issignaling macro is fixed for
13+
// older compiler
14+
#ifdef issignaling
15+
LIST_ISSIGNALING_TESTS(float, issignaling)
16+
#else
17+
int main() { return 0; }
18+
#endif
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Unittest for issignaling[l] macro ---------------------------------===//
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+
// SPDSList-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "IsSignalingTest.h"
10+
#include "include/llvm-libc-macros/math-function-macros.h"
11+
12+
// TODO: enable the test unconditionally when issignaling macro is fixed for
13+
// older compiler
14+
#ifdef issignaling
15+
LIST_ISSIGNALING_TESTS(long double, issignaling)
16+
#else
17+
int main() { return 0; }
18+
#endif

0 commit comments

Comments
 (0)