;///////////////////////////////////////////////////////////////////////////////
;/// @file memchr.asm
;///
;/// @brief Destiny3D Memory Manager memchr replacement
;///
;/// @author Lightning
;///
;/// This file is the intellectual property of Novus Delta, LLC.. Usage of the
;/// contents of this file is subject to the Destiny3D Member License which
;/// can be found at http://www.destiny3d.com.  Any other usage is prohibited.
;///
;/// This file is distributed "AS IS" without warranty of any kind.  Novus
;/// Delta, LLC. does not guarantee the fitness of the contents of this file
;/// for any particular purpose.
;///
;/// Copyright (C) 2001-2003 Novus Delta, LLC. All Rights Reserved.
;///
;/// <hr>
;///                                 Change History
;/// <hr>
;///
;/// @date May 2001
;/// @author Lightning
;/// @remarks Creation
;///
;///////////////////////////////////////////////////////////////////////////////


.586p					;pentium commands allowed
.mmx					;MMX allowed
.k3d					;3DNow allowed
.model flat, C

.CODE

;i take care of pushing and popping edi, esi, ebp, and ebx if I need to use them
;as it makes it easier to cut down on the code that the function goes thru

Internal_ASM_memchr proc
	;the Paramaters are void *Ptr1, char c, long Len

	push ebx
	mov ebx, esp
	align 4

	;divide by 16, edx = remainder
	mov eax, [ebx + 10h]	;Len
	mov edx, [ebx + 08h]	;Ptr1
	mov ebx, [ebx + 0Ch]	;c

	push eax

	;bl = character to compare against
	mov ecx, ebx
	shl ebx, 8
	add ebx, ecx
	mov ecx, ebx
	shl ebx, 16
	add ebx, ecx

	and eax, 0FFFFFFFCh
	jz Check3Bytes

	MemchrLoop:
		mov ecx, [edx]
		xor ecx, ebx
		
		test ecx, 0000000FFh
		jz short Found_1

		test ecx, 00000FF00h
		jz short Found_2

		test ecx, 000FF0000h
		jz short Found_3

		test ecx, 0FF000000h
		jz short Found_4

		add edx, 4
		sub eax, 4
		jnz short MemchrLoop

	Check3Bytes:
		pop eax
		push 0h			;push so that the Found's will work
		mov ecx, [edx]
		xor ecx, ebx
		and eax, 3
		jmp dword ptr CheckArray[eax * 4]

	Found_1:
		pop ecx
		pop ebx
		mov eax, edx
		ret

	Found_2:
		pop ecx
		pop ebx
		mov eax, edx
		inc eax
		ret

	Found_3:
		pop ecx
		pop ebx
		mov eax, edx
		add eax, 2
		ret

	Found_4:
		pop ecx
		pop ebx
		mov eax, edx
		add eax, 3
		ret

	Check1:
		test ecx, 0000000FFh
		jz short Found_1

		pop eax
		pop ebx
		ret

	Check2:
		test ecx, 0000000FFh
		jz short Found_1

		test ecx, 00000FF00h
		jz short Found_2

		pop eax
		pop ebx
		ret

	Check3:
		test ecx, 0000000FFh
		jz short Found_1

		test ecx, 00000FF00h
		jz short Found_2

		test ecx, 000FF0000h
		jz short Found_3

		pop eax
		pop ebx
		ret

	CheckDone:
		pop eax
		pop ebx
		ret

	CheckArray dd CheckDone, Check1, Check2, Check3

Internal_ASM_memchr endp

END