diff --git a/compiler/examples/test.p8 b/compiler/examples/test.p8 index c9d21e326..17c290e7b 100644 --- a/compiler/examples/test.p8 +++ b/compiler/examples/test.p8 @@ -13,7 +13,7 @@ ubyte[4] uba = [4,200,10,15] word[4] wa = [400,-200,-1000,1500] uword[4] uwa = [4000,200,1000,150] - float[4] fa = [-2.22, 3.33, -5.55, 1.11] + float[6] fa = [-2.22, 3.33, -5.55, 1.11, 9999.99, -999.99] c64scr.print(" X=") c64scr.print_ub(X) c64.CHROUT('\n') @@ -26,8 +26,8 @@ ; word wmax = max(wa) ; uword uwmin = min(uwa) ; uword uwmax = max(uwa) -; float fmin = min(fa) -; float fmax = max(fa) + float fmin = min(fa) + float fmax = max(fa) c64scr.print_b(bmin) c64.CHROUT(',') @@ -46,10 +46,10 @@ ; c64scr.print_uw(uwmax) ; c64.CHROUT('\n') ; -; c64flt.print_f(fmin) -; c64.CHROUT(',') -; c64flt.print_f(fmax) -; c64.CHROUT('\n') + c64flt.print_f(fmin) + c64.CHROUT(',') + c64flt.print_f(fmax) + c64.CHROUT('\n') c64scr.print(" X=") c64scr.print_ub(X) diff --git a/compiler/src/prog8/compiler/target/c64/Commodore64.kt b/compiler/src/prog8/compiler/target/c64/Commodore64.kt index c5fa506d6..6ccb1586a 100644 --- a/compiler/src/prog8/compiler/target/c64/Commodore64.kt +++ b/compiler/src/prog8/compiler/target/c64/Commodore64.kt @@ -12,8 +12,8 @@ import kotlin.math.pow // 5-byte cbm MFLPT format limitations: -const val FLOAT_MAX_POSITIVE = 1.7014118345e+38 -const val FLOAT_MAX_NEGATIVE = -1.7014118345e+38 +const val FLOAT_MAX_POSITIVE = 1.7014118345e+38 // bytes: 255,127,255,255,255 +const val FLOAT_MAX_NEGATIVE = -1.7014118345e+38 // bytes: 255,255,255,255,255 const val BASIC_LOAD_ADDRESS = 0x0801 const val RAW_LOAD_ADDRESS = 0xc000 diff --git a/prog8lib/prog8lib.p8 b/prog8lib/prog8lib.p8 index 7e7d32645..21ca81b8e 100644 --- a/prog8lib/prog8lib.p8 +++ b/prog8lib/prog8lib.p8 @@ -1206,9 +1206,37 @@ func_max_w .proc .pend func_max_f .proc - dex - rts .warn "todo func_max_f" + lda #<_min_float + ldy #>_min_float + jsr c64.MOVFM ; fac1=min(float) + lda #255 + sta _cmp_mod+1 ; compare using 255 so we keep larger values +_minmax_entry jsr pop_array_and_lengthY + stx SCRATCH_ZPREGX + dey + sty SCRATCH_ZPREG +- lda SCRATCH_ZPWORD1 + ldy SCRATCH_ZPWORD1+1 + jsr c64.FCOMP +_cmp_mod cmp #255 ; will be modified + bne + + ; fac1 is smaller/larger, so store the new value instead + lda SCRATCH_ZPWORD1 + ldy SCRATCH_ZPWORD1+1 + jsr c64.MOVFM ++ lda #5 + clc + adc SCRATCH_ZPWORD1 + sta SCRATCH_ZPWORD1 + bcc + + inc SCRATCH_ZPWORD1+1 ++ ldy SCRATCH_ZPREG + dey + sty SCRATCH_ZPREG + bpl - + jmp push_fac1_as_result +_min_float .byte 255,255,255,255,255 ; -1.7014118345e+38 .pend pop_array_and_lengthY .proc @@ -1276,11 +1304,15 @@ func_min_w .proc .pend func_min_f .proc - dex - rts - .warn "todo func_min_f" + lda #<_max_float + ldy #>_max_float + jsr c64.MOVFM ; fac1=max(float) + lda #1 + sta func_max_f._cmp_mod+1 ; compare using 1 so we keep smaller values + jmp func_max_f._minmax_entry +_max_float .byte 255,127,255,255,255 ; 1.7014118345e+38 .pend - + func_len_str .proc ; -- push length of 0-terminated string on stack