diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt --- a/libc/config/linux/aarch64/entrypoints.txt +++ b/libc/config/linux/aarch64/entrypoints.txt @@ -18,6 +18,9 @@ # errno.h entrypoints libc.src.errno.__errno_location + # stdlib.h entrypoints + libc.src.stdlib.abs + # 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 --- a/libc/config/linux/x86_64/entrypoints.txt +++ b/libc/config/linux/x86_64/entrypoints.txt @@ -34,6 +34,7 @@ # stdlib.h entrypoints libc.src.stdlib._Exit libc.src.stdlib.abort + libc.src.stdlib.abs # string.h entrypoints libc.src.string.bzero diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td --- a/libc/spec/stdc.td +++ b/libc/spec/stdc.td @@ -353,6 +353,7 @@ [], // Enumerations [ FunctionSpec<"abort", RetValSpec, [ArgSpec]>, + FunctionSpec<"abs", RetValSpec, [ArgSpec]>, FunctionSpec<"_Exit", RetValSpec, [ArgSpec]>, ] >; diff --git a/libc/src/stdlib/CMakeLists.txt b/libc/src/stdlib/CMakeLists.txt --- a/libc/src/stdlib/CMakeLists.txt +++ b/libc/src/stdlib/CMakeLists.txt @@ -20,3 +20,11 @@ libc.src.signal.raise ._Exit ) + +add_entrypoint_object( + abs + SRCS + abs.cpp + HDRS + abs.h +) diff --git a/libc/src/stdlib/abs.h b/libc/src/stdlib/abs.h new file mode 100644 --- /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 \ No newline at end of file diff --git a/libc/src/stdlib/abs.cpp b/libc/src/stdlib/abs.cpp new file mode 100644 --- /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" + +namespace __llvm_libc { + +int LLVM_LIBC_ENTRYPOINT(abs)(int n) { + if (n < 0) + return -n; + return n; +} + +} // namespace __llvm_libc \ No newline at end of file diff --git a/libc/test/src/stdlib/CMakeLists.txt b/libc/test/src/stdlib/CMakeLists.txt --- a/libc/test/src/stdlib/CMakeLists.txt +++ b/libc/test/src/stdlib/CMakeLists.txt @@ -24,3 +24,13 @@ 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 +) diff --git a/libc/test/src/stdlib/abs_test.cpp b/libc/test/src/stdlib/abs_test.cpp new file mode 100644 --- /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); }