473,778 Members | 2,804 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

nrand48 source code looks weird ?

This is the source code for nrand48 from gnuwin32 libgw32c

long int nrand48 (xsubi) unsigned short int xsubi[3];
{
long int result;

(void) __nrand48_r (xsubi, &__libc_drand48 _data, &result);

return result;
}

// long int nrand48 (xsubi) unsigned short int xsubi[3];

What does the 'unsigned short int xsubi[3];' part mean after '(xsubi)' ?

The prototype in stdlib.h from the binaries is: (for some reason stdlib.h
was not in source distribution ?)

extern long int nrand48 (unsigned short int __xsubi[3]) __THROW;

So at first sight I would guess it has to do with throwing exceptions... but
now that I look at it again it I think it's the type for xsubi.

The original looks really weird:

1. long int nrand48 (xsubi) unsigned short int xsubi[3];

Isn't that supposed to look like this:

2. long int nrand48 (unsigned short int xsubi[3] );

Why did they write it as 1. ?

Is that just their preferred way of writing it down... or does it have some
sort of functionality ?

The prototype then again looks like 2. !? huh ?! :)

Euhm... Could this be a case of wacky c programmers ? eh.

Bye,
Skybuck.
Nov 14 '05 #1
10 2166
Skybuck Flying <no****@hotmail .com> wrote:
This is the source code for nrand48 from gnuwin32 libgw32c long int nrand48 (xsubi) unsigned short int xsubi[3];
{
long int result; (void) __nrand48_r (xsubi, &__libc_drand48 _data, &result); return result;
} // long int nrand48 (xsubi) unsigned short int xsubi[3]; What does the 'unsigned short int xsubi[3];' part mean after '(xsubi)' ?
That's the very old way function definitions where written back in
the K&R days when there wasn't a C standard. It's basically equiva-
lent to

long int nrand48( unsigned short int xsubi[ 3 ] )

In code dating back from that time you typically had function definitions
starting with

