
#Comments from ULP2.asm in most cases apply here as well.

	.global	@__UseLibP3
	.text
	.sdreg	r2
	.global	_UseLibP3
	.align	4
_UseLibP3:
	mflr	r11
	stw	r11,8(r1)	#Save return address
	stwu	r1,-96(r1)      #Save stack pointer
	fmr	f16,f1		#Transfer yrel from f1 to f16
	lwz	r24,_MaxCnt(r2)		#MaxCnt in r24.
	lwz	r25,_LPixelBuf(r2)	#Get Location of LPixelBuf
	lwz	r27,_ColorTable32(r2)	#Start of color table
	lwz	r28,_BPP(r2)             #Number bytes per pixel
	lfd	f25,_Factor(r2)	#Get Factor into f25
	lfd	f22,_Two(r2)	#Get Two into f22 as constant
	lfd	f23,_Limit(r2)	#Get Limit into f23 as constant
	fadd	f14,f22,f22	#f14 is 4.
	fadd	f14,f14,f22	#f14 is 6.
	fdiv	f14,f14,f22	#f14 is 3.

#The above could have been done with a global definition of 3.000.

	li	r30,0		#xtemp = 0.
	fsub	f15,f22,f22	#(float)xtemp = 0.
	lfd	f10,_XCenter(r2)#f10 = XCenter
	fsub	f15,f15,f10	#xRel = xRel - XCenter
	fdiv	f18,f15,f25	#xRel = xRel/Factor

	lfd	f21,_DX(r2)	#Get the real value of Delta x.
	lwz	r29,_RightEdge(r2)
Start:
	fmr	f15,f18		#Initial value of Zr is xRel

#  The following line was moved before rather than after fmr f17,f16
#  to give the fpu a break.
	li	r23,1		#Initialize CurCnt = 1
	fmr	f17,f16		#yRel into f17 as initial Zi.
	b	l3		#Check to see if to MaxCnt
l2:
	fmul	f24,f15,f15	#Zr2 in f24
	fmul	f4,f17,f17	#Zi2 = Zi*Zi
	fadd	f19,f24,f4      #Zr2 + Zi2 -> f19
	fcmpu	cr0,f19,f23	#Compare with Limit in f23.
	ble	cr0,l6

#The following section is not very time-critical as it is only reached once.
#for each pixel
	cmpwi	cr0,r28,2	#See if BPP < 2
	blt	cr0,l8
l7:
	slwi	r10,r23,2	#4 times CurCnt for offset in ColorTable32
	add	r26,r27,r10	#Find location of pixel color in ColorTable32
	lwz	r11,0(r26)	#Get ARGB from ColorTable
	stw	r11,0(r25)	#Store color in LPixelBuf
	addi	r25,r25,4	#Point to next location in LPixelBuf
	b	l1
l8:	andi.	r3,r23,65535	#Prep for PlotIt as UWORD
	bl	_PlotIt
l9:	b	l1

#	Note that l6 - l3 is the most time sensitive part.
#	It is important that no two consecutive instructions have inputs
#	on the second that depend on the output of the previous.
l6:
	fmul	f20,f4,f14	#This is Zi2*3
	addi	r23,r23,1       #Increment CurCnt

	fmul	f26,f24,f14	#Zr2*3	#Better here than below!

	fsub	f20,f24,f20	#Zr3 = Zr2 - 3*Zi2
	fsub	f26,f26,f4	#3*Zr2 - Zi2

	fmul	f24,f20,f15	#Zr3 = (Zr2 - 3*Zi2)*Zr -> Zr2 = f24.

	fmul	f17,f26,f17	#Zi*(3*Zr2 - Zi2) -> Zi

	fadd	f15,f18,f24     #New Zr = xRel + Zr2

	fadd	f17,f16,f17	#Zi = Zi + yRel

l3:
	cmpw	cr0,r23,r24	#See if CurCnt < MaxCnt
	blt	cr0,l2		#Repeat until MaxCnt reached

	cmpwi	cr0,r28,2	#See if BPP < 2
	blt	cr0,l11
l10:

	lis	r12,4
	addi	r12,r12,-4
	add	r26,r27,r12	#Find location of pixel color
	lwz	r11,0(r26)      #Get color

	stw	r11,0(r25)	#Store color in LPixelBuf
	addi	r25,r25,4	#Point to next pixel in buffer
	b	l1		#Was l12
l11:
	lis	r11,1
	addi	r3,r11,-1	# ffff into r3 for PlotIt
	bl	_PlotIt
l1:
	fadd	f18,f18,f21	#Find new value of xRel (xRel = xRel + DX).
	addi	r30,r30,1	#Increment xtemp.
	cmpw	cr0,r30,r29	#See if to RightEdge

	blt	cr0,Start	#Repeat until row is done.
	stw	r25,_LPixelBuf(r2)  #New location in long pixel buffer
	addi	r1,r1,96        #Balance Stack pointer
	lwz	r11,8(r1)	#Get return address
	mtlr	r11
	fmr	f1,f20

	blr
	.type	_UseLibP3,@function
	.size	_UseLibP3,$-_UseLibP3

	.global	_MaxCnt
	.global	_RightEdge
	.global	_XCenter
	.global	_Factor
	.global	_Limit
	.global	_Two
	.global _Three
	.global	_Color
	.global	_LPixelBuf
	.global	_ColorTable32
	.global	_BPP
	.global	_DX
	.global	_PlotIt
	.global	@__PlotIt

#	.tocd
#	.align	3
#l13:
#	.long	0x43300000,0x80000000
#l14:	.word	0
#l15:	.word	0