Performance improvement to strnlen().

Florent Peterschmitt florent at peterschmitt.fr
Mon May 27 09:43:01 UTC 2013


Le 27/05/2013 10:37, Václav Zeman a écrit :
> On 26 May 2013 21:01, Lee Thomas wrote:
>> On 2013-05-26 08:00, Václav Zeman wrote:
>>>
>>> On 05/25/2013 10:27 PM, Lee Thomas wrote:
>>>>
>>>> +       lp = (const unsigned long *)((uintptr_t)str & ~LONGPTR_MASK);
>>>> +       va = (*lp - mask01);
>>>> +       vb = ((~*lp) & mask80);
>>>
>>> I do not think that this correct C. This is type punning violating the
>>> rules of the language.
>>
>>
>> Hello Václav,
>>
>> The aliasing here is safe, because there are no writes through either of the
>> pointers, and the reads are correctly aligned.
> I disagree. IANALL but AFAIK, this is simply not allowed by the
> language => UB => even though it seems to work in this instance, you
> are just lucky the UB is actually doing what you expect.

In that case we should rewrite strlen, it's the same code. That doesn't
mean it's a good code but I really don't think it's bad. Using
signedness is totally valid and what is done here appears valid too.

> --
> VZ

-- 
Florent Peterschmitt           |  /"\ ASCII Ribbon Campaign
florent at peterschmitt.fr        |  \ / - No HTML/RTF in E-mail
+33 (0)6 64 33 97 92           |   X  - No proprietary attachments
http://florent.peterschmitt.fr |  / \ - Respect for open standards

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 551 bytes
Desc: OpenPGP digital signature
URL: <http://lists.freebsd.org/pipermail/freebsd-hackers/attachments/20130527/04a8a95a/attachment.sig>


More information about the freebsd-hackers mailing list