Help needed (understand C function)

S

sandeep

Hi

I have the code below

I do not know if the code section in comments " CAN THE
FOLLOWING CODE EVER BE CALLED ? " is really called

Any help is appreciated. Thanks

---------------------------------------------------------------------

static void E0001(int IENTRY,int *status,double *x,double *fx,
double *xlo,double *xhi,unsigned long *qleft,
unsigned long *qhi,double *zabstl,double *zreltl,
double *zxhi,double *zxlo)
{
#define ftol(zx) (0.5e0*fifdmax1(abstol,reltol*fabs((zx))))
static double a,abstol,b,c,d,fa,fb,fc,fd,fda,fdb,m,mb,p,q,reltol,tol,w,xxhi,xxlo;
static int ext,i99999;
static unsigned long first,qrzero;
switch(IENTRY){case 0: goto DZROR; case 1: goto DSTZR;}
DZROR:
if(*status > 0) goto S280;
*xlo = xxlo;
*xhi = xxhi;
b = *x = *xlo;

i99999 = 1;
goto S270;
/* CAN THE FOLLOWING CODE EVER BE CALLED ? */
S10:
fb = *fx;
*xlo = *xhi;
a = *x = *xlo;
i99999 = 2;
goto S270;
S20:
if(!(fb < 0.0e0)) goto S40;
if(!(*fx < 0.0e0)) goto S30;
*status = -1;
*qleft = *fx < fb;
*qhi = 0;
return;
S40:
S30:
if(!(fb > 0.0e0)) goto S60;
if(!(*fx > 0.0e0)) goto S50;
*status = -1;
*qleft = *fx > fb;
*qhi = 1;
return;
S60:
S50:
fa = *fx;
first = 1;
S70:
c = a;
fc = fa;
ext = 0;
S80:
if(!(fabs(fc) < fabs(fb))) goto S100;
if(!(c != a)) goto S90;
d = a;
fd = fa;
S90:
a = b;
fa = fb;
*xlo = c;
b = *xlo;
fb = fc;
c = a;
fc = fa;
S100:
tol = ftol(*xlo);
m = (c+b)*.5e0;
mb = m-b;
if(!(fabs(mb) > tol)) goto S240;
if(!(ext > 3)) goto S110;
w = mb;
goto S190;
S110:
tol = fifdsign(tol,mb);
p = (b-a)*fb;
if(!first) goto S120;
q = fa-fb;
first = 0;
goto S130;
S120:
fdb = (fd-fb)/(d-b);
fda = (fd-fa)/(d-a);
p = fda*p;
q = fdb*fa-fda*fb;
S130:
if(!(p < 0.0e0)) goto S140;
p = -p;
q = -q;
S140:
if(ext == 3) p *= 2.0e0;
if(!(p*1.0e0 == 0.0e0 || p <= q*tol)) goto S150;
w = tol;
goto S180;
S150:
if(!(p < mb*q)) goto S160;
w = p/q;
goto S170;
S160:
w = mb;
S190:
S180:
S170:
d = a;
fd = fa;
a = b;
fa = fb;
b += w;
*xlo = b;
*x = *xlo;
i99999 = 3;
goto S270;
S200:
fb = *fx;
if(!(fc*fb >= 0.0e0)) goto S210;
goto S70;
S210:
if(!(w == mb)) goto S220;
ext = 0;
goto S230;
S220:
ext += 1;
S230:
goto S80;
S240:
*xhi = c;
qrzero = fc >= 0.0e0 && fb <= 0.0e0 || fc < 0.0e0 && fb >= 0.0e0;
if(!qrzero) goto S250;
*status = 0;
goto S260;
S250:
*status = -1;
S260:
return;
/* END OF CAN THE ABOVE CODES BE CALLED? */

DSTZR:
xxlo = *zxlo;
xxhi = *zxhi;
abstol = *zabstl;
reltol = *zreltl;
return;
S270:
*status = 1;
return;
S280:
switch((int)i99999){case 1: goto S10;case 2: goto S20;case 3: goto
S200;
default: break;}
#undef ftol

}
 
L

luserXtrog

Hi

I have the code below

I do not know if the code section in comments " CAN THE
FOLLOWING CODE EVER BE CALLED ? " is really called

Any help is appreciated. Thanks

---------------------------------------------------------------------

