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

Last change on this file since 473 was 473, checked in by alain, 6 years ago

Fix several GCC warning related to the -Wextra compilation option.

File size: 4.7 KB
RevLine 
[439]1/*
[445]2 * stdlib.c - User level <stdlib> library implementation.
[439]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
[444]24#include <stdlib.h>
[457]25#include <hal_shared_types.h>
[445]26#include <hal_user.h>
[457]27#include <syscalls_numbers.h>
[445]28#include <almosmkh.h>
[439]29#include <stdio.h>
30
[473]31//////////////////////////////////////////////////////////////////////////////////////////
32//           Global variables                   
33//////////////////////////////////////////////////////////////////////////////////////////
34
35unsigned int  rand_seed_value = 1;   // set by srand() , used by rand()
36
[439]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
[444]122//////////
123int rand()
[439]124{
[444]125    unsigned long long cycle;
[439]126
[444]127    get_cycle( &cycle );
[439]128
[473]129    unsigned int x = (unsigned int)cycle * rand_seed_value;
[439]130
[473]131    if ((x & 0xFF) > 0x7F) 
[439]132    {
[473]133        return (x*x & RAND_MAX);
[439]134    }
135    else 
136    {
[473]137        return (x*x*x & RAND_MAX);
[439]138    }
139}
140
[444]141///////////////////////////////
142void srand( unsigned int seed )
[439]143{
[473]144    rand_seed_value = seed;
[439]145}
146
[444]147//////////////////////////////////
148void * malloc( unsigned int size )
[439]149{
[444]150    // get cluster identifier
151    unsigned int cxy;
152    unsigned int lid;
153    get_core( &cxy , &lid );
[439]154
[444]155    return remote_malloc( size, cxy );
156} 
[439]157
[444]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 );
[439]166
[444]167    return remote_calloc( count , size , cxy );
168}
[439]169
[444]170///////////////////////////////////
171void * realloc ( void        * ptr,
172                 unsigned int  size )
[439]173{
[444]174    // get calling core cluster identifier
175    unsigned int cxy;
176    unsigned int lid;
177    get_core( &cxy , &lid );
[439]178
[444]179    return remote_realloc( ptr , size , cxy );
[439]180}
181
[444]182///////////////////////
183void free( void * ptr )
[439]184{
[444]185    // get calling core cluster identifier
186    unsigned int cxy;
187    unsigned int lid;
188    get_core( &cxy , &lid );
[439]189
[444]190    remote_free( ptr , cxy );
[439]191}
192
[444]193///////////////////////
194void exit( int status )
[439]195{
[444]196    hal_user_syscall( SYS_EXIT,
197                      (reg_t)status, 0, 0, 0 );
[439]198}
[443]199
200
Note: See TracBrowser for help on using the repository browser.