Home » Extra libraries, Code snippets, applications etc. » C++ language problems and code snippets » Optimized memcmp for x86
Re: Optimized memcmp for x86 [message #14335 is a reply to message #14308] |
Fri, 22 February 2008 19:08 |
mr_ped
Messages: 825 Registered: November 2005 Location: Czech Republic - Praha
|
Experienced Contributor |
|
|
luzr wrote on Fri, 22 February 2008 11:07 |
if(len & 2)
if(*(word *)x != *(word *)y)
return int(_byteswap_ushort(*(word *)x) - _byteswap_ushort(*(word *)y));
if(len & 1)
return int(*((byte *)x + 2)) - int(*((byte *)y + 2));
|
I don't get this end.
switch (len & 3)
0: it looks ok to me.
1: the return int(*((byte *)x)) - int(*((byte *)y)); should be returned?
2: looks ok
3: looks ok
I would maybe try masking out unused bytes, but that would lead to read out of buffer boundary. Is it safe?
I mean something like this
...
const static dword masks[4] = { 0x00000000, 0x000000FF, 0x0000FFFF, 0x00FFFFFF }; //Intel-like endian only!
return int(_byteswap_ulong(*x & masks[len&3]) - _byteswap_ulong(*y & masks[len&3]));
I'm not sure I got the byteswap purpose correctly, but I think I got, so my code is probably ok (but I didn't test it).
Of course it reads beyond buffer end, so you need to know it will not raise exception or crash the application on target platform.
[Updated on: Fri, 22 February 2008 19:11] Report message to a moderator
|
|
|
Goto Forum:
Current Time: Sat May 04 09:46:54 CEST 2024
Total time taken to generate the page: 0.02754 seconds
|