Cross compiling woes

Discussion of chess software programming and technical issues.

Moderators: hgm, Dann Corbit, Harvey Williamson

User avatar
Evert
Posts: 2929
Joined: Sat Jan 22, 2011 12:42 am
Location: NL

Cross compiling woes

Post by Evert »

I have an annoying problem with cross-compiled binaries.

Both in Sjaak and in Leonidas I use exponential functions to initialise a number of evaluation tables. This works fine in 32 and 64 bit under Linux and under OS X, and (apparently) under Windows if compiled there natively.
However, I don't have Windows myself, so I generate Windows binaries by cross-compiling. This is fine in 32 bit, but the program crashes in 64 bit (under Windows, but not under Wine) when it calls the exponential function.

Does anyone have experience with something like this? Is there a linker option I'm missing, or a compiler flag? I have -lm to link to libm, but it actually seems not to be required anyway (confirmed by documentation as well as experimentation).
User avatar
lucasart
Posts: 3232
Joined: Mon May 31, 2010 1:29 pm
Full name: lucasart

Re: Cross compiling woes

Post by lucasart »

Evert wrote:I have an annoying problem with cross-compiled binaries.

Both in Sjaak and in Leonidas I use exponential functions to initialise a number of evaluation tables. This works fine in 32 and 64 bit under Linux and under OS X, and (apparently) under Windows if compiled there natively.
However, I don't have Windows myself, so I generate Windows binaries by cross-compiling. This is fine in 32 bit, but the program crashes in 64 bit (under Windows, but not under Wine) when it calls the exponential function.

Does anyone have experience with something like this? Is there a linker option I'm missing, or a compiler flag? I have -lm to link to libm, but it actually seems not to be required anyway (confirmed by documentation as well as experimentation).
From what you describe, it sounds like the bug is in mingw-w64. Is it possible to replicate the problem with a trivial program that just calls the exp() function?
Theory and practice sometimes clash. And when that happens, theory loses. Every single time.
User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

Re: Cross compiling woes

Post by sje »

Try coding an explicit routine for calculating e^x using series expansion.
User avatar
Evert
Posts: 2929
Joined: Sat Jan 22, 2011 12:42 am
Location: NL

Re: Cross compiling woes

Post by Evert »

lucasart wrote:
Evert wrote:I have an annoying problem with cross-compiled binaries.

Both in Sjaak and in Leonidas I use exponential functions to initialise a number of evaluation tables. This works fine in 32 and 64 bit under Linux and under OS X, and (apparently) under Windows if compiled there natively.
However, I don't have Windows myself, so I generate Windows binaries by cross-compiling. This is fine in 32 bit, but the program crashes in 64 bit (under Windows, but not under Wine) when it calls the exponential function.

Does anyone have experience with something like this? Is there a linker option I'm missing, or a compiler flag? I have -lm to link to libm, but it actually seems not to be required anyway (confirmed by documentation as well as experimentation).
From what you describe, it sounds like the bug is in mingw-w64. Is it possible to replicate the problem with a trivial program that just calls the exp() function?
Worth a shot...

I've uploaded an archive with both the crashing Sjaak (it crashes after receiving a "new" command) and the following test program

Code: Select all

#include <stdio.h>
#include <math.h>

int main&#40;void&#41;
&#123;
   double x, y;

   y = 0;
   for &#40;x = -1.; x< 2.; x+=0.1&#41;
      y += exp&#40;x&#41;;

   printf&#40;"%g\n", y&#41;;
   return 0;
&#125;
at http://www.eglebbk.dds.nl/program/downl ... 6M-win.zip

Perhaps someone with Windows could give this a try?

Note that google seems to be completely silent on anything like this issue...
Last edited by Evert on Sun Feb 17, 2013 7:35 am, edited 1 time in total.
User avatar
Evert
Posts: 2929
Joined: Sat Jan 22, 2011 12:42 am
Location: NL

Re: Cross compiling woes

Post by Evert »

I've considered that, but it seems so silly to write a replacement for a standard library function because a cross compiler for one particular platform seems to generate buggy code when you call that function...
User avatar
lucasart
Posts: 3232
Joined: Mon May 31, 2010 1:29 pm
Full name: lucasart

Re: Cross compiling woes

Post by lucasart »

Evert wrote:I've considered that, but it seems so silly to write a replacement for a standard library function because a cross compiler for one particular platform seems to generate buggy code when you call that function...
But why do you need exp() in your programs ? Is there no way to do without it, and avoid the problem ?
Theory and practice sometimes clash. And when that happens, theory loses. Every single time.
User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

Re: Cross compiling woes

Post by sje »

Evert wrote:I've considered that, but it seems so silly to write a replacement for a standard library function because a cross compiler for one particular platform seems to generate buggy code when you call that function...
Just use the standard power series for e^x. It is probably the simplest of all math library functions.

e^x = 1 + x + x^2/2! + x^3/3! + ...
Alexander Schmidt
Posts: 1181
Joined: Thu May 10, 2007 2:49 pm

Re: Cross compiling woes

Post by Alexander Schmidt »

Evert wrote:Perhaps someone with Windows could give this a try?

Note that google seems to be completely silent on anything like this issue...
Both crash here.
Alex
User avatar
smrf
Posts: 484
Joined: Mon Mar 13, 2006 11:08 am
Location: Klein-Gerau, Germany

Re: Cross compiling woes

Post by smrf »

To calculate y := exp(x) you also could use a continuous fraction:

first calculate z := x/(-2 + x²/(-6 + x²/(-10 + x²/(-14 + ... ... )))

then you will get y := (1 - z) / (1 + z) using higher precision.
User avatar
Evert
Posts: 2929
Joined: Sat Jan 22, 2011 12:42 am
Location: NL

Re: Cross compiling woes

Post by Evert »

lucasart wrote: But why do you need exp() in your programs ? Is there no way to do without it, and avoid the problem ?
It's used for the initialisation of tables used in progressive evaluation.
For instance, the king-safety evaluation uses a Fermi-Dirac distribution for the shape, so the value is small if risk is low and rises (steeply) above a certain threshold and then saturates. The slope, inflection point and saturation value are all adjustable parameters that can be tuned.

Sure, I could do something different, and it may not even be worse. I like this approach though because it's straightforward and natural for me. And, it works properly, except apparently under cross-compiled Windows executables. I'd prefer to fix the problem rather than work around it...

For Jazz and Leonidas I can do the tuning under Linux and simply hard-code the results for the Windows version, but for Sjaak I can't because the pieces that are used in the game are defined at run time.