[libc][math][c23] Add fabsf16 C23 math function by overmighty · Pull Request #93567 · llvm/llvm-project (original) (raw)

@llvm/pr-subscribers-libc

Author: OverMighty (overmighty)

Changes

cc @lntue


Full diff: https://github.com/llvm/llvm-project/pull/93567.diff

16 Files Affected:

diff --git a/libc/cmake/modules/CheckCompilerFeatures.cmake b/libc/cmake/modules/CheckCompilerFeatures.cmake index 3a9e1e3b1cf8b..17806588550eb 100644 --- a/libc/cmake/modules/CheckCompilerFeatures.cmake +++ b/libc/cmake/modules/CheckCompilerFeatures.cmake @@ -2,7 +2,7 @@

Compiler features definition and flags

------------------------------------------------------------------------------

-set(ALL_COMPILER_FEATURES "float128" "fixed_point") +set(ALL_COMPILER_FEATURES "float16" "float128" "fixed_point")

Making sure ALL_COMPILER_FEATURES is sorted.

list(SORT ALL_COMPILER_FEATURES) @@ -54,7 +54,9 @@ foreach(feature IN LISTS ALL_COMPILER_FEATURES) ) if(has_feature) list(APPEND AVAILABLE_COMPILER_FEATURES ${feature})

+#error unsupported +#endif diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt index 5e3ddd34fb4dc..03cd17a8090cb 100644 --- a/libc/config/linux/x86_64/entrypoints.txt +++ b/libc/config/linux/x86_64/entrypoints.txt @@ -528,6 +528,13 @@ set(TARGET_LIBM_ENTRYPOINTS libc.src.math.ufromfpxl )

+if(LIBC_TYPES_HAS_FLOAT16)

