C vs ASM

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

Peer Gynt

Re: C vs ASM

Post by Peer Gynt »

Most things should be done in C, but if you can improve on the efficiency of your C code in the most heavily loaded parts of your program by writing some assembly code by hand, why shouldn't you?

Not everything is possible to do at all efficiently in C. You may have noticed that C has no rotate operator f.ex., no byte swap function, no bit scan functions etc.

As for working with bitboards (sets of squares) the MMX registers and instructions are indispensable on a 32 bit computer. Does your compiler produce them as appropriate? If so, on what C code? I'd like to see the following code implemented in pure C. It computes the pawn captures as the set of destination squares:

Code: Select all

#include <stdio.h>

char occ&#91;8&#93; = &#123;4,0x40,0x40,0x40,0x40,0x40,0x40,4&#125;;
char pawns&#91;8&#93; = &#123;2,2,2,2,2,2,2,2&#125;;
char cap&#91;8&#93; = &#123;0,0,0,0,0,0,0,0&#125;;

void pawn_caps&#40;)
&#123;
	__asm &#123; movq	mm0, pawns
			movq	mm1, mm0
			psllq	mm0, 9
			psrlq	mm1, 7
			por		mm0, mm1
			pand	mm0, occ
			movq	cap, mm0
		  &#125;
&#125;

int main&#40;int argc, char* argv&#91;&#93;)
&#123;
	pawn_caps&#40;);
	
	int i;
	for &#40;i = 7; i >= 0; i--) printf&#40;"%2X\n", cap&#91;i&#93;);

	return 0;
&#125;
mar
Posts: 2566
Joined: Fri Nov 26, 2010 2:00 pm
Location: Czech Republic
Full name: Martin Sedlak

Re: C vs ASM

Post by mar »

Peer Gynt wrote:As for working with bitboards (sets of squares) the MMX registers and instructions are indispensable on a 32 bit computer. Does your compiler produce them as appropriate? If so, on what C code?
I don't think so. AFAIK MMX shares the registers with FPU, you have to call a very expensive emms instruction when done with MMX stuff, otherwise fpu instructions won't work (you would get wrong results).
I don't think any compiler uses MMX automatically but I may be wrong.
SSE superseded MMX these days.
MikeGL
Posts: 1010
Joined: Thu Sep 01, 2011 2:49 pm

Re: C vs ASM

Post by MikeGL »

Peer Gynt wrote:Most things should be done in C, but if you can improve on the efficiency of your C code in the most heavily loaded parts of your program by writing some assembly code by hand, why shouldn't you?
I agree. At times, you could also drop down to ASM if you are not sure what your compiler is doing.
Also, quite possible to detect compiler bugs by just letting your C compiler
spit out assembly code instead of compiling.

So learning ASM is important for debugging your C code too.
Peer Gynt

Re: C vs ASM

Post by Peer Gynt »

mar wrote: I don't think so. AFAIK MMX shares the registers with FPU, you have to call a very expensive emms instruction when done with MMX stuff, otherwise fpu instructions won't work (you would get wrong results).
I know, but do you need FPU in a chess program? What for? Try using integers instead.
mar wrote: I don't think any compiler uses MMX automatically but I may be wrong.
SSE superseded MMX these days.
That's too bad if you are right. Only goes to show that compilers have their limitations and that you cannot rely on them 100%.

SSE is SIMD for floating point calculations. It extended MMX with a few instructions but I don't need them. MMX is SIMD for integer calculations which is what you presumably need in a chess program.
syzygy
Posts: 5569
Joined: Tue Feb 28, 2012 11:56 pm

Re: C vs ASM

Post by syzygy »

Peer Gynt wrote:
mar wrote:I don't think any compiler uses MMX automatically but I may be wrong.
SSE superseded MMX these days.
That's too bad if you are right. Only goes to show that compilers have their limitations and that you cannot rely on them 100%.

SSE is SIMD for floating point calculations. It extended MMX with a few instructions but I don't need them. MMX is SIMD for integer calculations which is what you presumably need in a chess program.
SSE2 added integer support to SSE. There is little reason to use MMX.
Peer Gynt

Re: C vs ASM

Post by Peer Gynt »

MikeGL wrote: I agree. At times, you could also drop down to ASM if you are not sure what your compiler is doing.
Also, quite possible to detect compiler bugs by just letting your C compiler
spit out assembly code instead of compiling.

So learning ASM is important for debugging your C code too.
Yeah, exactly. The least thing you can do is inspect the assembly code produced by the compiler and see how it implements the C language constructs. Good to know what exactly the C language means under a particular compiler. If you don't know ASM you have no means of evaluating and validating your compiler.

I tried it on the __int64 type of MS VC++ to see how that works inside, and learned that it doesn't use any hardware support, it's just big integer calculations like the ones used in cryptography.
abulmo
Posts: 151
Joined: Thu Nov 12, 2009 6:31 pm

Re: C vs ASM

Post by abulmo »

Rein Halbersma wrote: In C you do that by explicitly using (builtin) types and short variable names. In C++11 the way to do that is to use the auto-keyword to hide type deduction and to use longer meaningful variable names for clarity, e.g. square and board instead of i and b.
As nobody jumps on this "cliché", I'll do it.
In C you can use long and meaningful variable names. In C++ you can use short or obfuscated variable names. Nor C neither C++ programming languages are involved here, this is just a question of programming style.
Richard
Rein Halbersma
Posts: 741
Joined: Tue May 22, 2007 11:13 am

Re: C vs ASM

Post by Rein Halbersma »

abulmo wrote:
Rein Halbersma wrote: In C you do that by explicitly using (builtin) types and short variable names. In C++11 the way to do that is to use the auto-keyword to hide type deduction and to use longer meaningful variable names for clarity, e.g. square and board instead of i and b.
As nobody jumps on this "cliché", I'll do it.
In C you can use long and meaningful variable names. In C++ you can use short or obfuscated variable names. Nor C neither C++ programming languages are involved here, this is just a question of programming style.
You are right, this was a cliche. In my defense: this quote was in reply to a (IMO) rather biased and unfounded critique on C++ in general by someone who doesn't like the language for (again IMO) biased and largely unfounded reasons. Please keep that context in mind.
mar
Posts: 2566
Joined: Fri Nov 26, 2010 2:00 pm
Location: Czech Republic
Full name: Martin Sedlak

Re: C vs ASM

Post by mar »

Peer Gynt wrote:I know, but do you need FPU in a chess program? What for? Try using integers instead.
MMX was wonderful back then (x years ago), I remember using it for software rasterization and software (audio) mixing, it was simply awesome.
Do you really need SIMD for a bitboard chess engine? Now that we have 64-bit cpus? I guess not. The only interesting SSE instruction (for chess) is popcnt which is SSE4.x (not sure). MMX may only be interesting in 32-bit mode but AFAIK not many optimize their engines for that anyway. It may be interesting to do a performance comparison - MMX assembly/intrinsics vs 32-bit (assembly/C) code. I doubt MMX will have any advantage on a 64-bit CPU, but I'm just guessing here.
Also you won't be able to interleave mmx assembly with other (complier generated) instructions to remove dependencies/optimize pipeline so hard to say.
A perfomance comparison would be nice. Until then it's just guesswork.
User avatar
hgm
Posts: 27837
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: C vs ASM

Post by hgm »

Note that modern compilers never use FPU instructions for floating point arithmetic. In stead they use SSE, which, ever since its first introduction, also contains a set of 'scalar' floating point instruction, only acting on the lowest member of the XMM/YMM vector registers.