Skip to content

Commit

Permalink
test__issue1864.py: Disable on Python 2.7; it failed there for reason…
Browse files Browse the repository at this point in the history
…s that aren't clear. I verified that Ofast or fast-math was not given. Perhaps some underlying lib had that set?

I'm not very concerned about it though.
  • Loading branch information
jamadden committed Oct 4, 2022
1 parent c105c1b commit a48c33b
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/gevent/tests/test__issue1864.py
@@ -1,20 +1,27 @@
import sys
import unittest

from gevent.testing import skipOnPy2

class TestSubnormalFloatsAreNotDisabled(unittest.TestCase):
"""
Enabling the -Ofast compiler flag resulted in subnormal floats getting
disabled the moment when gevent was imported. This impacted libraries
that expect subnormal floats to be enabled.
"""

@skipOnPy2('This test always fails on Python 2')
def test_subnormal_is_not_zero(self):
import gevent
# Enabling the -Ofast compiler flag resulted in subnormal floats getting
# disabled the moment when gevent was imported. This impacted libraries
# that expect subnormal floats to be enabled.
#
# NOTE: This test is supposed to catch that. It doesn't seem to work perfectly, though.
# The test passes under Python 2 on macOS no matter whether -ffast-math is given or not;
# perhaps this is a difference in clang vs gcc? In contrast, the test on Python 2.7 always
# *fails* on GitHub actions (in both CPython 2.7 and PyPy). We're far past the EOL of
# Python 2.7 so I'm not going to spend much time investigating.

This comment has been minimized.

Copy link
@ikonst

ikonst Oct 5, 2022

Contributor

@jamadden FYI works for me, but agree it sounds like a pain to debug:

$ cat > /tmp/math.c

with contents:

// https://github.com/gcc-mirror/gcc/blob/master/libgcc/config/i386/crtfastmath.c#L94-L96
#define MXCSR_DAZ (1 << 6)	/* Enable denormals are zero mode */
#define MXCSR_FTZ (1 << 15)	/* Enable flush to zero mode */

const int FAST_MATH_FLAGS = MXCSR_DAZ | MXCSR_FTZ;

void set_fast_math()
{
  unsigned int mxcsr = __builtin_ia32_stmxcsr ();
  mxcsr |= FAST_MATH_FLAGS;
  __builtin_ia32_ldmxcsr (mxcsr);
}

void set_slow_math()
{
  unsigned int mxcsr = __builtin_ia32_stmxcsr ();
  mxcsr &= ~FAST_MATH_FLAGS;
  __builtin_ia32_ldmxcsr (mxcsr);
}

then

$ clang -shared -o /tmp/math.so /tmp/math.c

and then

$ uname -m
x86_64
$ python2
Python 2.7.12 (default, Mar  1 2021, 11:38:31)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.

and trying this:

import sys
print((sys.float_info.min / 2) > 0)

from ctypes import cdll
lib = cdll.LoadLibrary("/tmp/math.so")

lib.set_fast_math()
print((sys.float_info.min / 2) > 0)

lib.set_slow_math()
print((sys.float_info.min / 2) > 0)

yields in

True
False
True
__import__('gevent')

# `sys.float_info.min` is the minimum representable positive normalized
# float, so dividing it by two gives us a positive subnormal float,
# as long as subnormals floats are not disabled.
assert (sys.float_info.min / 2) > 0
self.assertGreater(sys.float_info.min / 2, 0)


if __name__ == "__main__":
Expand Down

0 comments on commit a48c33b

Please sign in to comment.