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"
|
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-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
|
|
|
/*******************************************************************************/
|