[libc] Add aarch64 flavors of floor, round, sqrt and trunc.

Only single and double precision flavors have been added.

Reviewed By: lntue, sdesmalen

Differential Revision: https://reviews.llvm.org/D95999

GitOrigin-RevId: c90c8d38d38e00d9ed7f3bce192e99665a386ef3
diff --git a/src/math/aarch64/CMakeLists.txt b/src/math/aarch64/CMakeLists.txt
index 716fbe3..6ce8944 100644
--- a/src/math/aarch64/CMakeLists.txt
+++ b/src/math/aarch64/CMakeLists.txt
@@ -17,3 +17,83 @@
   COMPILE_OPTIONS
     -O2
 )
+
+add_entrypoint_object(
+  floor
+  SRCS
+    floor.cpp
+  HDRS
+    ../floor.h
+  COMPILE_OPTIONS
+    -O2
+)
+
+add_entrypoint_object(
+  floorf
+  SRCS
+    floorf.cpp
+  HDRS
+    ../floorf.h
+  COMPILE_OPTIONS
+    -O2
+)
+
+add_entrypoint_object(
+  trunc
+  SRCS
+    trunc.cpp
+  HDRS
+    ../trunc.h
+  COMPILE_OPTIONS
+    -O2
+)
+
+add_entrypoint_object(
+  truncf
+  SRCS
+    truncf.cpp
+  HDRS
+    ../truncf.h
+  COMPILE_OPTIONS
+    -O2
+)
+
+add_entrypoint_object(
+  round
+  SRCS
+    round.cpp
+  HDRS
+    ../round.h
+  COMPILE_OPTIONS
+    -O2
+)
+
+add_entrypoint_object(
+  roundf
+  SRCS
+    roundf.cpp
+  HDRS
+    ../roundf.h
+  COMPILE_OPTIONS
+    -O2
+)
+
+add_entrypoint_object(
+  sqrt
+  SRCS
+    sqrt.cpp
+  HDRS
+    ../sqrt.h
+  COMPILE_OPTIONS
+    -O2
+)
+
+add_entrypoint_object(
+  sqrtf
+  SRCS
+    sqrtf.cpp
+  HDRS
+    ../sqrtf.h
+  COMPILE_OPTIONS
+    -O2
+)
diff --git a/src/math/aarch64/ceil.cpp b/src/math/aarch64/ceil.cpp
index c39dbfc..73c3cc0 100644
--- a/src/math/aarch64/ceil.cpp
+++ b/src/math/aarch64/ceil.cpp
@@ -13,12 +13,7 @@
 
 LLVM_LIBC_FUNCTION(double, ceil, (double x)) {
   double y;
-  __asm__ __volatile__("ldr d0, %1\n"
-                       "frintp d0, d0\n"
-                       "str d0, %0\n"
-                       : "=m"(y)
-                       : "m"(x)
-                       : "d0");
+  __asm__ __volatile__("frintp %d0, %d1\n\t" : "=w"(y) : "w"(x));
   return y;
 }
 
diff --git a/src/math/aarch64/ceilf.cpp b/src/math/aarch64/ceilf.cpp
index 1d80477..2268989 100644
--- a/src/math/aarch64/ceilf.cpp
+++ b/src/math/aarch64/ceilf.cpp
@@ -13,12 +13,7 @@
 
 LLVM_LIBC_FUNCTION(float, ceilf, (float x)) {
   float y;
-  __asm__ __volatile__("ldr s0, %1\n"
-                       "frintp s0, s0\n"
-                       "str s0, %0\n"
-                       : "=m"(y)
-                       : "m"(x)
-                       : "s0");
+  __asm__ __volatile__("frintp %s0, %s1\n\t" : "=w"(y) : "w"(x));
   return y;
 }
 
