Javascript port of Stockfish

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

whittenizer
Posts: 85
Joined: Sun May 29, 2011 11:56 pm
Location: San Diego

Javascript port of Stockfish

Post by whittenizer »

Hi all,

I'm undertaking a pretty ambitious task in porting Stockfish over to JavaScript. Some may ask, why? Well, it's just an excercise to see how well I can get it to work. I've recently seen how Garbo chess works, and it does play pretty good, I think porting the C++ code to JavaScript can be done with some time consuming work. Also, I will probably find that no direct conversions are available so I'll find a way to get it done. For starters, the rkiss.h does this rand64() function as something like this:

return (x << k) | (x >> (64 - k));

Now, from what I know JavaScript operates only on 32bit when doing bitwise shift operations. If I change it to:

return (x << k) | (x >> (32 - k));

will this work? I mean, nothing blows up but I havent't really gone to far with this yet. I'm giong through the code and trying to fnd areas where I know will be a concern and gettings the mechanics down first.

Thanks,

David
gladius
Posts: 568
Joined: Tue Dec 12, 2006 10:10 am
Full name: Gary Linscott

Re: Javascript port of Stockfish

Post by gladius »

I'm the author of Garbochess. I actually did consider doing a relatively straight "port" of an existing engine to Javascript. The big problem is that there is no 64 bit integer type in Javascript. So bitboards end up being implemented as two 32 bit integers. It's certainly doable, but it would be a lot of work.

Anyhow, I'd love to see more strong javascript engines around. So good luck :).

Edit: Btw, changing the rotation function like that is not a good idea. RNG's are tested heavily, and small changes to the algorithm can greatly weaken them.
whittenizer
Posts: 85
Joined: Sun May 29, 2011 11:56 pm
Location: San Diego

Re: Javascript port of Stockfish

Post by whittenizer »

Hi Gary,

Thanks for the reply. Yeah, it's going to be alot of work. I'm going through the C++ code and identifying areas where I know things will be rough and trying to iron those out first before I proceed with anything else. The 64 bit thing will be the hardest part. I didn't take too deep of a look at your code but how did you handle the 64 bit restriction? Did you implement to two 32 bit approach?

Anyways, I'll keep you posted on my progress.

Thanks again,

David
gladius
Posts: 568
Joined: Tue Dec 12, 2006 10:10 am
Full name: Gary Linscott

Re: Javascript port of Stockfish

Post by gladius »

I avoided bit boards entirely actually. It's a real pain to do all the operations manually. You could have wrapper functions, but it will be slow.
mar
Posts: 2554
Joined: Fri Nov 26, 2010 2:00 pm
Location: Czech Republic
Full name: Martin Sedlak

re: PRNG

Post by mar »

whittenizer wrote:Hi all,

I'm undertaking a pretty ambitious task in porting Stockfish over to JavaScript. Some may ask, why? Well, it's just an excercise to see how well I can get it to work. I've recently seen how Garbo chess works, and it does play pretty good, I think porting the C++ code to JavaScript can be done with some time consuming work. Also, I will probably find that no direct conversions are available so I'll find a way to get it done. For starters, the rkiss.h does this rand64() function as something like this:

return (x << k) | (x >> (64 - k));

Now, from what I know JavaScript operates only on 32bit when doing bitwise shift operations. If I change it to:

return (x << k) | (x >> (32 - k));

will this work? I mean, nothing blows up but I havent't really gone to far with this yet. I'm giong through the code and trying to fnd areas where I know will be a concern and gettings the mechanics down first.

Thanks,

David
Hi David,
I think that PRNG used in StockFish (Bob Jenkins' public domain code) will work well with 32-bit integers (will test tomorrow). The fun part is that the claim that it passes all dieharder tests is false, it's weak in one/more of rgb_lagged_sums. But "p's happen" and I don't think this will have an impact on anything.
I tried to write a small fast PRNG myself (for example Microsoft CRT's default PRNG fails badly even old diehard tests!, too bad i use static tables generated using this flawed rng in my engine), but without success to pass all the tests without any weak p's.

Good luck

Martin
whittenizer
Posts: 85
Joined: Sun May 29, 2011 11:56 pm
Location: San Diego

Re: re: PRNG

Post by whittenizer »

Hi Martin,

Thanks for the reply. Yeah, I'm getting mixed results when changing the 64-k to 32-k. The C++ is pretty intense and needs to be studied a bit more first I think.

Anyways, I'm stubbing out all of the classes to js files, doing all the prototype fun stuff, then I'll come back to this 64/32 bit thing at some point.

Let me know what you find out with your tests.

THanks much
whittenizer
Posts: 85
Joined: Sun May 29, 2011 11:56 pm
Location: San Diego

Re: re: PRNG

Post by whittenizer »

Hi Martin,

I found a nice link for PRNG's. Alea seems pretty nice with the Mash.js. Have you used this? I may try this and see how it behaves.

http://baagoe.com/en/RandomMusings/java ... x.xml#Alea

Anyways, I'm off to the races.

David
mar
Posts: 2554
Joined: Fri Nov 26, 2010 2:00 pm
Location: Czech Republic
Full name: Martin Sedlak

Re: re: PRNG

Post by mar »

whittenizer wrote:Hi Martin,

I found a nice link for PRNG's. Alea seems pretty nice with the Mash.js. Have you used this? I may try this and see how it behaves.

http://baagoe.com/en/RandomMusings/java ... x.xml#Alea

Anyways, I'm off to the races.

David
Hi David,
yes I have seen that site. I can test Alea as well though I'm not familiar with JS. I think x | 0 is a trick for fast conversion to integer in javascript?
Basically anything (Alea looks like MWC with state cycling) is better than LCGs.

Martin
mar
Posts: 2554
Joined: Fri Nov 26, 2010 2:00 pm
Location: Czech Republic
Full name: Martin Sedlak

Re: re: PRNG

Post by mar »

whittenizer wrote:Hi Martin,

Thanks for the reply. Yeah, I'm getting mixed results when changing the 64-k to 32-k. The C++ is pretty intense and needs to be studied a bit more first I think.

Anyways, I'm stubbing out all of the classes to js files, doing all the prototype fun stuff, then I'll come back to this 64/32 bit thing at some point.

Let me know what you find out with your tests.

THanks much
Hi David,

Tests finished and I have to say it performs the same as the 64-bit version in the tests (passes all but two where it's weak, just like its 64-bit counterpart). Replacing 64-k with 32-k won't do however, you have to change the last rotation from 37 to 5 (unless you already have done so).

Martin
whittenizer
Posts: 85
Joined: Sun May 29, 2011 11:56 pm
Location: San Diego

Re: re: PRNG

Post by whittenizer »

Hey,

Yes, x | 0 works in javascript. Anyways, Alea seems to be working for me at the moment as the "init_zobrist" port seems to work just fine. So much more to go.

I'll keep you posted. Thanks for the replys.