static void E0001(int IENTRY,int *status,double *x,double *fx,
                  double *xlo,double *xhi,unsigned long *qleft,
                  unsigned long *qhi,double *zabstl,double *zreltl,
                  double *zxhi,double *zxlo)
{
#define ftol(zx) (0.5e0*fifdmax1(abstol,reltol*fabs((zx))))
static double a,abstol,b,c,d,fa,fb,fc,fd,fda,fdb,m,mb,p,q,reltol,tol,w,xxhi,xxlo;
static int ext,i99999;
static unsigned long first,qrzero;
    switch(IENTRY){case 0: goto DZROR; case 1: goto DSTZR;}
DZROR:
    if(*status > 0) goto S280;
    *xlo = xxlo;
    *xhi = xxhi;
    b = *x = *xlo;

    i99999 = 1;
    goto S270;
/* CAN THE FOLLOWING CODE EVER BE CALLED ? */
S10:
    fb = *fx;
    *xlo = *xhi;
    a = *x = *xlo;
     i99999 = 2;
    goto S270;
S20:
     if(!(fb < 0.0e0)) goto S40;
    if(!(*fx < 0.0e0)) goto S30;
    *status = -1;
    *qleft = *fx < fb;
    *qhi = 0;
    return;
S40:
S30:
    if(!(fb > 0.0e0)) goto S60;
    if(!(*fx > 0.0e0)) goto S50;
    *status = -1;
    *qleft = *fx > fb;
    *qhi = 1;
    return;
S60:
S50:
    fa = *fx;
    first = 1;
S70:
    c = a;
    fc = fa;
    ext = 0;
S80:
    if(!(fabs(fc) < fabs(fb))) goto S100;
    if(!(c != a)) goto S90;
    d = a;
    fd = fa;
S90:
    a = b;
    fa = fb;
    *xlo = c;
    b = *xlo;
    fb = fc;
    c = a;
    fc = fa;
S100:
    tol = ftol(*xlo);
    m = (c+b)*.5e0;
    mb = m-b;
    if(!(fabs(mb) > tol)) goto S240;
    if(!(ext > 3)) goto S110;
    w = mb;
    goto S190;
S110:
    tol = fifdsign(tol,mb);
    p = (b-a)*fb;
    if(!first) goto S120;
    q = fa-fb;
    first = 0;
    goto S130;
S120:
    fdb = (fd-fb)/(d-b);
    fda = (fd-fa)/(d-a);
    p = fda*p;
    q = fdb*fa-fda*fb;
S130:
    if(!(p < 0.0e0)) goto S140;
    p = -p;
    q = -q;
S140:
    if(ext == 3) p *= 2.0e0;
    if(!(p*1.0e0 == 0.0e0 || p <= q*tol)) goto S150;
    w = tol;
    goto S180;
S150:
    if(!(p < mb*q)) goto S160;
    w = p/q;
    goto S170;
S160:
    w = mb;
S190:
S180:
S170:
    d = a;
    fd = fa;
    a = b;
    fa = fb;
    b += w;
    *xlo = b;
    *x = *xlo;
   i99999 = 3;
    goto S270;
S200:
    fb = *fx;
    if(!(fc*fb >= 0.0e0)) goto S210;
    goto S70;
S210:
    if(!(w == mb)) goto S220;
    ext = 0;
    goto S230;
S220:
    ext += 1;
S230:
    goto S80;
S240:
    *xhi = c;
    qrzero = fc >= 0.0e0 && fb <= 0.0e0 || fc < 0.0e0 && fb >= 0.0e0;
    if(!qrzero) goto S250;
    *status = 0;
    goto S260;
S250:
    *status = -1;
S260:
    return;
/* END OF CAN THE ABOVE CODES BE CALLED? */

DSTZR:
    xxlo = *zxlo;
    xxhi = *zxhi;
    abstol = *zabstl;
    reltol = *zreltl;
    return;
S270:
    *status = 1;
    return;
S280:
    switch((int)i99999){case 1: goto S10;case 2: goto S20;case 3: goto


^^^ voila ^^^^


S200;
      default: break;}
#undef ftol

}

hth
 
J

jacob navia

That code can never be called

Reason:
S10 is preceded by a goto so to get there there must be a
goto S10;

