Skip to content

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

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 12 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 4 additions & 2 deletions libc/cmake/modules/CheckCompilerFeatures.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions libc/cmake/modules/compiler_features/check_float16.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "include/llvm-libc-macros/float16-macros.h"

#ifndef LIBC_TYPES_HAS_FLOAT16
#error unsupported
#endif
7 changes: 7 additions & 0 deletions libc/config/linux/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion libc/docs/math/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
Expand Down
1 change: 1 addition & 0 deletions libc/include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions libc/include/llvm-libc-macros/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions libc/include/llvm-libc-macros/float16-macros.h
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions libc/include/math.h.def
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
1 change: 1 addition & 0 deletions libc/spec/spec.td
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions libc/spec/stdc.td
Original file line number Diff line number Diff line change
Expand Up @@ -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>]>,
Expand Down
16 changes: 10 additions & 6 deletions libc/src/__support/CPP/type_traits/is_floating_point.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ template <typename T> struct is_floating_point {
}

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 =
Expand Down
2 changes: 1 addition & 1 deletion libc/src/__support/FPUtil/FPBits.h
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ struct FPRepImpl : public FPRepSem<fp_type, RetT> {

// 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
Expand Down
1 change: 1 addition & 0 deletions libc/src/__support/macros/properties/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
29 changes: 7 additions & 22 deletions libc/src/__support/macros/properties/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions libc/src/math/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 20 additions & 0 deletions libc/src/math/fabsf16.h
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions libc/src/math/generic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions libc/src/math/generic/fabsf16.cpp
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions libc/test/src/math/smoke/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions libc/test/src/math/smoke/fabsf16_test.cpp
Original file line number Diff line number Diff line change
@@ -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)
Loading