News:

Download Pelles C here: http://www.pellesc.se

Main Menu

Maybe a optimization bug.

Started by ander_cc, April 14, 2026, 03:38:30 PM

Previous topic - Next topic

Vortex

Hi Pelle,

This code below outputs the correct result. Tested with Pelles C v14 :

#include <stdio.h>

int main(void)
{
    int i;
    int s=0;
    for (i=1;i<11;i++)
    {
        s=s+i;
    }
    printf("Sum = %d\n",s);
    return 0;
}

Sum = 55
Optimizations : Maximize speed
Code it... That's all...

John Z


Are you sure because adding the print of i to check shows maybe not-
If i becomes corrupt but it is not looked at - is it really corrupt  ;D
Schrödinger's cat according to quantum theory, i is simultaneously good and bad until viewed
 ;)

#include <stdio.h>

int main(void)
{
    int i;
    int s=0;
    for (i=1;i<11;i++)
    {
        s=s+i;
    }
    printf("i = %d\n",i);
    printf("Sum = %d\n",s);
    return 0;
}

This also appears to be OK as long as not looking at the value of i -
#include <stdio.h>

int main(void)
{
    int i = 1;
    int s = 0;
   
    do {
        s = s + i;
        i++;
    } while (i <= 10);
    printf("i = %d\n", i);
    printf("Sum = %d\n", s);
    return 0;
}

using volatile int i has 'fixed' it every time...

John Z

John Z

Quote from: Pelle on April 18, 2026, 05:52:29 PMI my experience, enabling the optimizer on poorly written C code is more of a problem than the optimizer itself.

I agree with this also. In general I am seeing less and less issues as I program more...

John Z

Vortex

#18
I was too fast, same issue :

_text   SEGMENT PARA 'CODE'

main    PROC
        sub     rsp, 40
        lea     rcx, [@154]
        call    printf    
        lea     rcx, [@156]
        mov     edx, 55   
        call    printf    
        xor     eax, eax  
        add     rsp, 40   
        ret               
main    ENDP

_text   ENDS

.rdata  SEGMENT PARA 'CONST'                          

@156    label byte
        db 53H, 75H, 6DH, 20H, 3DH, 20H, 25H, 64H       ; 0000 _ Sum = %d
        db 0AH, 00H                                  

@154    label byte
        db 69H, 20H, 3DH, 20H, 25H, 64H, 0AH, 00H       ; 000A _ i = %d..

.rdata  ENDS

Again, this line is fixing the issue :

    for (i=1;i<11;i=i-(-1))
Code it... That's all...

John Z

#19
Many variations can complicate the use of i enough that the optimizer does not affect i.

Both of these variations within the loop also 'work' to inhibit the optimization -
s = s + (i*i)/i;
and
s = s + ((i << 1) >> 1);

We can probably find more too, but I'm fairly sure this is not helping -

#include <stdio.h>

int main(void)
{
    int i = 1;
    int s = 0;

    do {
        //s = s + (i*i)/i;         // works
          s = s + ((i << 1) >> 1); // works
        i++;   
    } while (i <= 10);
    printf("i = %d\n", i);
    printf("Sum = %d\n", s);
    return 0;
}

Another 'working' version
#include <stdio.h>

int main(void)
{
    int i = 0;
    int s = 0;

    do {
++i;
s = s + i;
       } while (i <= 9);
    printf("i = %d\n", i);
    printf("Sum = %d\n", s);
    return 0;
}

John Z

TimoVJL

Hi John Z, have a fun with Clang with limits with optimizations.
Optimizations for static code isn't so important and you know reasons for that.
May the source be with you

Vortex

The code below compiled as 32-bit application works fine :

#include <stdio.h>

int main(void)
{
    int i;
    int s=0;
    for (i=1;i<11;)
    {
s=s+i;
__asm inc i;
    }
    printf("i=%d , sum = %d\n",i,s);
    return 0;
}

i=11 , sum = 55
Code it... That's all...