Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Floating point exception is raised in caml_startup if FE_INEXACT or FE_INVALID are set #4932

Closed
vicuna opened this issue Dec 1, 2009 · 3 comments

Comments

@vicuna
Copy link

vicuna commented Dec 1, 2009

Original bug ID: 4932
Reporter: erwan
Status: resolved (set by @xavierleroy on 2012-08-02T07:20:23Z)
Resolution: suspended
Priority: normal
Severity: minor
Category: ~DO NOT USE (was: OCaml general)
Monitored by: erwan @Chris00

Bug description

I've copied the example from the doc that illustrate how to call ocaml code
from a main in C.

http://caml.inria.fr/pub/docs/manual-ocaml/manual032.html#toc135

and then I've add a call to

"feenableexcept(FE_INVALID);"

before the call to caml_startup(), and I get a
« Floating point exception ». The same occurs with FE_INEXACT.

The fix, from the user point of view, is quite easy : save/restore the fe flag before/after the call to caml_startup().

fe=fegetexcept();
fedisableexcept(FE_ALL_EXCEPT);
caml_startup(argv);
feenableexcept(fe);

But I would say that it would be better to do it once for all inside
caml_startup (or to fix the line that triggers the excp, but most likely it is no something dangerous).

Additional information

I've tried with 3.09.0 and 3.10.2 ; to reproduce it, just:

tar xvfz fe_ocaml.tgz
cd fe_ocaml
make

File attachments

@vicuna
Copy link
Author

vicuna commented Nov 17, 2010

Comment author: erwan

At the end of asmrun/floats.c, it is written

void caml_init_ieee_floats(void)
{
#if defined(FreeBSD) && (__FreeBSD_version < 400017)
fpsetmask(0);
#endif
}

And here, http://www.gsp.com/cgi-bin/man.cgi?section=3&topic=fpsetmask
it is written that fpsetmask is depreacated.

Moreorer, the problem described in this issue occured under linux.
Therefore, I would suggest to patch this file with something like

#if (defined(_GNU_SOURCE)
fedisableexcept(FE_ALL_EXCEPT);
#else
#if defined(FreeBSD) && (__FreeBSD_version < 400017)
fpsetmask(0);
#endif
#endif

But my problem is to be able to do the same under MINGW32, for which _GNU_SOURCE
(end hence fedisableexcept) is not defined...

@vicuna
Copy link
Author

vicuna commented Nov 17, 2010

Comment author: erwan

In asmrun/floats.c, it is written

/* The [caml_init_ieee_float] function should initialize floating-point
hardware
so that it behaves as much as possible like the IEEE standard.
In particular, return special numbers like Infinity and NaN instead
of signalling exceptions. Currently, everyone is in IEEE mode
at program startup, except FreeBSD prior to 4.0R. */

For some reasons, I need to embed a camlruntime into a dll/so that is therefore not loaded at program startup. Hence fp signals I get when launching camlstartup (under linux and mingw).

So to sum-up, the only reasonnable solution is to fix the manual and to hint users to wrap camlstartup with fedisableexcept/
feenableexcept as I've suggested in the description section above (and to forget my note above, sorry for the noise).

@vicuna
Copy link
Author

vicuna commented Aug 2, 2012

Comment author: @xavierleroy

ISO C99 standardizes <fenv.h> functions such as feholdexcept() to select "non-stop" float handling (no signalling exceptions). However, Microsoft doesn't implement C99, so there is still no truly portable solution.

ISO C99 also specifies that all programs start in "non-stop" mode, so the issue affects only OCaml code embedded in DLLs.

My feeling is that there is no better solution than what the reporter proposes: on the C/C++ application side, wrap calls to caml_startup() with operations appropriate to select non-stop mode and restore the original mode. In C99. that would be feholdexcept() and fesetenv(). For Win32, I have no idea.

I'm "suspending" this PR until Win32 supports C99...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant