On Thu, 17 May 2012 21:01:08 -0400, Eric Sosman
<eso...@ieee-dot-org.invalid> wrote:
> If the machine can do arithmetic on oddball types, the compiler
>is permitted to use its special capabilities -- as long as the final
>result is the same as would have been achieved by going through the
>whole conversion/promotion rigamarole. In your case, I think a
>sufficiently smart compiler might have eliminated the test altogether,
>knowing that the high-order zeroes of `*a' could not possibly equal
>the high-order ones of `~*b', so the outcome is foreordained: The
>`!=' comparison will always yield true.
That caught my eye as well. I did notice that the test can't be false
for any value of the arguments.
> For the case at hand, I suggest you try some variant on
>
> if ((*a ^ *b) != 0)
>
>... which (1) works as you desire even in the face of promotions,
>and (2) may give the compiler more opportunity to take shortcuts
>if it feels they are warranted.
That is ingenious and may be the best solution.
With the ARM, the same instruction that plucks the bytes *a or *b from
memory into a 32-bit register also guarantees that the upper 24 bits
are clear. So the compiler doesn't need to insert any additional
adjustment on the load.
I assume that the ARM has an XOR instruction, so it would just XOR the
bytes loaded into 32-bit registers.
Here are the results.
EXPERIMENT #1
;;;723 if ((unsigned char)(*a) != (unsigned char)(~(*b)))
0001b2 7828 LDRB r0,[r5,#0] ;723
0001b4 7827 LDRB r7,[r4,#0] ;723
0001b6 43ff MVNS r7,r7 ;723
0001b8 b2ff UXTB r7,r7 ;723
0001ba 42b8 CMP r0,r7 ;723
0001bc d001 BEQ |L1.450|
EXPERIMENT #2
;;;715 if (*a != ((~(*b)) & 0x000000FF))
0001b2 7828 LDRB r0,[r5,#0] ;715
0001b4 7827 LDRB r7,[r4,#0] ;715
0001b6 43ff MVNS r7,r7 ;715
0001b8 b2ff UXTB r7,r7 ;715
0001ba 42b8 CMP r0,r7 ;715
0001bc d001 BEQ |L1.450|
EXPERIMENT #3
;;;715 if (*a ^ *b)
0001b2 7820 LDRB r0,[r4,#0] ;715
0001b4 782f LDRB r7,[r5,#0] ;715
0001b6 4078 EORS r0,r0,r7 ;715
0001b8 d001 BEQ |L1.446|
Your method saved two instructions.
Clearly, you've had training in the teachings of Mr. Boole and Mr.
Karnaugh ...
Thanks for the clarification and implementation suggestion.
Dave A.