If I don't need the overhead needed to call strcmp, I compare strings with short string literals, as described in the following code example:
#ifdef LITTLE_ENDIAN
gcc compiles this example while I compile without optimization options enabled. When optimization is enabled, I get a warning:
"dereferenced pointer type will violate strict anti-aliasing rules"
Even optimization with -O3 does not lead to efficient code, as the example shows:
//abstest.c
objdump -d -Mintel abstest:
080483d0 <AbsCompare1>: 80483d0: 55 push ebp 80483d1: 89 e5 mov ebp,esp 80483d3: 8b 45 08 mov eax,DWORD PTR [ebp+0x8] 80483d6: 5d pop ebp 80483d7: 81 38 61 62 73 00 cmp DWORD PTR [eax],0x736261 80483dd: 0f 94 c0 sete al 80483e0: 0f b6 c0 movzx eax,al 80483e3: c3 ret 080483f0 <AbsCompare2>: 80483f0: 55 push ebp 80483f1: 0f b6 0d 5c 85 04 08 movzx ecx,BYTE PTR ds:0x804855c 80483f8: 89 e5 mov ebp,esp 80483fa: 8b 55 08 mov edx,DWORD PTR [ebp+0x8] 80483fd: 0f b6 02 movzx eax,BYTE PTR [edx] 8048400: 29 c8 sub eax,ecx 8048402: 75 2b jne 804842f <AbsCompare2+0x3f> 8048404: 0f b6 42 01 movzx eax,BYTE PTR [edx+0x1] 8048408: 0f b6 0d 5d 85 04 08 movzx ecx,BYTE PTR ds:0x804855d 804840f: 29 c8 sub eax,ecx 8048411: 75 1c jne 804842f <AbsCompare2+0x3f> 8048413: 0f b6 42 02 movzx eax,BYTE PTR [edx+0x2] 8048417: 0f b6 0d 5e 85 04 08 movzx ecx,BYTE PTR ds:0x804855e 804841e: 29 c8 sub eax,ecx 8048420: 75 0d jne 804842f <AbsCompare2+0x3f> 8048422: 0f b6 42 03 movzx eax,BYTE PTR [edx+0x3] 8048426: 0f b6 15 5f 85 04 08 movzx edx,BYTE PTR ds:0x804855f 804842d: 29 d0 sub eax,edx 804842f: 5d pop ebp 8048430: c3 ret
Is it possible to directly compare this short literal without bypassing the embedding of chr_p in the union, especially because I want to compare chr_p with arbitrary indices like "& chr_p [1]"?
source share