my_func( a, b, c )
int a,
char *b,
unsigned short c
{

which nowadays would be written as

int my_func( int a, char *b, unsigned short c )
{
The prototype in stdlib.h from the binaries is: (for some reason stdlib.h
was not in source distribution ?) extern long int nrand48 (unsigned short int __xsubi[3]) __THROW; So at first sight I would guess it has to do with throwing exceptions... but
now that I look at it again it I think it's the type for xsubi.


Better don't look at the header files unless you have very good reasons
to (or just out of curiosity). Lots of system-dependend magic is going
on there.
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@p hysik.fu-berlin.de
\______________ ____________ http://www.toerring.de
Nov 14 '05 #2

<Je***********@ physik.fu-berlin.de> wrote in message
news:2l******** ****@uni-berlin.de...
Skybuck Flying <no****@hotmail .com> wrote:
This is the source code for nrand48 from gnuwin32 libgw32c

long int nrand48 (xsubi) unsigned short int xsubi[3];
{
long int result;

(void) __nrand48_r (xsubi, &__libc_drand48 _data, &result);

return result;
}

// long int nrand48 (xsubi) unsigned short int xsubi[3];

What does the 'unsigned short int xsubi[3];' part mean after '(xsubi)' ?


That's the very old way function definitions where written back in
the K&R days when there wasn't a C standard. It's basically equiva-
lent to

long int nrand48( unsigned short int xsubi[ 3 ] )

In code dating back from that time you typically had function definitions
starting with

my_func( a, b, c )
int a,
char *b,
unsigned short c
{

which nowadays would be written as

int my_func( int a, char *b, unsigned short c )
{


Yes, I was starting to think that C was a really #^#@$ language :)

Visual C/C++ 6.0 doesn't the support the old way...

<<<--- sigh --->>>

So now I have to re-write it all...

Fortunately for me... It's only a little bit of code... and fortunately I
rarely program in C :P =D

Thx for the description... Lol... it must be painfull to code in C :D

Bye,
Skybuck.
Nov 14 '05 #3
"Skybuck Flying" <no****@hotmail .com> wrote:
<Je***********@ physik.fu-berlin.de> wrote in message
news:2l******** ****@uni-berlin.de...
That's the very old way function definitions where written back in
the K&R days when there wasn't a C standard.
Visual C/C++ 6.0 doesn't the support the old way...
Then it's not a C compiler. Are you sure you're not pretending to
compile C++?
Thx for the description... Lol... it must be painfull to code in C :D


Certainly not. It's a lot less painful if you actually learn to program.

Richard
Nov 14 '05 #4
Finally...

A working nrand48() :)

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <float.h>

//typedef unsigned long long uint64_t

typedef signed __int8 sint8;
typedef unsigned __int8 uint8;
typedef signed __int16 sint16;
typedef unsigned __int16 uint16;
typedef signed __int32 sint32;
typedef unsigned __int32 uint32;
typedef signed __int64 sint64;
typedef unsigned __int64 uint64;
/* Data structure for communication with thread safe versions. This
type is to be regarded as opaque. It's only exported because users
have to allocate objects of this type. */
struct drand48_data
{
unsigned short int __x[3]; /* Current state. */
unsigned short int __old_x[3]; /* Old state. */
unsigned short int __c; /* Additive const. in congruential formula. */
unsigned short int __init; /* Flag for initializing. */
uint64 __a; /* Factor in congruential formula. */
};
/* Global state for non-reentrant functions. */
struct drand48_data __libc_drand48_ data;
int __drand48_itera te ( unsigned short int xsubi[3], struct drand48_data
*buffer )
{
uint64 X;
uint64 result;

/* Initialize buffer, if not yet done. */

// built in bullshit removed
// if (__builtin_expe ct (!buffer->__init, 0))
if (buffer->__init == 0)
{
buffer->__a = 0x5deece66du;
buffer->__c = 0xb;
buffer->__init = 1;
}

/* Do the real work. We choose a data type which contains at least
48 bits. Because we compute the modulus it does not care how
many bits really are computed. */

X = (uint64) xsubi[2] << 32 | (uint32) xsubi[1] << 16 | xsubi[0];

result = X * buffer->__a + buffer->__c;

xsubi[0] = result & 0xffff;
xsubi[1] = (result >> 16) & 0xffff;
xsubi[2] = (result >> 32) & 0xffff;

return 0;
}
int __nrand48_r (
unsigned short int xsubi[3],
struct drand48_data *buffer,
long int *result )
{

/* Compute next state. */
if (__drand48_iter ate (xsubi, buffer) < 0)
{
return -1;
}

/* Store the result. */
if (sizeof (unsigned short int) == 2)
{
*result = xsubi[2] << 15 | xsubi[1] >> 1;
}
else
{
*result = xsubi[2] >> 1;
}

return 0;
}

long int nrand48 (unsigned short int xsubi[3])
{
long int result;

(void) __nrand48_r (xsubi, &__libc_drand48 _data, &result);

return result;
}

int main()
{
unsigned short state48[3];

printf("%d \n", nrand48(state48 ) );
return 0;
}

Compiling...
Main.cpp
C:\C TESTEN\TestNRan d48\Main.cpp(59 ) : warning C4244: '=' : conversion from
'unsigned __int64' to 'unsigned short', possible loss of data
C:\C TESTEN\TestNRan d48\Main.cpp(60 ) : warning C4244: '=' : conversion from
'unsigned __int64' to 'unsigned short', possible loss of data
C:\C TESTEN\TestNRan d48\Main.cpp(61 ) : warning C4244: '=' : conversion from
'unsigned __int64' to 'unsigned short', possible loss of data
Linking...

TestNRand48.exe - 0 error(s), 3 warning(s)
Except these three lines produce a warning in visual c 6

xsubi[0] = result & 0xffff;
xsubi[1] = (result >> 16) & 0xffff;
xsubi[2] = (result >> 32) & 0xffff;

Seeing the 0xffff I can conclude that unsigned short must be a unsigned 16
bit integer.

So these warnings can probably be safely surpressed >D by using:

xsubi[0] = uint16( result & 0xffff );
xsubi[1] = uint16( (result >> 16) & 0xffff );
xsubi[2] = uint16( (result >> 32) & 0xffff );

Jip..

That did the trick.

Bye,
Skybuck

P.S.: Take care ;)
Nov 14 '05 #5
"Skybuck Flying" <no****@hotmail .com> wrote:
typedef unsigned __int16 uint16;
[ Snip loads of code including lots of non-standard types and
identifiers in the implementation' s namespace. ]
So these warnings can probably be safely surpressed >D by using:

xsubi[0] = uint16( result & 0xffff );


uint16 is a type, not a function. Are you _very_ sure you're not
compiling C++?

Richard
Nov 14 '05 #6
In article <2l************ @uni-berlin.de>,
<Je***********@ physik.fu-berlin.de> wrote:
In code dating back from that time you typically had function definitions
starting with

my_func( a, b, c )
int a,
char *b,
unsigned short c
{


No. We had function staring like this:

my_func( a, b, c )
int a;
char *b;
unsigned short c;
{

--
Göran Larsson http://www.mitt-eget.com/
Nov 14 '05 #7

"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:40******** ********@news.i ndividual.net.. .
"Skybuck Flying" <no****@hotmail .com> wrote:
typedef unsigned __int16 uint16;


[ Snip loads of code including lots of non-standard types and
identifiers in the implementation' s namespace. ]
So these warnings can probably be safely surpressed >D by using:

xsubi[0] = uint16( result & 0xffff );


uint16 is a type, not a function. Are you _very_ sure you're not
compiling C++?


Hmm when splitting it up into multiple files it indeed produced an error

I think the correct typecast code to use is:

Not:

xsubi[0] = uint16( result & 0xffff );

Butttt:

xsubi[0] = (uint16) ( result & 0xffff );

:DD

Skybuck.

Nov 14 '05 #8
On Wed, 7 Jul 2004 15:15:44 +0200, "Skybuck Flying"
<no****@hotmail .com> wrote:

<Je*********** @physik.fu-berlin.de> wrote in message
news:2l******* *****@uni-berlin.de...
Skybuck Flying <no****@hotmail .com> wrote:
Thx for the description... Lol... it must be painfull to code in C :D


We're all sadists in this manner :)
--
aib

ISP e-mail accounts are good for receiving spam.
Nov 14 '05 #9
Skybuck Flying wrote:
[... old-vs-new function definitions ...]

Visual C/C++ 6.0 doesn't the support the old way...

<<<--- sigh --->>>

So now I have to re-write it all...

[...]

Excuse me? I have a program with several hundred thousand line of code
and 99.9% of the functions are coded "the old way". (We support platforms
that still don't allow "the new way".) It compiles just fine under VC6.0.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody at spamcop.net | www.fptech.com | #include <std_disclaimer .h> |
+-------------------------+--------------------+-----------------------------+
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1706
by: Andy | last post by:
I use Netbeans to write all my code and I've noticed that if I do it wrong and the program throws an (unexpected) exception, it becomes possible to see the source code for the all the classes used leading up to the exception. I presume it must be possible therefore to somehow view the source code for all the classes in sun's library (e.g. Swing, awt, sql). I reckon this would be a pretty good way of learning how to write good java.
9
3895
by: Michael Dekson | last post by:
Hello, Can I exe file made in Microsoft Visual C++ decompile into source code. If it is possibly please tell me how. Thanks
7
2278
by: harish.mallipeddi | last post by:
Hi all, This might sound a bit weird but anyways here I go. Recently after witnessing the popularity of AJAX/DHTML, and after enjoying Gmail's fairly cool UI, I'm left wondering...is there going to be a point in the future when Javascript developers would like to hide their sourcecode from prying eyes because they think they'd put in enough efforts into coding and they wouldn't want to give up the commercial value in their code for...
46
2496
by: Profetas | last post by:
Hi, I know that this is off topic. but I didn't know where to post. Do you comment your source code while coding or after coding. for example: you write a procedure and after it is working, you'll comment it.
8
1923
by: G Patel | last post by:
Can people please comment on the layout/style of my problem? The major issue I had was the layout. I ended up having to put a relatively large switch statement, inside an if statement, which is inside a while loop. If someone can tell me how I can rearrange these elements a little to make it cleaner, I would appreciate that. I've tested it on the command line, and it works *well*, but the source code layout is bugging me. I'd also...
1
1886
by: rahul8143 | last post by:
hello, In kernel source code there is ip_fragment.c file my question is regarding pointer function and casting for that look at required snippet from that file There is structure defined for queueing ip fragments as struct ipq { struct ipq *next; /* linked list pointers */ struct list_head lru_list; /* lru list member */ u32 user; u32 saddr;
66
7476
by: Jon Skeet [C# MVP] | last post by:
I'm sure the net will be buzzing with this news fairly soon, but just in case anyone hasn't seen it yet: Microsoft are going to make the source code for the .NET framework (parts of it, including the BCL, ASP.NET and LINQ) available both for viewing and debugging into. I won't go into all the details here, as they're covered on Scott Guthrie's blog:
10
1669
by: Jani Järvinen [MVP] | last post by:
Hello, for your information in case you didn't yet notice the news: Microsoft has announced plans to give developers the ability to view .NET class library source code. This is announced here: http://weblogs.asp.net/scottgu/archive/2007/10/03/releasing-the-source-code-for-the-net-framework-libraries.aspx Says the blog:
21
3589
by: mikhal80 | last post by:
Is it possible to write a program which would print out it's own source code, using C++?
0
9629
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9465
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10127
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10068
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9923
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8954
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6723
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4031
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3627
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.