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

Last change on this file since 483 was 476, checked in by viala@…, 6 years ago

[mini-libc] Add void type to function prototypes with no parameter

File size: 4.7 KB
Line 
1/*
2 * stdlib.c - User level <stdlib> 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 <hal_shared_types.h>
26#include <hal_user.h>
27#include <syscalls_numbers.h>
28#include <almosmkh.h>
29#include <stdio.h>
30
31//////////////////////////////////////////////////////////////////////////////////////////
32//           Global variables                   
33//////////////////////////////////////////////////////////////////////////////////////////
34
35unsigned int  rand_seed_value = 1;   // set by srand() , used by rand()
36
37//////////////////////////
38int atoi(const char * str)
39{
40    int res  = 0;      // Initialize result
41    int sign = 1;      // Initialize sign as positive
42    int i    = 0;      // Initialize index of first digit
43
44    if( (str[0] == '0') && ((str[1] == 'x') || (str[1] == 'X')) )   // hexa
45    {
46        i = 2;
47
48        while( str[i] != 0 )
49        {
50            if     ( (str[i] >= '0') && (str[i] <= '9') ) res = (res<<4) + (str[i] - '0');
51            else if( (str[i] >= 'A') && (str[i] <= 'F') ) res = (res<<4) + (str[i] - 'A');
52            else if( (str[i] >= 'a') && (str[i] <= 'f') ) res = (res<<4) + (str[i] - 'a');
53            else return 0;
54            i++;
55        }
56    }
57    else                                                            // decimal
58    {
59        if (str[0] == '-')  //  number is negative, update sign
60        {
61            sign = -1; 
62            i++;           // Also update index of first digit
63        }
64
65        while( str[i] != 0 )
66        {
67            if( (str[i] >= '0') && (str[i] <= '9') ) res = (res*10) + (str[i] - '0');
68            else return 0;
69            i++;
70        }
71    }
72
73    // Return result with sign
74    return sign*res;
75}
76
77////////////////////////////
78double atof(const char *str)
79{
80    const char *pstr = str;
81    double res = 0;
82    double exp = 0.1;
83    short sign = 1;
84    short dec = 0;
85
86    while (*pstr != '\0')
87    {
88        if (*pstr == '-')
89        {
90            if (str != pstr) break;
91            sign = -1;
92        }
93       
94        else if (*pstr == '.')
95        {
96            if (dec) break;
97            dec = 1;
98        }
99       
100        else if (*pstr >= '0' && *pstr <= '9')
101        {
102            if (dec)
103            {
104                res = res + ((*pstr - '0')*exp);
105                exp = exp / 10;
106            }
107            else
108            {
109                res = (res * 10) + (*pstr - '0');
110            }
111        }
112       
113        else
114        {
115            break;
116        }
117        pstr++;
118    }
119    return sign * res;
120}
121
122//////////
123int rand( void )
124{
125    unsigned long long cycle;
126
127    get_cycle( &cycle );
128
129    unsigned int x = (unsigned int)cycle * rand_seed_value;
130
131    if ((x & 0xFF) > 0x7F) 
132    {
133        return (x*x & RAND_MAX);
134    }
135    else 
136    {
137        return (x*x*x & RAND_MAX);
138    }
139}
140
141///////////////////////////////
142void srand( unsigned int seed )
143{
144    rand_seed_value = seed;
145}
146
147//////////////////////////////////
148void * malloc( unsigned int size )
149{
150    // get cluster identifier
151    unsigned int cxy;
152    unsigned int lid;
153    get_core( &cxy , &lid );
154
155    return remote_malloc( size, cxy );
156} 
157
158///////////////////////////////////
159void * calloc ( unsigned int count,
160                unsigned int size )
161{
162    // get calling core cluster identifier
163    unsigned int cxy;
164    unsigned int lid;
165    get_core( &cxy , &lid );
166
167    return remote_calloc( count , size , cxy );
168}
169
170///////////////////////////////////
171void * realloc ( void        * ptr,
172                 unsigned int  size )
173{
174    // get calling core cluster identifier
175    unsigned int cxy;
176    unsigned int lid;
177    get_core( &cxy , &lid );
178
179    return remote_realloc( ptr , size , cxy );
180}
181
182///////////////////////
183void free( void * ptr )
184{
185    // get calling core cluster identifier
186    unsigned int cxy;
187    unsigned int lid;
188    get_core( &cxy , &lid );
189
190    remote_free( ptr , cxy );
191}
192
193///////////////////////
194void exit( int status )
195{
196    hal_user_syscall( SYS_EXIT,
197                      (reg_t)status, 0, 0, 0 );
198}
199
200
Note: See TracBrowser for help on using the repository browser.