mystery code?

J

John Smith

I ran across this code from a statistics library. The header
source code was not available.

long double __declspec(naked) poisson_distribution(int k,long
double m)
{
}
long double pdtrl(int k,long double m )
{
long double v;

if( (k < 0) || (m <= 0.0L) )
{
mtherr( "pdtrl", DOMAIN );
return( 0.0L );
}
v = k+1;
return( igamcl( v, m ) );
}

What would be the purpose of "__declspec(naked)" in the signature
of poisson_distribution()? Is this legal C syntax? Why would the
function body be empty?
 
J

John F

John Smith said:
I ran across this code from a statistics library. The header source code
was not available.

long double __declspec(naked) poisson_distribution(int k,long double m)
{
}

At least a return statement is missing...
long double pdtrl(int k,long double m )
{
long double v;

if( (k < 0) || (m <= 0.0L) )
{
mtherr( "pdtrl", DOMAIN );
return( 0.0L );
}
v = k+1;
return( igamcl( v, m ) );
}

What would be the purpose of "__declspec(naked)" in the signature of
poisson_distribution()?

from the MSDN:
For functions declared with the naked attribute, the compiler generates code
without prolog and epilog code. You can use this feature to write your own
prolog/epilog code sequences using inline assembler code. Naked functions
are particularly useful in writing virtual device drivers.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/msmod_25.asp
Is this legal C syntax?

__declspec is not C. The rest is OK.
Why would the function body be empty?

mostly to avoid errors or to tell the compiler that an assembler
implementation exists, but in this case it will create new ones...
 
B

Ben Pfaff

John Smith said:
What would be the purpose of "__declspec(naked)" in the signature
of poisson_distribution()? Is this legal C syntax?

__declspec is either an extension of a particular C
implementation, or it's the name of a macro. It's not standard
C.
Why would the function body be empty?

Beats me.
 
J

John F

Ben Pfaff said:
__declspec is either an extension of a particular C
implementation, or it's the name of a macro. It's not standard
C.


Beats me.

(together with __declspec(naked) it makes sense if the function is
implemented in assembler. the body is needed, since the __declspec(naked)
modifyer [OT, extension] is not allowed on prototypes in VC... that is why I
was guessing at VC... directing to the MSDN)
 
R

Rod Pemberton

John Smith said:
I ran across this code from a statistics library. The header
source code was not available.

long double __declspec(naked) poisson_distribution(int k,long
double m)
{
}
long double pdtrl(int k,long double m )
{
long double v;

if( (k < 0) || (m <= 0.0L) )
{
mtherr( "pdtrl", DOMAIN );
return( 0.0L );
}
v = k+1;
return( igamcl( v, m ) );
}

What would be the purpose of "__declspec(naked)" in the signature
of poisson_distribution()?

It removes the prologue and epilogue (i.e., stack frame), allocation of
variables, and the assembly language return statement for the procedure.
This is useful for writing interrupt service routines since you can change
the type of assembly language return, or preventing variables from being
pushed onto a stack.
Is this legal C syntax?

You'll see this with Microsoft and OpenWatcom compilers.
Why would the
function body be empty?

It should be thought of as a compiler modifier, like GCC's __attribute__(),
which is setup similar to a prototype.


Rod Pemberton
 
M

Malcolm

John Smith said:
I ran across this code from a statistics library. The header source code
was not available.

long double __declspec(naked) poisson_distribution(int k,long double m)
{
}
long double pdtrl(int k,long double m )
{
long double v;

if( (k < 0) || (m <= 0.0L) )
{
mtherr( "pdtrl", DOMAIN );
return( 0.0L );
}
v = k+1;
return( igamcl( v, m ) );
}

What would be the purpose of "__declspec(naked)" in the signature of
poisson_distribution()? Is this legal C syntax? Why would the function
body be empty?
__declspec(naked) is some sort of compiler-specific gibberish to make C
integrate with non-C code.
It is not legal, portable ANSI C, and won't compile on another compiler.
That's not the same as saying that it is bad code.

I would guess that the empty function body is a placeholder and the real
code is not written in C.
 
D

Dave Thompson

I ran across this code from a statistics library. The header
source code was not available.

long double __declspec(naked) poisson_distribution(int k,long
double m)
{
}
long double pdtrl(int k,long double m )
{
What would be the purpose of "__declspec(naked)" in the signature
of poisson_distribution()? Is this legal C syntax? Why would the
function body be empty?

As others have said, __declspec is not standard, it's an M$VC feature
perhaps adopted by some others for compatibility. In this case on that
implementation I'd bet 'naked' results in no generated code at all for
poisson_distribution just the entrypoint symbol, with the result that
calling that name actually results in calling (the body of) pdtr(),
which has the same parameters and return type and thus works --
assuming whatever igamcl() returns is in fact correct.

- David.Thompson1 at worldnet.att.net
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top