diff --git a/src/math/aarch64/floor.cpp b/src/math/aarch64/floor.cpp
new file mode 100644
index 0000000..8de1f67
--- /dev/null
+++ b/src/math/aarch64/floor.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of the floor function for aarch64 ------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/math/floor.h"
+#include "src/__support/common.h"
+
+namespace __llvm_libc {
+
+LLVM_LIBC_FUNCTION(double, floor, (double x)) {
+  double y;
+  __asm__ __volatile__("frintm %d0, %d1\n\t" : "=w"(y) : "w"(x));
+  return y;
+}
+
+} // namespace __llvm_libc
diff --git a/src/math/aarch64/floorf.cpp b/src/math/aarch64/floorf.cpp
new file mode 100644
index 0000000..6bb99eb
--- /dev/null
+++ b/src/math/aarch64/floorf.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of the floorf function for aarch64 -----------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/math/floorf.h"
+#include "src/__support/common.h"
+
+namespace __llvm_libc {
+
+LLVM_LIBC_FUNCTION(float, floorf, (float x)) {
+  float y;
+  __asm__ __volatile__("frintm %s0, %s1\n\t" : "=w"(y) : "w"(x));
+  return y;
+}
+
+} // namespace __llvm_libc
diff --git a/src/math/aarch64/round.cpp b/src/math/aarch64/round.cpp
new file mode 100644
index 0000000..6659060
--- /dev/null
+++ b/src/math/aarch64/round.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of the round function for aarch64 ------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/math/round.h"
+#include "src/__support/common.h"
+
+namespace __llvm_libc {
+
+LLVM_LIBC_FUNCTION(double, round, (double x)) {
+  double y;
+  __asm__ __volatile__("frinta %d0, %d1\n\t" : "=w"(y) : "w"(x));
+  return y;
+}
+
+} // namespace __llvm_libc
diff --git a/src/math/aarch64/roundf.cpp b/src/math/aarch64/roundf.cpp
new file mode 100644
index 0000000..6044c8b
--- /dev/null
+++ b/src/math/aarch64/roundf.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of the roundf function for aarch64 -----------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/math/roundf.h"
+#include "src/__support/common.h"
+
+namespace __llvm_libc {
+
+LLVM_LIBC_FUNCTION(float, roundf, (float x)) {
+  float y;
+  __asm__ __volatile__("frinta %s0, %s1\n\t" : "=w"(y) : "w"(x));
+  return y;
+}
+
+} // namespace __llvm_libc
diff --git a/src/math/aarch64/sqrt.cpp b/src/math/aarch64/sqrt.cpp
new file mode 100644
index 0000000..99ab7e3
--- /dev/null
+++ b/src/math/aarch64/sqrt.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of the sqrt function for aarch64 -------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/math/sqrt.h"
+#include "src/__support/common.h"
+
+namespace __llvm_libc {
+
+LLVM_LIBC_FUNCTION(double, sqrt, (double x)) {
+  double y;
+  __asm__ __volatile__("fsqrt %d0, %d1\n\t" : "=w"(y) : "w"(x));
+  return y;
+}
+
+} // namespace __llvm_libc
diff --git a/src/math/aarch64/sqrtf.cpp b/src/math/aarch64/sqrtf.cpp
new file mode 100644
index 0000000..a747520
--- /dev/null
+++ b/src/math/aarch64/sqrtf.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of the sqrtf function for aarch64 ------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/math/sqrtf.h"
+#include "src/__support/common.h"
+
+namespace __llvm_libc {
+
+LLVM_LIBC_FUNCTION(float, sqrtf, (float x)) {
+  float y;
+  __asm__ __volatile__("fsqrt %s0, %s1\n\t" : "=w"(y) : "w"(x));
+  return y;
+}
+
+} // namespace __llvm_libc
diff --git a/src/math/aarch64/trunc.cpp b/src/math/aarch64/trunc.cpp
new file mode 100644
index 0000000..280d919
--- /dev/null
+++ b/src/math/aarch64/trunc.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of the trunc function for aarch64 ------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/math/trunc.h"
+#include "src/__support/common.h"
+
+namespace __llvm_libc {
+
+LLVM_LIBC_FUNCTION(double, trunc, (double x)) {
+  double y;
+  __asm__ __volatile__("frintz %d0, %d1\n" : "=w"(y) : "w"(x));
+  return y;
+}
+
+} // namespace __llvm_libc
diff --git a/src/math/aarch64/truncf.cpp b/src/math/aarch64/truncf.cpp
new file mode 100644
index 0000000..a325ce3
--- /dev/null
+++ b/src/math/aarch64/truncf.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of the truncf function for aarch64 -----------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/math/truncf.h"
+#include "src/__support/common.h"
+
+namespace __llvm_libc {
+
+LLVM_LIBC_FUNCTION(float, truncf, (float x)) {
+  float y;
+  __asm__ __volatile__("frintz %s0, %s1\n\t" : "=w"(y) : "w"(x));
+  return y;
+}
+
+} // namespace __llvm_libc