Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/crt/bshl.src
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
.assume adl=1

.section .text

.global __bshl
.type __bshl, @function
.global __bshl_nz
.type __bshl_nz, @function

.ifdef PREFER_OS_CRT

.set __bshl, 0x000100
.set __bshl_nz, __bshl

.else

Expand All @@ -16,6 +20,7 @@ __bshl:
inc b
dec b
ret z
__bshl_nz:
push bc
.L.loop:
add a, a
Expand Down
4 changes: 4 additions & 0 deletions src/crt/bshrs.src
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
.assume adl=1

.section .text

.global __bshrs
.type __bshrs, @function
.global __bshrs_nz
.type __bshrs_nz, @function

__bshrs:
; CC: if B!=0: B*(5*r(PC)+1)+6*r(PC)+(ADL?6*r(SPL)+3*w(SPL):4*r(SPS)+2*w(SPS))+1
; if B==0: 4*r(PC)+(ADL?3*r(SPL):2*r(SPS))+2
inc b
dec b
ret z
__bshrs_nz:
push bc
.L.loop:
sra a
Expand Down
5 changes: 5 additions & 0 deletions src/crt/bshru.src
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
.assume adl=1

.section .text

.global __bshru
.type __bshru, @function
.global __bshru_nz
.type __bshru_nz, @function

.ifdef PREFER_OS_CRT

.set __bshru, 0x000104
.set __bshru_nz, __bshru

.else

Expand All @@ -16,6 +20,7 @@ __bshru:
inc b
dec b
ret z
__bshru_nz:
push bc
.L.loop:
srl a
Expand Down
11 changes: 6 additions & 5 deletions src/crt/dtof.src
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ __dtof_helper:
push de
pop bc
ld l, 5
call __lshru
call __lshru_nz
push bc
pop hl
.L.finish_inf_nan:
Expand Down Expand Up @@ -101,7 +101,8 @@ __dtof:
and a, $1F
.L.round_up:
.L.round_down:
call __llshru
; BC:UDE:UHL >>= 29
call __llshru_nz
; B, C, and UDE are zero here
or a, a
jr z, .L.no_round
Expand Down Expand Up @@ -192,7 +193,7 @@ __dtof:
and a, $0F
or a, $10

call __lshru
call __lshru_nz
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Proof: Shift amount should be 5-29, and the same shift amount was used in DJNZ (which would break if the shift amount could be zero)

xor a, a ; subnormal exponent
; HL = BC >> 1
scf
Expand All @@ -216,5 +217,5 @@ __dtof:

.extern __lland
.extern __llcmpzero
.extern __llshru
.extern __lshru
.extern __llshru_nz
.extern __lshru_nz
4 changes: 2 additions & 2 deletions src/crt/dtoll.src
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ __dtoull:
ld l, a
ex (sp), hl
; shift is non-zero and [1, 11] in the non-UB case
call c, __llshl
call c, __llshl_nz
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Proof: A is [1, 204] here, and call c, __llshl_nz will only call the function if the shift amount is less than 31.

jr .finish
.shift_right:
; A is [-1, -52]
Expand Down Expand Up @@ -85,5 +85,5 @@ __dtoull:
jr .finish_zero_or_one

.extern __llneg
.extern __llshl
.extern __llshl_nz
.extern __llshru
6 changes: 3 additions & 3 deletions src/crt/ftod.src
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ __ftod:
.L.subnormal:
; since this is subnormal, the LSB of the exponent is already zero
call __ictlz
ld c, a
ld c, a ; A is [1, 23]
; shift until the MSB of the mantissa is the LSB of the exponent
call __ishl
call __ishl_nz
Copy link
Contributor Author

@ZERICO2005 ZERICO2005 Mar 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Proof: rcf \ adc hl, hl was done prior to .L.subnormal, which means that __ictlz will return a value that is at least 1 since the LSB will be cleared.

; (Float64_bias - Float32_bias) = $0380
; expon = (Float64_bias - Float32_bias) - clz_result
add hl, hl
Expand Down Expand Up @@ -101,4 +101,4 @@ __ftod:
jr .L.shift_28

.extern __ictlz
.extern __ishl
.extern __ishl_nz
4 changes: 4 additions & 0 deletions src/crt/i48shl.src
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
.assume adl=1

.section .text

.global __i48shl
.type __i48shl, @function
.global __i48shl_nz
.type __i48shl_nz, @function

__i48shl_nz:
__i48shl:
push bc
ld b, a
Expand Down
8 changes: 8 additions & 0 deletions src/crt/i48shr.src
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,29 @@
.assume adl=1

.section .text

.global __i48shrs
.type __i48shrs, @function
.global __i48shrs_nz
.type __i48shrs_nz, @function

.global __i48shru
.type __i48shru, @function
.global __i48shru_nz
.type __i48shru_nz, @function

__i48shrs:
inc c
dec c
ret z
__i48shrs_nz:
cp a, a ; Set Z flag
db $C2 ; inc c \ dec c \ ret z -> jp nz, *
__i48shru:
inc c
dec c
ret z
__i48shru_nz:
; NZ for unsigned shift, Z for signed shift
push bc
ld b, c
Expand Down
11 changes: 11 additions & 0 deletions src/crt/ishl.src
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
.assume adl=1

.section .text

.global __ishl
.type __ishl, @function
.global __ishl_nz
.type __ishl_nz, @function

