Skip to content

Commit 56124fe

Browse files
authored
[libc][math] Implement fpclassify macro. (#109519)
#109201
1 parent 2f664f2 commit 56124fe

File tree

8 files changed

+161
-0
lines changed

8 files changed

+161
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ add_macro_header(
121121
math_function_macros
122122
HDR
123123
math-function-macros.h
124+
DEPENDS
125+
.math_macros
124126
)
125127

126128
add_macro_header(

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@
99
#ifndef LLVM_LIBC_MACROS_MATH_FUNCTION_MACROS_H
1010
#define LLVM_LIBC_MACROS_MATH_FUNCTION_MACROS_H
1111

12+
#include "math-macros.h"
13+
1214
#define isfinite(x) __builtin_isfinite(x)
1315
#define isinf(x) __builtin_isinf(x)
1416
#define isnan(x) __builtin_isnan(x)
1517
#define signbit(x) __builtin_signbit(x)
1618
#define iszero(x) (x == 0)
19+
#define fpclassify(x) \
20+
__builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL, FP_ZERO, x)
1721

1822
#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+
fpclassify_test
86+
SUITE
87+
libc_include_tests
88+
SRCS
89+
fpclassify_test.cpp
90+
DEPENDS
91+
libc.include.llvm-libc-macros.math_function_macros
92+
)
93+
94+
add_libc_test(
95+
fpclassifyf_test
96+
SUITE
97+
libc_include_tests
98+
SRCS
99+
fpclassifyf_test.cpp
100+
DEPENDS
101+
libc.include.llvm-libc-macros.math_function_macros
102+
)
103+
104+
add_libc_test(
105+
fpclassifyl_test
106+
SUITE
107+
libc_include_tests
108+
SRCS
109+
fpclassifyl_test.cpp
110+
DEPENDS
111+
libc.include.llvm-libc-macros.math_function_macros
112+
)
113+
84114
add_libc_test(
85115
iszero_test
86116
SUITE
@@ -291,6 +321,21 @@ add_libc_test(
291321
libc.include.llvm-libc-macros.math_function_macros
292322
)
293323

324+
add_libc_test(
325+
fpclassify_c_test
326+
C_TEST
327+
UNIT_TEST_ONLY
328+
SUITE
329+
libc_include_tests
330+
SRCS
331+
fpclassify_test.c
332+
COMPILE_OPTIONS
333+
-Wall
334+
-Werror
335+
DEPENDS
336+
libc.include.llvm-libc-macros.math_function_macros
337+
)
338+
294339
add_libc_test(
295340
iszero_c_test
296341
C_TEST

libc/test/include/FpClassifyTest.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 fpclassify 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_FPCLASSIFY_H
10+
#define LLVM_LIBC_TEST_INCLUDE_MATH_FPCLASSIFY_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 FpClassifyTest : public LIBC_NAMESPACE::testing::Test {
19+
DECLARE_SPECIAL_CONSTANTS(T)
20+
21+
public:
22+
typedef int (*FpClassifyFunc)(T);
23+
24+
void testSpecialNumbers(FpClassifyFunc func) {
25+
EXPECT_EQ(func(aNaN), FP_NAN);
26+
EXPECT_EQ(func(neg_aNaN), FP_NAN);
27+
EXPECT_EQ(func(sNaN), FP_NAN);
28+
EXPECT_EQ(func(neg_sNaN), FP_NAN);
29+
EXPECT_EQ(func(inf), FP_INFINITE);
30+
EXPECT_EQ(func(neg_inf), FP_INFINITE);
31+
EXPECT_EQ(func(min_normal), FP_NORMAL);
32+
EXPECT_EQ(func(max_normal), FP_NORMAL);
33+
EXPECT_EQ(func(neg_max_normal), FP_NORMAL);
34+
EXPECT_EQ(func(min_denormal), FP_SUBNORMAL);
35+
EXPECT_EQ(func(neg_min_denormal), FP_SUBNORMAL);
36+
EXPECT_EQ(func(max_denormal), FP_SUBNORMAL);
37+
EXPECT_EQ(func(zero), FP_ZERO);
38+
EXPECT_EQ(func(neg_zero), FP_ZERO);
39+
}
40+
};
41+
42+
#define LIST_FPCLASSIFY_TESTS(T, func) \
43+
using LlvmLibcFpClassifyTest = FpClassifyTest<T>; \
44+
TEST_F(LlvmLibcFpClassifyTest, SpecialNumbers) { \
45+
auto fpclassify_func = [](T x) { return func(x); }; \
46+
testSpecialNumbers(fpclassify_func); \
47+
}
48+
49+
#endif // LLVM_LIBC_TEST_INCLUDE_MATH_FPCLASSIFY_H

libc/test/include/fpclassify_test.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===-- Unittests for fpclassify 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+
// check if macro is defined
13+
#ifndef fpclassify
14+
#error "fpclassify macro is not defined"
15+
#else
16+
int main(void) {
17+
assert(fpclassify(1.819f) == FP_NORMAL);
18+
assert(fpclassify(-1.726) == FP_NORMAL);
19+
assert(fpclassify(1.426L) == FP_NORMAL);
20+
assert(fpclassify(-0.0f) == FP_ZERO);
21+
assert(fpclassify(0.0) == FP_ZERO);
22+
assert(fpclassify(-0.0L) == FP_ZERO);
23+
return 0;
24+
}
25+
#endif

libc/test/include/fpclassify_test.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//===-- Unittest for fpclassify[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 "FpClassifyTest.h"
10+
#include "include/llvm-libc-macros/math-function-macros.h"
11+
12+
LIST_FPCLASSIFY_TESTS(double, fpclassify)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//===-- Unittest for fpclassify[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 "FpClassifyTest.h"
10+
#include "include/llvm-libc-macros/math-function-macros.h"
11+
12+
LIST_FPCLASSIFY_TESTS(float, fpclassify)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//===-- Unittest for fpclassify[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 "FpClassifyTest.h"
10+
#include "include/llvm-libc-macros/math-function-macros.h"
11+
12+
LIST_FPCLASSIFY_TESTS(long double, fpclassify)

0 commit comments

Comments
 (0)