This is what the NCD routine looks like in the pbppic18.lib file when stripping out the non runtime stuff:I'll say what I usually say which is that I pretty much suck at assembly language stuff but that looks pretty tight to me. Obviously the execution time will vary depending on how many times it has to iterate thru the loop before it finds a bit that is set. Then there are helper macros which puts the byte/Word in question into the correct register (R0) and so on, if interested they are in the pbppic18.mac file.Code:;**************************************************************** ;* NCD : Priority encode * ;* * ;* Input : R0 = 16 bit value * ;* Output : W = bit number * ;* * ;* Notes : * ;**************************************************************** NCD clrf R0 + 1 NCDL movwf R0 movlw 17 ; Set result ncdloop addlw -1 ; Count down result - sets C so loop will end rlcf R0, F ; Shift upper bit to carry rlcf R0 + 1, F bnc ncdloop ; If carry set then done goto DUNN
Is there a faster way? Perhaps but I'd give NCD a try and get some performance data on that first - otherwise you don't know what to beat (or if you even need to beet it) - IMHO of course.
/Henrik.




Bookmarks