Skip to content

Commit 2668714

Browse files
author
Siva Chandra
committed
[libc] Add hardware implementations of ceil and ceilf for aarch64.
This change also introduces a new source layout for adding machine specific and generic implementations. To keep the scope of this change small, this new pattern is only applied for ceil, ceilf and ceill. Follow up changes will switch all math functions in to the new pattern. Reviewed By: lntue Differential Revision: https://reviews.llvm.org/D95850
1 parent 5b7619c commit 2668714

File tree

10 files changed

+183
-36
lines changed

10 files changed

+183
-36
lines changed

libc/src/math/CMakeLists.txt

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,41 @@
1+
add_subdirectory(generic)
2+
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_MACHINE})
3+
add_subdirectory(${LIBC_TARGET_MACHINE})
4+
endif()
5+
6+
function(add_math_entrypoint_object name)
7+
# We prefer machine specific implementation if available. Hence we check
8+
# that first and retrun early if we are able to add an alias target for the
9+
# machine specific implementation.
10+
get_fq_target_name("${LIBC_TARGET_MACHINE}.${name}" fq_machine_specific_target_name)
11+
if(TARGET ${fq_machine_specific_target_name})
12+
add_entrypoint_object(
13+
${name}
14+
ALIAS
15+
DEPENDS
16+
.${LIBC_TARGET_MACHINE}.${name}
17+
)
18+
return()
19+
endif()
20+
21+
get_fq_target_name("generic.${name}" fq_generic_target_name)
22+
if(TARGET ${fq_generic_target_name})
23+
add_entrypoint_object(
24+
${name}
25+
ALIAS
26+
DEPENDS
27+
.generic.${name}
28+
)
29+
return()
30+
endif()
31+
32+
message(FATAL_ERROR "No machine specific or generic implementation found for ${name}.")
33+
endfunction()
34+
35+
add_math_entrypoint_object(ceil)
36+
add_math_entrypoint_object(ceilf)
37+
add_math_entrypoint_object(ceill)
38+
139
add_object_library(
240
math_utils
341
SRCS
@@ -128,42 +166,6 @@ add_entrypoint_object(
128166
-O2
129167
)
130168

131-
add_entrypoint_object(
132-
ceil
133-
SRCS
134-
ceil.cpp
135-
HDRS
136-
ceil.h
137-
DEPENDS
138-
libc.utils.FPUtil.fputil
139-
COMPILE_OPTIONS
140-
-O2
141-
)
142-
143-
add_entrypoint_object(
144-
ceilf
145-
SRCS
146-
ceilf.cpp
147-
HDRS
148-
ceilf.h
149-
DEPENDS
150-
libc.utils.FPUtil.fputil
151-
COMPILE_OPTIONS
152-
-O2
153-
)
154-
155-
add_entrypoint_object(
156-
ceill
157-
SRCS
158-
ceill.cpp
159-
HDRS
160-
ceill.h
161-
DEPENDS
162-
libc.utils.FPUtil.fputil
163-
COMPILE_OPTIONS
164-
-O2
165-
)
166-
167169
add_entrypoint_object(
168170
floor
169171
SRCS

libc/src/math/aarch64/CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
add_entrypoint_object(
2+
ceil
3+
SRCS
4+
ceil.cpp
5+
HDRS
6+
../ceil.h
7+
COMPILE_OPTIONS
8+
-O2
9+
)
10+
11+
add_entrypoint_object(
12+
ceilf
13+
SRCS
14+
ceilf.cpp
15+
HDRS
16+
../ceilf.h
17+
COMPILE_OPTIONS
18+
-O2
19+
)

libc/src/math/aarch64/ceil.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===-- Implementation of the ceil function for aarch64 -------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/math/ceil.h"
10+
#include "src/__support/common.h"
11+
12+
namespace __llvm_libc {
13+
14+
LLVM_LIBC_FUNCTION(double, ceil, (double x)) {
15+
double y;
16+
__asm__ __volatile__("ldr d0, %1\n"
17+
"frintp d0, d0\n"
18+
"str d0, %0\n"
19+
: "=m"(y)
20+
: "m"(x)
21+
: "d0");
22+
return y;
23+
}
24+
25+
} // namespace __llvm_libc

libc/src/math/aarch64/ceilf.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===-- Implementation of the ceilf function for aarch64 ------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/math/ceilf.h"
10+
#include "src/__support/common.h"
11+
12+
namespace __llvm_libc {
13+
14+
LLVM_LIBC_FUNCTION(float, ceilf, (float x)) {
15+
float y;
16+
__asm__ __volatile__("ldr s0, %1\n"
17+
"frintp s0, s0\n"
18+
"str s0, %0\n"
19+
: "=m"(y)
20+
: "m"(x)
21+
: "s0");
22+
return y;
23+
}
24+
25+
} // namespace __llvm_libc

libc/src/math/generic/CMakeLists.txt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
add_entrypoint_object(
2+
ceil
3+
SRCS
4+
ceil.cpp
5+
HDRS
6+
../ceil.h
7+
DEPENDS
8+
libc.utils.FPUtil.fputil
9+
COMPILE_OPTIONS
10+
-O2
11+
)
12+
13+
add_entrypoint_object(
14+
ceilf
15+
SRCS
16+
ceilf.cpp
17+
HDRS
18+
../ceilf.h
19+
DEPENDS
20+
libc.utils.FPUtil.fputil
21+
COMPILE_OPTIONS
22+
-O2
23+
)
24+
25+
add_entrypoint_object(
26+
ceill
27+
SRCS
28+
ceill.cpp
29+
HDRS
30+
../ceill.h
31+
DEPENDS
32+
libc.utils.FPUtil.fputil
33+
COMPILE_OPTIONS
34+
-O2
35+
)
File renamed without changes.
File renamed without changes.
File renamed without changes.

libc/test/src/math/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,3 +1070,5 @@ add_fp_unittest(
10701070
libc.src.math.fmaf
10711071
libc.utils.FPUtil.fputil
10721072
)
1073+
1074+
add_subdirectory(generic)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
add_fp_unittest(
2+
ceil_test
3+
NEED_MPFR
4+
SUITE
5+
libc_math_unittests
6+
SRCS
7+
../ceil_test.cpp
8+
DEPENDS
9+
libc.include.math
10+
libc.src.math.generic.ceil
11+
libc.utils.FPUtil.fputil
12+
)
13+
14+
add_fp_unittest(
15+
ceilf_test
16+
NEED_MPFR
17+
SUITE
18+
libc_math_unittests
19+
SRCS
20+
../ceilf_test.cpp
21+
DEPENDS
22+
libc.include.math
23+
libc.src.math.generic.ceilf
24+
libc.utils.FPUtil.fputil
25+
)
26+
27+
add_fp_unittest(
28+
ceill_test
29+
NEED_MPFR
30+
SUITE
31+
libc_math_unittests
32+
SRCS
33+
../ceill_test.cpp
34+
DEPENDS
35+
libc.include.math
36+
libc.src.math.generic.ceill
37+
libc.utils.FPUtil.fputil
38+
)
39+

0 commit comments

Comments
 (0)