Reworked parseHexadecimal and parseDecimal, other minor tweaks (#85)

I've tweaked nearly every function in this file, so I'll go through them one by one.
parseDecimal has been reworked a little so that `a` can be used instead of `b` for checking for overflow. I had originally intended to redo it to work like the old parseDecimal, but I think the current method (once reworked a little) is cleaner and smaller, and should be just as fast. 7 bytes and 27 cycles saved.
parseHexadecimal has been changed to load hex digits into `b` `d` `c` `e` from the right (so all the digits move along to the left so the new digit can be inserted on the right), and then only at the end is any shifting done, using the faster `add a, a` to do left shifts. 9 bytes saved and 78 cycles saved inside the loop, and then 49 cycles added after the loop. 
parseBinaryLiteral had a few instructions moved around, saving two bytes and 5 cycles inside the loop, and a further 15 cycles saved on error.
parseLiteral has been reworked slightly, the isDigit call has been replaced with an inline parseDecimalDigit, saving a byte and around 20-30 cycles, with around 16 more cycles saved if the number is a decimal. The .char routine has been reduced by a byte, and 6 cycles saved on success, but 5 cycles added on error.
isDigit has been reduced by 4 bytes and 10 cycles on success, with a few more cycles saved on fail (hard to estimate due to branching).
This commit is contained in:
Clanmaster21 2020-01-08 21:12:40 +00:00 committed by Virgil Dupras
parent fb2117dc2e
commit 927d5f2392
1 changed files with 66 additions and 51 deletions

View File

@ -21,7 +21,6 @@ parseHex:
add a, 10 ; C is clear, map back to 0xA-0xF add a, 10 ; C is clear, map back to 0xA-0xF
ret ret
; Parse string at (HL) as a decimal value and return value in DE. ; Parse string at (HL) as a decimal value and return value in DE.
; Reads as many digits as it can and stop when: ; Reads as many digits as it can and stop when:
; 1 - A non-digit character is read ; 1 - A non-digit character is read
@ -44,10 +43,10 @@ parseDecimal:
; During this routine, we switch between HL and its shadow. On one side, ; During this routine, we switch between HL and its shadow. On one side,
; we have HL the string pointer, and on the other side, we have HL the ; we have HL the string pointer, and on the other side, we have HL the
; numerical result. We also use EXX to preserve BC, saving us a push. ; numerical result. We also use EXX to preserve BC, saving us a push.
parseDecimalSkip: ; enter here to skip parsing the first digit
exx ; HL as a result exx ; HL as a result
ld h, 0 ld h, 0
ld l, a ; load first digit in without multiplying ld l, a ; load first digit in without multiplying
ld b, 0 ; We use B to detect overflow
.loop: .loop:
exx ; HL as a string pointer exx ; HL as a string pointer
@ -58,26 +57,24 @@ parseDecimal:
; same as other above ; same as other above
add a, 0xff-'9' add a, 0xff-'9'
sub 0xff-9 sub 0xff-9
jr c, .end jr c, .end
ld b, a ; we can now use a for overflow checking
add hl, hl ; x2 add hl, hl ; x2
; We do this to detect overflow at each step sbc a, a ; a=0 if no overflow, a=0xFF otherwise
rl b
ld d, h ld d, h
ld e, l ; de is x2 ld e, l ; de is x2
add hl, hl ; x4 add hl, hl ; x4
rl b rla
add hl, hl ; x8 add hl, hl ; x8
rl b rla
add hl, de ; x10 add hl, de ; x10
rl b rla
ld d, 0 ld d, a ; a is zero unless there's an overflow
ld e, a ld e, b
add hl, de add hl, de
rl b adc a, a ; same as rla except affects Z
; Did we oveflow? ; Did we oveflow?
xor a
or b
jr z, .loop ; No? continue jr z, .loop ; No? continue
; error, NZ already set ; error, NZ already set
exx ; HL is now string pointer, restore BC exx ; HL is now string pointer, restore BC
@ -106,33 +103,49 @@ parseDecimalC:
; Sets Z on success. ; Sets Z on success.
parseHexadecimal: parseHexadecimal:
ld a, (hl) ld a, (hl)
call parseHex call parseHex ; before "ret c" is "sub 0xfa" in parseHex
jp c, unsetZ ; we need at least one char ; so carry implies not zero
ret c ; we need at least one char
push bc push bc
ld de, 0 ld de, 0
ld b, 0 ld b, d
ld c, d
; The idea here is that the 4 hex digits of the result can be represented "bdce",
; where each register holds a single digit. Then the result is simply
; e = (c << 4) | e, d = (b << 4) | d
; However, the actual string may be of any length, so when loading in the most
; significant digit, we don't know which digit of the result it actually represents
; To solve this, after a digit is loaded into a (and is checked for validity),
; all digits are moved along, with e taking the latest digit.
.loop: .loop:
; we push to B to verify overflow dec b
rl e \ rl d \ rl b inc b ; b should be 0, else we've overflowed
rl e \ rl d \ rl b jr nz, .end ; Z already unset if overflow
rl e \ rl d \ rl b ld b, d
rl e \ rl d \ rl b ld d, c
or e ld c, e
ld e, a ld e, a
; did we overflow? inc hl
ld a, b ld a, (hl)
or a call parseHex
jr nz, .end ; overflow, NZ already set jr nc, .loop
; next char ld a, b
inc hl add a, a \ add a, a \ add a, a \ add a, a
ld a, (hl) or d
call parseHex ld d, a
jr nc, .loop
cp a ; ensure Z ld a, c
.end: add a, a \ add a, a \ add a, a \ add a, a
or e
ld e, a
xor a ; ensure z
.end:
pop bc pop bc
ret ret
; Parse string at (HL) as a binary value (010101) without the "0b" prefix and ; Parse string at (HL) as a binary value (010101) without the "0b" prefix and
; return value in E. D is always zero. ; return value in E. D is always zero.
; HL is advanced to the character following the last successfully read char. ; HL is advanced to the character following the last successfully read char.
@ -144,10 +157,10 @@ parseBinaryLiteral:
add a, 0xff-'1' add a, 0xff-'1'
sub 0xff-1 sub 0xff-1
jr c, .end jr c, .end
rl e rlc e ; sets carry if overflow, and affects Z
ret c ; Z unset if carry set, since bit 0 of e must be set
add a, e add a, e
ld e, a ld e, a
jp c, unsetZ ; overflow
inc hl inc hl
jr .loop jr .loop
.end: .end:
@ -167,10 +180,13 @@ parseLiteral:
ld a, (hl) ld a, (hl)
cp 0x27 ; apostrophe cp 0x27 ; apostrophe
jr z, .char jr z, .char
call isDigit
ret nz ; inline parseDecimalDigit
cp '0' add a, 0xc6 ; maps '0'-'9' onto 0xf6-0xff
jp nz, parseDecimal sub 0xf6 ; maps to 0-9 and carries if not a digit
ret c
; a already parsed so skip first few instructions of parseDecimal
jp nz, parseDecimalSkip
; maybe hex, maybe binary ; maybe hex, maybe binary
inc hl inc hl
ld a, (hl) ld a, (hl)
@ -195,14 +211,13 @@ parseLiteral:
ld e, (hl) ; our result ld e, (hl) ; our result
inc hl inc hl
cp (hl) cp (hl)
jr nz, .charError ; not ending with an apostrophe ; advance HL and return if good char
; good char, advance HL and return
inc hl inc hl
; Z already set ret z
ret
.charError: ; Z unset and there's an error
; In all error conditions, HL is advanced by 2. Rewind. ; In all error conditions, HL is advanced by 3. Rewind.
dec hl \ dec hl dec hl \ dec hl \ dec hl
; NZ already set ; NZ already set
ret ret
@ -215,9 +230,9 @@ isLiteralPrefix:
; Returns whether A is a digit ; Returns whether A is a digit
isDigit: isDigit:
cp '0' cp '0' ; carry implies not zero for cp
jp c, unsetZ ret c
cp '9'+1 cp '9' ; zero unset for a > '9', but set for a='9'
jp nc, unsetZ ret nc
cp a ; ensure Z cp a ; ensure Z
ret ret