Skip to content

Commit f5f64f9

Browse files
committed
[libc][math][c23] Add fabsf16 C23 math function
1 parent 737a301 commit f5f64f9

File tree

16 files changed

+147
-26
lines changed

16 files changed

+147
-26
lines changed

libc/cmake/modules/CheckCompilerFeatures.cmake

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Compiler features definition and flags
33
# ------------------------------------------------------------------------------
44

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

77
# Making sure ALL_COMPILER_FEATURES is sorted.
88
list(SORT ALL_COMPILER_FEATURES)
@@ -54,7 +54,9 @@ foreach(feature IN LISTS ALL_COMPILER_FEATURES)
5454
)
5555
if(has_feature)
5656
list(APPEND AVAILABLE_COMPILER_FEATURES ${feature})
57-
if(${feature} STREQUAL "float128")
57+
if(${feature} STREQUAL "float16")
58+
set(LIBC_TYPES_HAS_FLOAT16 TRUE)
59+
elseif(${feature} STREQUAL "float128")
5860
set(LIBC_TYPES_HAS_FLOAT128 TRUE)
5961
elseif(${feature} STREQUAL "fixed_point")
6062
set(LIBC_COMPILER_HAS_FIXED_POINT TRUE)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "src/__support/macros/properties/types.h"
2+
3+
#ifndef LIBC_TYPES_HAS_FLOAT16
4+
#error unsupported
5+
#endif

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,13 @@ set(TARGET_LIBM_ENTRYPOINTS
528528
libc.src.math.ufromfpxl
529529
)
530530

