DOCUMENT:Q194608 07-MAY-2001 [visualc] TITLE :FIX: Incorrect Code Generated with /Og Optimization PRODUCT :Microsoft C Compiler PROD/VER::5.0,6.0 OPER/SYS: KEYWORDS:kbCodeGen kbCompiler kbVC500bug kbVC600bug kbVC600fix ====================================================================== ------------------------------------------------------------------------------- The information in this article applies to: - Microsoft Visual C++, 32-bit Enterprise Edition, versions 5.0, 6.0 - Microsoft Visual C++, 32-bit Professional Edition, versions 5.0, 6.0 - Microsoft Visual C++, 32-bit Learning Edition, version 6.0 ------------------------------------------------------------------------------- SYMPTOMS ======== A code sequence similar to the following generates incorrect code when compiled with global (/Og) optimization: ( ( ConstantValue << Variable ) + OptimizableValue ); RESOLUTION ========== You can work around this problem by removing global optimization from the command line or by disabling global optimization on a per-function basis with the optimize pragma. Another workaround is to simplify the code and use a temporary global variable as illustrated in the "More Information" section of this article. STATUS ====== Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. This problem was corrected in Visual C++ version 6.0, Service Pack 3 (SP3). MORE INFORMATION ================ The following code outputs 0 if compiled with /Og and 1 if compiled with /Od. Sample Code Workaround One -------------------------- #include #define SIZE (unsigned int)512 static unsigned int new_depth = 1; // #pragma optimize("g",off) // Uncomment for Workaround One. int main() { unsigned int result = ( ( 8 << new_depth ) + SIZE - 1 ) / SIZE; std::cout << "Result = " << result ; return 0; } // #pragma optimize("g",on) // Uncomment for Workaround One. Sample Code Workaround Two -------------------------- #include #define SIZE (unsigned int)512 static unsigned int new_depth = 1; unsigned int result1; int main() { result1 = ( 8 << new_depth ); unsigned int result2 = ( ( result1 + SIZE - 1 ) / SIZE ); std::cout << "Result = " << result2 ; return 0; } REFERENCES ========== service pack 3 sp3 Additional query words: compiler ====================================================================== Keywords : kbCodeGen kbCompiler kbVC500bug kbVC600bug kbVC600fix Technology : kbVCsearch kbAudDeveloper kbVC500 kbVC600 kbVC32bitSearch kbVC500Search Version : :5.0,6.0 Issue type : kbbug Solution Type : kbfix ============================================================================= THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY. Copyright Microsoft Corporation 2001.