Skip to content

Commit

Permalink
[libc] Add [l|ll]abs implementation.
Browse files Browse the repository at this point in the history
Implement abs, labs and llabs with template.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D92626
  • Loading branch information
ChennngWang committed Dec 11, 2020
1 parent e52881a commit 1fd32dc
Show file tree
Hide file tree
Showing 16 changed files with 263 additions and 0 deletions.
5 changes: 5 additions & 0 deletions libc/config/linux/aarch64/entrypoints.txt
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions libc/spec/spec.td
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions libc/spec/stdc.td
Expand Up @@ -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>]>,
]
>;
Expand Down
36 changes: 36 additions & 0 deletions libc/src/stdlib/CMakeLists.txt
Expand Up @@ -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
Expand All @@ -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
)
20 changes: 20 additions & 0 deletions 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
18 changes: 18 additions & 0 deletions 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
22 changes: 22 additions & 0 deletions 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
20 changes: 20 additions & 0 deletions 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
18 changes: 18 additions & 0 deletions 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
20 changes: 20 additions & 0 deletions 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
18 changes: 18 additions & 0 deletions 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
30 changes: 30 additions & 0 deletions libc/test/src/stdlib/CMakeLists.txt
Expand Up @@ -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
)
16 changes: 16 additions & 0 deletions 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); }
16 changes: 16 additions & 0 deletions 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); }
16 changes: 16 additions & 0 deletions 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); }

0 comments on commit 1fd32dc

Please sign in to comment.