NO

Author Topic: Assignment and Ternary Operator  (Read 20560 times)

JohnF

  • Guest
Re: Assignment and Ternary Operator
« Reply #15 on: September 15, 2010, 05:03:58 PM »
Frankie, here's your code with just a few lines commented out. Run it and see the answer.

Code: [Select]
#include <stdio.h>
#include <stdlib.h>

#define MIN(x,y) ( (x<y) ? (x) : (y) )
#define MAX(x,y) ( (x>y) ? (x) : (y) )

#define NELEMS 50

double vector[NELEMS];

int main(void)
{
double min1,min2,min3, max1,max2,max3, minx=0.0, maxx=0.0;

for(int i=0;i<NELEMS;i++)
// vector[i]=(double) (rand())/RAND_MAX*100;
vector[i] = (double) i * 3.55;


min1=min2=min3=vector[0];
max1=max2=max3=vector[0];

double x = 0.0;

for(int i=1;i<NELEMS;i++) {

x = vector[i];

// CASE 1: this code doesn´t work *********************
// min1=MIN(min1, x);
// max1=MAX(max1, x);

// Case 2: neither this *******************************
    min2 = (min2 > x ? x : min2);
    max2 = (max2 < x ? x : max2);

// Case 3: but this works *****************************
// if( min3 > x ) min3 = x;
// if( max3 < x ) max3 = x;

}

printf("\n\n");
// printf("Case 1 -> MIN =%8.4f MAX =%8.4f\n",min1,max1);
printf("Case 2 -> MIN =%8.4f MAX =%8.4f\n",min2,max2);
// printf("Case 3 -> MIN =%8.4f MAX =%8.4f\n",min3,max3);
             printf("\n\n");

return EXIT_SUCCESS;
}

I get the same as I posted earlier.

John

CommonTater

  • Guest
Re: Assignment and Ternary Operator
« Reply #16 on: September 15, 2010, 05:22:48 PM »
FWIW... my results coincide with Frankie's ...


JohnF

  • Guest
Re: Assignment and Ternary Operator
« Reply #17 on: September 15, 2010, 05:34:23 PM »
Have you run it with maximum speed optimisation? With the code that I posted last.

EDIT: Also 'minimise size' has the problem.

John
« Last Edit: September 15, 2010, 05:55:15 PM by JohnF »

eheredia2511

  • Guest
Re: Assignment and Ternary Operator
« Reply #18 on: September 15, 2010, 06:09:55 PM »
I get the following answers from the code modified to show the preloaded values:

Clip 1.png -> Case 1
Clip 2.png -> Case 2
Clip 3.png -> Case 3

The version of Pelles C is 6.00.4 and I use the default options to compile and execute.

I think this is a bug!

[]s.

« Last Edit: September 15, 2010, 06:17:51 PM by eheredia2511 »

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Assignment and Ternary Operator
« Reply #19 on: September 15, 2010, 06:18:14 PM »
Yup John, you are right!
The compiler doesn't preserve the value of the loop variable 'i', and make a single looping.
Another bug of the new register allocator  :'(.
This revision of the compiler, while much more efficient of the previous release 5.00, is really buggy!!
Pelle have to rework completely the register allocator.
To make it work I had to declare the variable 'i' as volatile. You can check the code below (still another variation). If you remove the qualifier 'volatile' the problem reappears.
Code: [Select]
#include <stdio.h>
#include <stdlib.h>

#define MIN(x,y) ( (x<y) ? (x) : (y) )
#define MAX(x,y) ( (x>y) ? (x) : (y) )

#define NELEMS 50

double vector[NELEMS];

int main(void)
{
double min1,min2,min3, max1,max2,max3, minx=0.0, maxx=0.0;
volatile int i;

// for(int i=0;i<NELEMS;i++)
for(i=0;i<NELEMS;i++)
// vector[i]=(double) (rand())/RAND_MAX*100;
vector[i] = (double) i * 3.55;


min1=min2=min3=vector[0];
max1=max2=max3=vector[0];

double x = 0.0;

//for(int i=1;i<NELEMS;i++) {
for(i=1;i<NELEMS;i++) {

x = vector[i];

// CASE 1: this code doesn´t work *********************
//min1=MIN(min1, x);
//max1=MAX(max1, x);

// Case 2: neither this *******************************
    min2 = (min2 > x ? x : min2);
    max2 = (max2 < x ? x : max2);

// Case 3: but this works *****************************
//if( min3 > x ) min3 = x;
//if( max3 < x ) max3 = x;

}

printf("\n\n");
// printf("Case 1 -> MIN =%8.4f MAX =%8.4f\n",min1,max1);
printf("Case 2 -> MIN =%8.4f MAX =%8.4f\n",min2,max2);
// printf("Case 3 -> MIN =%8.4f MAX =%8.4f\n",min3,max3);
             printf("\n\n");

return EXIT_SUCCESS;
}
« Last Edit: September 15, 2010, 06:20:50 PM by frankie »
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

