On 10/10/2013 11:44 PM, Richard Yao wrote:
> On 10/10/2013 11:38 PM, Richard Yao wrote:
>> On 10/10/2013 11:29 PM, Xin Li wrote:
>>> On 10/10/13 20:18, Richard Yao wrote:
>>>> Thanks for letting us know about this. I have a few comments:
>>>
>>>> 1. We could eliminate a branch entirely by doing this:
>>>
>>>> mlen = MIN(d_end - dst, mlen); while (--mlen >= 0) *dst++ = *cpy++
>>>
>>> I don't think this eliminates the branching as MIN is usually a macro
>>> that expands to a > b ? b : a.
>>
>> My mistake. I was thinking of generic swap routines. I do think that
>> using the MIN() macro is more readable though.
> 
> On second thought, I was right the first time. It is possible to do this
> without branching:
> 
> #define MIN(x, y) ((y) ^ (((x) ^ (y)) & -((x) < (y))))
> #define MIN(x, y) ((x) ^ (((x) ^ (y)) & -((x) < (y))))
> 
> http://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax
> 
> This makes MIN(d_end - dst, mlen) look inefficient, but a proper
> optimizing compiler should store the result of d_end - dst in a register
> to avoid doing the subtraction 3 times.

In hindsight, I should have left this for tomorrow before I replied. It
is close to midnight and it shows in the number of errors in what I write.

In the above email, the second MIN macro was actually a MAX macro and
that this trick only works with signed integers. Also, a good optimizing
compiler should be able to recognize MIN/MAX and generate something like
this when it is beneficial; that makes it unnecessary to worry too much
about this.

Received: from [140.211.166.183] (helo=smtp.gentoo.org)
        by node002.open-zfs.net
        with esmtp (HybridCluster distributed mail proxy)
        (envelope-from <[email protected]>); Fri, 11 Oct 2013 03:52:30 -0000
With that said, using the MIN macro as I suggested makes the code more
readable.

Attachment: signature.asc
Description: OpenPGP digital signature

_______________________________________________
developer mailing list
[email protected]
http://lists.open-zfs.org/mailman/listinfo/developer

Reply via email to