[fpc-devel] LoadLibrary and FPU control word

LacaK lacak at zoznam.sk
Wed Aug 17 09:30:05 CEST 2011

I encounter strange thing. Look at this program please:

  LibraryHandle: TLibHandle;
  cw: word;
  writeln('CW before:',cw, ' IntPower:', intpower(10,-6));
  writeln('CW after:',Get8087CW, ' IntPower:', intpower(10,-6));

CW before:4978 IntPower: 1.0000000000000000E-0006
CW after:4722 IntPower: 2.0000000000000000E-0006      <--- Wrong result 
of IntPower!!!

As you can see, LoadLibrary changes FPU control word (from $1372 to 
$1272, from 64bit precision to 53bit precision).
Which leads to wrong result of PowerInt.
But it seems, that this happens on *Windows98* only, on Windows XP,Vista 
it does not happen.
Do you have same experience on Windows98?

If yes, then I think, that good idea will be use SafeLoadLibrary instead 
of LoadLibrary (which partialy solves this problem).

But this error I have encounter when running fcl-db tests suite with 
IBConnection, where is PowerInt used.
When I change in "ibase60.inc" LoadLibrary to SafeLoadLibrary problem 
still exists, because later call to "isc_attach_database" again changes 
FPU control word. So I must save CW before call to "isc_attach_database" 
and then reset CW to original value.
This solves problem.

Please look at attached patches . Do you think, that this approach is 
acceptable or is there any other solution ?

When I look at function SafeLoadLibrary in dynlibs.pas there is used :
{$ifdef i386}

When I look at function SafeLoadLibrary in SysUtils.inc there is used :
{$if defined(cpui386) or defined(cpux86_64)}

Which conditional is correct ? ("i386" or "cpui386")
Looking at 
http://www.freepascal.org/docs-html/prog/progap7.html#x316-331000G ... I 
see no "i386" ?
And what "FPUX87" ? Can not be used this ?


-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: ibase60_cw.diff
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20110817/db4e40af/attachment.ksh>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: ibconnection_cw.diff
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20110817/db4e40af/attachment-0001.ksh>

More information about the fpc-devel mailing list