news.volyx.in

Someone’s Been Messing with My Subnormals (moyix.blogspot.com)

442 points by jpegqs · 1464 days ago · 127 comments on HN

Article summary

The article discusses how a Python package, gevent, was found to be causing issues with floating point math due to being compiled with the -ffast-math flag, which sets the FTZ/DAZ flags in the MXCSR register. This flag can cause problems with numerical algorithms that rely on standard floating point behavior. The author created a script to scan the top 25% of Python packages on PyPI and found that over 2,500 packages could potentially cause incorrect numerical results. The issue is not limited to gevent and can affect any package compiled with -ffast-math.

Main themes

  • compiler flags
  • floating point math
  • dynamic linking
  • numerical algorithms
  • Python packages
  • FPU control register
  • performance optimization
  • compiler optimizations
  • testing and verification

What commenters say

  • Dynamic linking is a root cause of the problem, allowing a single library to affect the behavior of other libraries.
  • The issue is not with dynamic linking, but rather with the global state of the FPU control register.
  • Compiling packages with -ffast-math can have unintended consequences and should be done with caution.
  • The -fno-fast-math flag does not disable fast math when used with -Ofast, and neither does -fno-unsafe-math-optimizations.
  • Explicitly enabling or disabling FTZ/DAZ flags at the borders of code regions that require special treatment is a better approach than relying on compiler flags.
  • Modern microarchitectures may not incur significant performance penalties for twiddling MXCSR bits, contrary to earlier claims.
  • The naming of compiler flags, such as -Ofast, can be misleading and lead to unintended consequences.
  • Thorough testing of fixes is necessary to ensure that they actually resolve the issues they are intended to address.