Skip to content

[libc][math] Implement issignaling and iscanonical macro. #111403

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions libc/cmake/modules/LLVMLibCTestRules.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ function(get_object_files_for_test result skipped_entrypoints_list)
endif()
get_target_property(object_file_raw ${dep} "OBJECT_FILE_RAW")
if(object_file_raw)
if(fq_target_name STREQUAL "libc.test.include.issignaling_c_test.__unit__" OR fq_target_name STREQUAL "libc.test.include.iscanonical_c_test.__unit__")
string(REPLACE ".__internal__" "" object_file_raw ${object_file_raw})
endif()
list(APPEND dep_obj ${object_file_raw})
endif()
elseif(${dep_type} STREQUAL ${ENTRYPOINT_OBJ_VENDOR_TARGET_TYPE})
Expand Down
17 changes: 13 additions & 4 deletions libc/include/llvm-libc-macros/math-function-macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@

#include "math-macros.h"

#ifndef __cplusplus
#define issignaling(x) \
_Generic((x), \
float: issignalingf, \
double: issignaling, \
long double: issignalingl)(x)
#define iscanonical(x) \
_Generic((x), \
float: iscanonicalf, \
double: iscanonical, \
long double: iscanonicall)(x)
#endif

#define isfinite(x) __builtin_isfinite(x)
#define isinf(x) __builtin_isinf(x)
#define isnan(x) __builtin_isnan(x)
Expand All @@ -20,9 +33,5 @@
__builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL, FP_ZERO, x)
#define isnormal(x) __builtin_isnormal(x)
#define issubnormal(x) (fpclassify(x) == FP_SUBNORMAL)
#if (defined(__clang__) && __clang_major__ >= 18) || \
(defined(__GNUC__) && __GNUC__ >= 13)
#define issignaling(x) __builtin_issignaling(x)
#endif

#endif // LLVM_LIBC_MACROS_MATH_FUNCTION_MACROS_H
51 changes: 21 additions & 30 deletions libc/test/include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,36 +81,6 @@ add_libc_test(
libc.include.llvm-libc-macros.stdckdint_macros
)

add_libc_test(
issignaling_test
SUITE
libc_include_tests
SRCS
issignaling_test.cpp
DEPENDS
libc.include.llvm-libc-macros.math_function_macros
)

add_libc_test(
issignalingf_test
SUITE
libc_include_tests
SRCS
issignalingf_test.cpp
DEPENDS
libc.include.llvm-libc-macros.math_function_macros
)

add_libc_test(
issignalingl_test
SUITE
libc_include_tests
SRCS
issignalingl_test.cpp
DEPENDS
libc.include.llvm-libc-macros.math_function_macros
)

add_libc_test(
issubnormal_test
SUITE
Expand Down Expand Up @@ -409,6 +379,27 @@ add_libc_test(
-Werror
DEPENDS
libc.include.llvm-libc-macros.math_function_macros
libc.src.math.issignaling
libc.src.math.issignalingf
libc.src.math.issignalingl
)

add_libc_test(
iscanonical_c_test
C_TEST
UNIT_TEST_ONLY
SUITE
libc_include_tests
SRCS
iscanonical_test.c
COMPILE_OPTIONS
-Wall
-Werror
DEPENDS
libc.include.llvm-libc-macros.math_function_macros
libc.src.math.iscanonical
libc.src.math.iscanonicalf
libc.src.math.iscanonicall
)

add_libc_test(
Expand Down
49 changes: 0 additions & 49 deletions libc/test/include/IsSignalingTest.h

This file was deleted.

29 changes: 29 additions & 0 deletions libc/test/include/iscanonical_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//===-- Unittests for iscanonical macro -----------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDSList-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
int iscanonical(double);
int iscanonicalf(float);
int iscanonicall(long double);

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

#include <assert.h>

// check if macro is defined
#ifndef iscanonical
#error "iscanonical macro is not defined"
#else
int main(void) {
assert(iscanonical(__builtin_nans("")) == 0);
assert(iscanonical(__builtin_nansf("")) == 0);
assert(iscanonical(__builtin_nansl("")) == 0);
assert(iscanonical(1.819f) == 1);
assert(iscanonical(-1.726) == 1);
assert(iscanonical(1.426L) == 1);
return 0;
}
#endif
13 changes: 9 additions & 4 deletions libc/test/include/issignaling_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,25 @@
// SPDSList-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
int issignaling(double);
int issignalingf(float);
int issignalingl(long double);

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

#include <assert.h>

// TODO: enable the test unconditionally when issignaling macro is fixed for
// older compiler
// check if macro is defined
#ifndef issignaling
#error "issignaling macro is not defined"
#else
int main(void) {
#ifdef issignaling
assert(issignaling(__builtin_nans("")) == 1);
assert(issignaling(__builtin_nansf("")) == 1);
assert(issignaling(__builtin_nansl("")) == 1);
assert(issignaling(1.819f) == 0);
assert(issignaling(-1.726) == 0);
assert(issignaling(1.426L) == 0);
#endif
return 0;
}
#endif
18 changes: 0 additions & 18 deletions libc/test/include/issignaling_test.cpp

This file was deleted.

18 changes: 0 additions & 18 deletions libc/test/include/issignalingf_test.cpp

This file was deleted.

18 changes: 0 additions & 18 deletions libc/test/include/issignalingl_test.cpp

This file was deleted.

Loading