OpenCores
URL https://opencores.org/ocsvn/thor/thor/trunk

Subversion Repositories thor

[/] [thor/] [trunk/] [software/] [c64libc/] [source/] [stdlib.c] - Blame information for rev 33

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 22 robfinch
#include <stdlib.h>
2
 
3
int (abs)(int i)
4
{
5
        return ((i < 0) ? -i : i);
6
}
7
 
8
div_t (div)(int numer, int denom)
9
{
10
        div_t val;
11
 
12
        val.quot = numer / denom;
13
        val.rem = numer - denom * val.quot;
14
        if (val.quot < 0 && 0 < val.rem) {
15
                val.quot += 1;
16
                val.rem -= denom;
17
        }
18
        return (val);
19
}
20
 
21
long (labs)(long i)
22
{
23
        return ((i < 0) ? -i : i);
24
}
25
 
26
ldiv_t (ldiv)(long numer, long denom)
27
{
28
        ldiv_t val;
29
 
30
        val.quot = numer / denom;
31
        val.rem = numer - denom * val.quot;
32
        if (val.quot < 0 && 0 < val.rem) {
33
                val.quot += 1;
34
                val.rem -= denom;
35
        }
36
        return (val);
37
}
38
 
39
void *(bsearch)(const void *key, const void *base, size_t nelem, size_t size, _Cmpfun *cmp)
40
{
41
        const char *p;
42
        size_t n;
43
 
44
        p = base;
45
        for (p = base, n = nelem; 0 < n;) {
46
                const size_t pivot = n >> 1;
47
                const char *q = p + size * pivot;
48
                const int val = (*cmp)(key,q);
49
 
50
                if (val < 0)
51
                        n = pivot;
52
                else if (val == 0)
53
                        return ((void *)q);
54
                else {
55
                        p = q + size;
56
                        n -= pivot + 1;
57
                }
58
        }
59
        return NULL;
60
}
61
 
62
void (qsort)(void *base, size_t n, size_t size, _Cmpfun *cmp)
63
{
64
        size_t i;
65
        size_t j;
66
        char *qi;
67
        char *qj;
68
        char *qp;
69
        char *q1;
70
        char *q2;
71
        char buf[256];
72
        size_t m, ms;
73
 
74
        while (1 < n) {
75
                i = 0;
76
                j = n - 1;
77
                qi = base;
78
                qj = qi + size * j;
79
                qp = qj;
80
 
81
                while (i < j) {
82
                        while (i < j && (*cmp)(qi,qp) <= 0)
83
                                ++i, qi += size;
84
                        while (i < j && (*cmp)(qp,qj) <= 0)
85
                                --j, qj -= size;
86
                        if (i < j) {
87
                                for (ms = size; 0 < ms; ms -= m, q1 += m, q2 -= m) {
88
                                        m = ms < sizeof(buf) ? ms : sizeof(buf);
89
                                        memcpy(buf, q1, m);
90
                                        memcpy(q1, q2, m);
91
                                        memcpy(q2, buf, m);
92
                                }
93
                                ++i, qi += size;
94
                                --j, qj -= size;
95
                        }
96
                }
97
                if (qi != qp) {
98
                        for (m = size; 0 < ms; ms -= m, q1 += m, q2 -= m) {
99
                                m = ms < sizeof(buf) ? ms : sizeof(buf);
100
                                memcpy(buf, q1, m);
101
                                memcpy(q1, q2, m);
102
                                memcpy(q2, buf, m);
103
                        }
104
                }
105
                j = n - i;
106
                if (j < i) {
107
                        if (1 < j)
108
                                qsort(qi, j, size, cmp);
109
                        n = i;
110
                }
111
                else {
112
                        if (1 < i)
113
                                qsort(base, i, size, cmp);
114
                        base = qi;
115
                        n = j;
116
                }
117
        }
118
}
119
 
120
 
121
// seed the random number generator
122
void (srand)(unsigned int seed)
123
{
124
        _Randseed = seed;
125
}
126
 
127
// generate a random number in the range 0 to max-1
128
int (rand)(void)
129
{
130
        _Randseed = _Randseed * 1103515245 + 12345;
131
        return ((unsigned int)(_Randseed >> 16) & RAND_MAX);
132
}
133
 
134
int (atoi)(const char *s)
135
{
136
        return ((int)_Stoul(s, NULL, 10));
137
}
138
 
139
long (atol)(const char *s)
140
{
141
        return ((long)_Stoul(s, NULL, 10));
142
}
143
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.