flashjazzcat Posted April 14, 2016 Share Posted April 14, 2016 I need to add two 16-bit integers (let's call them A and B; A is signed, B isn't) and get the result, less 1. Sure I can do a 16-bit decrement on the result of the addition, but 16-bit DECs require a branch instruction (for MSB roll-over) and I'm trying to save cycles. I considered using a 256 byte look up table of the index value less 1, but this comes unstuck when then LSB is zero, since element 0 is $FF: ldx a lda Less1Tab,x clc adc b sta c lda a+1 adc b+1 sta c+1 rts Less1Tab .byte $FF ?Value = $0 .rept 255 .byte ?Value ?Value = ?Value+1 .endr Making the first element of the LUT $FF seemed like a great idea until I realized it sets the carry flag when subtracting 1 from 0, messing up the MSB. Any more complex and it'll be easier to DEC the result. Any ingenious ideas? Quote Link to comment Share on other sites More sharing options...
+JAC! Posted April 14, 2016 Share Posted April 14, 2016 I don't see why the branch is an issue. This is the first straight optimized way that comes to my mind clc lda a adc b tax "+2 lda a+1 adc b+1 cpx #0 "+2 beq not_0 "+2/+3 sbc #1 "+2/0, C==1 because X>0 not_0 dex "+2 stx c sta c+1 sums up to 9/10 additional cycles 1 Quote Link to comment Share on other sites More sharing options...
flashjazzcat Posted April 14, 2016 Author Share Posted April 14, 2016 Doing the usual 16-bit decrement after the fact averages at eleven cycles, assuming ZP, no page cross on branch, and the MSB being avoided most of the time. So yours is a bit better! Quote Link to comment Share on other sites More sharing options...
mono Posted April 14, 2016 Share Posted April 14, 2016 Not so fast, but maybe it will be helpfull: clc lda a adc b php clc adc #$ff sta c lda a+1 adc b+1 plp adc #$ff sta c+1 1 Quote Link to comment Share on other sites More sharing options...
flashjazzcat Posted April 14, 2016 Author Share Posted April 14, 2016 Neat! Quote Link to comment Share on other sites More sharing options...
MaPa Posted April 15, 2016 Share Posted April 15, 2016 The code from JAC! does not need the branch... clc lda a adc b tax lda a+1 adc b+1 cpx #1 sbc #0 sta c+1 dex stx c 3 Quote Link to comment Share on other sites More sharing options...
+JAC! Posted April 15, 2016 Share Posted April 15, 2016 Argh, should have seen this. Looks like I need more practise coding A8 directly again 1 Quote Link to comment Share on other sites More sharing options...
flashjazzcat Posted April 15, 2016 Author Share Posted April 15, 2016 No matter: excellent. Quote Link to comment Share on other sites More sharing options...
Wrathchild Posted April 15, 2016 Share Posted April 15, 2016 I did consider if subtracting the one's complement would do this but that ends up adding one instead. Is result for zero plus zero then minus one (all bits ones). Are you using signed or unsigned numbers? Quote Link to comment Share on other sites More sharing options...
flashjazzcat Posted April 15, 2016 Author Share Posted April 15, 2016 (edited) A is signed, B is not (which is just to say B is never >32767). So result may be negative.Just tested Mapa's modified version of Peter's code and it works nicely, of course. I'll take the three cycle saving, and it looks less clumsy than sticking a DEW after the addition. Thanks for your help. I thought this quite an interesting little problem. The chosen solution also has the advantage that the second set of coordinates are left pre-loaded in the A and X registers, meaning I can fall straight into the first signed 16 bit clipping comparison without re-loading anything. .proc Clip clc lda cx1 adc Width tax lda cx1+1 adc Width+1 cpx #1 sbc #0 sta cx2+1 dex stx cx2 cpx XMin ; is cx2 < XMin? sbc XMin+1 svc eor #$80 bmi NoDraw clc lda cy1 adc Height tax lda cy1+1 adc Height+1 cpx #1 sbc #0 sta cy2+1 dex stx cy2 cpx YMin ; is cy2 < YMin? sbc YMin+1 svc eor #$80 bmi NoDraw cmps16 YMax cy1 ; is YMax < cy? bmi NoDraw cmps16 XMax cx1 ; is XMax < cx? NoDraw rts .endp Edited April 15, 2016 by flashjazzcat 5 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.