Thanks everyone.
Providing a useful intrinsic for this would not be easy. ..
Yes I was thinking that too. To be honest this was just an random thought experiment, and I figured it might be interesting to discuss.
I had some short functions that were pretty heavily used in some code I'm running, and I wanted to make them inline and as efficient as possible. So I was just playing around with ideas for making branch free code to do some simple stuff like conditional addition.
I came up with the following which does work really well. (This was just to implement:
if x<0 then x:=x+a; )
var x,a,maskx : Longword;
begin
...
maskx := sarLongint(x,31);
Result := Result + (a and maskx); // if Result<0 then Result := Result + a;
end;
But then in another function I also nedded to do a similar conditional addition, but on the condition that (x<>0), and that one was a bit more difficult. I can implement it in asm (similar to what I did in the opening post), but that precludes making it inline which kind of defeats the purpose (of having short branch free code).