[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 | |
---|
| 35 | unsigned int rand_seed_value = 1; // set by srand() , used by rand() |
---|
| 36 | |
---|
[439] | 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 | |
---|
[589] | 122 | //////////////// |
---|
[476] | 123 | int rand( void ) |
---|
[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 | /////////////////////////////// |
---|
| 142 | void srand( unsigned int seed ) |
---|
[439] | 143 | { |
---|
[473] | 144 | rand_seed_value = seed; |
---|
[439] | 145 | } |
---|
| 146 | |
---|
[444] | 147 | ////////////////////////////////// |
---|
| 148 | void * malloc( unsigned int size ) |
---|
[439] | 149 | { |
---|
[444] | 150 | unsigned int cxy; |
---|
| 151 | unsigned int lid; |
---|
[439] | 152 | |
---|
[637] | 153 | // get cluster identifier |
---|
| 154 | get_core_id( &cxy , &lid ); |
---|
| 155 | |
---|
[444] | 156 | return remote_malloc( size, cxy ); |
---|
| 157 | } |
---|
[439] | 158 | |
---|
[444] | 159 | /////////////////////////////////// |
---|
| 160 | void * calloc ( unsigned int count, |
---|
| 161 | unsigned int size ) |
---|
| 162 | { |
---|
| 163 | unsigned int cxy; |
---|
| 164 | unsigned int lid; |
---|
[439] | 165 | |
---|
[637] | 166 | // get cluster identifier |
---|
| 167 | get_core_id( &cxy , &lid ); |
---|
| 168 | |
---|
[444] | 169 | return remote_calloc( count , size , cxy ); |
---|
| 170 | } |
---|
[439] | 171 | |
---|
[444] | 172 | /////////////////////////////////// |
---|
| 173 | void * realloc ( void * ptr, |
---|
| 174 | unsigned int size ) |
---|
[439] | 175 | { |
---|
[444] | 176 | unsigned int cxy; |
---|
| 177 | unsigned int lid; |
---|
[439] | 178 | |
---|
[637] | 179 | // get cluster identifier |
---|
| 180 | get_core_id( &cxy , &lid ); |
---|
| 181 | |
---|
[444] | 182 | return remote_realloc( ptr , size , cxy ); |
---|
[439] | 183 | } |
---|
| 184 | |
---|
[444] | 185 | /////////////////////// |
---|
| 186 | void free( void * ptr ) |
---|
[439] | 187 | { |
---|
[444] | 188 | unsigned int cxy; |
---|
| 189 | unsigned int lid; |
---|
[439] | 190 | |
---|
[637] | 191 | // get cluster identifier |
---|
| 192 | get_core_id( &cxy , &lid ); |
---|
| 193 | |
---|
[444] | 194 | remote_free( ptr , cxy ); |
---|
[439] | 195 | } |
---|
| 196 | |
---|
[444] | 197 | /////////////////////// |
---|
| 198 | void exit( int status ) |
---|
[439] | 199 | { |
---|
[444] | 200 | hal_user_syscall( SYS_EXIT, |
---|
| 201 | (reg_t)status, 0, 0, 0 ); |
---|
[439] | 202 | } |
---|
[443] | 203 | |
---|
| 204 | |
---|