[HN Gopher] Bit Test and Reset vs. Compilers
       ___________________________________________________________________
        
       Bit Test and Reset vs. Compilers
        
       Author : gbrown_
       Score  : 30 points
       Date   : 2021-12-22 19:42 UTC (1 days ago)
        
 (HTM) web link (0x80.pl)
 (TXT) w3m dump (0x80.pl)
        
       | tyfighter wrote:
       | Why not use x86's BSF or BSR instructions? They've been around
       | since 386.
        
         | secondcoming wrote:
         | Those give undefined results if the operand is zero, so maybe
         | having to do that check defeats the point?
        
       | tzs wrote:
       | Most architectures seem to have an instruction for finding the
       | leftmost set bit in a word [1] and most major C/C++
       | compilers/libraries have a function for it [2].
       | 
       | I wonder how using that would compare?
       | 
       | You'd find the leftmost bit, invoke the func_true for that, and
       | clear the leftmost bit.
       | 
       | Then loop finding the leftmost bit, invoking the func_false for
       | the bits between the current leftmost bit and the previous
       | leftmost bit, func_true for the leftmost bit, and then clear that
       | bit.
       | 
       | I wouldn't expect it to be dramatically different, but it is
       | trading test and clear on the off bits and a conditional branch
       | off of the result for a loop calling func_false a predetermined
       | number of times. One of those options might be a little faster
       | than the other.
       | 
       | [1]
       | https://en.wikipedia.org/wiki/Find_first_set#Hardware_suppor...
       | 
       | [2]
       | https://en.wikipedia.org/wiki/Find_first_set#Tool_and_librar...
        
       ___________________________________________________________________
       (page generated 2021-12-23 23:02 UTC)