diff --git a/libc/config/darwin/arm/entrypoints.txt b/libc/config/darwin/arm/entrypoints.txt --- a/libc/config/darwin/arm/entrypoints.txt +++ b/libc/config/darwin/arm/entrypoints.txt @@ -54,6 +54,7 @@ libc.src.string.strndup # inttypes.h entrypoints + libc.src.inttypes.imaxabs libc.src.inttypes.imaxdiv libc.src.inttypes.strtoimax libc.src.inttypes.strtoumax 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 @@ -59,6 +59,7 @@ libc.src.string.strndup # inttypes.h entrypoints + libc.src.inttypes.imaxabs libc.src.inttypes.imaxdiv libc.src.inttypes.strtoimax libc.src.inttypes.strtoumax diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt --- a/libc/config/linux/arm/entrypoints.txt +++ b/libc/config/linux/arm/entrypoints.txt @@ -39,6 +39,7 @@ libc.src.string.strtok_r # inttypes.h entrypoints + libc.src.inttypes.imaxabs libc.src.inttypes.imaxdiv libc.src.inttypes.strtoimax libc.src.inttypes.strtoumax 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 @@ -59,6 +59,7 @@ libc.src.string.strndup # inttypes.h entrypoints + libc.src.inttypes.imaxabs libc.src.inttypes.imaxdiv libc.src.inttypes.strtoimax libc.src.inttypes.strtoumax diff --git a/libc/config/windows/entrypoints.txt b/libc/config/windows/entrypoints.txt --- a/libc/config/windows/entrypoints.txt +++ b/libc/config/windows/entrypoints.txt @@ -54,6 +54,7 @@ libc.src.string.strndup # inttypes.h entrypoints + libc.src.inttypes.imaxabs libc.src.inttypes.imaxdiv libc.src.inttypes.strtoimax libc.src.inttypes.strtoumax diff --git a/libc/src/inttypes/CMakeLists.txt b/libc/src/inttypes/CMakeLists.txt --- a/libc/src/inttypes/CMakeLists.txt +++ b/libc/src/inttypes/CMakeLists.txt @@ -28,3 +28,14 @@ libc.include.inttypes libc.src.__support.integer_operations ) + +add_entrypoint_object( + imaxabs + SRCS + imaxabs.cpp + HDRS + imaxabs.h + DEPENDS + libc.include.inttypes + libc.src.__support.integer_operations +) diff --git a/libc/src/inttypes/imaxabs.h b/libc/src/inttypes/imaxabs.h new file mode 100644 --- /dev/null +++ b/libc/src/inttypes/imaxabs.h @@ -0,0 +1,20 @@ +//===-- Implementation header for imaxabs -----------------------*- 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_INTTYPES_IMAXABS_H +#define LLVM_LIBC_SRC_INTTYPES_IMAXABS_H + +#include + +namespace __llvm_libc { + +intmax_t imaxabs(intmax_t j); + +} // namespace __llvm_libc + +#endif // LLVM_LIBC_SRC_INTTYPES_IMAXABS_H diff --git a/libc/src/inttypes/imaxabs.cpp b/libc/src/inttypes/imaxabs.cpp new file mode 100644 --- /dev/null +++ b/libc/src/inttypes/imaxabs.cpp @@ -0,0 +1,17 @@ +//===-- Implementation of imaxabs -----------------------------------------===// +// +// 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/inttypes/imaxabs.h" +#include "src/__support/common.h" +#include "src/__support/integer_operations.h" + +namespace __llvm_libc { + +LLVM_LIBC_FUNCTION(intmax_t, imaxabs, (intmax_t j)) { return integer_abs(j); } + +} // namespace __llvm_libc diff --git a/libc/test/src/inttypes/CMakeLists.txt b/libc/test/src/inttypes/CMakeLists.txt --- a/libc/test/src/inttypes/CMakeLists.txt +++ b/libc/test/src/inttypes/CMakeLists.txt @@ -32,3 +32,17 @@ libc.include.stdlib libc.src.inttypes.imaxdiv ) + +add_libc_unittest( + imaxabs_test + SUITE + libc_inttypes_unittests + SRCS + imaxabs_test.cpp + HDRS + ../stdlib/DivTest.h + DEPENDS + libc.include.stdlib + libc.src.inttypes.imaxabs +) + diff --git a/libc/test/src/inttypes/imaxabs_test.cpp b/libc/test/src/inttypes/imaxabs_test.cpp new file mode 100644 --- /dev/null +++ b/libc/test/src/inttypes/imaxabs_test.cpp @@ -0,0 +1,22 @@ +//===-- Unittests for imaxabs ---------------------------------------------===// +// +// 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/inttypes/imaxabs.h" +#include "utils/UnitTest/Test.h" + +TEST(LlvmLibcImaxAbsTest, Zero) { + EXPECT_EQ(__llvm_libc::imaxabs(0), intmax_t(0)); +} + +TEST(LlvmLibcImaxAbsTest, Positive) { + EXPECT_EQ(__llvm_libc::imaxabs(1), intmax_t(1)); +} + +TEST(LlvmLibcImaxAbsTest, Negative) { + EXPECT_EQ(__llvm_libc::imaxabs(-1), intmax_t(1)); +}