2018-09-28 20:35:51 +02:00
|
|
|
/*******************************************************************************/
|
|
|
|
/* COS2000 - Compatible Operating System - LGPL v3 - Hordé Nicolas */
|
|
|
|
/* */
|
2007-04-02 16:34:32 +02:00
|
|
|
#include "types.h"
|
2018-08-22 17:36:30 +02:00
|
|
|
#include "timer.h"
|
2018-10-19 10:13:29 +02:00
|
|
|
#include "math.h"
|
2007-04-02 16:34:32 +02:00
|
|
|
|
2018-09-28 20:35:51 +02:00
|
|
|
/*******************************************************************************/
|
|
|
|
/* Arithmétique 64 bits */
|
2018-09-27 21:01:02 +02:00
|
|
|
|
|
|
|
unsigned long long __udivdi3 (unsigned long long num, unsigned long long den)
|
|
|
|
{
|
|
|
|
|
|
|
|
unsigned long long quot, qbit;
|
|
|
|
|
|
|
|
quot = 0;
|
|
|
|
qbit = 1;
|
|
|
|
|
|
|
|
if (den == 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((long long) den >= 0)
|
|
|
|
{
|
|
|
|
den <<= 1;
|
|
|
|
qbit <<= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (qbit)
|
|
|
|
{
|
|
|
|
if (den <= num)
|
|
|
|
{
|
|
|
|
num -= den;
|
|
|
|
quot += qbit;
|
|
|
|
}
|
|
|
|
den >>= 1;
|
|
|
|
qbit >>= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return quot;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long long __umoddi3 (unsigned long long n, unsigned long long d)
|
|
|
|
{
|
|
|
|
return n - d * __udivdi3 (n, d);
|
|
|
|
}
|
|
|
|
|
2018-10-19 10:13:29 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
/* Fonctions qui retournent le sinus et cosinus */
|
|
|
|
|
|
|
|
double cos(double x){
|
|
|
|
if( x < 0.0 )
|
|
|
|
x = -x;
|
|
|
|
while( M_PI < x )
|
|
|
|
x -= M_2_PI;
|
2018-10-26 14:09:43 +02:00
|
|
|
double result=1.0 - (x*x/2.0)*( 1.0 - (x*x/12.0) * ( 1.0 - (x*x/30.0) * (1.0 - x*x/56.0 )));
|
|
|
|
return result;
|
2018-10-19 10:13:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
double sin(double x){
|
|
|
|
return cos(x-M_PI_2);
|
|
|
|
}
|
|
|
|
|
|
|
|
float cosf(float x){
|
|
|
|
if( x < 0.0f )
|
|
|
|
x = -x;
|
|
|
|
while( M_PI < x )
|
|
|
|
x -= M_2_PI;
|
2018-10-26 14:09:43 +02:00
|
|
|
float result=1.0f - (x*x/2.0f)*( 1.0f - (x*x/12.0f) * ( 1.0f - (x*x/30.0f) * (1.0f - x*x/56.0f )));
|
|
|
|
return result;
|
2018-10-19 10:13:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
float sinf(float x){
|
2018-10-26 14:09:43 +02:00
|
|
|
return cosf(x-M_PI_2);
|
2018-10-19 10:13:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/* Fonction qui retourne la valeur absolue */
|
|
|
|
|
|
|
|
float fabsf(float n)
|
|
|
|
{
|
|
|
|
return (*((int *) &n) &= 0x7fffffff);
|
|
|
|
}
|
|
|
|
|
|
|
|
double fabs(double n)
|
|
|
|
{
|
|
|
|
return (*(((int *) &n) + 1) &= 0x7fffffff);
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/* Fonction qui retourne la racine */
|
|
|
|
|
|
|
|
float sqrtf(float n)
|
|
|
|
{
|
|
|
|
|
|
|
|
float x = n;
|
|
|
|
float y = 1;
|
|
|
|
double e = 0.000001;
|
|
|
|
while(x - y > e)
|
|
|
|
{
|
|
|
|
x = (x + y)/2;
|
|
|
|
y = n/x;
|
|
|
|
}
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
double sqrt(double n)
|
|
|
|
{
|
|
|
|
|
|
|
|
double x = n;
|
|
|
|
double y = 1;
|
|
|
|
double e = 0.000001;
|
|
|
|
while(x - y > e)
|
|
|
|
{
|
|
|
|
x = (x + y)/2;
|
|
|
|
y = n/x;
|
|
|
|
}
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/* Fonction qui retourne l'inverse de la racine */
|
|
|
|
|
|
|
|
float rsqrtf(float n)
|
|
|
|
{
|
|
|
|
return 1 / sqrt(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
double rsqrt(double n)
|
|
|
|
{
|
|
|
|
return 1 / sqrt(n);
|
|
|
|
}
|
|
|
|
|
2018-10-04 21:49:06 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
/* Fonction qui retourne la puissance n de a */
|
|
|
|
|
|
|
|
u32 pow(u32 a, u8 n) {
|
|
|
|
u32 r = 1;
|
|
|
|
while (n > 0) {
|
|
|
|
if (n & 1)
|
|
|
|
r *= a;
|
|
|
|
a *= a;
|
|
|
|
n >>= 1;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2018-08-29 10:12:00 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
/* Fonction qui retourne le logarithme 2 */
|
2018-09-27 21:01:02 +02:00
|
|
|
|
|
|
|
u8 log2(u64 n)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (n == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
int logValue = -1;
|
|
|
|
|
|
|
|
while (n) {
|
|
|
|
|
|
|
|
logValue++;
|
|
|
|
|
|
|
|
n >>= 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return logValue + 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-08-29 10:12:00 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
/* Fonction qui retourne le logarithme 10 */
|
2018-09-27 21:01:02 +02:00
|
|
|
|
|
|
|
u8 log10(u64 n)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (n >= 10000000000000000000u) ? 19 : (n >= 100000000000000000u) ? 18 :
|
|
|
|
(n >= 100000000000000000u) ? 17 : (n >= 10000000000000000u) ? 16 :
|
|
|
|
(n >= 1000000000000000u) ? 15 : (n >= 100000000000000u) ? 14 :
|
|
|
|
(n >= 10000000000000u) ? 13 : (n >= 1000000000000u) ? 12 :
|
|
|
|
(n >= 100000000000u) ? 11 : (n >= 10000000000u) ? 10 :
|
|
|
|
(n >= 1000000000u) ? 9 : (n >= 100000000u) ? 8 :
|
2018-09-27 17:47:27 +02:00
|
|
|
(n >= 10000000u) ? 7 : (n >= 1000000u) ? 6 :
|
|
|
|
(n >= 100000u) ? 5 : (n >= 10000u) ? 4 :
|
2018-09-27 21:01:02 +02:00
|
|
|
(n >= 1000u) ? 3 : (n >= 100u) ? 2 : (n >= 10u) ? 1u : 0u;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-28 20:35:51 +02:00
|
|
|
/******************************************************************************/
|
2018-08-17 16:46:56 +02:00
|
|
|
/* Fonction qui retourne la valeur absolue */
|
2018-09-27 21:01:02 +02:00
|
|
|
|
|
|
|
u32 abs(int x)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (x < 0)
|
|
|
|
|
|
|
|
x = -x;
|
|
|
|
|
|
|
|
return (u32) x;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-08-22 17:36:30 +02:00
|
|
|
/******************************************************************************/
|
2018-09-27 17:47:27 +02:00
|
|
|
/* Fonction qui initialise le générateur de nombre aléatoire */
|
2018-09-27 21:01:02 +02:00
|
|
|
|
|
|
|
static u32 seed = 0x12341234;
|
|
|
|
|
|
|
|
|
2018-09-28 20:35:51 +02:00
|
|
|
void randomize(void)
|
2018-09-27 21:01:02 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
seed = gettimer();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-22 17:36:30 +02:00
|
|
|
/******************************************************************************/
|
2018-09-27 17:47:27 +02:00
|
|
|
/* Fonction qui renvoie un nombre aléatoire */
|
2018-09-27 21:01:02 +02:00
|
|
|
|
2018-09-28 20:35:51 +02:00
|
|
|
u32 rand(void)
|
2018-09-27 21:01:02 +02:00
|
|
|
{
|
|
|
|
u32 next = seed;
|
|
|
|
int result;
|
|
|
|
next *= 1103515245;
|
|
|
|
next += 12345;
|
|
|
|
result = (unsigned int)(next / 65536) % 2048;
|
|
|
|
next *= 1103515245;
|
|
|
|
next += 12345;
|
|
|
|
result <<= 10;
|
|
|
|
result ^= (unsigned int)(next / 65536) % 1024;
|
|
|
|
next *= 1103515245;
|
|
|
|
next += 12345;
|
|
|
|
result <<= 10;
|
|
|
|
result ^= (unsigned int)(next / 65536) % 1024;
|
|
|
|
seed = next;
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-08-22 17:36:30 +02:00
|
|
|
/******************************************************************************/
|
2018-09-27 17:47:27 +02:00
|
|
|
/* Fonction qui renvoie un nombre aléatoire borné */
|
2018-09-27 21:01:02 +02:00
|
|
|
|
|
|
|
u32 random(u32 lower, u32 upper)
|
2018-09-28 20:35:51 +02:00
|
|
|
{
|
2018-09-27 21:01:02 +02:00
|
|
|
return (rand() % (upper - lower + 1)) + lower;
|
|
|
|
}
|
|
|
|
|
2018-09-28 20:35:51 +02:00
|
|
|
/*******************************************************************************/
|