From a1e799234f4702606170369a061ba1396ef9ad10 Mon Sep 17 00:00:00 2001 From: sharkautarch <128002472+sharkautarch@users.noreply.github.com> Date: Tue, 3 Dec 2024 12:46:48 -0500 Subject: [PATCH] Remove global -ffast-math flag, but apply fast math to just color_helpers.h/color_helpers.cpp --- meson.build | 4 ---- src/Utils/Directives.h | 16 ++++++++++++++++ src/color_helpers.cpp | 9 +++++++-- src/color_helpers.h | 5 ++++- src/color_helpers_impl.h | 2 ++ src/color_tests.cpp | 2 +- src/steamcompmgr.cpp | 2 +- 7 files changed, 31 insertions(+), 9 deletions(-) create mode 100644 src/Utils/Directives.h diff --git a/meson.build b/meson.build index b562ec3512..9d0b487eb0 100644 --- a/meson.build +++ b/meson.build @@ -37,10 +37,6 @@ add_project_arguments(cppc.get_supported_arguments([ '-Wno-missing-braces', ]), language: 'cpp') -add_project_arguments(cppc.get_supported_arguments([ - '-ffast-math', -]), language: 'cpp') - pipewire_dep = dependency('libpipewire-0.3', required: get_option('pipewire')) librt_dep = cppc.find_library('rt', required : get_option('pipewire')) hwdata_dep = dependency('hwdata', required : false) diff --git a/src/Utils/Directives.h b/src/Utils/Directives.h new file mode 100644 index 0000000000..deccf12a9a --- /dev/null +++ b/src/Utils/Directives.h @@ -0,0 +1,16 @@ +#pragma once + +#ifdef __clang__ +# define FAST_MATH_ON _Pragma("float_control(push)"); \ + _Pragma("float_control(precise, off)") //https://clang.llvm.org/docs/LanguageExtensions.html#extensions-to-specify-floating-point-flags +# define FAST_MATH_OFF _Pragma("float_control(pop)") + +#elif defined(__GNUC__) +# define FAST_MATH_ON _Pragma("GCC push_options"); \ + _Pragma("GCC optimize(\"-ffast-math\")") +# define FAST_MATH_OFF _Pragma("GCC pop_options") + +#else +# define FAST_MATH_ON +# define FAST_MATH_OFF +#endif diff --git a/src/color_helpers.cpp b/src/color_helpers.cpp index 2075eca463..fc55ce09c2 100644 --- a/src/color_helpers.cpp +++ b/src/color_helpers.cpp @@ -1,6 +1,7 @@ #define COLOR_HELPERS_CPP #include "color_helpers_impl.h" + #include #include #include @@ -9,9 +10,11 @@ #include #include +#include + +FAST_MATH_ON #include #include -#include glm::vec3 xyY_to_XYZ( const glm::vec2 & xy, float Y ) @@ -214,7 +217,7 @@ inline void lerp_rgb(float* out, const float* a, const float* b, const float* c, inline float ClampAndSanitize( float a, float min, float max ) { -#ifndef __FAST_MATH__ +#if !( defined(__FAST_MATH__) || defined(__FINITE_MATH_ONLY__) ) return std::isfinite( a ) ? std::min(std::max(min, a), max) : min; #else return std::min(std::max(min, a), max); @@ -910,3 +913,5 @@ const glm::mat3 k_xyz_from_2020 = normalised_primary_matrix( displaycolorimetry_ const glm::mat3 k_2020_from_xyz = glm::inverse( k_xyz_from_2020 ); const glm::mat3 k_2020_from_709 = k_2020_from_xyz * k_xyz_from_709; + +FAST_MATH_OFF \ No newline at end of file diff --git a/src/color_helpers.h b/src/color_helpers.h index 66213e1594..2a8a964760 100644 --- a/src/color_helpers.h +++ b/src/color_helpers.h @@ -9,9 +9,11 @@ #include // glm::vec2 #include // glm::vec3 -#include // glm::mat3 #include +FAST_MATH_ON +#include // glm::mat3 + // Color utils inline int quantize( float fVal, float fMaxVal ) { @@ -493,3 +495,4 @@ extern const glm::mat3 k_xyz_from_2020; extern const glm::mat3 k_2020_from_xyz; extern const glm::mat3 k_2020_from_709; +FAST_MATH_OFF \ No newline at end of file diff --git a/src/color_helpers_impl.h b/src/color_helpers_impl.h index 3e8c2d5a5e..772107f8da 100644 --- a/src/color_helpers_impl.h +++ b/src/color_helpers_impl.h @@ -1,4 +1,6 @@ #pragma once +#include "Utils/Directives.h" + #include "color_helpers.h" namespace rendervulkan { diff --git a/src/color_tests.cpp b/src/color_tests.cpp index 2d682bf661..b33e4d9480 100644 --- a/src/color_tests.cpp +++ b/src/color_tests.cpp @@ -1,4 +1,4 @@ -#include "color_helpers.h" +#include "color_helpers_impl.h" #include //#include diff --git a/src/steamcompmgr.cpp b/src/steamcompmgr.cpp index a8d959f78a..a146e908f1 100644 --- a/src/steamcompmgr.cpp +++ b/src/steamcompmgr.cpp @@ -5069,7 +5069,7 @@ steamcompmgr_latch_frame_done( steamcompmgr_win_t *w, uint64_t vblank_idx ) static inline float santitize_float( float f ) { -#ifndef __FAST_MATH__ +#if !( defined(__FAST_MATH__) || defined(__FINITE_MATH_ONLY__) ) return ( std::isfinite( f ) ? f : 0.f ); #else return f;