Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add USE_FLOAT_EXCEPTIONS to enable floating point exceptions #1451

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

illwieckz
Copy link
Member

@illwieckz illwieckz commented Dec 2, 2024

  • Add USE_FLOAT_EXCEPTIONS to enable floating point exceptions (disabled by default)
  • Add USE_FAST_MATH to enable or disable fast math (enabled by default)

When USE_DEBUG_FPE is used, nothing is done unless some of those cvars are enabled:

  • common.floatExceptions.invalid
  • common.floatExceptions.divByZero
  • common.floatExceptions.overflow

The USE_FLOAT_EXCEPTIONS option is required to specialize the build to make it possible to enable them.

@illwieckz
Copy link
Member Author

I have not tested the Apple and MSVC code.

@illwieckz illwieckz force-pushed the illwieckz/catch-0div branch 5 times, most recently from f05b27f to 4993a7f Compare December 3, 2024 09:00
src/engine/framework/System.cpp Outdated Show resolved Hide resolved
src/engine/framework/System.cpp Outdated Show resolved Hide resolved
src/engine/framework/System.cpp Outdated Show resolved Hide resolved
src/engine/framework/System.cpp Outdated Show resolved Hide resolved
@illwieckz illwieckz changed the title Add USE_DEBUG_FPE to enable floating point exceptions Add USE_FLOAT_EXCEPTIONS to enable floating point exceptions Dec 4, 2024
@illwieckz illwieckz force-pushed the illwieckz/catch-0div branch from 4993a7f to 8687854 Compare December 4, 2024 08:31
@illwieckz
Copy link
Member Author

I renamed the cmake option to USE_FLOAT_EXCEPTIONS.

@illwieckz illwieckz force-pushed the illwieckz/catch-0div branch 2 times, most recently from b878d3a to 7c60e69 Compare December 4, 2024 08:35
@@ -276,6 +298,69 @@ static void CloseSingletonSocket()
#endif
}

static void SetFloatingPointExceptions()
{
// Must be done after Sys::Init() to read cvars from command line.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong comment

static void SetFloatingPointExceptions()
{
// Must be done after Sys::Init() to read cvars from command line.
#if defined(DAEMON_USE_FLOAT_EXCEPTIONS_AVAILABLE)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our code style is to have these on the left side and never indent them based on indentation of non-preprocessor code.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's bad practice within a block, really.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What? This style is used very consistently in our code and is common in other code bases too. Preprocessor and non-preprocessor lines do not syntactically nest with each other, so it makes a sort of sense that neither one affects the other's indentation.

unsigned int exceptions = 0;
#endif

// Operations with NaN.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not very accurate, cos(1.001) also does this for example

cmake/DaemonFlags.cmake Outdated Show resolved Hide resolved
@@ -148,13 +148,20 @@ else()
set(WARNMODE "no-")
endif()

# Compiler options
option(USE_FLOAT_EXCEPTIONS "Use floating point exceptions" OFF)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use description of this is not quite accurate. It doesn't turn on exceptions; it alters compiler options in a way that makes the exceptions more likely to be useful. Maybe you could call it USE_FLOAT_DEBUG_MODE or something

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I think the name is fine but it would be good to mention the cvars. Enable floating point exceptions with common.floatException.* cvars

@illwieckz illwieckz force-pushed the illwieckz/catch-0div branch from 7c60e69 to a65cdc7 Compare December 5, 2024 07:56
@illwieckz
Copy link
Member Author

Hmm, my mac doesn't support more than Catalina, so no more than macOS 10.15.7 with Clang 12.0.0, and I get these errors:

src/engine/framework/System.cpp:319:19: error: use of
      undeclared identifier '__fpcr_trap_invalid'
                                exceptions |= __fpcr_trap_invalid;
                                              ^
src/engine/framework/System.cpp:331:19: error: use of
      undeclared identifier '__fpcr_trap_divbyzero'
                                exceptions |= __fpcr_trap_divbyzero;
                                              ^
src/engine/framework/System.cpp:343:19: error: use of
      undeclared identifier '__fpcr_trap_overflow'
                                exceptions |= __fpcr_trap_overflow;
                                              ^
src/engine/framework/System.cpp:356:8: error: no member named
      '__fpcr' in 'fenv_t'
                        env.__fpcr = env.__fpcr | exceptions;
                        ~~~ ^
src/engine/framework/System.cpp:356:21: error: no member named
      '__fpcr' in 'fenv_t'
                        env.__fpcr = env.__fpcr | exceptions;

So I cannot test macOS right now.

@illwieckz illwieckz force-pushed the illwieckz/catch-0div branch 3 times, most recently from 10070cb to 4086988 Compare December 5, 2024 10:12
@illwieckz
Copy link
Member Author

illwieckz commented Dec 6, 2024

I face a weird cmake bug, if I do that:

	if (USE_FLOAT_EXCEPTIONS)
		message(STATUS "test true")
		# Floating point exceptions requires trapping math
		# to avoid false positives on architectures with SSE.
		set_c_cxx_flag("-ffp-model=strict")
	endif()

The “test true” message is printed, but the -ffp-model=strict flag isn't set.

But if I do:

	set_c_cxx_flag("-ffp-model=strict")
	if (USE_FLOAT_EXCEPTIONS)
		message(STATUS "test true")
		# Floating point exceptions requires trapping math
		# to avoid false positives on architectures with SSE.
	endif()

The flag is set.

So to sum it up:

  • I know the test is true,
  • I know the block is executed,
  • I know the function works,

but the whole combination doesn't work…

@illwieckz
Copy link
Member Author

illwieckz commented Dec 6, 2024

If I do instead:

	if (USE_FLOAT_EXCEPTIONS)
		message(STATUS "test true")
		try_c_cxx_flag(FFP_MODEL_STRICT "-ffp-model=strict")
		# Floating point exceptions requires trapping math
		# to avoid false positives on architectures with SSE.
	endif()

I get this printed:

-- test true
-- Performing Test FLAG_FFP_MODEL_STRICT
-- Performing Test FLAG_FFP_MODEL_STRICT - Success

But the flag is not added to the compiler command line.

On the contrary if I do that:

	try_c_cxx_flag(FFP_MODEL_STRICT "-ffp-model=strict")
	if (USE_FLOAT_EXCEPTIONS)
		message(STATUS "test true")
		# Floating point exceptions requires trapping math
		# to avoid false positives on architectures with SSE.
	endif()

I get this printed:

-- Performing Test FLAG_FFP_MODEL_STRICT
-- Performing Test FLAG_FFP_MODEL_STRICT - Success
-- test true

And the flag is added to the compiler command line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants