Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- LongIntegerSqrt:
- ld a, [H_QUOTIENT]
- and a
- jp z, .sqrt32bit
- ld a, [H_QUOTIENT+1]
- and a
- jp z, .regularSqrt
- ld de, 255 * 255
- ld a, [H_QUOTIENT+3]
- sub e
- ld [H_QUOTIENT+3], a
- ld a, [H_QUOTIENT+2]
- sbc d
- ld [H_QUOTIENT+2], a
- ld a, [H_QUOTIENT+1]
- sbc $0
- ld [H_QUOTIENT+1], a
- ld de, 509 ; (255 * 2) - 1
- ld bc, 255
- .sqrtLongLoop_24Bit
- inc bc
- inc de
- inc e
- ld a, [H_QUOTIENT+3]
- sub e
- ld [H_QUOTIENT+3], a
- ld a, [H_QUOTIENT+2]
- sbc d
- ld [H_QUOTIENT+2], a
- ld a, [H_QUOTIENT+1]
- sbc $0
- ld [H_QUOTIENT+1], a
- jr nc, .sqrtHLLoop
- ld h, b
- ld l, c
- ret
- .sqrt32Bit
- ld de, (4095 * 4095) & $ffff
- ld a, [H_QUOTIENT+3]
- sub e
- ld [H_QUOTIENT+3], a
- ld a, [H_QUOTIENT+2]
- sbc d
- ld [H_QUOTIENT+2], a
- ld a, [H_QUOTIENT+1]
- sbc (4095 * 4095) / $10000
- ld [H_QUOTIENT+1], a
- ld a, [H_QUOTIENT]
- sbc $0
- ld [H_QUOTIENT], a
- ld c, 0
- ld de, 8189 ; (4095 * 2) - 1
- ld hl, 4095
- .sqrtLongLoop_32Bit
- inc de
- ld a, d
- or e
- jr nz, .noCarry
- inc c
- .noCarry
- inc e
- inc hl
- ld a, [H_QUOTIENT+3]
- sub e
- ld [H_QUOTIENT+3], a
- ld a, [H_QUOTIENT+2]
- sbc d
- ld [H_QUOTIENT+2], a
- ld a, [H_QUOTIENT+1]
- sbc c
- ld [H_QUOTIENT+1], a
- ld a, [H_QUOTIENT]
- sbc $0
- ld [H_QUOTIENT], a
- jr nc, .sqrtLongLoop_32Bit
- ret
- .regularSqrt
- ld a, [H_QUOTIENT+2]
- ld h, a
- ld a, [H_QUOTIENT+3]
- ld l, a
- ld a, -1
- ld bc, $1
- .sqrtHLLoop
- inc a
- dec c
- dec bc
- add hl, bc
- jr c, .sqrtHLLoop
- ld l, a
- ld h, $0
- ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement