View Single Post
  #3  
Old 01-19-2017, 08:47
chants chants is offline
VIP
 
Join Date: Jul 2016
Posts: 826
Rept. Given: 47
Rept. Rcvd 50 Times in 31 Posts
Thanks Given: 737
Thanks Rcvd at 1,140 Times in 529 Posts
chants Reputation: 51
It is a bit early in the morning :-D

What I really meant to ask, was can we take an expression that is 0 or non-zero and convert it to 0 or -1. Either an all-0 or all-1 bitmask.

0->0
non-zero->-1

With pure arithmetic and no conditional statements. With conditional statements, there are many ways and all are easy. But without, its not as simple.

I noticed that (A OR -A) = 0 for 0, but turns the high bit on except for the sign bit power of 2 value e.g. 0x80000000. Perhaps using CDQ.

Code:
mov eax, [val]
mov edx, eax
neg edx
or eax, edx
cdq
mov eax, edx
Could that really be the simplest algorithm to do it?

The only other interesting property like this I could think of is <0 -> -1, 0 -> 0, >1 ->1 which also would be about as tricky without conditionals and purely arithmetically.

Actually you do something along the lines of multiply the output of previous code by the carry flag after subtracting from 0(sub, adc, mul) and I don't think you can avoid the mul.

Any thoughts
Reply With Quote
The Following User Says Thank You to chants For This Useful Post:
niculaita (02-08-2017)