Floating-point rounding modes
and setjmp/longjmp

David N. Williams

Last change: June 19, 2005

The Single UNIX 3, C99 specification for setjmp() and longjmp() is silent about what part of the floating point environment shall be saved by a setjmp() and restored by a corresponding longjmp() return, except for a mention in the longjmp() spec of the example that the floating-point status flags are allowed to change between the two, and should not be restored.

In particular, it is silent about the floating-point control modes, such as the IEEE 754 rounding modes FE_TONEAREST, FE_TOWARDZERO, FE_DOWNWARD, and FE_UPWARD.

We noticed that the setjmp/longjmp behavior changed between the Panther and Tiger versions of Mac OS X (10.3.x and 10.4.x), from not restoring to restoring the rounding directions; and when we asked about that on the darwin-dev Apple mailing list (May 28, 2005), Matt Watson suggested it would be good to check what other systems do. Several list participants graciously ran the C program we supplied to tell whether longjmp() restores the rounding modes, and the results are tabulated here:


setjmp/longjmp rounding mode save/restore
system restores mode contributer
darwin 7.9.0 (panther) gcc 3.3 no David Williams
darwin 8.1.0 (tiger) gcc 3.3
gcc 4.0.0
yes
yes
David Williams
FreeBSD yes Matt Watson
FreeBSD 5.3-RELEASE-p1 i386
gcc version 3.4.2
yes Andrew Gallatin
HP-UX B.11.11 U 9000/785
HP ANSI C++ B3910B A.03.37
no Benoit Foucher
HP-UX B.11.11 9000/800
HP ANSI C++ B3910B A.03.52
no Kevin Harris
HP-UX B.11.23 ia64
HP aC++/ANSI C B3910B A.05.55 [Dec 04 2003]
yes Eric Gouriou
IBM AIX 5.2 / VisualAge 6.0 no Benoit Foucher
IBM AIX 5.3 Power5, xlc vers 6 no Andrew Gallatin
Linux (GNU) 2.4.29 i686
gcc version 3.3.1
no David Williams
Linux (SuSE) 2.6.5-7.97-pseries64 ppc64 (SLES 9)
gcc version 3.3.3
no Andrew Gallatin
SGI IRIX 6.5.27 / MIPSpro 7.4.3 yes Steve Allen
Solaris 9 Forte Developer 7 C 5.4 2002/03/09 no Benoit Foucher
SunOS stonehenge 5.8 Generic_108528-18
sun4u sparc Sun WorkShop 6 update 2
C   5.3 Patch 111679-09 2002/11/12
C++ 5.3 Patch 111685-12 2002/12/16


no
no
Kyle Dean
SunOS 5.10 i86pc gcc 3.3.2 no Andrew Gallatin


We personally prefer that setjmp() not save the rounding mode for restoration by longjmp(). The situation should be the same as for changing without restoring the original mode in functions, which Single UNIX 3 allows (see the Application Usage section of the spec for fenv.h), with an admonition to document such changes. Indeed, setjmp() and longjmp() are typically embedded in different functions, where a change of mode is otherwise allowed. To have setjmp/longjmp automatically restore the floating-point rounding mode seems to us a gratuitous, unnecessary restriction on the programmer.

Accessed times since June 19, 2005.