Skip to content

Commit fddf018

Browse files
committed
fixup! [libc][math] Optimize nearest integer functions using builtins when available
Optimize rint{,f,f16} using __builtin_rint{,f} when available.
1 parent c83a56e commit fddf018

18 files changed

+79
-16
lines changed

libc/cmake/modules/CheckCompilerFeatures.cmake

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
set(
66
ALL_COMPILER_FEATURES
7-
"builtin_ceil_floor_trunc"
7+
"builtin_ceil_floor_rint_trunc"
88
"builtin_round"
99
"builtin_roundeven"
1010
"float16"
@@ -76,8 +76,8 @@ foreach(feature IN LISTS ALL_COMPILER_FEATURES)
7676
set(LIBC_TYPES_HAS_FLOAT128 TRUE)
7777
elseif(${feature} STREQUAL "fixed_point")
7878
set(LIBC_COMPILER_HAS_FIXED_POINT TRUE)
79-
elseif(${feature} STREQUAL "builtin_ceil_floor_trunc")
80-
set(LIBC_COMPILER_HAS_BUILTIN_CEIL_FLOOR_TRUNC TRUE)
79+
elseif(${feature} STREQUAL "builtin_ceil_floor_rint_trunc")
80+
set(LIBC_COMPILER_HAS_BUILTIN_CEIL_FLOOR_RINT_TRUNC TRUE)
8181
elseif(${feature} STREQUAL "builtin_round")
8282
set(LIBC_COMPILER_HAS_BUILTIN_ROUND TRUE)
8383
elseif(${feature} STREQUAL "builtin_roundeven")

libc/cmake/modules/LLVMLibCCompileOptionRules.cmake

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ function(_get_compile_options_from_flags output_var)
2323
# have SSE4.1.
2424
list(APPEND compile_options "-msse4.2")
2525
endif()
26-
if(LIBC_COMPILER_HAS_BUILTIN_CEIL_FLOOR_TRUNC)
27-
list(APPEND compile_options "-D__LIBC_USE_BUILTIN_CEIL_FLOOR_TRUNC")
26+
if(LIBC_COMPILER_HAS_BUILTIN_CEIL_FLOOR_RINT_TRUNC)
27+
list(APPEND compile_options
28+
"-D__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC")
2829
endif()
2930
if(LIBC_COMPILER_HAS_BUILTIN_ROUND)
3031
list(APPEND compile_options "-D__LIBC_USE_BUILTIN_ROUND")
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
float try_builtin_ceilf(float x) { return __builtin_ceilf(x); }
22
float try_builtin_floorf(float x) { return __builtin_floorf(x); }
3+
float try_builtin_rintf(float x) { return __builtin_rintf(x); }
34
float try_builtin_truncf(float x) { return __builtin_truncf(x); }
45

56
double try_builtin_ceil(double x) { return __builtin_ceil(x); }
67
double try_builtin_floor(double x) { return __builtin_floor(x); }
8+
double try_builtin_rint(double x) { return __builtin_rint(x); }
79
double try_builtin_trunc(double x) { return __builtin_trunc(x); }
810

911
int main() {}

libc/src/math/generic/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,8 @@ add_entrypoint_object(
862862
-O3
863863
DEPENDS
864864
libc.src.__support.FPUtil.nearest_integer_operations
865+
FLAGS
866+
ROUND_OPT
865867
)
866868

867869
add_entrypoint_object(
@@ -874,6 +876,8 @@ add_entrypoint_object(
874876
-O3
875877
DEPENDS
876878
libc.src.__support.FPUtil.nearest_integer_operations
879+
FLAGS
880+
ROUND_OPT
877881
)
878882

879883
add_entrypoint_object(
@@ -899,6 +903,9 @@ add_entrypoint_object(
899903
DEPENDS
900904
libc.src.__support.macros.properties.types
901905
libc.src.__support.FPUtil.nearest_integer_operations
906+
libc.src.__support.macros.properties.architectures
907+
FLAGS
908+
ROUND_OPT
902909
)
903910

904911
add_entrypoint_object(

libc/src/math/generic/ceil.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
namespace LIBC_NAMESPACE_DECL {
1515

1616
LLVM_LIBC_FUNCTION(double, ceil, (double x)) {
17-
#ifdef __LIBC_USE_BUILTIN_CEIL_FLOOR_TRUNC
17+
#ifdef __LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC
1818
return __builtin_ceil(x);
1919
#else
2020
return fputil::ceil(x);

libc/src/math/generic/ceilf.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
namespace LIBC_NAMESPACE_DECL {
1515

1616
LLVM_LIBC_FUNCTION(float, ceilf, (float x)) {
17-
#ifdef __LIBC_USE_BUILTIN_CEIL_FLOOR_TRUNC
17+
#ifdef __LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC
1818
return __builtin_ceilf(x);
1919
#else
2020
return fputil::ceil(x);

libc/src/math/generic/ceilf16.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
namespace LIBC_NAMESPACE_DECL {
1616

1717
LLVM_LIBC_FUNCTION(float16, ceilf16, (float16 x)) {
18-
#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_TRUNC) && \
18+
#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC) && \
1919
defined(LIBC_TARGET_ARCH_IS_AARCH64)
2020
return static_cast<float16>(__builtin_ceilf(x));
2121
#else

libc/src/math/generic/floor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
namespace LIBC_NAMESPACE_DECL {
1515

1616
LLVM_LIBC_FUNCTION(double, floor, (double x)) {
17-
#ifdef __LIBC_USE_BUILTIN_CEIL_FLOOR_TRUNC
17+
#ifdef __LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC
1818
return __builtin_floor(x);
1919
#else
2020
return fputil::floor(x);

libc/src/math/generic/floorf.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
namespace LIBC_NAMESPACE_DECL {
1515

1616
LLVM_LIBC_FUNCTION(float, floorf, (float x)) {
17-
#ifdef __LIBC_USE_BUILTIN_CEIL_FLOOR_TRUNC
17+
#ifdef __LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC
1818
return __builtin_floorf(x);
1919
#else
2020
return fputil::floor(x);

libc/src/math/generic/floorf16.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
namespace LIBC_NAMESPACE_DECL {
1616

1717
LLVM_LIBC_FUNCTION(float16, floorf16, (float16 x)) {
18-
#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_TRUNC) && \
18+
#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC) && \
1919
defined(LIBC_TARGET_ARCH_IS_AARCH64)
2020
return static_cast<float16>(__builtin_floorf(x));
2121
#else

libc/src/math/generic/rint.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414
namespace LIBC_NAMESPACE_DECL {
1515

1616
LLVM_LIBC_FUNCTION(double, rint, (double x)) {
17+
#ifdef __LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC
18+
return __builtin_rint(x);
19+
#else
1720
return fputil::round_using_current_rounding_mode(x);
21+
#endif
1822
}
1923

2024
} // namespace LIBC_NAMESPACE_DECL

libc/src/math/generic/rintf.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414
namespace LIBC_NAMESPACE_DECL {
1515

1616
LLVM_LIBC_FUNCTION(float, rintf, (float x)) {
17+
#ifdef __LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC
18+
return __builtin_rintf(x);
19+
#else
1720
return fputil::round_using_current_rounding_mode(x);
21+
#endif
1822
}
1923

2024
} // namespace LIBC_NAMESPACE_DECL

libc/src/math/generic/rintf16.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,17 @@
1010
#include "src/__support/FPUtil/NearestIntegerOperations.h"
1111
#include "src/__support/common.h"
1212
#include "src/__support/macros/config.h"
13+
#include "src/__support/macros/properties/architectures.h"
1314

1415
namespace LIBC_NAMESPACE_DECL {
1516

1617
LLVM_LIBC_FUNCTION(float16, rintf16, (float16 x)) {
18+
#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC) && \
19+
defined(LIBC_TARGET_ARCH_IS_AARCH64)
20+
return static_cast<float16>(__builtin_rintf(x));
21+
#else
1722
return fputil::round_using_current_rounding_mode(x);
23+
#endif
1824
}
1925

2026
} // namespace LIBC_NAMESPACE_DECL

libc/src/math/generic/trunc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
namespace LIBC_NAMESPACE_DECL {
1515

1616
LLVM_LIBC_FUNCTION(double, trunc, (double x)) {
17-
#ifdef __LIBC_USE_BUILTIN_CEIL_FLOOR_TRUNC
17+
#ifdef __LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC
1818
return __builtin_trunc(x);
1919
#else
2020
return fputil::trunc(x);

libc/src/math/generic/truncf.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
namespace LIBC_NAMESPACE_DECL {
1515

1616
LLVM_LIBC_FUNCTION(float, truncf, (float x)) {
17-
#ifdef __LIBC_USE_BUILTIN_CEIL_FLOOR_TRUNC
17+
#ifdef __LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC
1818
return __builtin_truncf(x);
1919
#else
2020
return fputil::trunc(x);

libc/src/math/generic/truncf16.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
namespace LIBC_NAMESPACE_DECL {
1616

1717
LLVM_LIBC_FUNCTION(float16, truncf16, (float16 x)) {
18-
#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_TRUNC) && \
18+
#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC) && \
1919
defined(LIBC_TARGET_ARCH_IS_AARCH64)
2020
return static_cast<float16>(__builtin_truncf(x));
2121
#else

libc/test/src/math/performance_testing/CMakeLists.txt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function(add_perf_binary target_name)
2121
"PERF"
2222
"" # No optional arguments
2323
"SUITE;CXX_STANDARD" # Single value arguments
24-
"SRCS;HDRS;DEPENDS;COMPILE_OPTIONS" # Multi-value arguments
24+
"SRCS;HDRS;DEPENDS;COMPILE_OPTIONS;LINK_LIBRARIES" # Multi-value arguments
2525
${ARGN}
2626
)
2727
if(NOT PERF_SRCS)
@@ -64,9 +64,13 @@ function(add_perf_binary target_name)
6464
)
6565
endif()
6666

67+
set(link_libraries ${link_object_files})
68+
foreach(lib IN LISTS PERF_LINK_LIBRARIES)
69+
list(APPEND link_libraries ${lib}.unit)
70+
endforeach()
6771
target_link_libraries(
6872
${fq_target_name}
69-
PRIVATE ${link_object_files} libc_diff_test_utils)
73+
PRIVATE ${link_libraries} libc_diff_test_utils)
7074

7175
set_target_properties(${fq_target_name}
7276
PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
@@ -385,6 +389,8 @@ add_perf_binary(
385389
libc.src.math.ceilf16
386390
libc.src.math.floorf
387391
libc.src.math.floorf16
392+
libc.src.math.rintf
393+
libc.src.math.rintf16
388394
libc.src.math.roundevenf
389395
libc.src.math.roundevenf16
390396
libc.src.math.roundf
@@ -393,4 +399,6 @@ add_perf_binary(
393399
libc.src.math.truncf16
394400
COMPILE_OPTIONS
395401
-fno-builtin
402+
LINK_LIBRARIES
403+
LibcFPTestHelpers
396404
)

libc/test/src/math/performance_testing/nearest_integer_funcs_perf.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,23 @@
1111
#include "src/math/ceilf16.h"
1212
#include "src/math/floorf.h"
1313
#include "src/math/floorf16.h"
14+
#include "src/math/rintf.h"
15+
#include "src/math/rintf16.h"
1416
#include "src/math/roundevenf.h"
1517
#include "src/math/roundevenf16.h"
1618
#include "src/math/roundf.h"
1719
#include "src/math/roundf16.h"
1820
#include "src/math/truncf.h"
1921
#include "src/math/truncf16.h"
22+
#include "test/UnitTest/RoundingModeUtils.h"
2023
#include "test/src/math/performance_testing/Timer.h"
2124

2225
#include <fstream>
2326
#include <math.h>
2427

28+
using LIBC_NAMESPACE::fputil::testing::ForceRoundingMode;
29+
using LIBC_NAMESPACE::fputil::testing::RoundingMode;
30+
2531
namespace LIBC_NAMESPACE::testing {
2632

2733
template <typename T> class NearestIntegerPerf {
@@ -164,5 +170,30 @@ int main() {
164170
NEAREST_INTEGER_PERF(float, LIBC_NAMESPACE::truncf, ::truncf, FLOAT_ROUNDS,
165171
"truncf_perf.log")
166172

173+
if (ForceRoundingMode r(RoundingMode::Upward); r.success) {
174+
NEAREST_INTEGER_PERF(float16, LIBC_NAMESPACE::rintf16, ::placeholderf16,
175+
FLOAT16_ROUNDS, "rintf16_upward_perf.log")
176+
NEAREST_INTEGER_PERF(float, LIBC_NAMESPACE::rintf, ::rintf, FLOAT_ROUNDS,
177+
"rintf_upward_perf.log")
178+
}
179+
if (ForceRoundingMode r(RoundingMode::Downward); r.success) {
180+
NEAREST_INTEGER_PERF(float16, LIBC_NAMESPACE::rintf16, ::placeholderf16,
181+
FLOAT16_ROUNDS, "rintf16_downward_perf.log")
182+
NEAREST_INTEGER_PERF(float, LIBC_NAMESPACE::rintf, ::rintf, FLOAT_ROUNDS,
183+
"rintf_downward_perf.log")
184+
}
185+
if (ForceRoundingMode r(RoundingMode::TowardZero); r.success) {
186+
NEAREST_INTEGER_PERF(float16, LIBC_NAMESPACE::rintf16, ::placeholderf16,
187+
FLOAT16_ROUNDS, "rintf16_towardzero_perf.log")
188+
NEAREST_INTEGER_PERF(float, LIBC_NAMESPACE::rintf, ::rintf, FLOAT_ROUNDS,
189+
"rintf_towardzero_perf.log")
190+
}
191+
if (ForceRoundingMode r(RoundingMode::Nearest); r.success) {
192+
NEAREST_INTEGER_PERF(float16, LIBC_NAMESPACE::rintf16, ::placeholderf16,
193+
FLOAT16_ROUNDS, "rintf16_nearest_perf.log")
194+
NEAREST_INTEGER_PERF(float, LIBC_NAMESPACE::rintf, ::rintf, FLOAT_ROUNDS,
195+
"rintf_nearest_perf.log")
196+
}
197+
167198
return 0;
168199
}

0 commit comments

Comments
 (0)