Skip to content

Commit fc21b4c

Browse files
committed
Prevent use of -Ofast optimization in gcc.
The C version of Astronomy Engine does not work correctly when gcc "fast math" optimizations are enabled. The problem is that Astronomy Engine uses NAN values to represent invalid/uninitialized floating point numbers. The -Ofast option breaks the ability of the runtime to check for NAN values, resulting in multiple failures and incorrect behaviors at runtime. Added a compile-time check for the __FAST_MATH__ preprocessor symbol, which gcc defines to signal that the optimization was enabled. If detected, this results in a compiler error to make it obvious that something is wrong before invalid code would be executed. This is not an ideal fix for two reasons: 1. I don't know if this will detect similar problems for other compilers than gcc. 2. If individual risky math optimizations are enabled, instead of the combination of options included in -Ofast, the __FAST_MATH__ preprocessor symbol will not be defined and no compiler error will occur. I could not find a way to detect individual risky optimizations. However, this change is much better than nothing, and hopefully it will prevent most cases of overly-aggressive optimization.
1 parent 18a0c56 commit fc21b4c

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

Diff for: generate/template/astronomy.c

+4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
#include <math.h>
3333
#include "astronomy.h"
3434

35+
#ifdef __FAST_MATH__
36+
#error Astronomy Engine does not support "fast math" optimization because it causes incorrect behavior. See: https://github.com/cosinekitty/astronomy/issues/245
37+
#endif
38+
3539
#ifdef __cplusplus
3640
extern "C" {
3741
#endif

Diff for: source/c/astronomy.c

+4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
#include <math.h>
3333
#include "astronomy.h"
3434

35+
#ifdef __FAST_MATH__
36+
#error Astronomy Engine does not support "fast math" optimization because it causes incorrect behavior. See: https://github.com/cosinekitty/astronomy/issues/245
37+
#endif
38+
3539
#ifdef __cplusplus
3640
extern "C" {
3741
#endif

0 commit comments

Comments
 (0)