NO

Author Topic: Simple argument bug  (Read 2167 times)

Offline Tom

  • Member
  • *
  • Posts: 2
Simple argument bug
« on: May 15, 2020, 06:11:51 PM »
This is so elementary, that I'm sure someone must have reported it at some point...
However, could not find a matching report at a quick glance, so might as well push it here, even if it ends up being a duplicate and wasting everyones time.

Here is a tiny program that demonstrates the issue:

Code: [Select]


#include <stdio.h>

struct Two_Floats
{
float hi;
float lo;
};

struct Two_Floats broken_Function( struct  Two_Floats clips ,  struct Two_Floats values )
{
if( values.hi <= clips.hi ) values.hi = clips.hi;
if( values.lo >= clips.lo ) values.lo = clips.lo;
return values;
}

struct Two_Floats working_Function( struct  Two_Floats clips ,  struct Two_Floats values )
{
struct Two_Floats temporary = values;

if( temporary.hi <= clips.hi ) temporary.hi = clips.hi;
if( temporary.lo >= clips.lo ) temporary.lo = clips.lo;
return temporary;
}

int main( void )
{
struct Two_Floats  values = { -50.0f , 25.0f  };
struct Two_Floats  limits = {   0.0f , 600.0f };

struct Two_Floats  broken =  broken_Function( limits , values );
struct Two_Floats working = working_Function( limits , values );

printf("And here we go: < %f , %f > \n", broken.hi  , broken.lo  );
printf("This was to be: < %f , %f > \n", working.hi , working.lo );
return 0;
}

Output:
Code: [Select]
And here we go: < -50.000000 , 25.000000 >
This was to be: < 0.000000 , 25.000000 >
Press any key to continue...

Modifying the the pass-by-value argument and returning it gives a wrong result.
Taking a temporary copy and returning it, won't.

Using version 9.009 at windows 10.
Same behavior occurs on both default debug and release configuration, with fast and strict floating point settings.

Peculiar.

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Simple argument bug
« Reply #1 on: May 16, 2020, 03:54:23 PM »
Bug verified.
The problem doesn't seem related to arguments pass, but to code generation bug. This suspect is enforced due to the problem persistence even when compiling with different optimizations, include no optimization at all.
As showed in the image attached the whole swapping code for the first element is not emitted.
Indeed the reverse works. I.e. reversing the structure element to swap as in:
Code: [Select]
int main(void)
{
struct Two_Floats values = { -50.0f, 25.0f };
// struct Two_Floats values = { 50.0f, 625.0f };
struct Two_Floats limits = { 0.0f, 600.0f };

struct Two_Floats broken = broken_Function(limits, values);
struct Two_Floats working = working_Function(limits, values);

printf("And here we go: < %f , %f > \n", broken.hi, broken.lo);
printf("This was to be: < %f , %f > \n", working.hi, working.lo);
return 0;
}
Demonstrating that the code for the second element swap is correctly emitted and the parameter passage and return value works correctly.
As a consequence the code generator is buggy steady to not emit due code.
« Last Edit: May 16, 2020, 04:04:53 PM by frankie »
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Re: Simple argument bug
« Reply #2 on: May 17, 2020, 11:33:03 PM »
It works with the upcoming version, so I''m not going to dig into this any further...
/Pelle

gjacc

  • Guest
Re: Simple argument bug
« Reply #3 on: May 22, 2020, 10:20:46 AM »
On my PC (Win 10) the ouput is :

And here we go: < 0.000000 , 25.000000 >
This was to be: < 0.000000 , 25.000000 >
Press any key to continue...