Skip to content

Instantly share code, notes, and snippets.

@bitRAKE
Last active May 17, 2022 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitRAKE/85217a0993a912b77c351fddfb6db8f5 to your computer and use it in GitHub Desktop.
Save bitRAKE/85217a0993a912b77c351fddfb6db8f5 to your computer and use it in GitHub Desktop.
general square root for fasmg
; with the remainder calculations can be split into pieces and extended later
calminstruction ROOT? result*, remainder*, N*
local rem, prox, bit, temp
compute rem, 0
check N = 0
jyes done
compute prox, 0
compute bit, 1 shl (((bsr N) + 1) and -2)
check N > 0
jyes more
arrange bit, =err 'square root of negative number'
assemble bit
exit
more:
compute temp, rem + bit + prox
check N < temp
compute rem, rem shr 1
jyes reduce
compute rem, rem + bit
compute prox, temp
reduce:
compute bit, bit shr 2
check bit
jyes more
done:
publish result, rem
compute rem, N - prox
publish remainder, rem
end calminstruction
@bitRAKE
Copy link
Author

bitRAKE commented May 17, 2022

N = result * result + remainder. In this version the remainder is always >= 0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment