diff options
| author | Cheng Wang <chennngwang@gmail.com> | 2020-12-03 20:17:24 +0800 |
|---|---|---|
| committer | Cheng Wang <chennngwang@gmail.com> | 2020-12-11 09:25:20 +0800 |
| commit | 1fd32dcb294e16781fcfcf1a468180d00cf1e3ca (patch) | |
| tree | 4e430b88e6df710581974f17a84b7cd3f09eeaf3 /libc/src | |
| parent | e52881a2870234f0dad6e338a0f084eddeaa9356 (diff) | |
[libc] Add [l|ll]abs implementation.
Implement abs, labs and llabs with template.
Reviewed By: sivachandra
Differential Revision: https://reviews.llvm.org/D92626
Diffstat (limited to 'libc/src')
| -rw-r--r-- | libc/src/stdlib/CMakeLists.txt | 36 | ||||
| -rw-r--r-- | libc/src/stdlib/abs.cpp | 20 | ||||
| -rw-r--r-- | libc/src/stdlib/abs.h | 18 | ||||
| -rw-r--r-- | libc/src/stdlib/abs_utils.h | 22 | ||||
| -rw-r--r-- | libc/src/stdlib/labs.cpp | 20 | ||||
| -rw-r--r-- | libc/src/stdlib/labs.h | 18 | ||||
| -rw-r--r-- | libc/src/stdlib/llabs.cpp | 20 | ||||
| -rw-r--r-- | libc/src/stdlib/llabs.h | 18 |
8 files changed, 172 insertions, 0 deletions
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 |
