[libc][math] Refactor sqrtf to header-only (#178778)

Refactors sqrtf to be header-only.

Closes #177649.
This commit is contained in:
Saina Daneshmand 2026-01-29 22:31:04 -07:00 committed by GitHub
parent a9a092ccd2
commit 737f4447ad
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 70 additions and 7 deletions

View File

@ -101,6 +101,7 @@
#include "math/sinhf16.h"
#include "math/sinpif.h"
#include "math/sqrt.h"
#include "math/sqrtf.h"
#include "math/sqrtf16.h"
#include "math/tan.h"
#include "math/tanf.h"

23
libc/shared/math/sqrtf.h Normal file
View File

@ -0,0 +1,23 @@
//===-- Shared header for sqrtf ---------------------------------*- 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_SHARED_MATH_SQRTF_H
#define LLVM_LIBC_SHARED_MATH_SQRTF_H
#include "shared/libc_common.h"
#include "src/__support/math/sqrtf.h"
namespace LIBC_NAMESPACE_DECL {
namespace shared {
using math::sqrtf;
} // namespace shared
} // namespace LIBC_NAMESPACE_DECL
#endif // LLVM_LIBC_SHARED_MATH_SQRTF_H

View File

@ -1461,6 +1461,14 @@ add_header_library(
libc.src.__support.FPUtil.sqrt
)
add_header_library(
sqrtf
HDRS
sqrtf.h
DEPENDS
libc.src.__support.FPUtil.sqrt
)
add_header_library(
dfmal
HDRS

View File

@ -0,0 +1,24 @@
//===-- Implementation header for sqrtf -------------------------*- 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___SUPPORT_MATH_SQRTF_H
#define LLVM_LIBC_SRC___SUPPORT_MATH_SQRTF_H
#include "src/__support/FPUtil/sqrt.h"
namespace LIBC_NAMESPACE_DECL {
namespace math {
LIBC_INLINE static float sqrtf(float x) { return fputil::sqrt<float>(x); }
} // namespace math
} // namespace LIBC_NAMESPACE_DECL
#endif // LLVM_LIBC_SRC___SUPPORT_MATH_SQRTF_H

View File

@ -2977,7 +2977,6 @@ add_entrypoint_object(
libc.src.__support.math.sqrt
)
add_entrypoint_object(
sqrtf
SRCS
@ -2985,7 +2984,7 @@ add_entrypoint_object(
HDRS
../sqrtf.h
DEPENDS
libc.src.__support.FPUtil.sqrt
libc.src.__support.math.sqrtf
)
add_entrypoint_object(

View File

@ -7,12 +7,10 @@
//===----------------------------------------------------------------------===//
#include "src/math/sqrtf.h"
#include "src/__support/FPUtil/sqrt.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/__support/math/sqrtf.h"
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(float, sqrtf, (float x)) { return fputil::sqrt<float>(x); }
LLVM_LIBC_FUNCTION(float, sqrtf, (float x)) { return math::sqrtf(x); }
} // namespace LIBC_NAMESPACE_DECL

View File

@ -99,6 +99,7 @@ add_fp_unittest(
libc.src.__support.math.sinhf16
libc.src.__support.math.sinpif
libc.src.__support.math.sqrt
libc.src.__support.math.sqrtf
libc.src.__support.math.tan
libc.src.__support.math.tanf
)

View File

@ -106,6 +106,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat) {
ASSERT_FP_EQ(0.0f, sin);
EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::sinpif(0.0f));
EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::sinf(0.0f));
EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::sqrtf(0.0f));
EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::tanf(0.0f));
}

View File

@ -3669,6 +3669,14 @@ libc_support_library(
],
)
libc_support_library(
name = "__support_math_sqrtf",
hdrs = ["src/__support/math/sqrtf.h"],
deps = [
":__support_fputil_sqrt",
],
)
libc_support_library(
name = "__support_math_hypotf",
hdrs = ["src/__support/math/hypotf.h"],
@ -5379,7 +5387,7 @@ libc_math_function(
libc_math_function(
name = "sqrtf",
additional_deps = [
":__support_fputil_sqrt",
":__support_math_sqrtf",
],
)