0

I have a legacy FORTRAN project with some very intense computations. I want this math code to be accessed by C/C++ code, so I built a FORTRAN dll, imported it in C/C++ and started to receive floating-point underflows from my FORTRAN dll.

At the same time, the FORTRAN dll code executes fine if I call it from a FORTRAN application.

Finally, I found out that the compiler I use (it's an FTN 95 integrated into VS2013) has an option (/UNDERFLOW). If this flag is not specified, all underflows are converted to zeroes by default. That happens in the FORTRAN app. When I use C code to execute methods from this dll, I receive underflows.

So, the question is: is there any way to force VC++ compiler to convert underflows to zeroes?

P.S.: yes, I understand that it is stupid to rely on a code that throws floating-point exceptions all the way. However, this code is old and at this point it is impossible to completely rewrite it using up-to-date techniques.

francescalus
  • 30,576
  • 16
  • 61
  • 96
Ilia
  • 331
  • 5
  • 12
  • What does it mean you receive a floating point underflow? You get an exception, or a denormal value or what? Also, too small numbers don't have to mean anything bad, it is just a number with a small magnitude, but it can be mathematically the correct result. Which compiler options do you use for the C++ project? Is it the Debug mode or the Release mode? – Vladimir F Героям слава Apr 13 '15 at 09:39
  • When I ran my C code with imported FORTRAN dll, at some point application crashes and built-in VS debugger informs me that an unhandled exception occured in my dll; the description says it's floating-point underflow. Also an address of the erroneous function is provided. The same happens with FORTRAN app with flag (/UNDERFLOW). What I want is to tell compiler I want all underflows to be treated as zeroes with no breakpoints/exceptions. FORTRAN compiler has such option, whilst VC++ seems to have none. – Ilia Apr 13 '15 at 11:12
  • OK, did you look into the manual? I never used VC++, but my googles shows this as the first hit https://msdn.microsoft.com/en-us/library/e7s85ffb.aspx TLDR: try `/fp:except-` – Vladimir F Героям слава Apr 13 '15 at 11:14
  • Yeah, I've read the documentation and tried several ways - still nothing. I know this is caused by my wild FORT/C interoperability, but now I am curious. I am not sure how fp controls affect doubles, because my code uses doubles and that the exceptions are essentially a double precision underflows. – Ilia Apr 13 '15 at 11:38
  • Yes, but I've found the solution accidentally. The problem was not in C code, but in FORTRAN compiler option difference between building a .dll and an .exe. I will post answer below. Anyway, thank you for useful links and insights into floating-point kitchen. – Ilia Apr 13 '15 at 11:46

1 Answers1

0

So, problem was with FTN95 compiler. The mentioned above flag (/UNDERFLOW) seems to be useful only when one build an application. The effect of this flag is neglected when the target output is DLL. Instead of this I found a compiler directive that is accessed through call to MASK_UNDERFLOW@() subroutine. After inserting an explicit call to this subroutine in the FORTRAN function that was throwing underflows and recompiling the DLL, I've managed to successfully launch a C program and perform necessary computations using functions from FORTRAN dlls. Also, an fp:/except- VC++ compiler flag was used to ensure that no other underflows will affect execution of C program.

Ilia
  • 331
  • 5
  • 12