There is one goto S10, but it is only possible when i99999 is one.

i99999 *is* set to one, but immediately after there is a goto S270,
that set status and returns, so THAT path can never lead to the switch
where a goto 10 is done if i99999 is one.


Note that the label S20 can never be reached either since it can be
reached ONLY if i99999 is two, and i99999 is set to two in the label
S10 that we know will never be reached.

Since label S20 can never be reached, label S40 can't be reached either
since the only goto S40 is in the S20 label. The same is true for S30.

This means that labels S40 and S30 can't be reached


Well, I stopped there.
 
L

luserXtrog

That code can never be called

Reason:
S10 is preceded by a goto  so to get there there must be a
goto S10;

There is one goto S10, but it is only possible when i99999 is one.

i99999 *is* set to one, but immediately after there is a goto S270,
that set status and returns, so THAT path can never lead to the switch
where a goto 10 is done if i99999 is one.

But there's this line a little before that:
if(*status > 0) goto S280;
But, of course, i99999 isn't assigned anything in that path,
so it's /possible/ that there's a 1 or 2 in there.
Note that the label S20 can never be reached either since it can be
reached ONLY if i99999 is two, and i99999 is set to two in the label
S10 that we know will never be reached.

Since label S20 can never be reached, label S40 can't be reached either
since the only goto S40 is in the S20 label. The same is true for S30.

This means that labels S40 and S30 can't be reached

Well, I stopped there.

I missed the returns.

What in satan's glorious name is this mess anyway?!
 
S

sandeep

Thanks buddy

Can you help me then to understand how the code needs to be corrected
please. I am not expert in C
 
L

luserXtrog

Thanks buddy

Can you help me then to understand how the code needs to be corrected
please. I am not expert in C

Where'd it come from?
How's it called?
What's is *supposed* to do?
 
S

sandeep

It's some FORTRAN code converted to C... someone who didn't understand
FORTRAN thought it would be easier in C, now we don't have the original
FORTRAN... I know a little C but not advanced C like this code.

Thanks for any help.
 
B

Ben Bacarisse

luserXtrog said:
What in satan's glorious name is this mess anyway?!

You've not seen old FORTRAN, I gather? I'd lay odds this is FORTRAN
translated into C rather too literally.

To the OP: the function name is one reserved for the implementation,
so you should use another name for it but you should do that anyway.
The name of a function should reflect what it does.
 
B

Ben Bacarisse

[Top posting -- i.e. writing stuff here -- is generally not a good
idea in technical groups.]

sandeep said:
Can you help me then to understand how the code needs to be corrected
please. I am not expert in C

My advice would depend the context. If this is a small part of a
large system, I'd leave it alone. If the rest is similar any changes
are going to very hard to make. If you need correct a bug, you should
make the very smallest change possible so as not to disrupt some
subtle linkage between all the functions.

If it new code, then nothing short of a re-design will do. The end
result will be a program that probably has no direct equivalent of
this code at all.

<snip>
 
J

James Kuyper

sandeep said:
Anyone???

This is usenet, not a chat room. Don't let the absence of a response
bother you until it's been at least 24 hours since you posted your
message. The person who eventually posts a response might be on the
opposite side of the world, working a very different schedule from yours.
 
J

James Kuyper

sandeep said:
It's some FORTRAN code converted to C... someone who didn't understand
FORTRAN thought it would be easier in C, now we don't have the original
FORTRAN... I know a little C but not advanced C like this code.

The C in this program is not advanced, it's degenerate - and that's the
problem. There's some argument about whether there's an occasional need
for gotos in C code, but I think you'll find widespread agreement that
most of the gotos in this code should be replaced with more appropriate
control structures. Questions like yours are evidence of why it's a bad
idea to structure your code in this fashion.
 
T

Tim Rentsch

sandeep said:
Hi

I have the code below

I do not know if the code section in comments " CAN THE
FOLLOWING CODE EVER BE CALLED ? " is really called

Any help is appreciated. Thanks

What steps have you considered taking to try to
discover the answer (ie, outside of posting the
problem in comp.lang.c and asking about it)?
 
L

luserXtrog

It's some FORTRAN code converted to C... someone who didn't understand
FORTRAN thought it would be easier in C, now we don't have the original
FORTRAN... I know a little C but not advanced C like this code.

Thanks for any help.

