home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.asm.x86      Ahh, the lost art of x86 assembly      4,675 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 3,799 of 4,675   
   Rick C. Hodgin to James Harris   
   Re: Fast conversion to a boolean of 0 or   
   06 Mar 19 12:55:19   
   
   From: rick.c.hodgin@gmail.com   
      
   On Wednesday, March 6, 2019 at 2:47:41 PM UTC-5, James Harris wrote:   
   > A small programming challenge if you are interested, just for fun.   
   >   
   > Say we want any value other than 0 in EAX to be reduced to 1 - e.g. so   
   > that if non-zero means true but we want true to be 1.   
   >   
   > Is there a faster solution than the naive   
   >   
   >      cmp eax, 0   
   >      je done   
   >      mov eax, 1   
   >    done:   
   >   
   > Naturally, the speed of that will depend on how predictable the input   
   > values are. Taking into account the usual suspect characteristics of x86   
   > CPUs is there a generally faster solution?   
   >   
   > I can think of three bit-twiddling ways but I'm not sure they would be   
   > faster than the above. In fact, I think they might be slower. In any   
   > case I'd be interested to see what others think. I suspect some of you   
   > may already have a preferred solution.   
   >   
   > So, any suggestions?   
      
      
   Visual Studio 2017 uses this version:   
      
       // unsigned int i;   
       // i = (unsigned int)rand();   
       // i = ((i == 0) ? 0 : 1);   
      
       neg    eax   
       sbb    edi,edi   
       neg    edi   
      
   It also consumes a register and switches registers to edi,   
   and shows 2.04 seconds.   
      
   --   
   Rick C. Hodgin   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca