"Paul B. Webster VK2BZC" wrote: > > Scott Dattalo wrote: > > > One of the tricks that this routine does is to simultaneously multiply > > and divide. This works well for two operands, but I don't see how to > > make it work for three. Also, the 'multiplication' is a bit-by-bit > > either-or operation. In other words, at each stage of the > > multiplication, the cumulative product is either being increased by > > one coefficient or the other. Short of going to trits, I don't see > > how to handle three operands at the same time. Could you be more > > specific? > > Easey-peasey! Let me find the original.... Cool, Paul CAN write PIC code. :) > > twist3: > movf y,w > movwf num > > btfsc A,0 > movf x,w > btfsc B,0 > movf z,w > addwf num,f > rrf num,f > > movf y,w ;A macro would work well here... > btfsc A,1 ;All we're doing is an un-rolled > movf x,w ;multiplication. If a bit in 'A' is > btfsc B,1 ;set, we add 'y' to the running sum > movf z,w ;or if a bit in 'B' is set, we add 'z' > addwf num,f ;otherwise we add 'x'. In either case > rrf num,f ;we divide by 16 overall. > > movf y,w > btfsc A,2 > movf x,w > btfsc B,2 > movf z,w > addwf num,f > rrf num,f > > movf y,w > btfsc A,3 > movf x,w > btfsc B,3 > movf z,w > addwf num,f > rrf num,f > > return > > Note that A & B = 0 (although the algorithm won't overflow) > Coefficients for x, y and z are (16-A-B)/16, A/16 and B/16 But unfortunately, this won't work as expected. The subtle trick is that the the coefficients for x and y are two's complements of one another in the routine I posted. In your twist3, the coefficients for x and for y are no longer necessarily two's complements. Consequently, there will be stages in the multiplication where it is necessary to add x and add y (and even maybe z). As an example, suppose the coefficients for x,y, and z are 5, 7, and 4 (the sum is 16 as required). If you look at the bit patterns: 5 - 0101 7 - 0111 4 - 0100 It can be seen that for stage 0 (the lsb) that x & y need to be added, stage 1 only y, stage 2 x & y & z, and on stage 3 nothing. Is this clear? Scott