Skip to content

Handle infinity and NaN with -ffinite-math-only #68

@fwyzard

Description

@fwyzard

gcc

According to the documentation of -ffinite-math-only:

Allow optimizations for floating-point arithmetic that assume that arguments and results are not NaNs or +-Infs.

When -ffinite-math-only is used, gcc

nvcc

According to the documentation --use_fast_math:

Make use of fast math library. --use_fast_math implies --ftz=true --prec-div=false --prec-sqrt=false --fmad=true.

When --use_fast_math is used, nvcc

  • compiles std::isnan(arg), ::isnan(arg) and __builtin_isnan(arg) to correct PTX (sm_90)

  • compiles the current implementation of xtd::isnan(arg) to correct PTX (sm_90)

clang

According to the documentation of -ffinite-math-only:

Allow floating-point optimizations that assume arguments and results are not NaNs or +-inf. This defines the __FINITE_MATH_ONLY__ preprocessor macro.

host compilation

When -ffinite-math-only is used, clang

  • may replace a call to std::isnan(arg) with 0, with a warning (x86-64, aarch64):

    warning: use of NaN is undefined behavior due to the currently enabled floating-point options [-Wnan-infinity-disabled]

  • optimises the current implementation of xtd::isnan(arg) and replaces it with 0 (x86-64, aarch64)

clang -x cu

When -ffinite-math-only is used, clang -x cu

  • may replace a call to __builtin_isnan(arg) with 0, with a warning (sm_90):

    warning: use of NaN is undefined behavior due to the currently enabled floating-point options [-Wnan-infinity-disabled]

  • optimises the current implementation of xtd::isnan(arg) and replaces it with 0 (sm_90)

clang -x hip, hipcc

When -ffinite-math-only is used, hipcc or clang -x hip

  • may replace a call to __builtin_isnan(arg), ::isnan(arg) or std::isnan(arg) with 0, with a warning (gfx906, gfx942)

  • optimises the current implementation of xtd::isnan(arg) and replaces it with 0 (gfx906)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions