#ifdef COMPILER_GCC inline dword _byteswap_ulong(dword x) { asm("bswap %0" : "=r" (x) : "0" (x)); return x; } inline uint64 _byteswap_uint64(uint64 x) { asm("bswap %0" : "=r" (x) : "0" (x)); return x; } inline word _byteswap_ushort(word x) { __asm__("xchgb %b0,%h0" : "=q" (x) : "0" (x)); return x; } #endif int MemCmp(const char *a, const char *b, size_t len) { if(((size_t)a & 3) | ((size_t)b & 3)) return memcmp(a, b, len); const dword *x = (dword *)a; const dword *y = (dword *)b; const dword *e = x + (len >> 2); while(x < e) { if(*x != *y) return int(_byteswap_ulong(*x) - _byteswap_ulong(*y)); x++; y++; } 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)); return 0; }
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));
... 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]));
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)); |
... 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]));
Quote: |
Than again if such OS allocators allow to allocate only for example 4kB chunks and not 13 bytes, I think it will never raise exception or crash and you may safely read beyond end of buffer. |