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 | int 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 | //////////////////////////// |
---|
72 | double 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 | int rand() |
---|
118 | { |
---|
119 | unsigned long long cycle; |
---|
120 | |
---|
121 | get_cycle( &cycle ); |
---|
122 | |
---|
123 | unsigned int x = (unsigned int)cycle; |
---|
124 | |
---|
125 | if ((x & 0xF) > 7) |
---|
126 | { |
---|
127 | return (x*x & 0xFFFF); |
---|
128 | } |
---|
129 | else |
---|
130 | { |
---|
131 | return (x*x*x & 0xFFFF); |
---|
132 | } |
---|
133 | } |
---|
134 | |
---|
135 | /////////////////////////////// |
---|
136 | void srand( unsigned int seed ) |
---|
137 | { |
---|
138 | printf("\n[ERROR] in %s : not implemented yet : do nothing\n", __FUNCTION__ ); |
---|
139 | } |
---|
140 | |
---|
141 | ////////////////////////////////// |
---|
142 | void * malloc( unsigned int size ) |
---|
143 | { |
---|
144 | // get cluster identifier |
---|
145 | unsigned int cxy; |
---|
146 | unsigned int lid; |
---|
147 | get_core( &cxy , &lid ); |
---|
148 | |
---|
149 | return remote_malloc( size, cxy ); |
---|
150 | } |
---|
151 | |
---|
152 | /////////////////////////////////// |
---|
153 | void * calloc ( unsigned int count, |
---|
154 | unsigned int size ) |
---|
155 | { |
---|
156 | // get calling core cluster identifier |
---|
157 | unsigned int cxy; |
---|
158 | unsigned int lid; |
---|
159 | get_core( &cxy , &lid ); |
---|
160 | |
---|
161 | return remote_calloc( count , size , cxy ); |
---|
162 | } |
---|
163 | |
---|
164 | /////////////////////////////////// |
---|
165 | void * realloc ( void * ptr, |
---|
166 | unsigned int size ) |
---|
167 | { |
---|
168 | // get calling core cluster identifier |
---|
169 | unsigned int cxy; |
---|
170 | unsigned int lid; |
---|
171 | get_core( &cxy , &lid ); |
---|
172 | |
---|
173 | return remote_realloc( ptr , size , cxy ); |
---|
174 | } |
---|
175 | |
---|
176 | /////////////////////// |
---|
177 | void free( void * ptr ) |
---|
178 | { |
---|
179 | // get calling core cluster identifier |
---|
180 | unsigned int cxy; |
---|
181 | unsigned int lid; |
---|
182 | get_core( &cxy , &lid ); |
---|
183 | |
---|
184 | remote_free( ptr , cxy ); |
---|
185 | } |
---|
186 | |
---|
187 | /////////////////////// |
---|
188 | void exit( int status ) |
---|
189 | { |
---|
190 | hal_user_syscall( SYS_EXIT, |
---|
191 | (reg_t)status, 0, 0, 0 ); |
---|
192 | } |
---|
193 | |
---|
194 | |
---|