#// Copyright 2010 The Code Cavern

.global bda_hamdist
bda_hamdist:
push %r12
push %r13
push %rbp
push %rbx
mov $0x5555555555555555,%r8
mov $0x3333333333333333,%r9
mov $0x0f0f0f0f0f0f0f0f,%r10
mov $0x0101010101010101,%r11
mov $0,%rax
sub $2,%rdx
jc skip
	mov 8(%rdi,%rdx,8),%rcx
	xor 8(%rsi,%rdx,8),%rcx
	mov (%rdi,%rdx,8),%r12
	xor (%rsi,%rdx,8),%r12
sub $2,%rdx
jc skiplp
.align 16
lp:
	mov %rcx,%rbp
	shr $1,%rcx
	and %r8,%rcx
	sub %rcx,%rbp
	mov %rbp,%rcx
	shr $2,%rbp
	and %r9,%rcx
	and %r9,%rbp
	add %rcx,%rbp
	
	mov %r12,%rbx
	shr $1,%r12
	and %r8,%r12
	sub %r12,%rbx
		mov 8(%rdi,%rdx,8),%rcx
	mov %rbx,%r12
	shr $2,%rbx
	and %r9,%r12
		xor 8(%rsi,%rdx,8),%rcx
	and %r9,%rbx
	add %r12,%rbx
	
	add %rbp,%rbx
	mov %rbx,%r13
		mov (%rdi,%rdx,8),%r12
		xor (%rsi,%rdx,8),%r12
	shr $4,%rbx
	and %r10,%r13
	and %r10,%rbx
	add %rbx,%r13
	imul %r11,%r13
	shr $56,%r13
	add %r13,%rax
	sub $2,%rdx
	jnc lp
skiplp:
	mov %rcx,%rbp
	shr $1,%rcx
	and %r8,%rcx
	sub %rcx,%rbp
	mov %rbp,%rcx
	shr $2,%rbp
	and %r9,%rcx
	and %r9,%rbp
	add %rcx,%rbp
	
	mov %r12,%rbx
	shr $1,%r12
	and %r8,%r12
	sub %r12,%rbx
	mov %rbx,%r12
	shr $2,%rbx
	and %r9,%r12
	and %r9,%rbx
	add %r12,%rbx
	
	add %rbp,%rbx
	mov %rbx,%r13
	shr $4,%rbx
	and %r10,%r13
	and %r10,%rbx
	add %rbx,%r13
	imul %r11,%r13
	shr $56,%r13
	add %r13,%rax
skip:	cmp $-2,%rdx
	jz  case0
case1:	mov 8(%rdi,%rdx,8),%rcx
	xor 8(%rsi,%rdx,8),%rcx
	mov %rcx,%rbp
	shr $1,%rcx
	and %r8,%rcx
	sub %rcx,%rbp
	mov %rbp,%rcx
	shr $2,%rbp
	and %r9,%rcx
	and %r9,%rbp
	add %rcx,%rbp
	mov %rbp,%r13
	shr $4,%rbp
	add %rbp,%r13
	and %r10,%r13
	imul %r11,%r13
	shr $56,%r13
	add %r13,%rax
case0:	pop %rbx
	pop %rbp
	pop %r13
	pop %r12
	ret