Yes, what does a call to this function look like?
We have to know what is in *status to answer the original question
since the control flow depends on that value.

*What* is function supposed to *do*?
Surely it has some task to accomplish, some behavior to perform?
Without some context as to its purpose, we can only speculate
on what it *appears to be trying to do*.
 
R

Ralf Damaschke

jacob said:
That code can never be called

The code is definitely reachable.
Reason:
S10 is preceded by a goto so to get there there must be a
goto S10;

There is one goto S10, but it is only possible when i99999 is one.

i99999 *is* set to one, but immediately after there is a goto S270,
that set status and returns, so THAT path can never lead to the switch
where a goto 10 is done if i99999 is one.

Right. So we stop here and call the function again, this time with
IENTRY=1. And make use of the static storage class of i99999.

-- Ralf
 
W

Willem

jacob navia wrote:
) That code can never be called

Yes it can.

) Reason:
) S10 is preceded by a goto so to get there there must be a
) goto S10;
)
) There is one goto S10, but it is only possible when i99999 is one.
)
) i99999 *is* set to one, but immediately after there is a goto S270,
) that set status and returns, so THAT path can never lead to the switch
) where a goto 10 is done if i99999 is one.

i99999 is static, so it retains its value from the previous call
(and it's initialized to 0, so no UB there either).


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
N

Nate Eldredge

sandeep said:
Hi

I have the code below

I do not know if the code section in comments " CAN THE
FOLLOWING CODE EVER BE CALLED ? " is really called

Call the function once with IENTRY = 0, *status = 0. Then call it a
second time with IENTRY = 0 and *status = 1.

If you actually want to understand the code, I think you're going to
have to rewrite it. As it stands it's essentially unmaintainable. It's
like trying to work on a C program by editing the assembly languge
output of the compiler.
 
R

Richard Bos

sandeep said:
I do not know if the code section in comments " CAN THE
FOLLOWING CODE EVER BE CALLED ? " is really called

That is the wrong question.

The right question is: should that code, or any of this function, ever
be read by a human at all? And the answer is: no. Auto-generated code
should be seen only by the compiler. If you want to understand it, read
the source in the original language instead. If you want to improve it,
the only realistic way is to throw it out and start over with human-
written code.

Richard
 
J

jfbode1029

Hi

I have the code below

I do not know if the code section in comments " CAN THE
FOLLOWING CODE EVER BE CALLED ? " is really called

Any help is appreciated. Thanks

---------------------------------------------------------------------

static void E0001(int IENTRY,int *status,double *x,double *fx,
                  double *xlo,double *xhi,unsigned long *qleft,
                  unsigned long *qhi,double *zabstl,double *zreltl,
                  double *zxhi,double *zxlo)
{
#define ftol(zx) (0.5e0*fifdmax1(abstol,reltol*fabs((zx))))
static double a,abstol,b,c,d,fa,fb,fc,fd,fda,fdb,m,mb,p,q,reltol,tol,w,xxhi,xxlo;
static int ext,i99999;
static unsigned long first,qrzero;

Note that all of the above variables are declared static; they are all
implicitly initialized to 0 or 0.0, and will retain their values
between function calls.
    switch(IENTRY){case 0: goto DZROR; case 1: goto DSTZR;}
DZROR:
    if(*status > 0) goto S280;
    *xlo = xxlo;
    *xhi = xxhi;
    b = *x = *xlo;

    i99999 = 1;
    goto S270;

Assuming that the first time this function is called, IENTRY is set to
0 and *status is equal to 0, then i99999 will be set to 1 and *status
will be set to 1, at which point the function will exit.

Assuming that IENTRY and *status are not changed, the next time this
function is called, it will branch to label S280. Since i99999 is 1,
it will then branch to S10. i99999 will be set to 2, *status will be
set to 1, and the function will exit.

Again assuming that IENTRY and *status are not changed, the third time
this function is called it will branch to label S280, then S20.

I haven't had the desire to analyze the code much beyond that, but the
short answer is yes, under the right circumstances the code between
your comments will be called.

The next time someone starts a "what's wrong with using goto" thread,
I'm going to show them that function. Granted, 75% of the function's
opacity is due to using such informative identifiers like i99999 and
xxlo, but that was likely due to Fortran's limitations on identifier
names.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top