Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > mystery code?

Reply
Thread Tools

mystery code?

 
 
John Smith
Guest
Posts: n/a
 
      02-18-2006
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?
 
Reply With Quote
 
 
 
 
John F
Guest
Posts: n/a
 
      02-18-2006

"John Smith" wrote:
>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/de...m/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...

--
regards
John


 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      02-18-2006
John Smith <> writes:

> 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.
--
"In My Egotistical Opinion, most people's C programs should be indented six
feet downward and covered with dirt." -- Blair P. Houghton
 
Reply With Quote
 
John F
Guest
Posts: n/a
 
      02-18-2006

"Ben Pfaff" wrote:
> John Smith writes:
>
>> 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.


(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)

--
John


 
Reply With Quote
 
Rod Pemberton
Guest
Posts: n/a
 
      02-18-2006

"John Smith" <> wrote in message
news:MOKJf.39443$sa3.37126@pd7tw1no...
> 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



 
Reply With Quote
 
Malcolm
Guest
Posts: n/a
 
      02-19-2006



"John Smith" <> wrote in message
news:MOKJf.39443$sa3.37126@pd7tw1no...
>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.
>

--
Buy my book 12 Common Atheist Arguments (refuted)
$1.25 download or $6.90 paper, available www.lulu.com


 
Reply With Quote
 
Dave Thompson
Guest
Posts: n/a
 
      02-27-2006
On Sat, 18 Feb 2006 19:44:44 GMT, John Smith <> wrote:

> 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 )
> {

<snip>
>
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Mystery Bill McCarty Firefox 10 05-27-2005 04:35 AM
wifi mystery Txl Wireless Networking 4 03-25-2005 06:28 PM
Mystery frame below status bar [Xandros Bus 2/Firefox 1.0] John Bartley Firefox 1 03-23-2005 04:01 PM
Mystery # 2 Bill McCarty Firefox 5 01-02-2005 06:07 PM
Mystery Bill McCarty Firefox 5 12-31-2004 10:57 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57