diff --git a/libc/include/llvm-libc-types/CMakeLists.txt b/libc/include/llvm-libc-types/CMakeLists.txt index 018b6c58316c3..6f3e123f78d8f 100644 --- a/libc/include/llvm-libc-types/CMakeLists.txt +++ b/libc/include/llvm-libc-types/CMakeLists.txt @@ -119,6 +119,7 @@ add_header(ENTRY HDR ENTRY.h) add_header(struct_hsearch_data HDR struct_hsearch_data.h) add_header(struct_epoll_event HDR struct_epoll_event.h) add_header(struct_epoll_data HDR struct_epoll_data.h) +add_header(float16 HDR float16.h) add_header( float128 HDR diff --git a/libc/include/llvm-libc-types/float16.h b/libc/include/llvm-libc-types/float16.h new file mode 100644 index 0000000000000..671d9c1b97b7f --- /dev/null +++ b/libc/include/llvm-libc-types/float16.h @@ -0,0 +1,38 @@ +//===-- Definition of float16 type ----------------------------------------===// +// +// 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_TYPES_FLOAT16_H +#define LLVM_LIBC_TYPES_FLOAT16_H + +#include "src/__support/macros/properties/architectures.h" +#include "src/__support/macros/properties/compiler.h" +#include "src/__support/macros/properties/cpu_features.h" + +#if defined(LIBC_TARGET_ARCH_IS_X86_64) && defined(LIBC_TARGET_CPU_HAS_SSE2) +#if (defined(LIBC_COMPILER_CLANG_VER) && (LIBC_COMPILER_CLANG_VER >= 1500)) || \

+#define LIBC_TYPES_HAS_FLOAT16 +using float16 = _Float16; +#endif +#endif +#if defined(LIBC_TARGET_ARCH_IS_AARCH64) +#if (defined(LIBC_COMPILER_CLANG_VER) && (LIBC_COMPILER_CLANG_VER >= 900)) || \

+#define LIBC_TYPES_HAS_FLOAT16 +using float16 = _Float16; +#endif +#endif +#if defined(LIBC_TARGET_ARCH_IS_ANY_RISCV) +#if (defined(LIBC_COMPILER_CLANG_VER) && (LIBC_COMPILER_CLANG_VER >= 1300)) || \

+#define LIBC_TYPES_HAS_FLOAT16 +using float16 = _Float16; +#endif +#endif + +#endif // LLVM_LIBC_TYPES_FLOAT16_H diff --git a/libc/spec/spec.td b/libc/spec/spec.td index ea8fa4cd373cf..966e1f5df47c1 100644 --- a/libc/spec/spec.td +++ b/libc/spec/spec.td @@ -53,6 +53,7 @@ def UnsignedCharType : NamedType<"unsigned char">; def UnsignedShortType : NamedType<"unsigned short">; def BoolType : NamedType<"bool">;

+def Float16Type : NamedType<"float16">; def Float128Type : NamedType<"float128">;

// Common types diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td index eb67c9b0b009b..6034a6e1aa703 100644 --- a/libc/spec/stdc.td +++ b/libc/spec/stdc.td @@ -378,6 +378,7 @@ def StdC : StandardSpec<"stdc"> { [ NamedType<"float_t">, NamedType<"double_t">,

@@ -395,6 +396,7 @@ def StdC : StandardSpec<"stdc"> { FunctionSpec<"fabs", RetValSpec, [ArgSpec], [ConstAttr]>, FunctionSpec<"fabsf", RetValSpec, [ArgSpec]>, FunctionSpec<"fabsl", RetValSpec, [ArgSpec]>,

diff --git a/libc/src/__support/CPP/type_traits/is_floating_point.h b/libc/src/__support/CPP/type_traits/is_floating_point.h index 4c8f50f4e91f9..39150d64b7876 100644 --- a/libc/src/__support/CPP/type_traits/is_floating_point.h +++ b/libc/src/__support/CPP/type_traits/is_floating_point.h @@ -24,7 +24,14 @@ template struct is_floating_point { }

public: -#if defined(LIBC_TYPES_HAS_FLOAT128) +#if defined(LIBC_TYPES_HAS_FLOAT16) && defined(LIBC_TYPES_HAS_FLOAT128)

+#elif defined(LIBC_TYPES_HAS_FLOAT16)

+#elif defined(LIBC_TYPES_HAS_FLOAT128) LIBC_INLINE_VAR static constexpr bool value = __is_unqualified_any_of<T, float, double, long double, float128>(); #else diff --git a/libc/src/__support/FPUtil/FPBits.h b/libc/src/__support/FPUtil/FPBits.h index ab050360c353b..d3c96d2d613d6 100644 --- a/libc/src/__support/FPUtil/FPBits.h +++ b/libc/src/__support/FPUtil/FPBits.h @@ -651,7 +651,7 @@ struct FPRepImpl : public FPRepSem<fp_type, RetT> {

// Modifiers LIBC_INLINE constexpr RetT abs() const {

}

// Observers diff --git a/libc/src/__support/macros/properties/types.h b/libc/src/__support/macros/properties/types.h index d43cf99e6859b..4baf9fae36f7f 100644 --- a/libc/src/__support/macros/properties/types.h +++ b/libc/src/__support/macros/properties/types.h @@ -12,6 +12,7 @@

#include "include/llvm-libc-macros/float-macros.h" // LDBL_MANT_DIG #include "include/llvm-libc-types/float128.h" // float128 +#include "include/llvm-libc-types/float16.h" // float16 #include "src/__support/macros/properties/architectures.h" #include "src/__support/macros/properties/compiler.h" #include "src/__support/macros/properties/cpu_features.h" @@ -39,28 +40,8 @@ #endif // defined(SIZEOF_INT128)

// -- float16 support --------------------------------------------------------- -// TODO: move this logic to "llvm-libc-types/float16.h" -#if defined(LIBC_TARGET_ARCH_IS_X86_64) && defined(LIBC_TARGET_CPU_HAS_SSE2) -#if (defined(LIBC_COMPILER_CLANG_VER) && (LIBC_COMPILER_CLANG_VER >= 1500)) || \

-#define LIBC_TYPES_HAS_FLOAT16 -using float16 = _Float16; -#endif -#endif -#if defined(LIBC_TARGET_ARCH_IS_AARCH64) -#if (defined(LIBC_COMPILER_CLANG_VER) && (LIBC_COMPILER_CLANG_VER >= 900)) || \

-#define LIBC_TYPES_HAS_FLOAT16 -using float16 = _Float16; -#endif -#endif -#if defined(LIBC_TARGET_ARCH_IS_ANY_RISCV) -#if (defined(LIBC_COMPILER_CLANG_VER) && (LIBC_COMPILER_CLANG_VER >= 1300)) || \

-#define LIBC_TYPES_HAS_FLOAT16 -using float16 = _Float16; -#endif -#endif +// LIBC_TYPES_HAS_FLOAT16 and 'float16' type are provided by +// "include/llvm-libc-types/float16.h"

// -- float128 support -------------------------------------------------------- // LIBC_TYPES_HAS_FLOAT128 and 'float128' type are provided by diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt index c34c58575441d..31df5d0ab8809 100644 --- a/libc/src/math/CMakeLists.txt +++ b/libc/src/math/CMakeLists.txt @@ -99,6 +99,7 @@ add_math_entrypoint_object(expm1f) add_math_entrypoint_object(fabs) add_math_entrypoint_object(fabsf) add_math_entrypoint_object(fabsl) +add_math_entrypoint_object(fabsf16) add_math_entrypoint_object(fabsf128)

add_math_entrypoint_object(fdim) diff --git a/libc/src/math/fabsf16.h b/libc/src/math/fabsf16.h new file mode 100644 index 0000000000000..532662a77e9a6 --- /dev/null +++ b/libc/src/math/fabsf16.h @@ -0,0 +1,20 @@ +//===-- Implementation header for fabsf16 ------------------------ 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_SRC_MATH_FABSF16_H +#define LLVM_LIBC_SRC_MATH_FABSF16_H + +#include "src/__support/macros/properties/types.h" + +namespace LIBC_NAMESPACE { + +float16 fabsf16(float16 x); + +} // namespace LIBC_NAMESPACE + +#endif // LLVM_LIBC_SRC_MATH_FABSF16_H diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt index daaf505008ca1..2a4dcdcf7ddde 100644 --- a/libc/src/math/generic/CMakeLists.txt +++ b/libc/src/math/generic/CMakeLists.txt @@ -241,6 +241,19 @@ add_entrypoint_object( -O2 )

+add_entrypoint_object(

+) + add_entrypoint_object( fabsf128 SRCS diff --git a/libc/src/math/generic/fabsf16.cpp b/libc/src/math/generic/fabsf16.cpp new file mode 100644 index 0000000000000..4de84f35da302 --- /dev/null +++ b/libc/src/math/generic/fabsf16.cpp @@ -0,0 +1,17 @@ +//===-- Implementation of fabsf16 function --------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include "src/math/fabsf16.h" +#include "src/__support/FPUtil/BasicOperations.h" +#include "src/__support/common.h" + +namespace LIBC_NAMESPACE { + +LLVM_LIBC_FUNCTION(float16, fabsf16, (float16 x)) { return fputil::abs(x); } + +} // namespace LIBC_NAMESPACE diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt index 112b2985829ca..c74f68daeb082 100644 --- a/libc/test/src/math/smoke/CMakeLists.txt +++ b/libc/test/src/math/smoke/CMakeLists.txt @@ -92,6 +92,19 @@ add_fp_unittest( libc.src.__support.FPUtil.fp_bits )

+add_fp_unittest(

+) + add_fp_unittest( fabsf128_test SUITE diff --git a/libc/test/src/math/smoke/fabsf16_test.cpp b/libc/test/src/math/smoke/fabsf16_test.cpp new file mode 100644 index 0000000000000..c43bd5090f90b --- /dev/null +++ b/libc/test/src/math/smoke/fabsf16_test.cpp @@ -0,0 +1,13 @@ +//===-- Unittests for fabsf16 ---------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include "FAbsTest.h" + +#include "src/math/fabsf16.h" + +LIST_FABS_TESTS(float16, LIBC_NAMESPACE::fabsf16)