Thread: Assembler crc32
View Single Post
Old 08-05-2004, 08:54 AM  
darkone
Disabled
 
darkone's Avatar
 
Join Date: Dec 2001
Posts: 2,230
Default Assembler crc32

Here's one in C (it's suprisingly hard to find algorithm that uses only 32bit registers)

Code:
inline
DWORD Crc32(LPVOID lpMemory, LONG lLength, DWORD dwCrc32)
{
	if (! lLength) {
		return dwCrc32;
	}

	__asm {
		push ebx
		mov  edi, lLength
		mov  eax, DWORD PTR dwCrc32		// load initial parameter (dwCrc32)
		mov  edx, DWORD PTR lpMemory	// load initial parameter pointer
		add  edx, edi
		mov  ecx, edi
		shl  ecx, 1
		sub  edi, ecx
 
	// for (i = -len;  i != 0 ;  i ++) 
	$L278:
		// dwCRC32 = crc32_tab[ (dwCRC32 ^ s[i + len]) & 0xff ] ^ (dwCRC32 >> 8)
		mov  ebx, DWORD PTR [edx + edi]		// load parameter value (s[i])
		xor  ebx, eax						// perform exclusive OR (dwCRC32 ^ s[i])
		and  ebx, 0xff						// clear upper 24 bits
		shr  eax, 8							// shift dwCRC32 value (divide by 256)
		xor  eax, DWORD PTR [crc32_table + ebx*4]	// exclusive OR with look up table
		inc  edi
		jnz  SHORT $L278
		Pop  ebx
	}
}
... uhh, yea... as usual use it only in io & gl scripts
darkone is offline