.global __sshl
.type __sshl, @function
.global __sshl_nz
.type __sshl_nz, @function

.ifdef PREFER_OS_CRT

.set __ishl, 0x000174
.set __ishl_nz, __ishl

.set __sshl, 0x000240
.set __sshl_nz, __sshl

.else

__ishl_nz:
__ishl:
; CC: if 0<=C<8: 23*r(PC)+3*r(SPL)+4+(C&1==0)+(C&2!=0)*(1*r(PC)-1)+(C&4!=0)*(3*r(PC)-1)
; if 8<=C<16: 30*r(PC)+3*r(SPL)+5+(C&1==0)+(C&2!=0)*(1*r(PC)-1)+(C&4!=0)*(3*r(PC)-1)
Expand Down Expand Up @@ -52,5 +62,6 @@ __ishl_8:
ret

.set __sshl, __ishl
.set __sshl_nz, __ishl_nz

.endif
11 changes: 11 additions & 0 deletions src/crt/ishrs.src
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
.assume adl=1

.section .text

.global __ishrs
.type __ishrs, @function
.global __ishrs_nz
.type __ishrs_nz, @function

.global __ishru
.type __ishru, @function
.global __ishru_nz
.type __ishru_nz, @function

.ifdef PREFER_OS_CRT

.set __ishrs, 0x00017C
.set __ishrs_nz, __ishrs

.set __ishru, 0x000184
.set __ishru_nz, __ishru

.else

__ishru_nz:
__ishru:
push af
xor a, a
add hl, hl
rla
db $DA ; push af \ add hl, hl \ sbc a, a -> jp c, *
__ishrs_nz:
__ishrs:
; CC: if 0<=C<8: 35*r(PC)+9*r(SPL)+9*w(SPL)+4+(C&1==0)*(1*r(PC)-1)+(C&2==0)*(3*r(PC)-1)+(C&4==0)*(7*r(PC)-1)
; if 8<=C<16: 34*r(PC)+9*r(SPL)+6*w(SPL)+5+(C&1==0)*(1*r(PC)-1)+(C&2==0)*(3*r(PC)-1)+(C&4==0)*(7*r(PC)-1)
Expand Down
4 changes: 4 additions & 0 deletions src/crt/llshl.src
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
.assume adl=1

.section .text

.global __llshl
.type __llshl, @function
.global __llshl_nz
.type __llshl_nz, @function

__llshl_nz:
__llshl:
; Suboptimal for large shift amounts
push af
Expand Down
7 changes: 7 additions & 0 deletions src/crt/llshrs.src
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@

.global __llshru
.type __llshru, @function
.global __llshru_nz
.type __llshru_nz, @function

.global __llshrs
.type __llshrs, @function
.global __llshrs_nz
.type __llshrs_nz, @function

__llshru_nz:
__llshru:
; Suboptimal for large shift amounts
push af
Expand All @@ -21,6 +27,7 @@ __llshru:
srl b
jr .L.hijack_llshru

__llshrs_nz:
__llshrs:
; Suboptimal for large shift amounts
push af
Expand Down
4 changes: 4 additions & 0 deletions src/crt/lshl.src
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
.section .text
.global __lshl
.type __lshl, @function
.global __lshl_nz
.type __lshl_nz, @function

.ifdef PREFER_OS_CRT

.set __lshl, 0x0001E4
.set __lshl_nz, __lshl

.else

Expand All @@ -17,6 +20,7 @@ __lshl:
inc l
dec l
ret z
__lshl_nz:
push bc
ld b, l
ex (sp), hl
Expand Down
5 changes: 5 additions & 0 deletions src/crt/lshrs.src
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
.assume adl=1

.section .text

.global __lshrs
.type __lshrs, @function
.global __lshrs_nz
.type __lshrs_nz, @function
.global __lshrs.hijack_lshru
.type __lshrs.hijack_lshru, @function

.ifdef PREFER_OS_CRT

.set __lshrs, 0x0001E8
.set __lshrs_nz, __lshrs

.else

Expand All @@ -19,6 +23,7 @@ __lshrs:
inc l
dec l
ret z
__lshrs_nz:
push hl
push bc
ld b, l
Expand Down
5 changes: 5 additions & 0 deletions src/crt/lshru.src
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
.assume adl=1

.section .text

.global __lshru
.type __lshru, @function
.global __lshru_nz
.type __lshru_nz, @function

.ifdef PREFER_OS_CRT

.set __lshru, 0x0001EC
.set __lshru_nz, __lshru

.else

Expand All @@ -17,6 +21,7 @@ __lshru:
inc l
dec l
ret z
__lshru_nz:
push hl
push bc
ld b, l
Expand Down
4 changes: 2 additions & 2 deletions src/crt/ltod.src
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ __lltod:
pop bc

ex (sp), hl ; (SP) = shift
call __llshru
call __llshru_nz
Copy link
Contributor Author

@ZERICO2005 ZERICO2005 Mar 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Proof: Shift amount is [1, 11], and the exact same shift amount was used for DJNZ, which would break if the shift amount were zero.

add a, 51 - 1 ; compensate for the implicit mantissa bit

; BC/exponent = [$434*, $43E*]
Expand Down Expand Up @@ -174,7 +174,7 @@ __ltod:
.extern __lctlz
.extern __llctlz
.extern __llshl
.extern __llshru
.extern __llshru_nz
.extern __llneg
.extern __lladd_1
.extern ___fe_cur_env
Loading
Loading