Path: blob/main/contrib/llvm-project/libc/src/__support/FPUtil/sqrt.h
213799 views
//===-- Square root of IEEE 754 floating point numbers ----------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLVM_LIBC_SRC___SUPPORT_FPUTIL_SQRT_H9#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_SQRT_H1011#include "src/__support/macros/properties/architectures.h"12#include "src/__support/macros/properties/cpu_features.h"1314#include "src/__support/FPUtil/generic/sqrt.h"1516// Generic instruction specializations with __builtin_elementwise_sqrt.17#if defined(LIBC_TARGET_CPU_HAS_FPU_FLOAT) || \18defined(LIBC_TARGET_CPU_HAS_FPU_DOUBLE)1920#if __has_builtin(__builtin_elementwise_sqrt)2122namespace LIBC_NAMESPACE_DECL {23namespace fputil {2425#ifdef LIBC_TARGET_CPU_HAS_FPU_FLOAT26template <> LIBC_INLINE float sqrt<float>(float x) {27return __builtin_elementwise_sqrt(x);28}29#endif // LIBC_TARGET_CPU_HAS_FPU_FLOAT3031#ifdef LIBC_TARGET_CPU_HAS_FPU_DOUBLE32template <> LIBC_INLINE double sqrt<double>(double x) {33return __builtin_elementwise_sqrt(x);34}35#endif // LIBC_TARGET_CPU_HAS_FPU_DOUBLE3637// Use 80-bit long double instruction on x86.38// https://godbolt.org/z/oWEaj6hxK39#ifdef LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT8040template <> LIBC_INLINE long double sqrt<long double>(long double x) {41return __builtin_elementwise_sqrt(x);42}43#endif // LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT804445} // namespace fputil46} // namespace LIBC_NAMESPACE_DECL4748#else // __builtin_elementwise_sqrt49// Use inline assembly when __builtin_elementwise_sqrt is not available.50#if defined(LIBC_TARGET_CPU_HAS_SSE2)51#include "x86_64/sqrt.h"52#elif defined(LIBC_TARGET_ARCH_IS_AARCH64) && defined(__ARM_FP)53#include "aarch64/sqrt.h"54#elif defined(LIBC_TARGET_ARCH_IS_ARM)55#include "arm/sqrt.h"56#elif defined(LIBC_TARGET_ARCH_IS_ANY_RISCV)57#include "riscv/sqrt.h"58#endif // Target specific header of inline asm.5960#endif // __builtin_elementwise_sqrt6162#endif // LIBC_TARGET_CPU_HAS_FPU_FLOAT or DOUBLE6364#endif // LLVM_LIBC_SRC___SUPPORT_FPUTIL_SQRT_H656667