Nicely spotted, I didn't realise the intel/amd distinction ;)

Unless I'm mistaken, it is possible for D to return 'out' parameters by value right? (in additional return registers, no touching the stack?) .. Assuming that's the case you would surely standardise something more like the win32 intrinsic rather than one resembling the PPC opcode.
If the function returns a bool that the value was zero or not, then I think it's fair to say the position is undefined (which supports the intel assertion).
PPC's approach is more cleanly factored into the win32 model than the other way around I think, in terms of allowing the optimiser to trim the unused code. If the intrinsic generates implicit code to produce a bool from the value, it will surely be trimmed by the optimiser if that result is not used.

While cmov might work nicely (although I really don't trust that opcode anyway, an intrinsic like bsr shouldn't be producing a hidden branch) on x86 to produce the PPC result, I'm not sure other architectures would have such a simple solution. Again, I think the win32 approach is easier for all architectures to produce and for the optimiser to truncate if the calculated result is unused.

bool bsf/bsr(int value, out int position); // this assumes that position will cleanly return in a second return register...


On 18 October 2011 22:50, Don <nospam@nospam.com> wrote:
On 18.10.2011 11:43, Manu wrote:
On 18 October 2011 12:12, Don <nospam@nospam.com
<mailto:nospam@nospam.com>> wrote:

   You mean bsr and bsf.
   Unfortunately, there are some big problems with them. What is bsr(0) ?


True ;) .. but that's why the API needs to be defined and standardised.
On PowerPC it returns 32 (or 64), and the x86 version returns 2 values,
the position, and also a bool telling you if it was zero or not (useful
for loop termination)

Even worse -- Intel says that the position value of bsr(0) is undefined. But AMD does define it, they say it's what was in the register before.



I think all hardware that I've seen is easy to factor into the win32
intrinsic api.

That would be nice. What do you think it should do for the zero case?
Note that on x86, one possibility is to do a bsr followed by a cmov, to get the PowerPC semantics.