Robert C. Seacord wrote:
Gerald,
There was a report (forwarded by Mark Mitchell) of Microsoft
Visual C++ 2005 performing that optimization (the resultant
object code was shown). Have you verified that this report
was false?
both chad and i have tested this with various options on Visual C++ 2005
and we have not found any combination of options that will cause this
optimization to occur.
I have not personally confirmed the VC++ 2005 report. However, the code
that I posted was pretty explicit about the optimization options used
and the resulting code. I've attached the entire listing here to try to
give you more information; it includes the exact version of the compiler
and shows the source code tested in comments.
Another user wrote to me regarding an unnamed version of MSVC that:
It appears that when adding large values (like 0xf0000000)
the if() check isn't optimized away, but for smaller
values it is (same as with gcc).
Small test:
char *buf="test";
unsigned int len = 0x40000000;
if(buf+len < buf) {
LOG_MSG("1");
} else {
LOG_MSG("2");
}
It doesn't matter if len is int or unsigned int.
A maybe relevant thing is that everything up to
0x7fffffff for len will trigger the optimization,
whereas larger values (larger in terms of unsigned
32bit int) don't.
Presumably, when MSVC thinks the value is negative (when cast to a
signed type), then it doesn't do the optimization, but, for positive
values it does.
--
Mark Mitchell
CodeSourcery
[EMAIL PROTECTED]
(650) 331-3385 x713
; Listing generated by Microsoft (R) Optimizing Compiler Version
14.00.50727.762
TITLE d:\src\prova\cert\cert.cpp
.686P
.XMM
include listing.inc
.model flat
$SG-5 DB 00H
PUBLIC ?f@@[EMAIL PROTECTED] ; f
; Function compile flags: /Ogtpy
; File d:\src\prova\cert\cert.cpp
_TEXT SEGMENT
?f@@[EMAIL PROTECTED] PROC ; f
; 2 : len = 1 << 30;
; 3 : if (buf + len < buf)
; 4 : return 1;
; 5 :
; 6 :
; 7 : return 0;
xor eax, eax
; 8 : }
ret 0
?f@@[EMAIL PROTECTED] ENDP ; f
_TEXT ENDS
PUBLIC _main
; Function compile flags: /Ogtpy
_TEXT SEGMENT
_main PROC
; 12 : char * b = "";
; 13 : for ( int l = 0; l < 1 << 30; ++l )
; 14 : f(b, l);
; 15 : }
xor eax, eax
ret 0
_main ENDP
_TEXT ENDS
END