1 | /* |
---|
2 | * stdlib.c - User level <stdlib> library implementation. |
---|
3 | * |
---|
4 | * Author Alain Greiner (2016,2017,2018,2019) |
---|
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 | |
---|
35 | unsigned int rand_seed_value = 1; // set by srand() , used by rand() |
---|
36 | |
---|
37 | ////////////////////////// |
---|
38 | int 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 | //////////////////////////// |
---|
78 | double 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 | //////////////// |
---|
123 | int 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 | /////////////////////////////// |
---|
142 | void srand( unsigned int seed ) |
---|
143 | { |
---|
144 | rand_seed_value = seed; |
---|
145 | } |
---|
146 | |
---|
147 | ////////////////////////////////// |
---|
148 | void * malloc( unsigned int size ) |
---|
149 | { |
---|
150 | unsigned int cxy; |
---|
151 | unsigned int lid; |
---|
152 | |
---|
153 | // get cluster identifier |
---|
154 | get_core_id( &cxy , &lid ); |
---|
155 | |
---|
156 | return remote_malloc( size, cxy ); |
---|
157 | } |
---|
158 | |
---|
159 | /////////////////////////////////// |
---|
160 | void * calloc ( unsigned int count, |
---|
161 | unsigned int size ) |
---|
162 | { |
---|
163 | unsigned int cxy; |
---|
164 | unsigned int lid; |
---|
165 | |
---|
166 | // get cluster identifier |
---|
167 | get_core_id( &cxy , &lid ); |
---|
168 | |
---|
169 | return remote_calloc( count , size , cxy ); |
---|
170 | } |
---|
171 | |
---|
172 | /////////////////////////////////// |
---|
173 | void * realloc ( void * ptr, |
---|
174 | unsigned int size ) |
---|
175 | { |
---|
176 | unsigned int cxy; |
---|
177 | unsigned int lid; |
---|
178 | |
---|
179 | // get cluster identifier |
---|
180 | get_core_id( &cxy , &lid ); |
---|
181 | |
---|
182 | return remote_realloc( ptr , size , cxy ); |
---|
183 | } |
---|
184 | |
---|
185 | /////////////////////// |
---|
186 | void free( void * ptr ) |
---|
187 | { |
---|
188 | unsigned int cxy; |
---|
189 | unsigned int lid; |
---|
190 | |
---|
191 | // get cluster identifier |
---|
192 | get_core_id( &cxy , &lid ); |
---|
193 | |
---|
194 | remote_free( ptr , cxy ); |
---|
195 | } |
---|
196 | |
---|
197 | /////////////////////// |
---|
198 | void exit( int status ) |
---|
199 | { |
---|
200 | hal_user_syscall( SYS_EXIT, |
---|
201 | (reg_t)status, 0, 0, 0 ); |
---|
202 | } |
---|
203 | |
---|
204 | |
---|