	TTL	CopyBlockHor

		AREA	|.drectve|, DRECTVE
	DCB	"-defaultlib:coredll.lib "
	DCB	"-defaultlib:corelibc.lib "
	
	EXPORT	|CopyBlockHor|

	AREA	|.pdata|, PDATA
|$T224|	DCD	|CopyBlockHor|
	DCD	0x40000300
	
	AREA	|.text|, CODE
	
|CopyBlockHor| PROC


;;; 
;;; void CopyBlockHor(unsigned char * Src, unsigned char * Dst, int Stride)
;;; {
;;; 	int dy, dx;
;;; 
;;; 	for (dy = 16; dy != 0; dy--) {
;;; 		for (dx = 0; dx < 16; dx++) {
;;; 			Dst[dx] = (Src[dx] + Src[dx+1]+1) >> 1; 
;;; 			// hor interpolation with rounding
;;; 		}
;;; 		Src += Stride;
;;; 		Dst += Stride;
;;; 	}
;;; }
	
;;; r0 is the src pointer
;;; r1 is the test pointer
;;; r2 is the stride (x and y)
;;; 
;;; 
;;; r8 is the source preload pointer
;;; 
;;; r9 is inner loop counter
;;; r10 incrementing src pointer
;;; r11 incrementing dst pointer
;;; r12 is outer loop counter

; 	b	mytarget	
	
	stmdb     sp!, {r4 - r10, r11, lr}  ; stmfd

	
;;; start the data in (first two strides)

	& 0xf5d0f000		; pld[r0] loop 1 src beginning  
	& 0xf5d0f007		; pld[r0,#7] loop 1 src end
	add	r8,r2,r0	; 
	& 0xf5d8f000		; pld[r8] loop 2 src beginning
	& 0xf5d8f007		; pld[r8,#7] loop 2 src end
	add	r8,r2,r8	; r8 has pointer to next src line

	mov	r12,#8		; outer loop counter
	
cbh_outer

	;; preload the n+2 stride
	& 0xf5d8f000		; pld[r8] loop N+2 src beginning
	& 0xf5d8f007		; pld[r8,#7] loop N+2 src end
	add	r8,r2,r8	; r8 has pointer to next src line

	;; get started on loop n
	
	;; load in src[0-4]
	mov	r10,r0		; copy of src address
	mov	r11,r1		; copy of dst address
	
	ldrb	r3,[r10],#1	; get the first byte

	mov	r9,#2		; inner loop counter (2 loops, 4 bytes each)

cbh_inner

	;; r3 is always assumed to have the (n*4)th word already (src0,4,8,12)
	ldrb	r4,[r10],#1
	ldrb	r5,[r10],#1
	ldrb	r6,[r10],#1
	ldrb	r7,[r10],#1

	;; calculate dst[0-3]

	;; dst[0] = (src[0] + src[1] + 1) >> 1
	add	r3,r4,r3	
	add	r3,r3,#1
	mov	r3,r3,ASR #1

	;; dst[1] = (src[n] + src[n+1] + 1) >> 1
	add	r4,r5,r4	
	add	r4,r4,#1
	mov	r4,r4,ASR #1

	;; dst[2] = (src[n] + src[n+1] + 1) >> 1
	add	r5,r6,r5	
	add	r5,r5,#1
	mov	r5,r5,ASR #1

	;; dst[3] = (src[n] + src[n+1] + 1) >> 1
	add	r6,r7,r6	
	add	r6,r6,#1
	mov	r6,r6,ASR #1

	;; store off first 4 destination words
	strb	r3,[r11],#1
	strb	r4,[r11],#1
	strb	r5,[r11],#1
	strb	r6,[r11],#1

	mov	r3,r7		; shift the last word back to r3
	
	subs	r9,r9,#1
	bne	cbh_inner

;;; adjust the stride
	add	r0,r0,r2	; src stride
	add	r1,r1,r2	; dst stride
	
	subs	r12,r12,#1
	bne	cbh_outer

	ldmia     sp!, {r4 - r10, r11, pc}  ; ldmfd
|$M31912|
	
	ENDP  ; |CopyBlockHor|

	END