531+
if(LIBC_TYPES_HAS_FLOAT16)
532+
list(APPEND TARGET_LIBM_ENTRYPOINTS
533+
# math.h C23 _Float16 entrypoints
534+
libc.src.math.fabsf16
535+
)
536+
endif()
537+
531538
if(LIBC_TYPES_HAS_FLOAT128)
532539
list(APPEND TARGET_LIBM_ENTRYPOINTS
533540
# math.h C23 _Float128 entrypoints

libc/include/llvm-libc-types/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ add_header(ENTRY HDR ENTRY.h)
119119
add_header(struct_hsearch_data HDR struct_hsearch_data.h)
120120
add_header(struct_epoll_event HDR struct_epoll_event.h)
121121
add_header(struct_epoll_data HDR struct_epoll_data.h)
122+
add_header(float16 HDR float16.h)
122123
add_header(
123124
float128
124125
HDR
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//===-- Definition of float16 type ----------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_TYPES_FLOAT16_H
10+
#define LLVM_LIBC_TYPES_FLOAT16_H
11+
12+
#include "src/__support/macros/properties/architectures.h"
13+
#include "src/__support/macros/properties/compiler.h"
14+
#include "src/__support/macros/properties/cpu_features.h"
15+
16+
#if defined(LIBC_TARGET_ARCH_IS_X86_64) && defined(LIBC_TARGET_CPU_HAS_SSE2)
17+
#if (defined(LIBC_COMPILER_CLANG_VER) && (LIBC_COMPILER_CLANG_VER >= 1500)) || \
18+
(defined(LIBC_COMPILER_GCC_VER) && (LIBC_COMPILER_GCC_VER >= 1201))
19+
#define LIBC_TYPES_HAS_FLOAT16
20+
using float16 = _Float16;
21+
#endif
22+
#endif
23+
#if defined(LIBC_TARGET_ARCH_IS_AARCH64)
24+
#if (defined(LIBC_COMPILER_CLANG_VER) && (LIBC_COMPILER_CLANG_VER >= 900)) || \
25+
(defined(LIBC_COMPILER_GCC_VER) && (LIBC_COMPILER_GCC_VER >= 1301))
26+
#define LIBC_TYPES_HAS_FLOAT16
27+
using float16 = _Float16;
28+
#endif
29+
#endif
30+
#if defined(LIBC_TARGET_ARCH_IS_ANY_RISCV)
31+
#if (defined(LIBC_COMPILER_CLANG_VER) && (LIBC_COMPILER_CLANG_VER >= 1300)) || \
32+
(defined(LIBC_COMPILER_GCC_VER) && (LIBC_COMPILER_GCC_VER >= 1301))
33+
#define LIBC_TYPES_HAS_FLOAT16
34+
using float16 = _Float16;
35+
#endif
36+
#endif
37+
38+
#endif // LLVM_LIBC_TYPES_FLOAT16_H

libc/spec/spec.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ def UnsignedCharType : NamedType<"unsigned char">;
5353
def UnsignedShortType : NamedType<"unsigned short">;
5454
def BoolType : NamedType<"bool">;
5555

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

5859
// Common types

libc/spec/stdc.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ def StdC : StandardSpec<"stdc"> {
378378
[
379379
NamedType<"float_t">,
380380
NamedType<"double_t">,
381+
Float16Type,
381382
NamedType<"float128">,
382383
],
383384
[], // Enumerations
@@ -395,6 +396,7 @@ def StdC : StandardSpec<"stdc"> {
395396
FunctionSpec<"fabs", RetValSpec<DoubleType>, [ArgSpec<DoubleType>], [ConstAttr]>,
396397
FunctionSpec<"fabsf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
397398
FunctionSpec<"fabsl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
399+
GuardedFunctionSpec<"fabsf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
398400
GuardedFunctionSpec<"fabsf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,
399401

400402
FunctionSpec<"fdim", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,

libc/src/__support/CPP/type_traits/is_floating_point.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,14 @@ template <typename T> struct is_floating_point {
2424
}
2525

2626
public:
27-
#if defined(LIBC_TYPES_HAS_FLOAT128)
27+
#if defined(LIBC_TYPES_HAS_FLOAT16) && defined(LIBC_TYPES_HAS_FLOAT128)
28+
LIBC_INLINE_VAR static constexpr bool value =
29+
__is_unqualified_any_of<T, float, double, long double, float16,
30+
float128>();
31+
#elif defined(LIBC_TYPES_HAS_FLOAT16)
32+
LIBC_INLINE_VAR static constexpr bool value =
33+
__is_unqualified_any_of<T, float, double, long double, float16>();
34+
#elif defined(LIBC_TYPES_HAS_FLOAT128)
2835
LIBC_INLINE_VAR static constexpr bool value =
2936
__is_unqualified_any_of<T, float, double, long double, float128>();
3037
#else

libc/src/__support/FPUtil/FPBits.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ struct FPRepImpl : public FPRepSem<fp_type, RetT> {
651651

652652
// Modifiers
653653
LIBC_INLINE constexpr RetT abs() const {
654-
return RetT(bits & UP::EXP_SIG_MASK);
654+
return RetT(static_cast<StorageType>(bits & UP::EXP_SIG_MASK));
655655
}
656656

657657
// Observers

libc/src/__support/macros/properties/types.h

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "hdr/float_macros.h" // LDBL_MANT_DIG
1414
#include "include/llvm-libc-types/float128.h" // float128
15+
#include "include/llvm-libc-types/float16.h" // float16
1516
#include "src/__support/macros/properties/architectures.h"
1617
#include "src/__support/macros/properties/compiler.h"
1718
#include "src/__support/macros/properties/cpu_features.h"
@@ -39,28 +40,8 @@
3940
#endif // defined(__SIZEOF_INT128__)
4041

4142
// -- float16 support ---------------------------------------------------------
42-
// TODO: move this logic to "llvm-libc-types/float16.h"
43-
#if defined(LIBC_TARGET_ARCH_IS_X86_64) && defined(LIBC_TARGET_CPU_HAS_SSE2)
44-
#if (defined(LIBC_COMPILER_CLANG_VER) && (LIBC_COMPILER_CLANG_VER >= 1500)) || \
45-
(defined(LIBC_COMPILER_GCC_VER) && (LIBC_COMPILER_GCC_VER >= 1201))
46-
#define LIBC_TYPES_HAS_FLOAT16
47-
using float16 = _Float16;
48-
#endif
49-
#endif
50-
#if defined(LIBC_TARGET_ARCH_IS_AARCH64)
51-
#if (defined(LIBC_COMPILER_CLANG_VER) && (LIBC_COMPILER_CLANG_VER >= 900)) || \
52-
(defined(LIBC_COMPILER_GCC_VER) && (LIBC_COMPILER_GCC_VER >= 1301))
53-
#define LIBC_TYPES_HAS_FLOAT16
54-
using float16 = _Float16;
55-
#endif
56-
#endif
57-
#if defined(LIBC_TARGET_ARCH_IS_ANY_RISCV)
58-
#if (defined(LIBC_COMPILER_CLANG_VER) && (LIBC_COMPILER_CLANG_VER >= 1300)) || \
59-
(defined(LIBC_COMPILER_GCC_VER) && (LIBC_COMPILER_GCC_VER >= 1301))
60-
#define LIBC_TYPES_HAS_FLOAT16
61-
using float16 = _Float16;
62-
#endif
63-
#endif
43+
// LIBC_TYPES_HAS_FLOAT16 and 'float16' type are provided by
44+
// "include/llvm-libc-types/float16.h"
6445

6546
// -- float128 support --------------------------------------------------------
6647
// LIBC_TYPES_HAS_FLOAT128 and 'float128' type are provided by

libc/src/math/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ add_math_entrypoint_object(expm1f)
9999
add_math_entrypoint_object(fabs)
100100
add_math_entrypoint_object(fabsf)
101101
add_math_entrypoint_object(fabsl)
102+
add_math_entrypoint_object(fabsf16)
102103
add_math_entrypoint_object(fabsf128)
103104

104105
add_math_entrypoint_object(fdim)

libc/src/math/fabsf16.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header for fabsf16 -----------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_MATH_FABSF16_H
10+
#define LLVM_LIBC_SRC_MATH_FABSF16_H
11+
12+
#include "src/__support/macros/properties/types.h"
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
float16 fabsf16(float16 x);
17+
18+
} // namespace LIBC_NAMESPACE
19+
20+
#endif // LLVM_LIBC_SRC_MATH_FABSF16_H

libc/src/math/generic/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,19 @@ add_entrypoint_object(
241241
-O2
242242
)
243243

244+
add_entrypoint_object(
245+
fabsf16
246+
SRCS
247+
fabsf16.cpp
248+
HDRS
249+
../fabsf16.h
250+
DEPENDS
251+
libc.src.__support.macros.properties.types
252+
libc.src.__support.FPUtil.basic_operations
253+
COMPILE_OPTIONS
254+
-O3
255+
)
256+
244257
add_entrypoint_object(
245258
fabsf128
246259
SRCS

libc/src/math/generic/fabsf16.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//===-- Implementation of fabsf16 function --------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/math/fabsf16.h"
10+
#include "src/__support/FPUtil/BasicOperations.h"
11+
#include "src/__support/common.h"
12+
13+
namespace LIBC_NAMESPACE {
14+
15+
LLVM_LIBC_FUNCTION(float16, fabsf16, (float16 x)) { return fputil::abs(x); }
16+
17+
} // namespace LIBC_NAMESPACE

libc/test/src/math/smoke/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,19 @@ add_fp_unittest(
9292
libc.src.__support.FPUtil.fp_bits
9393
)
9494

95+
add_fp_unittest(
96+
fabsf16_test
97+
SUITE
98+
libc-math-smoke-tests
99+
SRCS
100+
fabsf16_test.cpp
101+
HDRS
102+
FAbsTest.h
103+
DEPENDS
104+
libc.src.math.fabsf16
105+
libc.src.__support.FPUtil.fp_bits
106+
)
107+
95108
add_fp_unittest(
96109
fabsf128_test
97110
SUITE
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//===-- Unittests for fabsf16 ---------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "FAbsTest.h"
10+
11+
#include "src/math/fabsf16.h"
12+
13+
LIST_FABS_TESTS(float16, LIBC_NAMESPACE::fabsf16)

0 commit comments

Comments
 (0)