[llvm-branch-commits] [libc] 1fd32dc - [libc] Add [l|ll]abs implementation.

Cheng Wang via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Thu Dec 10 17:30:11 PST 2020


Author: Cheng Wang
Date: 2020-12-11T09:25:20+08:00
New Revision: 1fd32dcb294e16781fcfcf1a468180d00cf1e3ca

URL: https://github.com/llvm/llvm-project/commit/1fd32dcb294e16781fcfcf1a468180d00cf1e3ca
DIFF: https://github.com/llvm/llvm-project/commit/1fd32dcb294e16781fcfcf1a468180d00cf1e3ca.diff

LOG: [libc] Add [l|ll]abs implementation.

Implement abs, labs and llabs with template.

Reviewed By: sivachandra

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

Added: 
    libc/src/stdlib/abs.cpp
    libc/src/stdlib/abs.h
    libc/src/stdlib/abs_utils.h
    libc/src/stdlib/labs.cpp
    libc/src/stdlib/labs.h
    libc/src/stdlib/llabs.cpp
    libc/src/stdlib/llabs.h
    libc/test/src/stdlib/abs_test.cpp
    libc/test/src/stdlib/labs_test.cpp
    libc/test/src/stdlib/llabs_test.cpp

Modified: 
    libc/config/linux/aarch64/entrypoints.txt
    libc/config/linux/x86_64/entrypoints.txt
    libc/spec/spec.td
    libc/spec/stdc.td
    libc/src/stdlib/CMakeLists.txt
    libc/test/src/stdlib/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 3a3b050a6e06..534a4bdd6131 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -18,6 +18,11 @@ set(TARGET_LIBC_ENTRYPOINTS
     # errno.h entrypoints
     libc.src.errno.__errno_location
 
+    # stdlib.h entrypoints
+    libc.src.stdlib.abs
+    libc.src.stdlib.labs
+    libc.src.stdlib.llabs
+
     # string.h entrypoints
     libc.src.string.bzero
     libc.src.string.memchr

diff  --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 2b461c4eede5..4cae553ac6d4 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -41,6 +41,9 @@ set(TARGET_LIBC_ENTRYPOINTS
     # stdlib.h entrypoints
     libc.src.stdlib._Exit
     libc.src.stdlib.abort
+    libc.src.stdlib.abs
+    libc.src.stdlib.labs
+    libc.src.stdlib.llabs
 
     # string.h entrypoints
     libc.src.string.bzero

diff  --git a/libc/spec/spec.td b/libc/spec/spec.td
index 29c11a9c2199..9a31d85c148c 100644
--- a/libc/spec/spec.td
+++ b/libc/spec/spec.td
@@ -42,6 +42,8 @@ def IntType : NamedType<"int">;
 def FloatType : NamedType<"float">;
 def DoubleType : NamedType<"double">;
 def LongDoubleType : NamedType<"long double">;
+def LongLongType : NamedType<"long long">;
+def LongType : NamedType<"long">;
 def CharType : NamedType<"char">;
 
 // Common types

diff  --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index 70cc2600a612..051e0a89eb27 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -400,6 +400,9 @@ def StdC : StandardSpec<"stdc"> {
       [], // Enumerations
       [
           FunctionSpec<"abort", RetValSpec<NoReturn>, [ArgSpec<VoidType>]>,
+          FunctionSpec<"abs", RetValSpec<IntType>, [ArgSpec<IntType>]>,
+          FunctionSpec<"labs", RetValSpec<LongType>, [ArgSpec<LongType>]>,
+          FunctionSpec<"llabs", RetValSpec<LongLongType>, [ArgSpec<LongLongType>]>,
           FunctionSpec<"_Exit", RetValSpec<NoReturn>, [ArgSpec<IntType>]>,
       ]
   >;

diff  --git a/libc/src/stdlib/CMakeLists.txt b/libc/src/stdlib/CMakeLists.txt
index 50b7421944dd..a599d8a59065 100644
--- a/libc/src/stdlib/CMakeLists.txt
+++ b/libc/src/stdlib/CMakeLists.txt
@@ -2,6 +2,12 @@ if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
   add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
 endif()
 
+add_header_library(
+  abs_utils
+  HDRS
+    abs_utils.h
+)
+
 add_entrypoint_object(
   _Exit
   ALIAS
@@ -20,3 +26,33 @@ add_entrypoint_object(
     libc.src.signal.raise
     ._Exit
 )
+
+add_entrypoint_object(
+  abs
+  SRCS
+    abs.cpp
+  HDRS
+    abs.h
+  DEPENDS
+    .abs_utils
+)
+
+add_entrypoint_object(
+  labs
+  SRCS
+    labs.cpp
+  HDRS
+    labs.h
+  DEPENDS
+    .abs_utils
+)
+
+add_entrypoint_object(
+  llabs
+  SRCS
+    llabs.cpp
+  HDRS
+    llabs.h
+  DEPENDS
+    .abs_utils
+)

diff  --git a/libc/src/stdlib/abs.cpp b/libc/src/stdlib/abs.cpp
new file mode 100644
index 000000000000..74c47cdb242d
--- /dev/null
+++ b/libc/src/stdlib/abs.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of abs ---------------------------------------------===//
+//
+// 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/stdlib/abs.h"
+#include "src/__support/common.h"
+#include "src/stdlib/abs_utils.h"
+
+namespace __llvm_libc {
+
+int LLVM_LIBC_ENTRYPOINT(abs)(int n) {
+  // integer_abs from abs_utils.h.
+  return integer_abs(n);
+}
+
+} // namespace __llvm_libc

diff  --git a/libc/src/stdlib/abs.h b/libc/src/stdlib/abs.h
new file mode 100644
index 000000000000..42ef7f885ed6
--- /dev/null
+++ b/libc/src/stdlib/abs.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for abs ---------------------------*- 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_STDLIB_ABS_H
+#define LLVM_LIBC_SRC_STDLIB_ABS_H
+
+namespace __llvm_libc {
+
+int abs(int n);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_STDLIB_ABS_H

diff  --git a/libc/src/stdlib/abs_utils.h b/libc/src/stdlib/abs_utils.h
new file mode 100644
index 000000000000..c0943fe0ee69
--- /dev/null
+++ b/libc/src/stdlib/abs_utils.h
@@ -0,0 +1,22 @@
+//===-- Utils for abs and friends -------------------------------*- 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_STDLIB_ABS_UTILS_H
+#define LLVM_LIBC_SRC_STDLIB_ABS_UTILS_H
+
+namespace __llvm_libc {
+
+template <typename T> static inline T integer_abs(T n) {
+  if (n < 0)
+    return -n;
+  return n;
+}
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_STDLIB_ABS_UTILS_H

diff  --git a/libc/src/stdlib/labs.cpp b/libc/src/stdlib/labs.cpp
new file mode 100644
index 000000000000..427cb9d841ef
--- /dev/null
+++ b/libc/src/stdlib/labs.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of labs --------------------------------------------===//
+//
+// 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/stdlib/labs.h"
+#include "src/__support/common.h"
+#include "src/stdlib/abs_utils.h"
+
+namespace __llvm_libc {
+
+long LLVM_LIBC_ENTRYPOINT(labs)(long n) {
+  // integer_abs from abs_utils.h.
+  return integer_abs(n);
+}
+
+} // namespace __llvm_libc

diff  --git a/libc/src/stdlib/labs.h b/libc/src/stdlib/labs.h
new file mode 100644
index 000000000000..0f0ea99dc22a
--- /dev/null
+++ b/libc/src/stdlib/labs.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for labs --------------------------*- 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_STDLIB_LABS_H
+#define LLVM_LIBC_SRC_STDLIB_LABS_H
+
+namespace __llvm_libc {
+
+long labs(long n);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_STDLIB_LABS_H

diff  --git a/libc/src/stdlib/llabs.cpp b/libc/src/stdlib/llabs.cpp
new file mode 100644
index 000000000000..8878ce41c76b
--- /dev/null
+++ b/libc/src/stdlib/llabs.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of llabs -------------------------------------------===//
+//
+// 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/stdlib/llabs.h"
+#include "src/__support/common.h"
+#include "src/stdlib/abs_utils.h"
+
+namespace __llvm_libc {
+
+long long LLVM_LIBC_ENTRYPOINT(llabs)(long long n) {
+  // integer_abs from abs_utils.h.
+  return integer_abs(n);
+}
+
+} // namespace __llvm_libc

diff  --git a/libc/src/stdlib/llabs.h b/libc/src/stdlib/llabs.h
new file mode 100644
index 000000000000..f173431a14f1
--- /dev/null
+++ b/libc/src/stdlib/llabs.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for llabs -------------------------*- 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_STDLIB_LLABS_H
+#define LLVM_LIBC_SRC_STDLIB_LLABS_H
+
+namespace __llvm_libc {
+
+long long llabs(long long n);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_STDLIB_LLABS_H

diff  --git a/libc/test/src/stdlib/CMakeLists.txt b/libc/test/src/stdlib/CMakeLists.txt
index 06adaa2a9954..e5cb04a58db1 100644
--- a/libc/test/src/stdlib/CMakeLists.txt
+++ b/libc/test/src/stdlib/CMakeLists.txt
@@ -24,3 +24,33 @@ add_libc_unittest(
     libc.src.stdlib._Exit
     libc.src.signal.raise
 )
+
+add_libc_unittest(
+  abs_test
+  SUITE
+    libc_stdlib_unittests
+  SRCS
+    abs_test.cpp
+  DEPENDS
+    libc.src.stdlib.abs
+)
+
+add_libc_unittest(
+  labs_test
+  SUITE
+    libc_stdlib_unittests
+  SRCS
+    labs_test.cpp
+  DEPENDS
+    libc.src.stdlib.labs
+)
+
+add_libc_unittest(
+  llabs_test
+  SUITE
+    libc_stdlib_unittests
+  SRCS
+    llabs_test.cpp
+  DEPENDS
+    libc.src.stdlib.llabs
+)

diff  --git a/libc/test/src/stdlib/abs_test.cpp b/libc/test/src/stdlib/abs_test.cpp
new file mode 100644
index 000000000000..6f1a46ea8355
--- /dev/null
+++ b/libc/test/src/stdlib/abs_test.cpp
@@ -0,0 +1,16 @@
+//===-- Unittests for abs -------------------------------------------------===//
+//
+// 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/stdlib/abs.h"
+#include "utils/UnitTest/Test.h"
+
+TEST(AbsTest, Zero) { EXPECT_EQ(__llvm_libc::abs(0), 0); }
+
+TEST(AbsTest, Positive) { EXPECT_EQ(__llvm_libc::abs(1), 1); }
+
+TEST(AbsTest, Negative) { EXPECT_EQ(__llvm_libc::abs(-1), 1); }

diff  --git a/libc/test/src/stdlib/labs_test.cpp b/libc/test/src/stdlib/labs_test.cpp
new file mode 100644
index 000000000000..cd814252f809
--- /dev/null
+++ b/libc/test/src/stdlib/labs_test.cpp
@@ -0,0 +1,16 @@
+//===-- Unittests for labs ------------------------------------------------===//
+//
+// 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/stdlib/labs.h"
+#include "utils/UnitTest/Test.h"
+
+TEST(LabsTest, Zero) { EXPECT_EQ(__llvm_libc::labs(0l), 0l); }
+
+TEST(LabsTest, Positive) { EXPECT_EQ(__llvm_libc::labs(1l), 1l); }
+
+TEST(LabsTest, Negative) { EXPECT_EQ(__llvm_libc::labs(-1l), 1l); }

diff  --git a/libc/test/src/stdlib/llabs_test.cpp b/libc/test/src/stdlib/llabs_test.cpp
new file mode 100644
index 000000000000..4b5fde9a4c9b
--- /dev/null
+++ b/libc/test/src/stdlib/llabs_test.cpp
@@ -0,0 +1,16 @@
+//===-- Unittests for llabs -----------------------------------------------===//
+//
+// 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/stdlib/llabs.h"
+#include "utils/UnitTest/Test.h"
+
+TEST(LlabsTest, Zero) { EXPECT_EQ(__llvm_libc::llabs(0ll), 0ll); }
+
+TEST(LlabsTest, Positive) { EXPECT_EQ(__llvm_libc::llabs(1ll), 1ll); }
+
+TEST(LlabsTest, Negative) { EXPECT_EQ(__llvm_libc::llabs(-1ll), 1ll); }


        


More information about the llvm-branch-commits mailing list