From 6a18c83fa5ea687956291d8feb1a0d5f1e4e936c Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Fri, 21 Dec 2018 23:10:45 +0100 Subject: [PATCH] min/max word and uword --- compiler/examples/test.p8 | 31 ++++---- prog8lib/prog8lib.p8 | 157 +++++++++++++++++++++++++++++++++----- 2 files changed, 153 insertions(+), 35 deletions(-) diff --git a/compiler/examples/test.p8 b/compiler/examples/test.p8 index 17c290e7b..3ee40900f 100644 --- a/compiler/examples/test.p8 +++ b/compiler/examples/test.p8 @@ -11,8 +11,8 @@ byte[4] ba = [-1,2,-10,30] ubyte[4] uba = [4,200,10,15] - word[4] wa = [400,-200,-1000,1500] - uword[4] uwa = [4000,200,1000,150] + word[5] wa = [400,-200,-1000,9999,1500] + uword[7] uwa = [333,42,9999,12,150,1000,4000] float[6] fa = [-2.22, 3.33, -5.55, 1.11, 9999.99, -999.99] c64scr.print(" X=") c64scr.print_ub(X) @@ -22,13 +22,22 @@ byte bmax = max(ba) ubyte ubmin = min(uba) ubyte ubmax = max(uba) -; word wmin = min(wa) -; word wmax = max(wa) -; uword uwmin = min(uwa) -; uword uwmax = max(uwa) + word wmin = min(wa) + word wmax = max(wa) + uword uwmin = min(uwa) + uword uwmax = max(uwa) float fmin = min(fa) float fmax = max(fa) + c64scr.print_w(wmin) + c64.CHROUT(',') + c64scr.print_w(wmax) + c64.CHROUT('\n') + c64scr.print_uw(uwmin) + c64.CHROUT(',') + c64scr.print_uw(uwmax) + c64.CHROUT('\n') + c64scr.print_b(bmin) c64.CHROUT(',') c64scr.print_b(bmax) @@ -37,15 +46,7 @@ c64.CHROUT(',') c64scr.print_ub(ubmax) c64.CHROUT('\n') -; c64scr.print_w(wmin) -; c64.CHROUT(',') -; c64scr.print_w(wmax) -; c64.CHROUT('\n') -; c64scr.print_uw(uwmin) -; c64.CHROUT(',') -; c64scr.print_uw(uwmax) -; c64.CHROUT('\n') -; + c64flt.print_f(fmin) c64.CHROUT(',') c64flt.print_f(fmax) diff --git a/prog8lib/prog8lib.p8 b/prog8lib/prog8lib.p8 index 21ca81b8e..3eb2f6e24 100644 --- a/prog8lib/prog8lib.p8 +++ b/prog8lib/prog8lib.p8 @@ -1154,10 +1154,9 @@ _cmp_mod cpy #255 ; modified .pend func_max_ub .proc - jsr pop_array_and_lengthY + jsr pop_array_and_lengthmin1Y lda #0 sta SCRATCH_ZPB1 - dey - lda (SCRATCH_ZPWORD1),y cmp SCRATCH_ZPB1 bcc + @@ -1172,10 +1171,9 @@ func_max_ub .proc .pend func_max_b .proc - jsr pop_array_and_lengthY + jsr pop_array_and_lengthmin1Y lda #-128 sta SCRATCH_ZPB1 - dey - lda (SCRATCH_ZPWORD1),y sec sbc SCRATCH_ZPB1 @@ -1194,27 +1192,86 @@ func_max_b .proc .pend func_max_uw .proc - inx + lda #0 + sta _result_maxuw + sta _result_maxuw+1 + jsr pop_array_and_lengthmin1Y + tya + asl a + tay ; times 2 because of word array +_loop + iny + lda (SCRATCH_ZPWORD1),y + dey + cmp _result_maxuw+1 + bcc _lesseq + bne _greater + lda (SCRATCH_ZPWORD1),y + cmp _result_maxuw + bcc _lesseq +_greater lda (SCRATCH_ZPWORD1),y + sta _result_maxuw + iny + lda (SCRATCH_ZPWORD1),y + sta _result_maxuw+1 + dey +_lesseq dey + dey + bpl _loop + lda _result_maxuw + sta ESTACK_LO,x + lda _result_maxuw+1 + sta ESTACK_HI,x + dex rts - .warn "todo func_max_uw" +_result_maxuw .word 0 .pend func_max_w .proc - inx + lda #$00 + sta _result_maxw + lda #$80 + sta _result_maxw+1 + jsr pop_array_and_lengthmin1Y + tya + asl a + tay ; times 2 because of word array +_loop + lda (SCRATCH_ZPWORD1),y + cmp _result_maxw + iny + lda (SCRATCH_ZPWORD1),y + dey + sbc _result_maxw+1 + bvc + + eor #$80 ++ bmi _lesseq + lda (SCRATCH_ZPWORD1),y + sta _result_maxw + iny + lda (SCRATCH_ZPWORD1),y + sta _result_maxw+1 + dey +_lesseq dey + dey + bpl _loop + lda _result_maxw + sta ESTACK_LO,x + lda _result_maxw+1 + sta ESTACK_HI,x + dex rts - .warn "todo func_max_w" +_result_maxw .word 0 .pend func_max_f .proc - .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 +_minmax_entry jsr pop_array_and_lengthmin1Y stx SCRATCH_ZPREGX - dey sty SCRATCH_ZPREG - lda SCRATCH_ZPWORD1 ldy SCRATCH_ZPWORD1+1 @@ -1239,9 +1296,10 @@ _cmp_mod cmp #255 ; will be modified _min_float .byte 255,255,255,255,255 ; -1.7014118345e+38 .pend -pop_array_and_lengthY .proc +pop_array_and_lengthmin1Y .proc inx ldy ESTACK_LO,x + dey ; length minus 1, for iteration lda ESTACK_LO+1,x sta SCRATCH_ZPWORD1 lda ESTACK_HI+1,x @@ -1251,10 +1309,9 @@ pop_array_and_lengthY .proc .pend func_min_ub .proc - jsr pop_array_and_lengthY + jsr pop_array_and_lengthmin1Y lda #255 sta SCRATCH_ZPB1 - dey - lda (SCRATCH_ZPWORD1),y cmp SCRATCH_ZPB1 bcs + @@ -1270,10 +1327,9 @@ func_min_ub .proc func_min_b .proc - jsr pop_array_and_lengthY + jsr pop_array_and_lengthmin1Y lda #127 sta SCRATCH_ZPB1 - dey - lda (SCRATCH_ZPWORD1),y clc sbc SCRATCH_ZPB1 @@ -1292,15 +1348,76 @@ func_min_b .proc .pend func_min_uw .proc - inx + lda #$ff + sta _result_minuw + sta _result_minuw+1 + jsr pop_array_and_lengthmin1Y + tya + asl a + tay ; times 2 because of word array +_loop + iny + lda (SCRATCH_ZPWORD1),y + dey + cmp _result_minuw+1 + bcc _less + bne _gtequ + lda (SCRATCH_ZPWORD1),y + cmp _result_minuw + bcs _gtequ +_less lda (SCRATCH_ZPWORD1),y + sta _result_minuw + iny + lda (SCRATCH_ZPWORD1),y + sta _result_minuw+1 + dey +_gtequ dey + dey + bpl _loop + lda _result_minuw + sta ESTACK_LO,x + lda _result_minuw+1 + sta ESTACK_HI,x + dex rts - .warn "todo func_min_uw" +_result_minuw .word 0 .pend func_min_w .proc - inx + lda #$ff + sta _result_minw + lda #$7f + sta _result_minw+1 + jsr pop_array_and_lengthmin1Y + tya + asl a + tay ; times 2 because of word array +_loop + lda (SCRATCH_ZPWORD1),y + cmp _result_minw + iny + lda (SCRATCH_ZPWORD1),y + dey + sbc _result_minw+1 + bvc + + eor #$80 ++ bpl _gtequ + lda (SCRATCH_ZPWORD1),y + sta _result_minw + iny + lda (SCRATCH_ZPWORD1),y + sta _result_minw+1 + dey +_gtequ dey + dey + bpl _loop + lda _result_minw + sta ESTACK_LO,x + lda _result_minw+1 + sta ESTACK_HI,x + dex rts - .warn "todo func_min_w" +_result_minw .word 0 .pend func_min_f .proc