Skip to content

Commit

Permalink
rtlib: rename FB_RNDINTERNALS to FB_RNDSTATE
Browse files Browse the repository at this point in the history
  • Loading branch information
jayrm committed Nov 6, 2020
1 parent a8358a1 commit f11fd4f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
5 changes: 3 additions & 2 deletions inc/fbc-int/math.bi
Expand Up @@ -25,7 +25,7 @@

namespace FBC

type FB_RNDINTERNALS
type FB_RNDSTATE
dim algorithm as ulong '' algorithm number see FB_RND_ALGORITHMS
dim length as ulong '' length of Mersennse Twister states in (number of ulongs)
dim stateblock as ulong ptr '' Mersenne Twister state (pointer array)
Expand All @@ -41,11 +41,12 @@ end type

extern "rtlib"

'' built-in RANDOMIZE & RND & RND32
declare sub randomize alias "fb_Randomize" ( byval seed as double = -1.0, byval algorithm as long = FB.FB_RND_AUTO )
declare function rnd alias "fb_Rnd" ( byval n as single = 1.0 ) as double
declare function rnd32 alias "fb_Rnd32" ( ) as ulong

declare sub rndGetInternals alias "fb_RndGetInternals" ( byval info as FB_RNDINTERNALS ptr )
declare sub rndGetState alias "fb_RndGetState" ( byval info as FB_RNDSTATE ptr )

#if __FB_MT__

Expand Down
6 changes: 3 additions & 3 deletions src/rtlib/fb_math.h
Expand Up @@ -7,19 +7,19 @@ typedef enum _FB_RND_ALGORITHMS {
FB_RND_REAL
} FB_RND_ALGORITHMS;

typedef struct _FB_RNDINTERNALS {
typedef struct _FB_RNDSTATE {
uint32_t algorithm;
uint32_t length;
uint32_t *stateblock;
uint32_t **stateindex;
uint32_t *iseed;
double ( *rndproc )( float n );
uint32_t ( *rndproc32 )( void );
} FB_RNDINTERNALS;
} FB_RNDSTATE;

FBCALL double fb_Rnd ( float n );
FBCALL uint32_t fb_Rnd32 ( void );
FBCALL void fb_GetRndInternals ( FB_RNDINTERNALS *info );
FBCALL void fb_RndGetState ( FB_RNDSTATE *info );

FBCALL void fb_Randomize ( double seed, int algorithm );
FBCALL int fb_SGNSingle ( float x );
Expand Down
21 changes: 18 additions & 3 deletions src/rtlib/math_rnd.c
@@ -1,16 +1,26 @@
/* rnd# function */
/* rnd# function & randmoize statement for built-in PRNGs */

#include "fb.h"

/* Needed by FB_RND_REAL */
#if defined HOST_WIN32
#include <windows.h>
#include <wincrypt.h>
#elif defined HOST_LINUX
#include <fcntl.h>
#endif

/* rtlib is initialzied so that RND & RND32 call the
startup routines. This allows calling RND without
first call RANDOMIZE. After the startup routine is
called, the functions are remapped to the actual PRNG
functions.
*/

#define INITIAL_SEED 327680

/* MAX_STATE is number of 32-bit unsigned integers */
/* used by FB_RND_MTWIST & FB_RND_REAL */
#define MAX_STATE 624
#define PERIOD 397

Expand Down Expand Up @@ -65,6 +75,7 @@ static double hRnd_Startup ( float n )
return fb_Rnd( n );
}

/* FB_RND_CRT */
static uint32_t hRnd_CRT32 ( void )
{
return rand( );
Expand All @@ -79,6 +90,7 @@ static double hRnd_CRT ( float n )
return (double)hRnd_CRT32( ) * ( 1.0 / ( (double)RAND_MAX + 1.0 ) );
}

/* FB_RND_FAST */
static uint32_t hRnd_FAST32 ( void )
{
/* Constants from 'Numerical recipes in C' chapter 7.1 */
Expand All @@ -96,6 +108,7 @@ static double hRnd_FAST ( float n )
return (double)hRnd_FAST32() / (double)4294967296ULL;
}

/* rnd# function for FB_RND_MTWIST */
static uint32_t hRnd_MTWIST32 ( void )
{
uint32_t i, v, xor_mask[2] = { 0, 0x9908B0DF };
Expand Down Expand Up @@ -136,7 +149,7 @@ static double hRnd_MTWIST ( float n )

return (double)hRnd_MTWIST32() / (double)4294967296ULL;
}

/* FB_RND_QB */
static uint32_t hRnd_QB32 ( void )
{
iseed = ( ( iseed * 0xFD43FD ) + 0xC39EC3 ) & 0xFFFFFF;
Expand All @@ -162,6 +175,8 @@ static double hRnd_QB ( float n )
return (float)hRnd_QB32() / (float)0x1000000;
}

/* FB_RND_REAL */

#if defined HOST_WIN32 || defined HOST_LINUX
static int hRefillRealRndNumber( )
{
Expand Down Expand Up @@ -309,7 +324,7 @@ FBCALL void fb_Randomize ( double seed, int algorithm )

}

FBCALL void fb_RndGetInternals( FB_RNDINTERNALS *info )
FBCALL void fb_RndGetState( FB_RNDSTATE *info )
{
if( info )
{
Expand Down
8 changes: 4 additions & 4 deletions tests/fbc-int/math-rnd.bas
Expand Up @@ -20,8 +20,8 @@ SUITE( fbc_tests.fbc_int.math_rnd )
CU_ASSERT_EQUAL( fbc.rnd32(), 3027450565 )
CU_ASSERT_EQUAL( fbc.rnd32(), 217083232 )

dim info as FBC.FB_RNDINTERNALS
fbc.rndGetInternals( @info )
dim info as FBC.FB_RNDSTATE
fbc.rndGetState( @info )

CU_ASSERT( info.algorithm = FB.FB_RND_FAST )
CU_ASSERT( info.rndproc <> NULL )
Expand All @@ -40,8 +40,8 @@ SUITE( fbc_tests.fbc_int.math_rnd )
var x = rnd
var y = rnd32

dim info as FB_RNDINTERNALS
rndGetInternals( @info )
dim info as FB_RNDSTATE
rndGetState( @info )

CU_PASS()

Expand Down

0 comments on commit f11fd4f

Please sign in to comment.