#Most comments from ULP2.asm - ULP6.asm apply here as well.


	.global	@__UseLibP7
	.text
	.sdreg	r2
	.global	_UseLibP7
	.align	4
_UseLibP7:
	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.

#	f14 is 3.0

#	Use f27 as intermediate value in calculation of Zr4.

#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 Color as UWORD into r3 for PlotIt
	bl	_PlotIt
l9:	b	l1
l6:
	fmul	f20,f4,f14	#Zi2*3 -> f20
 	fmul	f27,f24,f14	#3*Zr2 -> f27
	fsub	f20,f24,f20	#Zr2 - 3*Zi2 -> f20
	fsub	f27,f27,f4	#(3Zr2 - Zi2) -> f27
	fmul	f20,f20,f15	#Zr*(Zr2 - 3Zi2) -> f20 = Zr3

	fmul	f27,f27,f17	#Zi*(3Zr2 - Zi2) -> f27 = Zi3

	fmul	f29,f20,f20	#Zr3*Zr3
	fmul	f28,f27,f27	#Zi3*Zi3
#	fmul	f29,f20,f20	#Zr3*Zr3
	fmul	f30,f20,f27	#Zr3*Zi3
	fsub	f29,f29,f28	#Zr3*Zr3 - Zi3*Zi3 = Zr6

	fmul	f30,f30,f22	#2*Zr3*Zi3 = Zi6
	fmul	f19,f30,f17	#Zi*Zi6
	fmul	f31,f29,f15	#Zr*Zr6
	fsub	f24,f31,f19	#(Zr*Zr6 - Zi*Zi6) -> Zr2
	fmul	f29,f29,f17	#Zi*Zr6
	fmul	f30,f30,f15	#Zr*Zi6
	fadd	f17,f29,f30	#(Zr6*Zi + Zi6*Zr) -> Zi


	fadd	f15,f18,f24     #New Zr = xRel + Zr2
	addi	r23,r23,1       #Increment CurCnt
	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	_UseLibP7,@function
	.size	_UseLibP7,$-_UseLibP7

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

