// File: SethKasmann[P]clever-girl/src/MagicMoves.cpp
// Function: initmagicmoves()
void initmagicmoves(void)
{
	int i;

	//for bitscans :
	//initmagicmoves_bitpos64_database[(x*C64(0x07EDD5E59A4E28C2))>>58]
	int initmagicmoves_bitpos64_database[64]={
	63,  0, 58,  1, 59, 47, 53,  2,
	60, 39, 48, 27, 54, 33, 42,  3,
	61, 51, 37, 40, 49, 18, 28, 20,
	55, 30, 34, 11, 43, 14, 22,  4,
	62, 57, 46, 52, 38, 26, 32, 41,
	50, 36, 17, 19, 29, 10, 13, 21,
	56, 45, 25, 31, 35, 16,  9, 12,
	44, 24, 15,  8, 23,  7,  6,  5};

#ifdef MINIMIZE_MAGIC
	//identical to magicmove_x_indices except without the const modifer
	U64* magicmoves_b_indices2[64]=
	{
		magicmovesbdb+4992, magicmovesbdb+2624,  magicmovesbdb+256,  magicmovesbdb+896,
		magicmovesbdb+1280, magicmovesbdb+1664, magicmovesbdb+4800, magicmovesbdb+5120,
		magicmovesbdb+2560, magicmovesbdb+2656,  magicmovesbdb+288,  magicmovesbdb+928,
		magicmovesbdb+1312, magicmovesbdb+1696, magicmovesbdb+4832, magicmovesbdb+4928,
		magicmovesbdb+0,     magicmovesbdb+128,  magicmovesbdb+320,  magicmovesbdb+960,
		magicmovesbdb+1344, magicmovesbdb+1728, magicmovesbdb+2304, magicmovesbdb+2432,
		magicmovesbdb+32,    magicmovesbdb+160,  magicmovesbdb+448, magicmovesbdb+2752,
		magicmovesbdb+3776, magicmovesbdb+1856, magicmovesbdb+2336, magicmovesbdb+2464,
		magicmovesbdb+64,    magicmovesbdb+192,  magicmovesbdb+576, magicmovesbdb+3264,
		magicmovesbdb+4288, magicmovesbdb+1984, magicmovesbdb+2368, magicmovesbdb+2496,
		magicmovesbdb+96,    magicmovesbdb+224,  magicmovesbdb+704, magicmovesbdb+1088,
		magicmovesbdb+1472, magicmovesbdb+2112, magicmovesbdb+2400, magicmovesbdb+2528,
		magicmovesbdb+2592, magicmovesbdb+2688,  magicmovesbdb+832, magicmovesbdb+1216,
		magicmovesbdb+1600, magicmovesbdb+2240, magicmovesbdb+4864, magicmovesbdb+4960,
		magicmovesbdb+5056, magicmovesbdb+2720,  magicmovesbdb+864, magicmovesbdb+1248,
		magicmovesbdb+1632, magicmovesbdb+2272, magicmovesbdb+4896, magicmovesbdb+5184
	};
	U64* magicmoves_r_indices2[64]=
	{
		magicmovesrdb+86016, magicmovesrdb+73728, magicmovesrdb+36864, magicmovesrdb+43008,
		magicmovesrdb+47104, magicmovesrdb+51200, magicmovesrdb+77824, magicmovesrdb+94208,
		magicmovesrdb+69632, magicmovesrdb+32768, magicmovesrdb+38912, magicmovesrdb+10240,
		magicmovesrdb+14336, magicmovesrdb+53248, magicmovesrdb+57344, magicmovesrdb+81920,
		magicmovesrdb+24576, magicmovesrdb+33792,  magicmovesrdb+6144, magicmovesrdb+11264,
		magicmovesrdb+15360, magicmovesrdb+18432, magicmovesrdb+58368, magicmovesrdb+61440,
		magicmovesrdb+26624,  magicmovesrdb+4096,  magicmovesrdb+7168,     magicmovesrdb+0,
		magicmovesrdb+2048,  magicmovesrdb+19456, magicmovesrdb+22528, magicmovesrdb+63488,
		magicmovesrdb+28672,  magicmovesrdb+5120,  magicmovesrdb+8192,  magicmovesrdb+1024,
		magicmovesrdb+3072,  magicmovesrdb+20480, magicmovesrdb+23552, magicmovesrdb+65536,
		magicmovesrdb+30720, magicmovesrdb+34816,  magicmovesrdb+9216, magicmovesrdb+12288,
		magicmovesrdb+16384, magicmovesrdb+21504, magicmovesrdb+59392, magicmovesrdb+67584,
		magicmovesrdb+71680, magicmovesrdb+35840, magicmovesrdb+39936, magicmovesrdb+13312,
		magicmovesrdb+17408, magicmovesrdb+54272, magicmovesrdb+60416, magicmovesrdb+83968,
		magicmovesrdb+90112, magicmovesrdb+75776, magicmovesrdb+40960, magicmovesrdb+45056,
		magicmovesrdb+49152, magicmovesrdb+55296, magicmovesrdb+79872, magicmovesrdb+98304
	};
#endif // MINIMIZE_MAGIC


#ifdef PERFECT_MAGIC_HASH
	for(i=0;i<1428;i++)
		magicmovesbdb[i]=0;
	for(i=0;i<4900;i++)
		magicmovesrdb[i]=0;
#endif

	for(i=0;i<64;i++)
	{
		int squares[64];
		int numsquares=0;
		U64 temp=magicmoves_b_mask[i];
		while(temp)
		{
			U64 bit=temp&-temp;
			squares[numsquares++]=initmagicmoves_bitpos64_database[(bit*C64(0x07EDD5E59A4E28C2))>>58];
			temp^=bit;
		}
		for(temp=0;temp<(((U64)(1))<<numsquares);temp++)
		{
			U64 tempocc=initmagicmoves_occ(squares,numsquares,temp);
			#ifndef PERFECT_MAGIC_HASH
				BmagicNOMASK2(i,tempocc)=initmagicmoves_Bmoves(i,tempocc);
			#else
				U64 moves=initmagicmoves_Bmoves(i,tempocc);
				U64 index=(((tempocc)*magicmoves_b_magics[i])>>MINIMAL_B_BITS_SHIFT);
				int j;
				for(j=0;j<1428;j++)
				{
					if(!magicmovesbdb[j])
					{
						magicmovesbdb[j]=moves;
						magicmoves_b_indices[i][index]=j;
						break;
					}
					else if(magicmovesbdb[j]==moves)
					{
						magicmoves_b_indices[i][index]=j;
						break;
					}
				}
			#endif
		}
	}
	for(i=0;i<64;i++)
	{
		int squares[64];
		int numsquares=0;
		U64 temp=magicmoves_r_mask[i];
		while(temp)
		{
			U64 bit=temp&-temp;
			squares[numsquares++]=initmagicmoves_bitpos64_database[(bit*C64(0x07EDD5E59A4E28C2))>>58];
			temp^=bit;
		}
		for(temp=0;temp<(((U64)(1))<<numsquares);temp++)
		{
			U64 tempocc=initmagicmoves_occ(squares,numsquares,temp);
			#ifndef PERFECT_MAGIC_HASH
				RmagicNOMASK2(i,tempocc)=initmagicmoves_Rmoves(i,tempocc);
			#else
				U64 moves=initmagicmoves_Rmoves(i,tempocc);
				U64 index=(((tempocc)*magicmoves_r_magics[i])>>MINIMAL_R_BITS_SHIFT);
				int j;
				for(j=0;j<4900;j++)
				{
					if(!magicmovesrdb[j])
					{
						magicmovesrdb[j]=moves;
						magicmoves_r_indices[i][index]=j;
						break;
					}
					else if(magicmovesrdb[j]==moves)
					{
						magicmoves_r_indices[i][index]=j;
						break;
					}
				}
			#endif
		}
	}
}

