Skip to content

Commit c5e07fb

Browse files
authored
[libc] Use correct instruction for arm32 sqrt inline asm. (#134968)
https://godbolt.org/z/3jT7jdrs9
1 parent bada5eb commit c5e07fb

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//===-- Square root of IEEE 754 floating point numbers ----------*- 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_FPUTIL_AARCH64_SQRT_H
10+
#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_AARCH64_SQRT_H
11+
12+
#include "src/__support/common.h"
13+
#include "src/__support/macros/config.h"
14+
#include "src/__support/macros/properties/architectures.h"
15+
#include "src/__support/macros/properties/cpu_features.h"
16+
17+
#if !defined(LIBC_TARGET_ARCH_IS_AARCH64)
18+
#error "Invalid include"
19+
#endif
20+
21+
namespace LIBC_NAMESPACE_DECL {
22+
namespace fputil {
23+
24+
#ifdef LIBC_TARGET_CPU_HAS_FPU_FLOAT
25+
template <> LIBC_INLINE float sqrt<float>(float x) {
26+
float y;
27+
asm("fsqrt %s0, %s1\n\t" : "=w"(y) : "w"(x));
28+
return y;
29+
}
30+
#endif // LIBC_TARGET_CPU_HAS_FPU_FLOAT
31+
32+
#ifdef LIBC_TARGET_CPU_HAS_FPU_DOUBLE
33+
template <> LIBC_INLINE double sqrt<double>(double x) {
34+
double y;
35+
asm("fsqrt %d0, %d1\n\t" : "=w"(y) : "w"(x));
36+
return y;
37+
}
38+
#endif // LIBC_TARGET_CPU_HAS_FPU_DOUBLE
39+
40+
} // namespace fputil
41+
} // namespace LIBC_NAMESPACE_DECL
42+
43+
#endif // LLVM_LIBC_SRC___SUPPORT_FPUTIL_AARCH64_SQRT_H

libc/src/__support/FPUtil/arm/sqrt.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include "src/__support/macros/properties/architectures.h"
1515
#include "src/__support/macros/properties/cpu_features.h"
1616

17-
#if !defined(LIBC_TARGET_ARCH_IS_ANY_ARM)
17+
#if !defined(LIBC_TARGET_ARCH_IS_ARM)
1818
#error "Invalid include"
1919
#endif
2020

@@ -24,15 +24,15 @@ namespace fputil {
2424
#ifdef LIBC_TARGET_CPU_HAS_FPU_FLOAT
2525
template <> LIBC_INLINE float sqrt<float>(float x) {
2626
float y;
27-
asm("fsqrt %s0, %s1\n\t" : "=w"(y) : "w"(x));
27+
asm("vsqrt %0, %1\n\t" : "=w"(y) : "w"(x));
2828
return y;
2929
}
3030
#endif // LIBC_TARGET_CPU_HAS_FPU_FLOAT
3131

3232
#ifdef LIBC_TARGET_CPU_HAS_FPU_DOUBLE
3333
template <> LIBC_INLINE double sqrt<double>(double x) {
3434
double y;
35-
asm("fsqrt %d0, %d1\n\t" : "=w"(y) : "w"(x));
35+
asm("vsqrt %0, %1\n\t" : "=w"(y) : "w"(x));
3636
return y;
3737
}
3838
#endif // LIBC_TARGET_CPU_HAS_FPU_DOUBLE

libc/src/__support/FPUtil/sqrt.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ template <> LIBC_INLINE double sqrt<double>(double x) {
4242
// Use inline assembly when __builtin_elementwise_sqrt is not available.
4343
#if defined(LIBC_TARGET_CPU_HAS_SSE2)
4444
#include "x86_64/sqrt.h"
45-
#elif defined(LIBC_TARGET_ARCH_IS_ANY_ARM)
45+
#elif defined(LIBC_TARGET_ARCH_IS_AARCH64)
46+
#include "aarch64/sqrt.h"
47+
#elif defined(LIBC_TARGET_ARCH_IS_ARM)
4648
#include "arm/sqrt.h"
4749
#elif defined(LIBC_TARGET_ARCH_IS_ANY_RISCV)
4850
#include "riscv/sqrt.h"

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1179,7 +1179,7 @@ sqrt_hdrs = selects.with_or({
11791179
"src/__support/FPUtil/x86_64/sqrt.h",
11801180
],
11811181
PLATFORM_CPU_ARM64: sqrt_common_hdrs + [
1182-
"src/__support/FPUtil/arm/sqrt.h",
1182+
"src/__support/FPUtil/aarch64/sqrt.h",
11831183
],
11841184
})
11851185

0 commit comments

Comments
 (0)