
; File:   bitmapscale.s
; Author: Mikael Kalms <mikael@kalms.org>
; Date:   1 Oct 1999
; Title:  Scaled bitmap rasterizer (FPU based)
;
; Description:
;   Bitmap stretch-blitter, subpixeling & subtexeling;
;   performs 2d-clipping


		section	code,code

; in		d0	bitmap sizex
;		d1	bitmap sizey
;		a0	screen
;		a1	bitmap
;		fp0	screen x0
;		fp1	screen y0
;		fp2	screen x1
;		fp3	screen y1
;		fp4	bitmap u0
;		fp5	bitmap v0
;		fp6	bitmap u1
;		fp7	bitmap v1

drawbitmapscale_clip
		movem.l	d2-d7/a3-a6,-(sp)

		move.l	#CHUNKYXMAX,d2
		move.l	#CHUNKYYMAX,d3

		fcmp.l	d2,fp0
		fbge	.skip
		fcmp.l	d3,fp1
		fbge	.skip
		ftst	fp2
		fblt	.skip
		ftst	fp3
		fblt	.skip

		fadd.s	#0.5,fp0
		fadd.s	#0.5,fp1
		fadd.s	#0.5,fp2
		fadd.s	#0.5,fp3

		fmove.l	fpcr,-(sp)
		fmove.l	#FPCR_RM,fpcr		; defined in fpu.i

		fsub	fp4,fp6
		fsub	fp5,fp7
		fsub	fp0,fp2
		fsub	fp1,fp3
		fmove.s	fp2,d2
		fmove.s	fp3,d3
		and.l	#$7fff0000,d2
		beq	.skip2
		and.l	#$7fff0000,d3
		beq	.skip2
		fdiv	fp2,fp6
		fdiv	fp3,fp7
		fadd	fp0,fp2
		fadd	fp1,fp3
		fmove.l	fp0,d2
		fmove.l	fp1,d3
		fmove.l	fp2,d4
		fmove.l	fp3,d5
		tst.l	d2
		bge.s	.nclipl
		moveq	#0,d2
.nclipl
		tst.l	d3
		bge.s	.nclipu
		moveq	#0,d3
.nclipu
		cmp.l	#CHUNKYXMAX,d4
		blt.s	.nclipr
		move.l	#CHUNKYXMAX,d4
.nclipr
		cmp.l	#CHUNKYYMAX,d5
		blt.s	.nclipd
		move.l	#CHUNKYYMAX,d5
.nclipd
		sub.l	d2,d4
		beq	.skip2
		sub.l	d3,d5
		beq	.skip2
		fneg	fp0
		fneg	fp1
		fadd.l	d2,fp0
		fadd.l	d3,fp1
		fmul	fp6,fp0
		fmul	fp7,fp1
		fadd	fp6,fp0
		fadd	fp7,fp1
		fadd	fp0,fp4
		fadd	fp1,fp5

		mulu.w	#CHUNKYXMAX,d3
		fmul.s	#65536.0,fp4
		add.l	d3,a1
		add.w	d2,a1
		fmul.s	#65536.0,fp6
		move.w	#CHUNKYXMAX,a3
		sub.w	d4,a3

		fmove.l	fp4,d1
		fmove.l	fp6,d7
		swap	d1
		swap	d7
		move.l	d1,a5

		lea	(a1,d4.w),a6
.y		fmove.l	fp5,d6
		muls.w	d0,d6
		move.l	a5,d1
		lea	(a0,d6.l),a4
		move.l	d7,d6
		clr.w	d6
		add.l	d6,d1
.x
		move.b	(a4,d1.w),d6
		addx.l	d7,d1
		move.b	d6,(a1)+
		cmp.l	a1,a6
		bne.s	.x
		fadd	fp7,fp5
		add.w	a3,a1
		add.w	#CHUNKYXMAX,a6
		subq.w	#1,d5
		bgt.s	.y

.skip2		fmove.l	(sp)+,fpcr
.skip		movem.l	(sp)+,d2-d7/a3-a6
		rts

