Skip to content

Commit aa62224

Browse files
[libc][math] Refactor sqrtbf16 function header-only (#187849)
1 parent 1120c97 commit aa62224

File tree

9 files changed

+81
-10
lines changed

9 files changed

+81
-10
lines changed

libc/shared/math.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@
246246
#include "math/sinpif.h"
247247
#include "math/sinpif16.h"
248248
#include "math/sqrt.h"
249+
#include "math/sqrtbf16.h"
249250
#include "math/sqrtf.h"
250251
#include "math/sqrtf128.h"
251252
#include "math/sqrtf16.h"

libc/shared/math/sqrtbf16.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===-- Shared sqrtbf16 function --------------------------------*- 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_SHARED_MATH_SQRTBF16_H
10+
#define LLVM_LIBC_SHARED_MATH_SQRTBF16_H
11+
12+
#include "shared/libc_common.h"
13+
#include "src/__support/math/sqrtbf16.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
namespace shared {
17+
18+
using math::sqrtbf16;
19+
20+
} // namespace shared
21+
} // namespace LIBC_NAMESPACE_DECL
22+
23+
#endif // LLVM_LIBC_SHARED_MATH_SQRTBF16_H

libc/src/__support/math/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3087,6 +3087,17 @@ add_header_library(
30873087
libc.src.__support.FPUtil.sqrt
30883088
)
30893089

3090+
add_header_library(
3091+
sqrtbf16
3092+
HDRS
3093+
sqrtbf16.h
3094+
DEPENDS
3095+
libc.src.__support.FPUtil.bfloat16
3096+
libc.src.__support.FPUtil.sqrt
3097+
libc.src.__support.common
3098+
libc.src.__support.macros.config
3099+
)
3100+
30903101
add_header_library(
30913102
sqrtf
30923103
HDRS

libc/src/__support/math/sqrtbf16.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===-- Implementation header for sqrtbf16 ----------------------*- 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___SUPPORT_MATH_SQRTBF16_H
10+
#define LLVM_LIBC_SRC___SUPPORT_MATH_SQRTBF16_H
11+
12+
#include "src/__support/FPUtil/bfloat16.h"
13+
#include "src/__support/FPUtil/sqrt.h"
14+
#include "src/__support/common.h"
15+
#include "src/__support/macros/config.h"
16+
17+
namespace LIBC_NAMESPACE_DECL {
18+
namespace math {
19+
20+
LIBC_INLINE bfloat16 sqrtbf16(bfloat16 x) { return fputil::sqrt<bfloat16>(x); }
21+
22+
} // namespace math
23+
} // namespace LIBC_NAMESPACE_DECL
24+
25+
#endif // LLVM_LIBC_SRC___SUPPORT_MATH_SQRTBF16_H

libc/src/math/generic/CMakeLists.txt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2897,11 +2897,7 @@ add_entrypoint_object(
28972897
HDRS
28982898
../sqrtbf16.h
28992899
DEPENDS
2900-
libc.src.__support.common
2901-
libc.src.__support.FPUtil.bfloat16
2902-
libc.src.__support.FPUtil.sqrt
2903-
libc.src.__support.macros.config
2904-
libc.src.__support.macros.properties.types
2900+
libc.src.__support.math.sqrtbf16
29052901
)
29062902

29072903
add_entrypoint_object(

libc/src/math/generic/sqrtbf16.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,12 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "src/math/sqrtbf16.h"
10-
#include "src/__support/FPUtil/bfloat16.h"
11-
#include "src/__support/FPUtil/sqrt.h"
12-
#include "src/__support/common.h"
13-
#include "src/__support/macros/config.h"
10+
#include "src/__support/math/sqrtbf16.h"
1411

1512
namespace LIBC_NAMESPACE_DECL {
1613

1714
LLVM_LIBC_FUNCTION(bfloat16, sqrtbf16, (bfloat16 x)) {
18-
return fputil::sqrt<bfloat16>(x);
15+
return math::sqrtbf16(x);
1916
}
2017

2118
} // namespace LIBC_NAMESPACE_DECL

libc/test/shared/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ add_fp_unittest(
246246
libc.src.__support.math.sinpif
247247
libc.src.__support.math.sinpif16
248248
libc.src.__support.math.sqrt
249+
libc.src.__support.math.sqrtbf16
249250
libc.src.__support.math.sqrtf
250251
libc.src.__support.math.tan
251252
libc.src.__support.math.tanf

libc/test/shared/shared_math_test.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,4 +494,5 @@ TEST(LlvmLibcSharedMathTest, AllBFloat16) {
494494
LIBC_NAMESPACE::shared::nexttowardbf16(bfloat16(0.0), 0.0L));
495495
EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::nextafterbf16(
496496
bfloat16(0.0), bfloat16(0.0)));
497+
EXPECT_FP_EQ(bfloat16(1.0), LIBC_NAMESPACE::shared::sqrtbf16(bfloat16(1.0)));
497498
}

utils/bazel/llvm-project-overlay/libc/BUILD.bazel

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5760,6 +5760,17 @@ libc_support_library(
57605760
],
57615761
)
57625762

5763+
libc_support_library(
5764+
name = "__support_math_sqrtbf16",
5765+
hdrs = ["src/__support/math/sqrtbf16.h"],
5766+
deps = [
5767+
":__support_common",
5768+
":__support_fputil_bfloat16",
5769+
":__support_fputil_sqrt",
5770+
":__support_macros_config",
5771+
],
5772+
)
5773+
57635774
libc_support_library(
57645775
name = "__support_math_sqrtf",
57655776
hdrs = ["src/__support/math/sqrtf.h"],
@@ -8351,6 +8362,11 @@ libc_math_function(
83518362
additional_deps = [":__support_math_sqrt"],
83528363
)
83538364

8365+
libc_math_function(
8366+
name = "sqrtbf16",
8367+
additional_deps = [":__support_math_sqrtbf16"],
8368+
)
8369+
83548370
libc_math_function(
83558371
name = "sqrtf",
83568372
additional_deps = [":__support_math_sqrtf"],

0 commit comments

Comments
 (0)