S
Skybuck Flying
Hi,
I would like to split main.c which contains all code into the following
Step 1. Machine.h
Step 2. drand.h and drand.c
Step 3. nrand.h and nrand.c
Main.c then only contains some test code.
The trouble starts at Step 2.
1. Machine.h should only contain integer types.
I have cut away any redundant/unneeded comments to safe space.
*** begin of Machine.h ***
#ifndef __MACHINE_H__
#define __MACHINE_H__
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;
#endif
*** end of Machine.h ***
So far this worked.
2. Splitting up code into drand.c and drand.h
This is were the trouble starts
*** begin of drand.h ***
#ifndef __DRAND_H__
#define __DRAND_H__
#include "Machine.h"
struct drand48_data
{
uint16 __x[3];
uint16 __old_x[3];
uint16 __c;
uint16 __init;
uint64 __a;
};
/* Global state for non-reentrant functions. */
struct drand48_data __libc_drand48_data;
sint32 __drand48_iterate( uint16 xsubi[3], struct drand48_data *buffer );
#endif
*** end of drand.h ***
*** begin of drand.c ***
#include "machine.h"
#include "drand.h"
sint32 __drand48_iterate( uint16 xsubi[3], struct drand48_data *buffer )
{
uint64 X;
uint64 result;
if (buffer->__init == 0)
{
buffer->__a = 0x5deece66du;
buffer->__c = 0xb;
buffer->__init = 1;
}
X = (uint64) xsubi[2] << 32 | (uint32) xsubi[1] << 16 | xsubi[0];
result = X * buffer->__a + buffer->__c;
xsubi[0] = (uint16) ( result & 0xffff );
xsubi[1] = (uint16) ( (result >> 16) & 0xffff );
xsubi[2] = (uint16) ( (result >> 32) & 0xffff );
return 0;
}
*** end of drand.c ***
*** begin of main.c ***
#include <stdlib.h>
#include <stdio.h>
#include "machine.h"
#include "drand.h"
// nrand interface
// forward declaration so that it can later be split up into .h and .c
sint32 nrand48 (uint16 xsubi[3]);
// nrand implementation
sint32 __nrand48_r ( uint16 xsubi[3], struct drand48_data *buffer, sint32
*result )
{
if (__drand48_iterate (xsubi, buffer) < 0)
{
return -1;
}
if (sizeof (unsigned short int) == 2)
{
*result = xsubi[2] << 15 | xsubi[1] >> 1;
}
else
{
*result = xsubi[2] >> 1;
}
return 0;
}
sint32 nrand48 (uint16 xsubi[3])
{
sint32 result;
(void) __nrand48_r (xsubi, &__libc_drand48_data, &result);
return result;
}
// test program
int main()
{
uint16 state48[3];
printf("%d \n", nrand48(state48) );
printf("%d \n", nrand48(state48) );
printf("%d \n", nrand48(state48) );
printf("%d \n", nrand48(state48) );
printf("%d \n", nrand48(state48) );
// the output is:
// 1288336289
// 893806678
// 810173679
// 37620279
// 474250687
return 0;
}
*** end of main.c ***
I am also using the #ifndef and #define and #endif stuff for headers as
learned in college... is that wise to do ?
Since I have seen other code *without* #ifndef #define #endif in the
headers. ?
( The ldpc source code does not use it in the headers )
I think the visual c settings are correct:
1. Machine.h, drand.h are included in header section.
2. drand.c and main.c are included in source files section.
For some reason the test program main.c won't compile:
--------------------Configuration: TestNRand48 - Win32
Debug--------------------
Compiling...
Main.cpp
Linking...
Main.obj : error LNK2001: unresolved external symbol "__int32 __cdecl
__drand48_iterate(unsigned __int16 * const,struct drand48_data *)"
(?__drand48_iterate@@YA_HQA_GPAUdrand48_data@@@Z)
Debug/TestNRand48.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
TestNRand48.exe - 2 error(s), 0 warning(s)
Why is that ?
Bye,
Skybuck.
I would like to split main.c which contains all code into the following
Step 1. Machine.h
Step 2. drand.h and drand.c
Step 3. nrand.h and nrand.c
Main.c then only contains some test code.
The trouble starts at Step 2.
1. Machine.h should only contain integer types.
I have cut away any redundant/unneeded comments to safe space.
*** begin of Machine.h ***
#ifndef __MACHINE_H__
#define __MACHINE_H__
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;
#endif
*** end of Machine.h ***
So far this worked.
2. Splitting up code into drand.c and drand.h
This is were the trouble starts
*** begin of drand.h ***
#ifndef __DRAND_H__
#define __DRAND_H__
#include "Machine.h"
struct drand48_data
{
uint16 __x[3];
uint16 __old_x[3];
uint16 __c;
uint16 __init;
uint64 __a;
};
/* Global state for non-reentrant functions. */
struct drand48_data __libc_drand48_data;
sint32 __drand48_iterate( uint16 xsubi[3], struct drand48_data *buffer );
#endif
*** end of drand.h ***
*** begin of drand.c ***
#include "machine.h"
#include "drand.h"
sint32 __drand48_iterate( uint16 xsubi[3], struct drand48_data *buffer )
{
uint64 X;
uint64 result;
if (buffer->__init == 0)
{
buffer->__a = 0x5deece66du;
buffer->__c = 0xb;
buffer->__init = 1;
}
X = (uint64) xsubi[2] << 32 | (uint32) xsubi[1] << 16 | xsubi[0];
result = X * buffer->__a + buffer->__c;
xsubi[0] = (uint16) ( result & 0xffff );
xsubi[1] = (uint16) ( (result >> 16) & 0xffff );
xsubi[2] = (uint16) ( (result >> 32) & 0xffff );
return 0;
}
*** end of drand.c ***
*** begin of main.c ***
#include <stdlib.h>
#include <stdio.h>
#include "machine.h"
#include "drand.h"
// nrand interface
// forward declaration so that it can later be split up into .h and .c
sint32 nrand48 (uint16 xsubi[3]);
// nrand implementation
sint32 __nrand48_r ( uint16 xsubi[3], struct drand48_data *buffer, sint32
*result )
{
if (__drand48_iterate (xsubi, buffer) < 0)
{
return -1;
}
if (sizeof (unsigned short int) == 2)
{
*result = xsubi[2] << 15 | xsubi[1] >> 1;
}
else
{
*result = xsubi[2] >> 1;
}
return 0;
}
sint32 nrand48 (uint16 xsubi[3])
{
sint32 result;
(void) __nrand48_r (xsubi, &__libc_drand48_data, &result);
return result;
}
// test program
int main()
{
uint16 state48[3];
printf("%d \n", nrand48(state48) );
printf("%d \n", nrand48(state48) );
printf("%d \n", nrand48(state48) );
printf("%d \n", nrand48(state48) );
printf("%d \n", nrand48(state48) );
// the output is:
// 1288336289
// 893806678
// 810173679
// 37620279
// 474250687
return 0;
}
*** end of main.c ***
I am also using the #ifndef and #define and #endif stuff for headers as
learned in college... is that wise to do ?
Since I have seen other code *without* #ifndef #define #endif in the
headers. ?
( The ldpc source code does not use it in the headers )
I think the visual c settings are correct:
1. Machine.h, drand.h are included in header section.
2. drand.c and main.c are included in source files section.
For some reason the test program main.c won't compile:
--------------------Configuration: TestNRand48 - Win32
Debug--------------------
Compiling...
Main.cpp
Linking...
Main.obj : error LNK2001: unresolved external symbol "__int32 __cdecl
__drand48_iterate(unsigned __int16 * const,struct drand48_data *)"
(?__drand48_iterate@@YA_HQA_GPAUdrand48_data@@@Z)
Debug/TestNRand48.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
TestNRand48.exe - 2 error(s), 0 warning(s)
Why is that ?
Bye,
Skybuck.