[libc][math][c23] Add fabsf16 C23 math function (#93567)

cc @lntue
This commit is contained in:
OverMighty 2024-05-30 21:37:15 +02:00 committed by GitHub
parent 7cfffe74ee
commit 0eb9e021b1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
21 changed files with 146 additions and 32 deletions

View File

@ -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})
if(${feature} STREQUAL "float128")
if(${feature} STREQUAL "float16")
set(LIBC_TYPES_HAS_FLOAT16 TRUE)
elseif(${feature} STREQUAL "float128")
set(LIBC_TYPES_HAS_FLOAT128 TRUE)
elseif(${feature} STREQUAL "fixed_point")
set(LIBC_COMPILER_HAS_FIXED_POINT TRUE)

View File

@ -0,0 +1,5 @@
#include "include/llvm-libc-macros/float16-macros.h"
#ifndef LIBC_TYPES_HAS_FLOAT16
#error unsupported
#endif

View File

@ -496,6 +496,13 @@ set(TARGET_LIBM_ENTRYPOINTS
libc.src.math.ufromfpxl
)
if(LIBC_TYPES_HAS_FLOAT16)
list(APPEND TARGET_LIBM_ENTRYPOINTS
# math.h C23 _Float16 entrypoints
libc.src.math.fabsf16
)
endif()
if(LIBC_TYPES_HAS_FLOAT128)
list(APPEND TARGET_LIBM_ENTRYPOINTS
# math.h C23 _Float128 entrypoints

View File

@ -528,6 +528,13 @@ set(TARGET_LIBM_ENTRYPOINTS
libc.src.math.ufromfpxl
)
if(LIBC_TYPES_HAS_FLOAT16)
list(APPEND TARGET_LIBM_ENTRYPOINTS
# math.h C23 _Float16 entrypoints
libc.src.math.fabsf16
)
endif()
if(LIBC_TYPES_HAS_FLOAT128)
list(APPEND TARGET_LIBM_ENTRYPOINTS
# math.h C23 _Float128 entrypoints

View File

@ -124,7 +124,7 @@ Basic Operations
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| dsub | N/A | N/A | | N/A | | 7.12.14.2 | F.10.11 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| fabs | |check| | |check| | |check| | | |check| | 7.12.7.3 | F.10.4.3 |
| fabs | |check| | |check| | |check| | |check| | |check| | 7.12.7.3 | F.10.4.3 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| fadd | N/A | | | N/A | | 7.12.14.1 | F.10.11 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+

View File

@ -111,6 +111,7 @@ add_gen_header(
GEN_HDR math.h
DEPENDS
.llvm_libc_common_h
.llvm-libc-macros.float16_macros
.llvm-libc-macros.math_macros
.llvm-libc-types.double_t
.llvm-libc-types.float_t

View File

@ -91,6 +91,12 @@ add_macro_header(
float-macros.h
)
add_macro_header(
float16_macros
HDR
float16-macros.h
)
add_macro_header(
limits_macros
HDR

View File

@ -0,0 +1,17 @@
//===-- Detection of _Float16 compiler builtin 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_MACROS_FLOAT16_MACROS_H
#define LLVM_LIBC_MACROS_FLOAT16_MACROS_H
#if defined(__FLT16_MANT_DIG__) && \
(!defined(__GNUC__) || __GNUC__ >= 13 || defined(__clang__))
#define LIBC_TYPES_HAS_FLOAT16
#endif
#endif // LLVM_LIBC_MACROS_FLOAT16_MACROS_H

View File

@ -10,6 +10,7 @@
#define LLVM_LIBC_MATH_H
#include "__llvm-libc-common.h"
#include "llvm-libc-macros/float16-macros.h"
#include "llvm-libc-macros/math-macros.h"
#include "llvm-libc-types/float128.h"

View File

@ -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

View File

@ -395,6 +395,7 @@ def StdC : StandardSpec<"stdc"> {
FunctionSpec<"fabs", RetValSpec<DoubleType>, [ArgSpec<DoubleType>], [ConstAttr]>,
FunctionSpec<"fabsf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
FunctionSpec<"fabsl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
GuardedFunctionSpec<"fabsf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
GuardedFunctionSpec<"fabsf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,
FunctionSpec<"fdim", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,

View File

@ -24,13 +24,17 @@ private:
}
public:
#if defined(LIBC_TYPES_HAS_FLOAT128)
LIBC_INLINE_VAR static constexpr bool value =
__is_unqualified_any_of<T, float, double, long double, float128>();
#else
LIBC_INLINE_VAR static constexpr bool value =
__is_unqualified_any_of<T, float, double, long double>();
#endif // LIBC_TYPES_HAS_FLOAT128
__is_unqualified_any_of<T, float, double, long double
#ifdef LIBC_TYPES_HAS_FLOAT16
,
float16
#endif
#ifdef LIBC_TYPES_HAS_FLOAT128
,
float128
#endif
>();
};
template <typename T>
LIBC_INLINE_VAR constexpr bool is_floating_point_v =

View File

@ -651,7 +651,7 @@ public:
// Modifiers
LIBC_INLINE constexpr RetT abs() const {
return RetT(bits & UP::EXP_SIG_MASK);
return RetT(static_cast<StorageType>(bits & UP::EXP_SIG_MASK));
}
// Observers

View File

@ -34,5 +34,6 @@ add_header_library(
.cpu_features
.os
libc.hdr.float_macros
libc.include.llvm-libc-macros.float16_macros
libc.include.llvm-libc-types.float128
)

View File

@ -11,7 +11,8 @@
#define LLVM_LIBC_SRC___SUPPORT_MACROS_PROPERTIES_TYPES_H
#include "hdr/float_macros.h" // LDBL_MANT_DIG
#include "include/llvm-libc-types/float128.h" // float128
#include "include/llvm-libc-macros/float16-macros.h" // LIBC_TYPES_HAS_FLOAT16
#include "include/llvm-libc-types/float128.h" // float128
#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,12 @@
#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)) || \
(defined(LIBC_COMPILER_GCC_VER) && (LIBC_COMPILER_GCC_VER >= 1201))
#define LIBC_TYPES_HAS_FLOAT16
// LIBC_TYPES_HAS_FLOAT16 is provided by
// "include/llvm-libc-macros/float16-macros.h"
#ifdef LIBC_TYPES_HAS_FLOAT16
// Type alias for internal use.
using float16 = _Float16;
#endif
#endif
#if defined(LIBC_TARGET_ARCH_IS_AARCH64)
#if (defined(LIBC_COMPILER_CLANG_VER) && (LIBC_COMPILER_CLANG_VER >= 900)) || \
(defined(LIBC_COMPILER_GCC_VER) && (LIBC_COMPILER_GCC_VER >= 1301))
#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)) || \
(defined(LIBC_COMPILER_GCC_VER) && (LIBC_COMPILER_GCC_VER >= 1301))
#define LIBC_TYPES_HAS_FLOAT16
using float16 = _Float16;
#endif
#endif
#endif // LIBC_TYPES_HAS_FLOAT16
// -- float128 support --------------------------------------------------------
// LIBC_TYPES_HAS_FLOAT128 and 'float128' type are provided by

View File

@ -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)

20
libc/src/math/fabsf16.h Normal file
View File

@ -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

View File

@ -241,6 +241,19 @@ add_entrypoint_object(
-O2
)
add_entrypoint_object(
fabsf16
SRCS
fabsf16.cpp
HDRS
../fabsf16.h
DEPENDS
libc.src.__support.macros.properties.types
libc.src.__support.FPUtil.basic_operations
COMPILE_OPTIONS
-O3
)
add_entrypoint_object(
fabsf128
SRCS

View File

@ -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

View File

@ -92,6 +92,18 @@ add_fp_unittest(
libc.src.__support.FPUtil.fp_bits
)
add_fp_unittest(
fabsf16_test
SUITE
libc-math-smoke-tests
SRCS
fabsf16_test.cpp
HDRS
FAbsTest.h
DEPENDS
libc.src.math.fabsf16
)
add_fp_unittest(
fabsf128_test
SUITE

View File

@ -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)