Quickest way to get highest set bit on port in to a variable?


Closed Thread
Results 1 to 29 of 29

Hybrid View

  1. #1
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,624


    Did you find this post helpful? Yes | No

    Default Re: Quickest way to get highest set bit on port in to a variable?

    This is what the NCD routine looks like in the pbppic18.lib file when stripping out the non runtime stuff:
    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
    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.

    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.

  2. #2
    Join Date
    Jan 2009
    Location
    Miami, Florida USA
    Posts
    704


    Did you find this post helpful? Yes | No

    Default Re: Quickest way to get highest set bit on port in to a variable?

    Like Archangel mentioned above, you should also try the LOOKUP command. This command takes only a few cycles to execute.
    "No one is completely worthless. They can always serve as a bad example."

    Anonymous

  3. #3


    Did you find this post helpful? Yes | No

    Default Re: Quickest way to get highest set bit on port in to a variable?

    Henric, never the less, not as quick as 8 rotates and 8 jump on carry in-line. Worst case, with no bits set in the target thats only 16 cycles.

    rscor01, I didn't realize LOOKUP would do that. How would that go?

    George

  4. #4
    Join Date
    Jan 2009
    Location
    Miami, Florida USA
    Posts
    704


    Did you find this post helpful? Yes | No

    Default Re: Quickest way to get highest set bit on port in to a variable?

    Quote Originally Posted by towlerg View Post
    rscor01, I didn't realize LOOKUP would do that. How would that go?
    Nevermind. You would need too many entries in the LOOKUP constant fields. That would be 64 entries to be precise since your variable PORT has 6 bits. It will be something like this

    Code:
    LOOKUP MyPort, [0,1,2,2,3,3,3,3,4,........], MyVar
    Still, it might be faster than NCD, but I don't know.
    "No one is completely worthless. They can always serve as a bad example."

    Anonymous

  5. #5
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default Re: Quickest way to get highest set bit on port in to a variable?

    Quote Originally Posted by towlerg View Post
    Henric, never the less, not as quick as 8 rotates and 8 jump on carry in-line. Worst case, with no bits set in the target thats only 16 cycles.

    rscor01, I didn't realize LOOKUP would do that. How would that go?

    George
    lookdown2 index, >= [%00000000,%00000001,%00000010],temp
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  6. #6
    Join Date
    Jan 2009
    Location
    Miami, Florida USA
    Posts
    704


    Did you find this post helpful? Yes | No

    Default Re: Quickest way to get highest set bit on port in to a variable?

    Quote Originally Posted by Archangel View Post
    lookdown2 index, >= [%00000000,%00000001,%00000010],temp
    Nice! Very clever indeed . I have never used this command before, but now I can see it's potential.
    "No one is completely worthless. They can always serve as a bad example."

    Anonymous

  7. #7
    Join Date
    Jan 2013
    Location
    Texas USA
    Posts
    229


    Did you find this post helpful? Yes | No

    Default Re: Quickest way to get highest set bit on port in to a variable?

    I may be wrong but I don't think this will work.
    LOOKDOWN2 Search,{Test}[Value{,Value...}],Var

    Lookdown2 compares the value of "Search" with the values in the list from left to right (index values 0 up to 255) with the "Test" comparison.

    So if "Search" = 5 and "Test" = "=>" then using your values for the Value in the list, the logic tests should look like this.
    (Values: 0, 1, 2, 4, 8, 16, 32, 64, 128) bits 0-7
    Test 1: is 5 => 0? : Yes, (0 will be stored in Var)
    The testing would stop right here for every value of "Search" (0-255) for bytes (0-65535) for words because the "Search" value will always be greater than or equal 0 (unless you're using Long variables)

    Maybe I'm wrong?

    As I see it, to use Lookdown2 you would still need to enter all the values from 0 to 255 inclusive into the list.
    And "Test" would need to be set to "="
    Lookdown2 supports up to 85 values in the list or up to 256 when using a PIC18.
    Last edited by Tabsoft; - 4th March 2015 at 03:03.
    Regards,
    TABSoft

  8. #8
    Join Date
    Jan 2009
    Location
    Miami, Florida USA
    Posts
    704


    Did you find this post helpful? Yes | No

    Default Re: Quickest way to get highest set bit on port in to a variable?

    Quote Originally Posted by Tabsoft View Post
    I may be wrong but I don't think this will work.
    LOOKDOWN2 Search,{Test}[Value{,Value...}],Var

    Lookdown2 compares the value of Search with the values in the list from left to right (index values 0 up to 255) with the "Test" comparison.

    So if Search = 5 and Test = "=>" then using your values for the Value in the list, the logic tests should look like this.
    (Values: 0, 1, 2, 4, 8, 16, 32, 64, 128)
    Test 1: is 5 => 0? : Yes, (0 will be stored in Var)
    The testing would stop right here for every value of Search (0-255) for bytes (0-65535) for words.

    Maybe I'm wrong?

    As I see it, to use Lookdown2 you would still need to enter all the values from 0 to 255 inclusive into the list.
    And
    Lookdown2 supports up to 85 values in the list or up to 256 when using a PIC18.
    No, I don't think that you are wrong. I think there is a minor change that needs to be done to Dave's code,

    Code:
    lookdown2 index, <= [%00000000,%00000001,%00000010,%00000100,%00001000,%00010000,%00100000,%01000000,%10000000,255],temp
    "No one is completely worthless. They can always serve as a bad example."

    Anonymous

Similar Threads

  1. Replies: 6
    Last Post: - 18th July 2012, 03:42
  2. Whats the quickest way to set bits?
    By bearpawz in forum mel PIC BASIC Pro
    Replies: 33
    Last Post: - 26th October 2010, 05:38
  3. Bit set question
    By nverma in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 2nd April 2007, 22:23
  4. set flags within a variable?
    By peterdeco1 in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 24th October 2006, 12:07
  5. How do I to set one bit in a register?
    By jessey in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 11th February 2006, 09:43

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts