https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120658
Bug ID: 120658 Summary: OPTIMIZATION: STRING HANDLING: wrong results under exotic conditions. Product: gcc Version: 14.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: newbie-02 at gmx dot de Target Milestone: --- Created attachment 61640 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=61640&action=edit The program which likes to fail, a simple reproducer. hi, I stumbled into the most confusing problem I have ever worked on, a "glitch in the matrix" or the "blackest swan ever"? I hope it's gcc and suitable to report here. In combination of: - a certain conversion routine for integers ( TO_BASE ), - executing it in a benchmarking loop ( TIMEITcp ), - compilation with -O2, -O3 or -Ofast optimization, - assignment of the result to a variable with more than 256 bytes and - in a certain call sequence ... the results break away from meaningful: ``` . compiled with -O1 ( sensitive optimization ) : . TO_BASE( (uint)x1i, 10 ) : 4287654321 00120792; 120792.000; 1000; '4287654321; str4 = TO_BASE( (uint)x1i, root ); TO_BASE( (uint)x1i, 11 ) : 19002A6175 00120655; 120655.000; 1000; '19002A6175; str4 = TO_BASE( (uint)x1i, root ); TO_BASE( (uint)x1i, 10 ) : 4287654321 00121041; 121041.000; 1000; '4287654321; str4 = TO_BASE( (uint)x1i, root ); 00122110; 122110.000; 1000; '4287654321; str5 = TO_BASE( (uint)x1i, root ); TO_BASE( (uint)x1i, 11 ) : 19002A6175 00120799; 120799.000; 1000; '19002A6175; str4 = TO_BASE( (uint)x1i, root ); 00121957; 121957.000; 1000; '19002A6175; str5 = TO_BASE( (uint)x1i, root ); ver. 01_01_03 ``` to meaningless e.g. ( the details of fail may vary from compilation to compilation ): ``` . compiled with -O2 ( most used balanced optimization ) : . TO_BASE( (uint)x1i, 10 ) : 4287654321 ; str4 = TO_BASE( (uint)x1i, root ); 21 ; str5 = TO_BASE( (uint)x1i, root ); 21 TO_BASE( (uint)x1i, 11 ) : 19002A6175 ; str4 = TO_BASE( (uint)x1i, root ); 75 ; str5 = TO_BASE( (uint)x1i, root ); 75 TO_BASE( (uint)x1i, 10 ) : 4287654321 ; str4 = TO_BASE( (uint)x1i, root ); 21 ; str5 = TO_BASE( (uint)x1i, root ); 21 TO_BASE( (uint)x1i, 11 ) : 19002A6175 ; str4 = TO_BASE( (uint)x1i, root ); 75 ; str5 = TO_BASE( (uint)x1i, root ); 75 ver. 01_01_03 ``` Which IMHO shouldn't be like that, small math inaccuracies are expected with optimizations, however not crippled strings or wrong returns. I've done what I can do, pinned it down to a comfortable small stable reproducer. Attached a program and a shell script ( in next comment ) for different compilations, put both into an empty! directory and start them with `./bin_dec_conversion_run.sh 1000`. The issue seems not dependent on the c version used, I tried c99, c11, c17 and c23, I didn't yet try if it occurs in C++. The program as attached produces fail - here -, outcommenting line 160 ( the first TIMEIT call with str5 = TO_BASE ) or using the "alternate I" or "alternate II" returns seem to heal, however the root of evil is still alive and will strike with other changes at other places. Alternate to this case, where it even slams away the print of the timing, I have observed swallowing the last digit of the converted value, returning trailing garbage, or even segfaults popping up with other little changes. There are three conversion warnings in compilation, I did not change that to keep these parts of the code identical to where they are borrowed from. I'm curious if someone can dig this down ??? Even if the result might be "me bad", which alas ever is an option. Environment: Kali ( Debian ) Linux, gcc (Debian 14.2.0-16) 14.2.0