Skip to content

Commit 4e33afe

Browse files
authored
[libc][math] Implement issignaling and iscanonical macro. (#111403)
#109201
1 parent 3a08551 commit 4e33afe

File tree

9 files changed

+76
-141
lines changed

9 files changed

+76
-141
lines changed

libc/cmake/modules/LLVMLibCTestRules.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ function(get_object_files_for_test result skipped_entrypoints_list)
6868
endif()
6969
get_target_property(object_file_raw ${dep} "OBJECT_FILE_RAW")
7070
if(object_file_raw)
71+
# TODO: Remove this once we stop suffixing the target with ".__internal__"
72+
if(fq_target_name STREQUAL "libc.test.include.issignaling_c_test.__unit__" OR fq_target_name STREQUAL "libc.test.include.iscanonical_c_test.__unit__")
73+
string(REPLACE ".__internal__" "" object_file_raw ${object_file_raw})
74+
endif()
7175
list(APPEND dep_obj ${object_file_raw})
7276
endif()
7377
elseif(${dep_type} STREQUAL ${ENTRYPOINT_OBJ_VENDOR_TARGET_TYPE})

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@
1111

1212
#include "math-macros.h"
1313

14+
#ifndef __cplusplus
15+
#define issignaling(x) \
16+
_Generic((x), \
17+
float: issignalingf, \
18+
double: issignaling, \
19+
long double: issignalingl)(x)
20+
#define iscanonical(x) \
21+
_Generic((x), \
22+
float: iscanonicalf, \
23+
double: iscanonical, \
24+
long double: iscanonicall)(x)
25+
#endif
26+
1427
#define isfinite(x) __builtin_isfinite(x)
1528
#define isinf(x) __builtin_isinf(x)
1629
#define isnan(x) __builtin_isnan(x)
@@ -20,9 +33,5 @@
2033
__builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL, FP_ZERO, x)
2134
#define isnormal(x) __builtin_isnormal(x)
2235
#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
2736

2837
#endif // LLVM_LIBC_MACROS_MATH_FUNCTION_MACROS_H

libc/test/include/CMakeLists.txt

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -81,36 +81,6 @@ 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-
11484
add_libc_test(
11585
issubnormal_test
11686
SUITE
@@ -409,6 +379,27 @@ add_libc_test(
409379
-Werror
410380
DEPENDS
411381
libc.include.llvm-libc-macros.math_function_macros
382+
libc.src.math.issignaling
383+
libc.src.math.issignalingf
384+
libc.src.math.issignalingl
385+
)
386+
387+
add_libc_test(
388+
iscanonical_c_test
389+
C_TEST
390+
UNIT_TEST_ONLY
391+
SUITE
392+
libc_include_tests
393+
SRCS
394+
iscanonical_test.c
395+
COMPILE_OPTIONS
396+
-Wall
397+
-Werror
398+
DEPENDS
399+
libc.include.llvm-libc-macros.math_function_macros
400+
libc.src.math.iscanonical
401+
libc.src.math.iscanonicalf
402+
libc.src.math.iscanonicall
412403
)
413404

414405
add_libc_test(

libc/test/include/IsSignalingTest.h

Lines changed: 0 additions & 49 deletions
This file was deleted.

libc/test/include/iscanonical_test.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//===-- Unittests for iscanonical 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+
int iscanonical(double);
9+
int iscanonicalf(float);
10+
int iscanonicall(long double);
11+
12+
#include "include/llvm-libc-macros/math-function-macros.h"
13+
14+
#include <assert.h>
15+
16+
// check if macro is defined
17+
#ifndef iscanonical
18+
#error "iscanonical macro is not defined"
19+
#else
20+
int main(void) {
21+
assert(iscanonical(__builtin_nans("")) == 0);
22+
assert(iscanonical(__builtin_nansf("")) == 0);
23+
assert(iscanonical(__builtin_nansl("")) == 0);
24+
assert(iscanonical(1.819f) == 1);
25+
assert(iscanonical(-1.726) == 1);
26+
assert(iscanonical(1.426L) == 1);
27+
return 0;
28+
}
29+
#endif

libc/test/include/issignaling_test.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,25 @@
55
// SPDSList-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
8+
int issignaling(double);
9+
int issignalingf(float);
10+
int issignalingl(long double);
11+
812
#include "include/llvm-libc-macros/math-function-macros.h"
913

1014
#include <assert.h>
1115

12-
// TODO: enable the test unconditionally when issignaling macro is fixed for
13-
// older compiler
16+
// check if macro is defined
17+
#ifndef issignaling
18+
#error "issignaling macro is not defined"
19+
#else
1420
int main(void) {
15-
#ifdef issignaling
1621
assert(issignaling(__builtin_nans("")) == 1);
1722
assert(issignaling(__builtin_nansf("")) == 1);
1823
assert(issignaling(__builtin_nansl("")) == 1);
1924
assert(issignaling(1.819f) == 0);
2025
assert(issignaling(-1.726) == 0);
2126
assert(issignaling(1.426L) == 0);
22-
#endif
2327
return 0;
2428
}
29+
#endif

libc/test/include/issignaling_test.cpp

Lines changed: 0 additions & 18 deletions
This file was deleted.

libc/test/include/issignalingf_test.cpp

Lines changed: 0 additions & 18 deletions
This file was deleted.

libc/test/include/issignalingl_test.cpp

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)