// Assembly:
subq $0x748, %rsp            # imm = 0x748
leaq 0x640(%rsp), %rdi
leaq 0x2084a(%rip), %rsi     # 0x40ad0
movl $0x100, %edx            # imm = 0x100
callq 0x51f0
leaq 0x440(%rsp), %rdi
leaq 0x30691(%rip), %rsi     # 0x50930
movl $0x200, %edx            # imm = 0x200
callq 0x51f0
leaq 0x240(%rsp), %rdi
leaq 0x30878(%rip), %rsi     # 0x50b30
movl $0x200, %edx            # imm = 0x200
callq 0x51f0
movl $0x0, 0x744(%rsp)
cmpl $0x40, 0x744(%rsp)
jge 0x20460
movl $0x0, 0x13c(%rsp)
movslq 0x744(%rsp), %rcx
leaq 0x205db(%rip), %rax     # 0x408d0
movq (%rax,%rcx,8), %rax
movq %rax, 0x130(%rsp)
cmpq $0x0, 0x130(%rsp)
je 0x2037d
movq 0x130(%rsp), %rax
xorl %ecx, %ecx
subq 0x130(%rsp), %rcx
andq %rcx, %rax
movq %rax, 0x128(%rsp)
movabsq $0x7edd5e59a4e28c2, %rax # imm = 0x7EDD5E59A4E28C2
imulq 0x128(%rsp), %rax
shrq $0x3a, %rax
movl 0x640(%rsp,%rax,4), %ecx
movl 0x13c(%rsp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, 0x13c(%rsp)
cltq
movl %ecx, 0x140(%rsp,%rax,4)
movq 0x128(%rsp), %rax
xorq 0x130(%rsp), %rax
movq %rax, 0x130(%rsp)
jmp 0x20301
movq $0x0, 0x130(%rsp)
movq 0x130(%rsp), %rax
movl 0x13c(%rsp), %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
cmpq %rcx, %rax
jae 0x20448
leaq 0x140(%rsp), %rdi
movl 0x13c(%rsp), %esi
movq 0x130(%rsp), %rdx
callq 0x1fde0
movq %rax, 0x120(%rsp)
movl 0x744(%rsp), %edi
movq 0x120(%rsp), %rsi
callq 0x20010
movq %rax, %rdx
movslq 0x744(%rsp), %rax
movq 0x440(%rsp,%rax,8), %rax
movq 0x120(%rsp), %rsi
movslq 0x744(%rsp), %rdi
leaq 0x202c2(%rip), %rcx     # 0x406d0
imulq (%rcx,%rdi,8), %rsi
movslq 0x744(%rsp), %rdi
leaq 0x201ae(%rip), %rcx     # 0x405d0
movl (%rcx,%rdi,4), %ecx
shrq %cl, %rsi
movq %rsi, %rcx
movq %rdx, (%rax,%rcx,8)
movq 0x130(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x130(%rsp)
jmp 0x20389
jmp 0x2044a
movl 0x744(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x744(%rsp)
jmp 0x202cd
movl $0x0, 0x744(%rsp)
cmpl $0x40, 0x744(%rsp)
jge 0x205b3
movl $0x0, 0x1c(%rsp)
movslq 0x744(%rsp), %rcx
leaq 0x1ff40(%rip), %rax     # 0x403d0
movq (%rax,%rcx,8), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, 0x10(%rsp)
je 0x204f4
movq 0x10(%rsp), %rax
xorl %ecx, %ecx
subq 0x10(%rsp), %rcx
andq %rcx, %rax
movq %rax, 0x8(%rsp)
movabsq $0x7edd5e59a4e28c2, %rax # imm = 0x7EDD5E59A4E28C2
imulq 0x8(%rsp), %rax
shrq $0x3a, %rax
movl 0x640(%rsp,%rax,4), %ecx
movl 0x1c(%rsp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, 0x1c(%rsp)
cltq
movl %ecx, 0x20(%rsp,%rax,4)
movq 0x8(%rsp), %rax
xorq 0x10(%rsp), %rax
movq %rax, 0x10(%rsp)
jmp 0x20499
movq $0x0, 0x10(%rsp)
movq 0x10(%rsp), %rax
movl 0x1c(%rsp), %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
cmpq %rcx, %rax
jae 0x2059b
leaq 0x20(%rsp), %rdi
movl 0x1c(%rsp), %esi
movq 0x10(%rsp), %rdx
callq 0x1fde0
movq %rax, (%rsp)
movl 0x744(%rsp), %edi
movq (%rsp), %rsi
callq 0x1fe60
movq %rax, %rdx
movslq 0x744(%rsp), %rax
movq 0x240(%rsp,%rax,8), %rax
movq (%rsp), %rsi
movslq 0x744(%rsp), %rdi
leaq 0x1fc69(%rip), %rcx     # 0x401d0
imulq (%rcx,%rdi,8), %rsi
movslq 0x744(%rsp), %rdi
leaq 0x1fb55(%rip), %rcx     # 0x400d0
movl (%rcx,%rdi,4), %ecx
shrq %cl, %rsi
movq %rsi, %rcx
movq %rdx, (%rax,%rcx,8)
movq 0x10(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x10(%rsp)
jmp 0x204fd
jmp 0x2059d
movl 0x744(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x744(%rsp)
jmp 0x2046b
addq $0x748, %rsp            # imm = 0x748
retq
nopl (%rax,%rax)