aboutsummaryrefslogtreecommitdiff
path: root/libc/stdlib.c
blob: bea3b650dfc2ecbb040b02c8b5d5d1681cc5d1f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdlib.h>

/* adapted from <http://www.strudel.org.uk/itoa/> */
char* itoa(int val, int base)
{
    static char buf[32] = {0};

    int neg = 0;

    if(val < 0)
    {
        val = -val;
        neg = 1;
    }

    int i = 30;

    do
    {
        buf[i] = "0123456789abcdef"[val % base];
        --i;
        val /= base;
    }
    while(val && i);

    if(neg)
    {
        buf[i] = '-';
    }

    return &buf[i+(neg?0:1)];
}

static unsigned long rand_s1 = 18, rand_s2 = 5, rand_s3 = 43;

#define M 1103515245UL
#define A 12345UL

/* this is a very fast tausworthe RNG */

unsigned int rand(void)
{
    rand_s1 = (((rand_s1&4294967294)<<12)^(((rand_s1<<13)^rand_s1)>>19));
    rand_s2 = (((rand_s2&4294967288)<< 4)^(((rand_s2<< 2)^rand_s2)>>25));
    rand_s3 = (((rand_s3&4294967280)<<17)^(((rand_s3<< 3)^rand_s3)>>11));
    return (rand_s1 ^ rand_s2 ^ rand_s3);
}

void srand(unsigned int seed)
{
    if(!seed)
        seed = 42;
    rand_s1 = seed++;
    rand_s2 = seed++;
    rand_s3 = seed++;
    /* "warm it up" */
    rand();
    rand();
    rand();
}

int abs(int val)
{
    return (val<0?-val:val);
}