xref: /llvm-project/libc/src/__support/FPUtil/x86_64/sqrt.h (revision 5ff3ff33ff930e4ec49da7910612d8a41eb068cb)
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_X86_64_SQRT_H
10 #define LLVM_LIBC_SRC___SUPPORT_FPUTIL_X86_64_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_X86_64) && defined(LIBC_TARGET_CPU_HAS_SSE2))
18 #error "sqrtss / sqrtsd need SSE2"
19 #endif
20 
21 #include "src/__support/FPUtil/generic/sqrt.h"
22 
23 namespace LIBC_NAMESPACE_DECL {
24 namespace fputil {
25 
26 template <> LIBC_INLINE float sqrt<float>(float x) {
27   float result;
28   __asm__ __volatile__("sqrtss %x1, %x0" : "=x"(result) : "x"(x));
29   return result;
30 }
31 
32 template <> LIBC_INLINE double sqrt<double>(double x) {
33   double result;
34   __asm__ __volatile__("sqrtsd %x1, %x0" : "=x"(result) : "x"(x));
35   return result;
36 }
37 
38 #ifdef LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64
39 template <> LIBC_INLINE long double sqrt<long double>(long double x) {
40   long double result;
41   __asm__ __volatile__("sqrtsd %x1, %x0" : "=x"(result) : "x"(x));
42   return result;
43 }
44 #else
45 template <> LIBC_INLINE long double sqrt<long double>(long double x) {
46   __asm__ __volatile__("fsqrt" : "+t"(x));
47   return x;
48 }
49 #endif
50 
51 } // namespace fputil
52 } // namespace LIBC_NAMESPACE_DECL
53 
54 #endif // LLVM_LIBC_SRC___SUPPORT_FPUTIL_X86_64_SQRT_H
55