#include "lib-utils.h"
|
#include "lib-utils.h"
|
|
|
/* Simple C functions */
|
/* Simple C functions */
|
|
|
/* memcpy */
|
/* memcpy */
|
|
|
void* memcpy( void* s1, void* s2, size_t n)
|
void* memcpy( void* s1, void* s2, size_t n)
|
{
|
{
|
char* r1 = s1;
|
char* r1 = (char *) s1;
|
const char* r2 = s2;
|
const char* r2 = (const char*) s2;
|
#ifdef __BCC__
|
#ifdef __BCC__
|
while (n--) {
|
while (n--) {
|
*r1++ = *r2++;
|
*r1++ = *r2++;
|
}
|
}
|
#else
|
#else
|
while (n) {
|
while (n) {
|
*r1++ = *r2++;
|
*r1++ = *r2++;
|
--n;
|
--n;
|
}
|
}
|
#endif
|
#endif
|
return s1;
|
return s1;
|
}
|
}
|
|
|
/* strlen */
|
/* strlen */
|
size_t strlen(const char*s)
|
size_t strlen(const char*s)
|
{
|
{
|
const char* p;
|
const char* p;
|
for (p=s; *p; p++);
|
for (p=s; *p; p++);
|
return p - s;
|
return p - s;
|
}
|
}
|
|
|
/* memchr */
|
/* memchr */
|
void *memchr(const void *s, int c, size_t n)
|
void *memchr(const void *s, int c, size_t n)
|
{
|
{
|
const unsigned char *r = (const unsigned char *) s;
|
const unsigned char *r = (const unsigned char *) s;
|
#ifdef __BCC__
|
#ifdef __BCC__
|
/* bcc can optimize the counter if it thinks it is a pointer... */
|
/* bcc can optimize the counter if it thinks it is a pointer... */
|
const char *np = (const char *) n;
|
const char *np = (const char *) n;
|
#else
|
#else
|
# define np n
|
# define np n
|
#endif
|
#endif
|
|
|
while (np) {
|
while (np) {
|
if (*r == ((unsigned char)c)) {
|
if (*r == ((unsigned char)c)) {
|
return (void *) r; /* silence the warning */
|
return (void *) r; /* silence the warning */
|
}
|
}
|
++r;
|
++r;
|
--np;
|
--np;
|
}
|
}
|
|
|
return NULL;
|
return NULL;
|
}
|
}
|
|
|
/* --------------------------------------------------------------------------*/
|
/* --------------------------------------------------------------------------*/
|
/*!Pseudo-random number generator
|
/*!Pseudo-random number generator
|
|
|
This should return pseudo-random numbers, based on a Galois LFSR
|
This should return pseudo-random numbers, based on a Galois LFSR
|
|
|
@return The next pseudo-random number */
|
@return The next pseudo-random number */
|
/* --------------------------------------------------------------------------*/
|
/* --------------------------------------------------------------------------*/
|
unsigned long int
|
unsigned long int
|
rand ()
|
rand ()
|
{
|
{
|
static unsigned long int lfsr = RAND_LFSR_SEED;
|
static unsigned long int lfsr = RAND_LFSR_SEED;
|
static int period = 0;
|
static int period = 0;
|
/* taps: 32 31 29 1; characteristic polynomial: x^32 + x^31 + x^29 + x + 1 */
|
/* taps: 32 31 29 1; characteristic polynomial: x^32 + x^31 + x^29 + x + 1 */
|
lfsr = (lfsr >> 1) ^ (unsigned long int)((0 - (lfsr & 1u)) & 0xd0000001u);
|
lfsr = (lfsr >> 1) ^ (unsigned long int)((0 - (lfsr & 1u)) & 0xd0000001u);
|
++period;
|
++period;
|
return lfsr;
|
return lfsr;
|
}
|
}
|
|
|