eheredia2511

  • Guest
Re: Assignment and Ternary Operator
« Reply #20 on: September 15, 2010, 06:32:47 PM »
Removing the sentence
Code: [Select]
x = vetor[i] and refactoring the code to use the expression
Code: [Select]
vetor[i] with the ternary operator, as in
Code: [Select]
min1=MIN(min1, vetor[i]), the execution raises an exception.

[]s
« Last Edit: September 15, 2010, 06:38:51 PM by eheredia2511 »

CommonTater

  • Guest
Re: Assignment and Ternary Operator
« Reply #21 on: September 15, 2010, 10:26:33 PM »
So it looks like there's rather nasty bug in 6.0.. is it only in the .4 version? I have .3 and haven't had the problem, that I know of...

Thing is, I'm mid stream in developing some very complex code and if there is a problem, I need to know now, while it's still confined to my machine.




JohnF

  • Guest
Re: Assignment and Ternary Operator
« Reply #22 on: September 16, 2010, 07:10:51 AM »
The problem is present for all incarnations of version 6.

Use version 5.

John

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Assignment and Ternary Operator
« Reply #23 on: September 16, 2010, 11:04:22 AM »
Sad to say, but V 6.00 could really make a big mess....  :'(
My personal suggestion is to use V5.00 Stable for development, and only when everything is fully tested and debugged give a try with V6.00 to eventually take advantage of the much more code generation efficiency of the latter.
I suspect that most of the problems come from the newly introduced register allocator, which mainly developed to take advantage from the large register set of the 64bit CPU, fails to consider the interactions between composite registers, like ax:de and other, that happens in the more limited 32bits mode.

P.S. the last problem reported by eheredia2511 is the same that John outlined. The 'i' variable, held in ax register, is not preserved so when program try to access to value vector(i) the calculated displacement points out of memory allocated to the process, and this raises the exception for invalid memory access.

***Anyway*** it is very important to continue to check and report the problems from V6.00 to help Pelle to fix the compiler for the more or less announced version 6.50, that now becomes really a must!
« Last Edit: September 16, 2010, 11:17:40 AM by frankie »
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

CommonTater

  • Guest
Re: Assignment and Ternary Operator
« Reply #24 on: September 16, 2010, 03:01:33 PM »
Oh crap... Thanks guys, I'm very happy this was discovered although it's about the last thing I wanted to hear.

Now comes the big upheaval as I revert to 5.00.


CommonTater

  • Guest
Re: Assignment and Ternary Operator
« Reply #25 on: September 16, 2010, 08:16:44 PM »
A question... Is is possible to use the 5.0 ide and compiler with the version 6 headers and libraries?

If not, I'm in for a pretty big re-write...



JohnF

  • Guest
Re: Assignment and Ternary Operator
« Reply #26 on: September 16, 2010, 08:20:18 PM »
Try it, and let us know. :)

John

CommonTater

  • Guest
Re: Assignment and Ternary Operator
« Reply #27 on: September 17, 2010, 12:51:13 AM »
Oh gosh... somehow that wasn't quite the answer I was looking for John...

But... will do and will do....

CommonTater

  • Guest
Re: Assignment and Ternary Operator
« Reply #28 on: September 17, 2010, 02:07:00 AM »
Ok, the best I can figure is that without some serious header editing, you're best to stay with the header and library set installed with the IDE. 

What I did was to snag a copy of the 6.00.03 headers and libraries and headers, copied them to a different drive (My projects drive) and then uninstalled the 6.00 setup.  Next I installed the 5.00.8 setup I have here and redirected the tools/folders to the version 6 headers... No dice, compile errors all over the place.

I don't know if this is easily fixable or not... but I really don't have the time to mess with it.  I'm going to have my hands full getting my project working on the 5.00 setup...












Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Assignment and Ternary Operator
« Reply #29 on: September 17, 2010, 10:58:03 AM »
CommonTater which errors you have?
The compiler shouldn't be sensitive to header or libraries versions.
Can you supply us more detail on the errors and so?
Anyway I suggest to act the reverse, I mean copy only the 5.00 compiler to the 6.00.
Try this make a full 6.00 installation, then make the same for version 5.00 on another drive or location, then copy pocc.exe in the 6.00 directory and remove the 5.00 installation.
This way only some pragmas shouldn't be recognized, but the rest should work.
I personally want to give a try as soon as I have time this weekend. If you test it let us know.
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide