From 7a56eb425114c448e3a84070b5b6e52e4973fe8f Mon Sep 17 00:00:00 2001 From: ZERICO2005 <71151164+ZERICO2005@users.noreply.github.com> Date: Sat, 17 May 2025 14:24:57 -0600 Subject: [PATCH 1/2] Saved 4 bytes in ftod --- src/crt/ftod.src | 43 ++++++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/src/crt/ftod.src b/src/crt/ftod.src index f000ff012..5d08ece5e 100644 --- a/src/crt/ftod.src +++ b/src/crt/ftod.src @@ -8,24 +8,26 @@ ; ouput BC:UDE:UHL (long double) ; NaN payloads are bitshifted __ftod: - bit 7, e + sla e ; extract signbit push af - res 7, e + srl e + ; signbit and carry are cleared - xor a, a ld a, e - - adc hl, hl ld b, $03 ; ld bc, $380 - 1 + + adc hl, hl jr z, .mant_zero ; zero inf or normal adc a, a jr z, .subnormal inc a - jr z, .nan + jr z, .nan .normal: ; (Float64_bias - Float32_bias) - 1 (undoing inc a) ; (Float64_bias - Float32_bias) = $0380 - add a, $80 - 1 + add a, $80 - 1 ; this can be changed to adc if needed +.subnorm_shift: + ; Same as ld c, a \ ld a, 0 \ adc a, b ld c, a adc a, b sub a, c @@ -44,12 +46,13 @@ __ftod: ; zero low bits .infinite: - or a, a +; or a, a ; not needed + ; f64_mant_bits - f32_mant_bits = lowest 29 bits will be zero ex de, hl sbc hl, hl .finish: pop af - ret z ; positive + ret nc ; positive set 7, b ret ; negative @@ -61,7 +64,7 @@ __ftod: jr nz, .normal ; infinite ld c, $F0 - ld b, e ; load all ones with the signbit + ld b, e ; ld b, $7F since infinity is all ones jr .infinite .subnormal: @@ -73,11 +76,21 @@ __ftod: ; (Float64_bias - Float32_bias) = $0380 ; expon = (Float64_bias - Float32_bias) - clz_result add hl, hl + ; the following three methods are equivilient +if 1 + ; ($FF - a) - $7F cpl - adc a, $80 - ld c, a - ld a, b ; ld a, $03 - jr .shift_28 + sub a, $80 - 1 +else if 0 + ; $FF - (a + $7F) + add a, $80 - 1 + cpl +else + ; $80 - c (c is equal to a) + ld a, $80 + sub a, c +end if + jr .subnorm_shift .nan: ld a, $07 @@ -85,4 +98,4 @@ __ftod: jr .shift_28 extern __ictlz - extern __ishl + extern __ishl \ No newline at end of file From cf12f87b7937da2954640a80393fcaac92395263 Mon Sep 17 00:00:00 2001 From: ZERICO2005 <71151164+ZERICO2005@users.noreply.github.com> Date: Sat, 17 May 2025 14:27:13 -0600 Subject: [PATCH 2/2] optimized the placement of the tailcall in i48abs --- src/crt/i48abs.src | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/crt/i48abs.src b/src/crt/i48abs.src index 08150c251..fd37abbfc 100644 --- a/src/crt/i48abs.src +++ b/src/crt/i48abs.src @@ -17,7 +17,8 @@ _i48abs: push bc ex de, hl - ret nc ; positive - jp __i48neg + jp c, __i48neg ; negative + ; positive + ret extern __i48neg