Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
fix: update FSQRT function doc
Browse files Browse the repository at this point in the history
  • Loading branch information
boriel committed Oct 16, 2022
1 parent 5697940 commit 79a313a
Showing 1 changed file with 33 additions and 29 deletions.
62 changes: 33 additions & 29 deletions docs/library/fsqrt.bas.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
##fSqrt

ZX BASIC uses the ZX Spectrum ROM routine to calculate many of the floating point math functions.
Unfortunately, some of the functions are notoriously slow. Square root being one of them -
Unfortunately, some of the functions are notoriously slow. Square root being one of them -
there wasn't enough space in the original ROM design to do a good routine, so they cheated.
They calculate x<sup>0.5</sup> instead, and roll straight into the exponent routine.
It turns out that though this works, it's exceptionally slow.
Expand All @@ -22,21 +22,23 @@ REM By Britlion
FUNCTION FASTCALL fSqrt (radicand as FLOAT) as FLOAT
ASM
push namespace core
; FLOAT value arrives in A ED CB
; A is the exponent.
AND A ; Test for zero argument
AND A ; Test for zero argument
RET Z ; Return with zero.
;Strictly we should test the number for being negative and quit if it is.
;But let's assume we like imaginary numbers, hmm?
; If you'd rather break it change to a jump to an error below.
;BIT 7,E ; Test the bit.
;JR NZ,REPORT ; back to REPORT_A
;JR NZ,REPORT ; back to REPORT_A
; 'Invalid argument'
RES 7,E ; Now it's a positive number, no matter what.
call __FPSTACK_PUSH ; Okay, We put it on the calc stack. Stack contains ABS(x)
; Halve the exponent to achieve a good guess.(accurate with .25 16 64 etc.)
; Remember, A is the exponent.
Expand All @@ -50,32 +52,34 @@ ASM
ASIS: XOR $80 ; restore sign.
call __FPSTACK_PUSH ; Okay, NOW we put the guess on the stack
rst 28h ; ROM CALC ;;guess,x
DEFB $C3 ;;st-mem-3
DEFB $02 ;;delete
SQRLOOP: DEFB $31 ;;duplicate
DEFB $E3 ;;get-mem-3
DEFB $C4 ;;st-mem-4
DEFB $05 ;;div
DEFB $E3 ;;get-mem-3
DEFB $0F ;;addition
DEFB $A2 ;;stk-half
DEFB $04 ;;multiply
DEFB $C3 ;;st-mem-3
DEFB $E4 ;;get-mem-4
DEFB $03 ;;subtract
DEFB $2A ;;abs
DEFB $37 ;;greater-0
DEFB $00 ;;jump-true
DEFB SQRLOOP - $ ;;to sqrloop
DEFB $02 ;;delete
DEFB $E3 ;;get-mem-3
DEFB $C3 ;;st-mem-3
DEFB $02 ;;delete
SQRLOOP: DEFB $31 ;;duplicate
DEFB $E3 ;;get-mem-3
DEFB $C4 ;;st-mem-4
DEFB $05 ;;div
DEFB $E3 ;;get-mem-3
DEFB $0F ;;addition
DEFB $A2 ;;stk-half
DEFB $04 ;;multiply
DEFB $C3 ;;st-mem-3
DEFB $E4 ;;get-mem-4
DEFB $03 ;;subtract
DEFB $2A ;;abs
DEFB $37 ;;greater-0
DEFB $00 ;;jump-true
DEFB SQRLOOP - $ ;;to sqrloop
DEFB $02 ;;delete
DEFB $E3 ;;get-mem-3
DEFB $38 ;;end-calc sqr x.
jp __FPSTACK_POP
pop namespace
END ASM
END FUNCTION
```

0 comments on commit 79a313a

Please sign in to comment.