Skip to content

[libc][math] Implement issubnormal macro. #109572

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 3 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
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/include/llvm-libc-macros/math-function-macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@
#define fpclassify(x) \
__builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL, FP_ZERO, x)
#define isnormal(x) __builtin_isnormal(x)
#define issubnormal(x) \
(__builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL, FP_ZERO, \
x) == FP_SUBNORMAL)

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

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

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

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

add_libc_test(
isnormal_test
SUITE
Expand Down Expand Up @@ -366,6 +396,21 @@ add_libc_test(
libc.include.llvm-libc-macros.math_function_macros
)

add_libc_test(
issubnormal_c_test
C_TEST
UNIT_TEST_ONLY
SUITE
libc_include_tests
SRCS
issubnormal_test.c
COMPILE_OPTIONS
-Wall
-Werror
DEPENDS
libc.include.llvm-libc-macros.math_function_macros
)

add_libc_test(
fpclassify_c_test
C_TEST
Expand Down
49 changes: 49 additions & 0 deletions libc/test/include/IsSubnormalTest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//===-- Utility class to test the issubnormal macro ------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_TEST_INCLUDE_MATH_ISSUBNORMAL_H
#define LLVM_LIBC_TEST_INCLUDE_MATH_ISSUBNORMAL_H

#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"

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

template <typename T>
class IsSubnormalTest : public LIBC_NAMESPACE::testing::Test {
DECLARE_SPECIAL_CONSTANTS(T)

public:
typedef bool (*IsSubnormalFunc)(T);

void testSpecialNumbers(IsSubnormalFunc func) {
EXPECT_FALSE(func(aNaN));
EXPECT_FALSE(func(neg_aNaN));
EXPECT_FALSE(func(sNaN));
EXPECT_FALSE(func(neg_sNaN));
EXPECT_FALSE(func(inf));
EXPECT_FALSE(func(neg_inf));
EXPECT_FALSE(func(min_normal));
EXPECT_FALSE(func(max_normal));
EXPECT_FALSE(func(neg_max_normal));
EXPECT_TRUE(func(min_denormal));
EXPECT_TRUE(func(neg_min_denormal));
EXPECT_TRUE(func(max_denormal));
EXPECT_FALSE(func(zero));
EXPECT_FALSE(func(neg_zero));
}
};

#define LIST_ISSUBNORMAL_TESTS(T, func) \
using LlvmLibcIsSubnormalTest = IsSubnormalTest<T>; \
TEST_F(LlvmLibcIsSubnormalTest, SpecialNumbers) { \
auto issubnormal_func = [](T x) { return func(x); }; \
testSpecialNumbers(issubnormal_func); \
}

#endif // LLVM_LIBC_TEST_INCLUDE_MATH_ISSUBNORMAL_H
24 changes: 24 additions & 0 deletions libc/test/include/issubnormal_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//===-- Unittests for issubnormal 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
//
//===----------------------------------------------------------------------===//
#include "include/llvm-libc-macros/math-function-macros.h"

#include <assert.h>

// check if macro is defined
#ifndef issubnormal
#error "issubnormal macro is not defined"
#else
int main(void) {
assert(issubnormal(1.819f) == 0);
assert(issubnormal(-1.726) == 0);
assert(issubnormal(1.426L) == 0);
assert(issubnormal(1e-308) == 1);
assert(issubnormal(-1e-308) == 1);
return 0;
}
#endif
12 changes: 12 additions & 0 deletions libc/test/include/issubnormal_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//===-- Unittest for issubnormal[d] 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
//
//===----------------------------------------------------------------------===//

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

LIST_ISSUBNORMAL_TESTS(double, issubnormal)
12 changes: 12 additions & 0 deletions libc/test/include/issubnormalf_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//===-- Unittest for issubnormal[f] 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
//
//===----------------------------------------------------------------------===//

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

LIST_ISSUBNORMAL_TESTS(float, issubnormal)
12 changes: 12 additions & 0 deletions libc/test/include/issubnormall_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//===-- Unittest for issubnormal[l] 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
//
//===----------------------------------------------------------------------===//

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

LIST_ISSUBNORMAL_TESTS(long double, issubnormal)
Loading