Computer Chess Club Archives


Search

Terms

Messages

Subject: MS compiler issue [OT]

Author: Gerd Isenberg

Date: 09:13:32 10/15/05


Hi,

msc6 compiler has some nice optimization features. An unsigned int division by
100 is done by reciprocal 10**37/100 multiplication. An unsigned mul 365 is done
by three lea-instructions, which is unfortunately not the fastest on amd-cpus.

Another funny thing is, that the expression

  year -= (month < 3);

generates following assembly using two instructions and one additional register
more than necessary:

 cmp	 esi, 3
 sbb	 eax, eax
 neg	 eax
 sub	 ecx, eax

instead of

 cmp	 esi, 3
 sbb	 ecx, 0

Here is some source, first with C, second with inline assembly i like to see for
x86-32. I am interested in generated assembly of the C-routine with more recent
compilers (my msc2005 trial has expired and i am thinking about to buy).

Thanks,
Gerd



unsigned int getDayIndex1March00(
	unsigned int day,
	unsigned int month,
	unsigned int year)
{
	static int daysTilMonth[12] =
	{
		6*31 + 4*30, // jan
		7*31 + 4*30, // feb
		0*31 + 0*30, // mar
		1*31 + 0*30, // apr
		1*31 + 1*30, // may
		2*31 + 1*30, // jun
		2*31 + 2*30, // jul
		3*31 + 2*30, // aug
		4*31 + 2*30, // sep
		4*31 + 3*30, // oct
		5*31 + 3*30, // nov
		5*31 + 4*30, // dec
	};
	unsigned int cent, didx;
	year -= (month < 3);
	cent  = year / 100;
	didx  = year * 365 + (year>>2) - cent + (cent>>2)
		  + daysTilMonth[month-1] + day;
	return didx;
}

unsigned int _getDayIndex1March00(
	unsigned int day,
	unsigned int month,
	unsigned int year)
{
	static int daysToMonth[12] = ...
	__asm {
		mov     ecx, [year]
		mov     esi, [month]
		cmp     esi, 3		; (month < 3)
		mov     eax, 1374389535 ; 51eb851fH 2**37/100
		sbb     ecx, 0  	; year -= (month < 3)
		mul     ecx		; result in edx:eax
		imul    eax, ecx, 365	; year*365
		shr     edx, 5		;   centuries
		add	eax, [day]
		sub	eax, edx	; - centuries
		shr     ecx, 2		;  year>>2
		shr     edx, 2		;  centuries>>2
		add     eax, [daysToMonth+esi*4-4]
		add	eax, ecx	; + (year>>2)
		add	eax, edx	; + (centuries>>2)
	}
}




This page took 0.01 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.