[libc][math] Optimize nearest integer functions using builtins when available by overmighty · Pull Request #98376 · llvm/llvm-project (original) (raw)

@llvm/pr-subscribers-libc

Author: OverMighty (overmighty)

Changes


Patch is 34.78 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/98376.diff

27 Files Affected:

diff --git a/libc/cmake/modules/CheckCompilerFeatures.cmake b/libc/cmake/modules/CheckCompilerFeatures.cmake index d84c07b35d2d7..83822892c8096 100644 --- a/libc/cmake/modules/CheckCompilerFeatures.cmake +++ b/libc/cmake/modules/CheckCompilerFeatures.cmake @@ -2,7 +2,15 @@

Compiler features definition and flags

------------------------------------------------------------------------------

-set(ALL_COMPILER_FEATURES "float16" "float128" "fixed_point") +set(

+)

Making sure ALL_COMPILER_FEATURES is sorted.

list(SORT ALL_COMPILER_FEATURES) @@ -39,11 +47,19 @@ endfunction() set(AVAILABLE_COMPILER_FEATURES "")

Try compile a C file to check if flag is supported.

-set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) foreach(feature IN LISTS ALL_COMPILER_FEATURES)

endif()

try_compile( @@ -60,6 +76,12 @@ foreach(feature IN LISTS ALL_COMPILER_FEATURES) set(LIBC_TYPES_HAS_FLOAT128 TRUE) elseif(${feature} STREQUAL "fixed_point") set(LIBC_COMPILER_HAS_FIXED_POINT TRUE)

@@ -16,8 +16,22 @@ function(_get_compile_options_from_flags output_var) list(APPEND compile_options "-D__LIBC_RISCV_USE_FMA") endif() endif()

diff --git a/libc/cmake/modules/LLVMLibCFlagRules.cmake b/libc/cmake/modules/LLVMLibCFlagRules.cmake index 18e36dfde5cc1..eca7ba8d183e6 100644 --- a/libc/cmake/modules/LLVMLibCFlagRules.cmake +++ b/libc/cmake/modules/LLVMLibCFlagRules.cmake @@ -277,6 +277,7 @@ if(NOT(LIBC_TARGET_ARCHITECTURE_IS_X86 AND (LIBC_CPU_FEATURES MATCHES "SSE2"))) endif()

Skip ROUND_OPT flag for targets that don't support SSE 4.2.

-if(NOT(LIBC_TARGET_ARCHITECTURE_IS_X86 AND (LIBC_CPU_FEATURES MATCHES "SSE4_2"))) +if(NOT((LIBC_TARGET_ARCHITECTURE_IS_X86 AND (LIBC_CPU_FEATURES MATCHES "SSE4_2")) OR

uint32_t trim_size = FPBits::FRACTION_LEN - exponent;

@@ -130,15 +132,17 @@ LIBC_INLINE T round(T x) { uint32_t trim_size = FPBits::FRACTION_LEN - exponent; bool half_bit_set = bool(bits.get_mantissa() & (StorageType(1) << (trim_size - 1)));

@@ -188,16 +192,17 @@ round_using_specific_rounding_mode(T x, int rnd) { }

uint32_t trim_size = FPBits::FRACTION_LEN - exponent;

diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt index 5e920307d39de..915fc076826f9 100644 --- a/libc/src/math/generic/CMakeLists.txt +++ b/libc/src/math/generic/CMakeLists.txt @@ -70,6 +70,8 @@ add_entrypoint_object( -O3 DEPENDS libc.src.__support.FPUtil.nearest_integer_operations

)

add_entrypoint_object( @@ -82,6 +84,8 @@ add_entrypoint_object( -O3 DEPENDS libc.src.__support.FPUtil.nearest_integer_operations

)

add_entrypoint_object( @@ -107,6 +111,9 @@ add_entrypoint_object( DEPENDS libc.src.__support.macros.properties.types libc.src.__support.FPUtil.nearest_integer_operations

)

add_entrypoint_object( @@ -455,6 +462,8 @@ add_entrypoint_object( -O3 DEPENDS libc.src.__support.FPUtil.nearest_integer_operations

)

add_entrypoint_object( @@ -467,6 +476,8 @@ add_entrypoint_object( -O3 DEPENDS libc.src.__support.FPUtil.nearest_integer_operations

)

add_entrypoint_object( @@ -492,6 +503,9 @@ add_entrypoint_object( DEPENDS libc.src.__support.macros.properties.types libc.src.__support.FPUtil.nearest_integer_operations

)

add_entrypoint_object( @@ -517,6 +531,8 @@ add_entrypoint_object( -O3 DEPENDS libc.src.__support.FPUtil.nearest_integer_operations

)

add_entrypoint_object( @@ -529,6 +545,8 @@ add_entrypoint_object( -O3 DEPENDS libc.src.__support.FPUtil.nearest_integer_operations

)

add_entrypoint_object( @@ -554,6 +572,9 @@ add_entrypoint_object( DEPENDS libc.src.__support.macros.properties.types libc.src.__support.FPUtil.nearest_integer_operations

)

add_entrypoint_object( @@ -579,6 +600,8 @@ add_entrypoint_object( -O3 DEPENDS libc.src.__support.FPUtil.nearest_integer_operations

)

add_entrypoint_object( @@ -591,6 +614,8 @@ add_entrypoint_object( -O3 DEPENDS libc.src.__support.FPUtil.nearest_integer_operations

)

add_entrypoint_object( @@ -616,6 +641,9 @@ add_entrypoint_object( DEPENDS libc.src.__support.macros.properties.types libc.src.__support.FPUtil.nearest_integer_operations

)

add_entrypoint_object( @@ -641,6 +669,8 @@ add_entrypoint_object( -O3 DEPENDS libc.src.__support.FPUtil.nearest_integer_operations

)

add_entrypoint_object( @@ -653,6 +683,8 @@ add_entrypoint_object( -O3 DEPENDS libc.src.__support.FPUtil.nearest_integer_operations

)

add_entrypoint_object( @@ -678,6 +710,9 @@ add_entrypoint_object( DEPENDS libc.src.__support.macros.properties.types libc.src.__support.FPUtil.nearest_integer_operations

)

add_entrypoint_object( diff --git a/libc/src/math/generic/ceil.cpp b/libc/src/math/generic/ceil.cpp index efd0f246a9b90..63da803033e22 100644 --- a/libc/src/math/generic/ceil.cpp +++ b/libc/src/math/generic/ceil.cpp @@ -12,6 +12,12 @@

namespace LIBC_NAMESPACE {

-LLVM_LIBC_FUNCTION(double, ceil, (double x)) { return fputil::ceil(x); } +LLVM_LIBC_FUNCTION(double, ceil, (double x)) { +#ifdef __LIBC_USE_BUILTIN_CEIL_FLOOR_TRUNC

} // namespace LIBC_NAMESPACE diff --git a/libc/src/math/generic/ceilf.cpp b/libc/src/math/generic/ceilf.cpp index d49b34242da4f..51ef68f1dd871 100644 --- a/libc/src/math/generic/ceilf.cpp +++ b/libc/src/math/generic/ceilf.cpp @@ -12,6 +12,12 @@

namespace LIBC_NAMESPACE {

-LLVM_LIBC_FUNCTION(float, ceilf, (float x)) { return fputil::ceil(x); } +LLVM_LIBC_FUNCTION(float, ceilf, (float x)) { +#ifdef __LIBC_USE_BUILTIN_CEIL_FLOOR_TRUNC

} // namespace LIBC_NAMESPACE diff --git a/libc/src/math/generic/ceilf16.cpp b/libc/src/math/generic/ceilf16.cpp index 205d7428f66e6..ee584c25a4ae9 100644 --- a/libc/src/math/generic/ceilf16.cpp +++ b/libc/src/math/generic/ceilf16.cpp @@ -9,9 +9,17 @@ #include "src/math/ceilf16.h" #include "src/__support/FPUtil/NearestIntegerOperations.h" #include "src/__support/common.h" +#include "src/__support/macros/properties/architectures.h"

namespace LIBC_NAMESPACE {

-LLVM_LIBC_FUNCTION(float16, ceilf16, (float16 x)) { return fputil::ceil(x); } +LLVM_LIBC_FUNCTION(float16, ceilf16, (float16 x)) { +#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_TRUNC) && \

} // namespace LIBC_NAMESPACE diff --git a/libc/src/math/generic/floor.cpp b/libc/src/math/generic/floor.cpp index 60386f0c9cf81..bb58ca6a35402 100644 --- a/libc/src/math/generic/floor.cpp +++ b/libc/src/math/generic/floor.cpp @@ -12,6 +12,12 @@

namespace LIBC_NAMESPACE {

-LLVM_LIBC_FUNCTION(double, floor, (double x)) { return fputil::floor(x); } +LLVM_LIBC_FUNCTION(double, floor, (double x)) { +#ifdef __LIBC_USE_BUILTIN_CEIL_FLOOR_TRUNC

} // namespace LIBC_NAMESPACE diff --git a/libc/src/math/generic/floorf.cpp b/libc/src/math/generic/floorf.cpp index 85666688685dc..459f338d897be 100644 --- a/libc/src/math/generic/floorf.cpp +++ b/libc/src/math/generic/floorf.cpp @@ -12,6 +12,12 @@

namespace LIBC_NAMESPACE {

-LLVM_LIBC_FUNCTION(float, floorf, (float x)) { return fputil::floor(x); } +LLVM_LIBC_FUNCTION(float, floorf, (float x)) { +#ifdef __LIBC_USE_BUILTIN_CEIL_FLOOR_TRUNC

} // namespace LIBC_NAMESPACE diff --git a/libc/src/math/generic/floorf16.cpp b/libc/src/math/generic/floorf16.cpp index db0b326c0e5f6..6d8c497946c84 100644 --- a/libc/src/math/generic/floorf16.cpp +++ b/libc/src/math/generic/floorf16.cpp @@ -9,9 +9,17 @@ #include "src/math/floorf16.h" #include "src/__support/FPUtil/NearestIntegerOperations.h" #include "src/__support/common.h" +#include "src/__support/macros/properties/architectures.h"

namespace LIBC_NAMESPACE {

-LLVM_LIBC_FUNCTION(float16, floorf16, (float16 x)) { return fputil::floor(x); } +LLVM_LIBC_FUNCTION(float16, floorf16, (float16 x)) { +#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_TRUNC) && \

} // namespace LIBC_NAMESPACE diff --git a/libc/src/math/generic/round.cpp b/libc/src/math/generic/round.cpp index ca8f19f35f7fe..d873524ad9f42 100644 --- a/libc/src/math/generic/round.cpp +++ b/libc/src/math/generic/round.cpp @@ -12,6 +12,12 @@

namespace LIBC_NAMESPACE {

-LLVM_LIBC_FUNCTION(double, round, (double x)) { return fputil::round(x); } +LLVM_LIBC_FUNCTION(double, round, (double x)) { +#ifdef __LIBC_USE_BUILTIN_ROUND

} // namespace LIBC_NAMESPACE diff --git a/libc/src/math/generic/roundeven.cpp b/libc/src/math/generic/roundeven.cpp index 5f2adf9b5fce6..76409d526e208 100644 --- a/libc/src/math/generic/roundeven.cpp +++ b/libc/src/math/generic/roundeven.cpp @@ -13,7 +13,11 @@ namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(double, roundeven, (double x)) { +#ifdef __LIBC_USE_BUILTIN_ROUNDEVEN

} // namespace LIBC_NAMESPACE diff --git a/libc/src/math/generic/roundevenf.cpp b/libc/src/math/generic/roundevenf.cpp index 353bec74ecf02..22538272bedbd 100644 --- a/libc/src/math/generic/roundevenf.cpp +++ b/libc/src/math/generic/roundevenf.cpp @@ -13,7 +13,11 @@ namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(float, roundevenf, (float x)) { +#ifdef __LIBC_USE_BUILTIN_ROUNDEVEN

} // namespace LIBC_NAMESPACE diff --git a/libc/src/math/generic/roundevenf16.cpp b/libc/src/math/generic/roundevenf16.cpp index 9ecf79ce6f6c2..90c75a10d3ddb 100644 --- a/libc/src/math/generic/roundevenf16.cpp +++ b/libc/src/math/generic/roundevenf16.cpp @@ -9,11 +9,17 @@ #include "src/math/roundevenf16.h" #include "src/__support/FPUtil/NearestIntegerOperations.h" #include "src/__support/common.h" +#include "src/__support/macros/properties/architectures.h"

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(float16, roundevenf16, (float16 x)) { +#if defined(__LIBC_USE_BUILTIN_ROUNDEVEN) && \

} // namespace LIBC_NAMESPACE diff --git a/libc/src/math/generic/roundf.cpp b/libc/src/math/generic/roundf.cpp index 9627390ea8b8d..8b3add7cb9e2d 100644 --- a/libc/src/math/generic/roundf.cpp +++ b/libc/src/math/generic/roundf.cpp @@ -12,6 +12,12 @@

namespace LIBC_NAMESPACE {

-LLVM_LIBC_FUNCTION(float, roundf, (float x)) { return fputil::round(x); } +LLVM_LIBC_FUNCTION(float, roundf, (float x)) { +#ifdef __LIBC_USE_BUILTIN_ROUND

} // namespace LIBC_NAMESPACE diff --git a/libc/src/math/generic/roundf16.cpp b/libc/src/math/generic/roundf16.cpp index 75a255d7798d5..fca0194ec5dbb 100644 --- a/libc/src/math/generic/roundf16.cpp +++ b/libc/src/math/generic/roundf16.cpp @@ -9,9 +9,16 @@ #include "src/math/roundf16.h" #include "src/__support/FPUtil/NearestIntegerOperations.h" #include "src/__support/common.h" +#include "src/__support/macros/properties/architectures.h"

namespace LIBC_NAMESPACE {

-LLVM_LIBC_FUNCTION(float16, roundf16, (float16 x)) { return fputil::round(x); } +LLVM_LIBC_FUNCTION(float16, roundf16, (float16 x)) { +#if defined(__LIBC_USE_BUILTIN_ROUND) && defined(LIBC_TARGET_ARCH_IS_AARCH64)

} // namespace LIBC_NAMESPACE diff --git a/libc/src/math/generic/trunc.cpp b/libc/src/math/generic/trunc.cpp index d171ab1f092fd..5761565646c36 100644 --- a/libc/src/math/generic/trunc.cpp +++ b/libc/src/math/generic/trunc.cpp @@ -12,6 +12,12 @@

namespace L... [truncated]