source: soft/giet_vm/giet_libs/stdlib.c @ 639

Last change on this file since 639 was 580, checked in by laurent, 9 years ago

Try

File size: 3.1 KB
Line 
1//////////////////////////////////////////////////////////////////////////////////
2// File     : stdlib.c
3// Date     : 05/12/2013
4// Author   : Clément DEVIGNE
5// Copyright (c) UPMC-LIP6
6///////////////////////////////////////////////////////////////////////////////////
7
8#include <stdlib.h>
9
10///////////////////////////////////////////////////////////////////////////////////
11int atoi(const char *str)
12///////////////////////////////////////////////////////////////////////////////////
13{
14    int res  = 0; // Initialize result
15    int sign = 1; // Initialize sign as positive
16    int i    = 0; // Initialize index of first digit
17
18    if (str[0] == '-') //If number is negative, then update sign
19    {
20        sign = -1; 
21        i++;           // Also update index of first digit
22    }
23
24    for (; str[i] != '\0'; ++i) // Iterate through all digits and update the result
25    {
26        res = res*10 + str[i] - '0';
27    }
28
29    // Return result with sign
30    return sign*res;
31}
32
33///////////////////////////////////////////////////////////////////////////////////
34double atof(const char *str)
35///////////////////////////////////////////////////////////////////////////////////
36{
37    const char *pstr = str;
38    double res = 0;
39    double exp = 0.1;
40    short sign = 1;
41    short dec = 0;
42
43    while (*pstr != '\0')
44    {
45        if (*pstr == '-')
46        {
47            if (str != pstr) break;
48            sign = -1;
49        }
50       
51        else if (*pstr == '.')
52        {
53            if (dec) break;
54            dec = 1;
55        }
56       
57        else if (*pstr >= '0' && *pstr <= '9')
58        {
59            if (dec)
60            {
61                res = res + ((*pstr - '0')*exp);
62                exp = exp / 10;
63            }
64            else
65            {
66                res = (res * 10) + (*pstr - '0');
67            }
68        }
69       
70        else
71        {
72            break;
73        }
74        pstr++;
75    }
76    return sign * res;
77}
78
79////////////////////////////////////////////////////////////////////////////////////////
80void * memcpy(void *_dst, const void * _src, unsigned int size) 
81////////////////////////////////////////////////////////////////////////////////////////
82{
83    unsigned int * dst = _dst;
84    const unsigned int * src = _src;
85    if (!((unsigned int) dst & 3) && !((unsigned int) src & 3) )
86    {
87        while (size > 3) 
88        {
89            *dst++ = *src++;
90            size -= 4;
91        }
92    }
93
94    unsigned char *cdst = (unsigned char*)dst;
95    unsigned char *csrc = (unsigned char*)src;
96
97    while (size--) 
98    {
99        *cdst++ = *csrc++;
100    }
101    return _dst;
102}
103
104////////////////////////////////////////////////////////////////////////////////////////
105inline void * memset(void * dst, int s, unsigned int size) 
106////////////////////////////////////////////////////////////////////////////////////////
107{
108    char * a = (char *) dst;
109    while (size--)
110    {
111        *a++ = (char)s;
112    }
113    return dst;
114}
115
116// Local Variables:
117// tab-width: 4
118// c-basic-offset: 4
119// c-file-offsets:((innamespace . 0)(inline-open . 0))
120// indent-tabs-mode: nil
121// End:
122// vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4
Note: See TracBrowser for help on using the repository browser.