[libc-commits] [libc] c179bcc - [libc] Add imaxabs
Alex Brachet via libc-commits
libc-commits at lists.llvm.org
Mon Jul 11 14:28:51 PDT 2022
Author: Alex Brachet
Date: 2022-07-11T21:28:21Z
New Revision: c179bcc151839f7cfea9f391f3c00665711ac537
URL: https://github.com/llvm/llvm-project/commit/c179bcc151839f7cfea9f391f3c00665711ac537
DIFF: https://github.com/llvm/llvm-project/commit/c179bcc151839f7cfea9f391f3c00665711ac537.diff
LOG: [libc] Add imaxabs
Differential Revision: https://reviews.llvm.org/D129517
Added:
libc/src/inttypes/imaxabs.cpp
libc/src/inttypes/imaxabs.h
libc/test/src/inttypes/imaxabs_test.cpp
Modified:
libc/config/darwin/arm/entrypoints.txt
libc/config/linux/aarch64/entrypoints.txt
libc/config/linux/arm/entrypoints.txt
libc/config/linux/x86_64/entrypoints.txt
libc/config/windows/entrypoints.txt
libc/src/inttypes/CMakeLists.txt
libc/test/src/inttypes/CMakeLists.txt
Removed:
################################################################################
diff --git a/libc/config/darwin/arm/entrypoints.txt b/libc/config/darwin/arm/entrypoints.txt
index 5c44d6a20c023..173b8d80eda13 100644
--- a/libc/config/darwin/arm/entrypoints.txt
+++ b/libc/config/darwin/arm/entrypoints.txt
@@ -54,6 +54,7 @@ set(TARGET_LIBC_ENTRYPOINTS
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
index 7b1abab3b395e..47f6c5adc058a 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -59,6 +59,7 @@ set(TARGET_LIBC_ENTRYPOINTS
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
index 39341224a386f..bb01b0759712d 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -39,6 +39,7 @@ set(TARGET_LIBC_ENTRYPOINTS
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
index e6a63ec2cd5b5..06258688b254d 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -59,6 +59,7 @@ set(TARGET_LIBC_ENTRYPOINTS
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
index 503c1f120da23..4fbbd5ffd705f 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -54,6 +54,7 @@ set(TARGET_LIBC_ENTRYPOINTS
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
index 00fd791320fb2..f26273c3a5738 100644
--- a/libc/src/inttypes/CMakeLists.txt
+++ b/libc/src/inttypes/CMakeLists.txt
@@ -28,3 +28,14 @@ add_entrypoint_object(
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.cpp b/libc/src/inttypes/imaxabs.cpp
new file mode 100644
index 0000000000000..6c29ea2354e46
--- /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/src/inttypes/imaxabs.h b/libc/src/inttypes/imaxabs.h
new file mode 100644
index 0000000000000..0add10d38071c
--- /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 <inttypes.h>
+
+namespace __llvm_libc {
+
+intmax_t imaxabs(intmax_t j);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_INTTYPES_IMAXABS_H
diff --git a/libc/test/src/inttypes/CMakeLists.txt b/libc/test/src/inttypes/CMakeLists.txt
index 9a6d6ac71bbe7..45d66a7db742c 100644
--- a/libc/test/src/inttypes/CMakeLists.txt
+++ b/libc/test/src/inttypes/CMakeLists.txt
@@ -32,3 +32,17 @@ add_libc_unittest(
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
index 0000000000000..ddd855c423d73
--- /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));
+}
More information about the libc-commits
mailing list