2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2009-08-17 12:41:51 +02:00
|
|
|
gbx_math.c
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2018-02-12 02:53:46 +01:00
|
|
|
(c) 2000-2017 Benoît Minisini <g4mba5@gmail.com>
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
2009-08-17 12:41:51 +02:00
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
2007-12-30 17:41:49 +01:00
|
|
|
any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
2011-06-03 02:51:09 +02:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
2011-12-31 03:39:20 +01:00
|
|
|
MA 02110-1301, USA.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __GBX_MATH_C
|
|
|
|
|
2012-04-28 17:08:24 +02:00
|
|
|
#include "gb_common.h"
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
#include <math.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
2011-12-31 02:02:42 +01:00
|
|
|
#include "gb_hash.h"
|
2007-12-30 17:41:49 +01:00
|
|
|
#include "gbx_math.h"
|
|
|
|
|
2012-04-28 17:08:24 +02:00
|
|
|
const double MATH_pow10_double[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
|
|
|
|
static const uint _pow10_uint[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
|
2012-04-25 23:12:03 +02:00
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
/* This is a twisted generalized feedback shift register
|
|
|
|
that generates pseudo-random numbers.
|
|
|
|
|
|
|
|
Source code from the paper of Yann Guidon
|
|
|
|
in March edition of GNU/Linux Magazine France
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define CRC32_STD 0x04C11DB7
|
|
|
|
#define GFSR_SIZE 15
|
|
|
|
|
|
|
|
static uint GFSR_table[GFSR_SIZE];
|
|
|
|
static uint GFSR_temp;
|
|
|
|
static uint GFSR_index;
|
|
|
|
|
|
|
|
static void GFSR_init(uint seed)
|
|
|
|
{
|
|
|
|
int i = 0, j;
|
|
|
|
uint t = seed;
|
|
|
|
|
2015-10-18 23:10:13 +02:00
|
|
|
if (t == 0)
|
|
|
|
t = (uint)-1;
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
do
|
|
|
|
{
|
|
|
|
t ^= (t >> 5) ^ (t << 1);
|
|
|
|
j = t >> 31;
|
|
|
|
t <<= 1;
|
|
|
|
if (j)
|
|
|
|
t ^= CRC32_STD;
|
|
|
|
GFSR_table[i++] = t;
|
|
|
|
}
|
|
|
|
while (i < GFSR_SIZE);
|
|
|
|
|
|
|
|
GFSR_temp = GFSR_table[GFSR_SIZE - 1];
|
|
|
|
GFSR_index = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint GFSR_random(void)
|
|
|
|
{
|
|
|
|
int t;
|
|
|
|
|
|
|
|
GFSR_temp ^= GFSR_table[GFSR_index];
|
|
|
|
|
|
|
|
t = GFSR_temp;
|
|
|
|
GFSR_temp <<= 1;
|
|
|
|
if (t < 0)
|
|
|
|
GFSR_temp ^= CRC32_STD;
|
|
|
|
|
|
|
|
GFSR_table[GFSR_index] = GFSR_temp;
|
|
|
|
|
|
|
|
if (++GFSR_index >= GFSR_SIZE)
|
|
|
|
GFSR_index = 0;
|
|
|
|
|
|
|
|
return GFSR_temp;
|
|
|
|
}
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
double frac(double x)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
x = fabs(x);
|
|
|
|
return x - floor(x);
|
|
|
|
}
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
int lsgn(int x)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
return ((x > 0) ? 1 : ((x < 0) ? (-1) : 0));
|
|
|
|
}
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
int llsgn(int64_t x)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
return ((x > 0) ? 1 : ((x < 0) ? (-1) : 0));
|
|
|
|
}
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
/*int64_t llabs(int64_t x)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
return ((x < 0) ? (-x) : x);
|
2008-01-17 22:39:26 +01:00
|
|
|
}*/
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
int fsgn(double x)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
return ((x > 0) ? 1 : ((x < 0) ? (-1) : 0));
|
|
|
|
}
|
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
float fixf(float x)
|
|
|
|
{
|
|
|
|
if (x >= 0)
|
|
|
|
return floorf(x);
|
|
|
|
else
|
|
|
|
return -floorf(fabsf(x));
|
|
|
|
}
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
double fix(double x)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
if (x >= 0)
|
|
|
|
return floor(x);
|
|
|
|
else
|
|
|
|
return -floor(fabs(x));
|
|
|
|
}
|
|
|
|
|
2011-12-30 02:28:10 +01:00
|
|
|
double frexp10(double x, int *exp)
|
|
|
|
{
|
|
|
|
int p;
|
|
|
|
|
|
|
|
if (x == 0.0)
|
|
|
|
{
|
|
|
|
*exp = 0;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = (int)log10(x);
|
2012-04-28 17:08:24 +02:00
|
|
|
x /= pow10(p);
|
2011-12-30 02:28:10 +01:00
|
|
|
|
|
|
|
if (x >= 1)
|
|
|
|
{
|
|
|
|
x /= 10;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
*exp = p;
|
|
|
|
return x;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void randomize(bool set, uint seed)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
if (!set && gettimeofday(&tv, NULL) == 0)
|
2015-10-19 21:30:17 +02:00
|
|
|
seed = 0xD1C2B3A4 + (tv.tv_sec << 20) + tv.tv_usec;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
GFSR_init(seed);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
double rnd(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
/*seed = 16807L * (seed % 127773L) - 2836L * (seed / 127773L);
|
|
|
|
if (seed <= 0) seed += 2147483647;
|
|
|
|
|
|
|
|
return (double)seed / 2147483648.0;*/
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
uint64_t val;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
val = GFSR_random();
|
|
|
|
val <<= 32;
|
|
|
|
val |= GFSR_random();
|
|
|
|
|
2009-05-15 19:57:29 +02:00
|
|
|
return (double)val / 18446744073709551616.0; //0xFFFFFFFFFFFFFFFFULL;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2010-08-31 10:54:51 +02:00
|
|
|
#ifndef HAVE_EXP10
|
2009-12-24 03:02:05 +01:00
|
|
|
double exp10(double x)
|
|
|
|
{
|
|
|
|
return pow(10, x);
|
|
|
|
}
|
2010-08-31 10:54:51 +02:00
|
|
|
#endif
|
2009-12-24 03:02:05 +01:00
|
|
|
|
2010-08-31 10:54:51 +02:00
|
|
|
#ifndef HAVE_LOG2
|
2009-12-24 03:02:05 +01:00
|
|
|
double log2(double x)
|
|
|
|
{
|
|
|
|
return log(x) / M_LN2;
|
|
|
|
}
|
2010-08-31 10:54:51 +02:00
|
|
|
#endif
|
2009-12-24 03:02:05 +01:00
|
|
|
|
2010-08-31 10:54:51 +02:00
|
|
|
#ifndef HAVE_EXP2
|
2009-12-24 03:02:05 +01:00
|
|
|
double exp2(double x)
|
|
|
|
{
|
|
|
|
return pow(2, x);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-12-31 03:09:12 +01:00
|
|
|
static int nbits(uint n)
|
|
|
|
{
|
|
|
|
uint c;
|
|
|
|
for (c = 0; n; c++)
|
|
|
|
n &= n - 1;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void MATH_init(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2011-12-31 03:09:12 +01:00
|
|
|
uint seed;
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
randomize(FALSE, 0);
|
2011-12-31 03:09:12 +01:00
|
|
|
|
2011-12-31 02:02:42 +01:00
|
|
|
// Internet condom
|
2011-12-31 03:09:12 +01:00
|
|
|
do
|
|
|
|
seed = GFSR_random();
|
2015-10-19 21:30:17 +02:00
|
|
|
while ((seed & 1) == 0 || nbits(seed) < 16);
|
2011-12-31 03:09:12 +01:00
|
|
|
|
|
|
|
HASH_seed = seed;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
2012-04-28 17:08:24 +02:00
|
|
|
|
|
|
|
uint64_t pow10_uint64_p(int n)
|
|
|
|
{
|
|
|
|
uint64_t v = 1;
|
|
|
|
|
|
|
|
while (n > 8)
|
|
|
|
{
|
|
|
|
v *= 100000000;
|
|
|
|
n -= 8;
|
|
|
|
}
|
|
|
|
v *= _pow10_uint[n];
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|