From f1f052f9b000ac9cbbdd38ca9f665d2d4088b605 Mon Sep 17 00:00:00 2001 From: "Mikhail R. Gadelha" Date: Wed, 8 Mar 2023 16:16:40 -0300 Subject: [PATCH] [libc] Add support for sqrt in riscv Reviewed By: lntue Differential Revision: https://reviews.llvm.org/D145593 --- libc/src/__support/FPUtil/riscv64/sqrt.h | 39 ++++++++++++++++++++++++ libc/src/__support/FPUtil/sqrt.h | 2 ++ 2 files changed, 41 insertions(+) create mode 100644 libc/src/__support/FPUtil/riscv64/sqrt.h diff --git a/libc/src/__support/FPUtil/riscv64/sqrt.h b/libc/src/__support/FPUtil/riscv64/sqrt.h new file mode 100644 index 0000000000000..887000bda7be6 --- /dev/null +++ b/libc/src/__support/FPUtil/riscv64/sqrt.h @@ -0,0 +1,39 @@ +//===-- Square root of IEEE 754 floating point numbers ----------*- 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_FPUTIL_RISCV64_SQRT_H +#define LLVM_LIBC_SRC_SUPPORT_FPUTIL_RISCV64_SQRT_H + +#include "src/__support/common.h" +#include "src/__support/macros/properties/architectures.h" + +#if !defined(LIBC_TARGET_ARCH_IS_RISCV64) +#error "Invalid include" +#endif + +#include "src/__support/FPUtil/generic/sqrt.h" + +namespace __llvm_libc { +namespace fputil { + +template <> LIBC_INLINE float sqrt(float x) { + float result; + __asm__ __volatile__("fsqrt.s %0, %1\n\t" : "=f"(result) : "f"(x)); + return result; +} + +template <> LIBC_INLINE double sqrt(double x) { + double result; + __asm__ __volatile__("fsqrt.d %0, %1\n\t" : "=f"(result) : "f"(x)); + return result; +} + +} // namespace fputil +} // namespace __llvm_libc + +#endif // LLVM_LIBC_SRC_SUPPORT_FPUTIL_RISCV64_SQRT_H diff --git a/libc/src/__support/FPUtil/sqrt.h b/libc/src/__support/FPUtil/sqrt.h index d64fd22d3f1f9..132e7237c810c 100644 --- a/libc/src/__support/FPUtil/sqrt.h +++ b/libc/src/__support/FPUtil/sqrt.h @@ -15,6 +15,8 @@ #include "x86_64/sqrt.h" #elif defined(LIBC_TARGET_ARCH_IS_AARCH64) #include "aarch64/sqrt.h" +#elif defined(LIBC_TARGET_ARCH_IS_RISCV64) +#include "riscv64/sqrt.h" #else #include "generic/sqrt.h"