Notice: Trying to access array offset on value of type bool in /mnt/cpcwiki-httpdocs/includes/libs/IPSet.php on line 243

Notice: Trying to access array offset on value of type bool in /mnt/cpcwiki-httpdocs/includes/libs/IPSet.php on line 246

Notice: Trying to access array offset on value of type bool in /mnt/cpcwiki-httpdocs/includes/libs/IPSet.php on line 243

Notice: Trying to access array offset on value of type bool in /mnt/cpcwiki-httpdocs/includes/libs/IPSet.php on line 246

Notice: Trying to access array offset on value of type null in /mnt/cpcwiki-httpdocs/includes/profiler/SectionProfiler.php on line 104

Notice: Trying to access array offset on value of type null in /mnt/cpcwiki-httpdocs/includes/profiler/SectionProfiler.php on line 104

Notice: Trying to access array offset on value of type null in /mnt/cpcwiki-httpdocs/includes/profiler/SectionProfiler.php on line 105

Notice: Trying to access array offset on value of type null in /mnt/cpcwiki-httpdocs/includes/profiler/SectionProfiler.php on line 105

Notice: Trying to access array offset on value of type null in /mnt/cpcwiki-httpdocs/includes/profiler/SectionProfiler.php on line 106

Notice: Trying to access array offset on value of type null in /mnt/cpcwiki-httpdocs/includes/profiler/SectionProfiler.php on line 106
Programming:Fast 16 bit Square Root - CPCWiki
Last modified on 19 September 2007, at 08:22

Programming:Fast 16 bit Square Root

Revision as of 08:22, 19 September 2007 by Executioner (Talk | contribs) (Fixed last unroll)

This routine was written by The Executioner based on the fast pre-calculated square routine and an algorithm by Milos "baze" Bazelides, with 16 bit arithmetic removed for faster performance.

The initialisation routine, used to build the squares table (same as for the Pre-calculated Square table (my version)).

	ld de,1
	ld hl,0
	ld ix,sqtab
sqloop:	ld (ix + 0),l
	inc hx
	ld (ix + 0),h
	dec hx
	add hl,de
	inc de
	inc de
	inc lx
	jr nz,sqloop

The actual 16 bit square root routine.

Input: DE = The number you want to find the square root of

Output: A = Square root of the number supplied in DE

Destroys: HL

Call: CALL SqrtDE

SqrtDE:	ld hl,sqtab + #180
	ld a,d
	cp (hl)
	jr nz,$ + 6
	dec h
	ld a,e
	cp (hl)
	inc h
	set 6,l
	jr nc,$ + 4
	res 7,l

	ld a,d
	cp (hl)
	jr nz,$ + 6
	dec h
	ld a,e
	cp (hl)
	inc h
	set 5,l
	jr nc,$ + 4
	res 6,l

	ld a,d
	cp (hl)
	jr nz,$ + 6
	dec h
	ld a,e
	cp (hl)
	inc h
	set 4,l
	jr nc,$ + 4
	res 5,l

	ld a,d
	cp (hl)
	jr nz,$ + 6
	dec h
	ld a,e
	cp (hl)
	inc h
	set 3,l
	jr nc,$ + 4
	res 4,l

	ld a,d
	cp (hl)
	jr nz,$ + 6
	dec h
	ld a,e
	cp (hl)
	inc h
	set 2,l
	jr nc,$ + 4
	res 3,l

	ld a,d
	cp (hl)
	jr nz,$ + 6
	dec h
	ld a,e
	cp (hl)
	inc h
	set 1,l
	jr nc,$ + 4
	res 2,l

	ld a,d
	cp (hl)
	jr nz,$ + 6
	dec h
	ld a,e
	cp (hl)
	inc h
	inc l
	jr nc,$ + 4
	res 1,l

	ld a,d
	cp (hl)
	jr nz,$ + 5
	dec h
	ld a,e
	cp (hl)
	ld a,l
	ret nc
	dec a
	ret