Sync shared.c and shared.h with baseq2, import rand.c

This commit is contained in:
Yamagi Burmeister 2012-06-26 14:33:37 +02:00
parent ad3fe0b674
commit 88bbdc4779
5 changed files with 176 additions and 39 deletions

View file

@ -31,7 +31,7 @@ endif
# ----------
# The compiler
CC := gcc
#CC := gcc
# ----------
@ -140,6 +140,7 @@ XATRIX_OBJS_ = \
src/player/weapon.o \
src/savegame/savegame.o \
src/shared/flash.o \
src/shared/rand.o \
src/shared/shared.o
# ----------

View file

@ -118,6 +118,9 @@ game_export_t *GetGameAPI (game_import_t *import)
globals.edict_size = sizeof(edict_t);
/* Initalize the PRNG */
randk_seed();
return &globals;
}

View file

@ -35,7 +35,12 @@ typedef enum {false, true} qboolean;
#define MAX_TOKEN_CHARS 128 /* max length of an individual token */
#define MAX_QPATH 64 /* max length of a quake game pathname */
#ifdef _WIN32
#define MAX_OSPATH 256 /* max length of a filesystem pathname (same as MAX_PATH) */
#else
#define MAX_OSPATH 128 /* max length of a filesystem pathname */
#endif
/* */
/* per-level limits */
@ -178,6 +183,8 @@ void Com_sprintf(char *dest, int size, char *fmt, ...);
void Com_PageInMemory(byte *buffer, int size);
char *strlwr ( char *s );
/* ============================================= */
/* portable case insensitive compare */
@ -209,6 +216,14 @@ void Info_RemoveKey(char *s, char *key);
void Info_SetValueForKey(char *s, char *key, char *value);
qboolean Info_Validate(char *s);
/* ============================================= */
/* Random number generator */
int randk(void);
float frandk(void);
float crandk(void);
void randk_seed(void);
/*
* ==============================================================
*
@ -221,7 +236,6 @@ extern int curtime; /* time returned by last Sys_Milliseconds */
int Sys_Milliseconds(void);
void Sys_Mkdir(char *path);
void Sys_Rmdir(char *path);
char *strlwr(char *s);
/* large block stack allocation routines */

97
src/shared/rand.c Normal file
View file

@ -0,0 +1,97 @@
/*
* KISS PRNG (c) 2011 Shinobu
*
* This file was optained from zuttobenkyou.wordpress.com
* and modified by the Yamagi Quake II developers.
*
* LICENSE: Public domain
*
* =======================================================================
*
* KISS PRNG, as devised by Dr. George Marsaglia
*
* =======================================================================
*/
#include <stdint.h>
#define QSIZE 0x200000
#define CNG (cng = 6906969069ULL * cng + 13579)
#define XS (xs ^= (xs << 13), xs ^= (xs >> 17), xs ^= (xs << 43))
#define KISS (B64MWC() + CNG + XS)
static uint64_t QARY[QSIZE];
static int j;
static uint64_t carry;
static uint64_t xs;
static uint64_t cng;
uint64_t
B64MWC(void)
{
uint64_t t, x;
j = (j + 1) & (QSIZE - 1);
x = QARY[j];
t = (x << 28) + carry;
carry = (x >> 36) - (t < x);
return QARY[j] = t - x;
}
/*
* Generate a pseudorandom
* integer >0.
*/
int
randk(void)
{
int r;
r = (int)KISS;
r = (r < 0) ? (r * -1) : r;
return r;
}
/*
* Generate a pseudorandom
* signed float between
* 0 and 1.
*/
float
frandk(void)
{
return (randk()&32767)* (1.0/32767);
}
/* Generate a pseudorandom
* float between -1 and 1.
*/
float
crandk(void)
{
return (randk()&32767)* (2.0/32767) - 1;
}
/*
* Seeds the PRNG
*/
void
randk_seed(void)
{
uint64_t i;
/* Seed QARY[] with CNG+XS: */
for (i = 0; i < QSIZE; i++)
{
QARY[i] = CNG + XS;
}
/* Run through several rounds
to warm up the state */
for (i = 0; i < 256; i++)
{
randk();
}
}

View file

@ -6,6 +6,8 @@
* =======================================================================
*/
#include <ctype.h>
#include "../header/shared.h"
#define DEG2RAD(a) (a * M_PI) / 180.0F
@ -773,8 +775,8 @@ BigShort(short l)
short
LittleShort(short l)
{return
_LittleShort(l);
{
return _LittleShort(l);
}
int
@ -875,6 +877,7 @@ Swap_Init(void)
_LittleLong = LongNoSwap;
_BigFloat = FloatSwap;
_LittleFloat = FloatNoSwap;
Com_Printf("Byte ordering: little endian\n\n");
}
else
{
@ -885,7 +888,11 @@ Swap_Init(void)
_LittleLong = LongSwap;
_BigFloat = FloatNoSwap;
_LittleFloat = FloatSwap;
Com_Printf("Byte ordering: big endian\n\n");
}
if (LittleShort(*(short *)swaptest) != 1)
assert("Error in the endian conversion!");
}
/*
@ -1085,13 +1092,29 @@ Com_sprintf(char *dest, int size, char *fmt, ...)
if ((len >= size) || (len == size))
{
Com_Printf("Com_sprintf: overflow\n");
len = size - 1;
dest = NULL;
return;
}
bigbuffer[size - 1] = '\0';
strcpy(dest, bigbuffer);
}
char *
strlwr ( char *s )
{
char *p = s;
while ( *s )
{
*s = tolower( *s );
s++;
}
return ( p );
}
/*
* =====================================================================
*
@ -1313,4 +1336,3 @@ Info_SetValueForKey(char *s, char *key, char *value)
*s = 0;
}