source: trunk/libs/mini-libc/stdlib.c @ 444

Last change on this file since 444 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 4.4 KB
Line 
1/*
2 * stdlib.c - User level C library implementation.
3 *
4 * Author     Alain Greiner (2016,2017)
5 *
6 * Copyright (c) UPMC Sorbonne Universites
7 *
8 * This file is part of ALMOS-MKH.
9 *
10 * ALMOS-MKH is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2.0 of the License.
13 *
14 * ALMOS-MKH is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24#include <stdlib.h>
25#include <stdio.h>
26#include <almos-mkh.h>
27#include <hal_user.h>
28#include <syscalls_numbers.h>
29
30#define  reg_t     int
31//////////////////////////
32int atoi(const char * str)
33{
34    int res  = 0;      // Initialize result
35    int sign = 1;      // Initialize sign as positive
36    int i    = 0;      // Initialize index of first digit
37
38    if( (str[0] == '0') && ((str[1] == 'x') || (str[1] == 'X')) )   // hexa
39    {
40        i = 2;
41
42        while( str[i] != 0 )
43        {
44            if     ( (str[i] >= '0') && (str[i] <= '9') ) res = (res<<4) + (str[i] - '0');
45            else if( (str[i] >= 'A') && (str[i] <= 'F') ) res = (res<<4) + (str[i] - 'A');
46            else if( (str[i] >= 'a') && (str[i] <= 'f') ) res = (res<<4) + (str[i] - 'a');
47            else return 0;
48            i++;
49        }
50    }
51    else                                                            // decimal
52    {
53        if (str[0] == '-')  //  number is negative, update sign
54        {
55            sign = -1; 
56            i++;           // Also update index of first digit
57        }
58
59        while( str[i] != 0 )
60        {
61            if( (str[i] >= '0') && (str[i] <= '9') ) res = (res*10) + (str[i] - '0');
62            else return 0;
63            i++;
64        }
65    }
66
67    // Return result with sign
68    return sign*res;
69}
70
71////////////////////////////
72double atof(const char *str)
73{
74    const char *pstr = str;
75    double res = 0;
76    double exp = 0.1;
77    short sign = 1;
78    short dec = 0;
79
80    while (*pstr != '\0')
81    {
82        if (*pstr == '-')
83        {
84            if (str != pstr) break;
85            sign = -1;
86        }
87       
88        else if (*pstr == '.')
89        {
90            if (dec) break;
91            dec = 1;
92        }
93       
94        else if (*pstr >= '0' && *pstr <= '9')
95        {
96            if (dec)
97            {
98                res = res + ((*pstr - '0')*exp);
99                exp = exp / 10;
100            }
101            else
102            {
103                res = (res * 10) + (*pstr - '0');
104            }
105        }
106       
107        else
108        {
109            break;
110        }
111        pstr++;
112    }
113    return sign * res;
114}
115
116
117
118//////////
119int rand()
120{
121    unsigned long long cycle;
122
123    get_cycle( &cycle );
124
125    unsigned int x = (unsigned int)cycle;
126
127    if ((x & 0xF) > 7) 
128    {
129        return (x*x & 0xFFFF);
130    }
131    else 
132    {
133        return (x*x*x & 0xFFFF);
134    }
135}
136
137///////////////////////////////
138void srand( unsigned int seed )
139{
140    printf("\n[ERROR] in %s : not implemented yet : do nothing\n", __FUNCTION__ );
141}
142
143
144
145
146//////////////////////////////////
147void * malloc( unsigned int size )
148{
149    // get cluster identifier
150    unsigned int cxy;
151    unsigned int lid;
152    get_core( &cxy , &lid );
153
154    return remote_malloc( size, cxy );
155} 
156
157
158
159///////////////////////////////////
160void * calloc ( unsigned int count,
161                unsigned int size )
162{
163    // get calling core cluster identifier
164    unsigned int cxy;
165    unsigned int lid;
166    get_core( &cxy , &lid );
167
168    return remote_calloc( count , size , cxy );
169}
170
171///////////////////////////////////
172void * realloc ( void        * ptr,
173                 unsigned int  size )
174{
175    // get calling core cluster identifier
176    unsigned int cxy;
177    unsigned int lid;
178    get_core( &cxy , &lid );
179
180    return remote_realloc( ptr , size , cxy );
181}
182
183///////////////////////
184void free( void * ptr )
185{
186    // get calling core cluster identifier
187    unsigned int cxy;
188    unsigned int lid;
189    get_core( &cxy , &lid );
190
191    remote_free( ptr , cxy );
192}
193
194///////////////////////
195void exit( int status )
196{
197    hal_user_syscall( SYS_EXIT,
198                      (reg_t)status, 0, 0, 0 );
199}
200
201
Note: See TracBrowser for help on using the repository browser.