|
0
|
1 #ifndef CYGONCE_LIBC_STDLIB_INL |
|
|
2 #define CYGONCE_LIBC_STDLIB_INL |
|
|
3 //=========================================================================== |
|
|
4 // |
|
|
5 // stdlib.inl |
|
|
6 // |
|
|
7 // Inline implementations for the ANSI standard utility functions |
|
|
8 // defined in section 7.10 of the standard |
|
|
9 // |
|
|
10 //=========================================================================== |
|
|
11 //####COPYRIGHTBEGIN#### |
|
|
12 // |
|
|
13 // ------------------------------------------- |
|
|
14 // The contents of this file are subject to the Cygnus eCos Public License |
|
|
15 // Version 1.0 (the "License"); you may not use this file except in |
|
|
16 // compliance with the License. You may obtain a copy of the License at |
|
|
17 // http://sourceware.cygnus.com/ecos |
|
|
18 // |
|
|
19 // Software distributed under the License is distributed on an "AS IS" |
|
|
20 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
|
|
21 // License for the specific language governing rights and limitations under |
|
|
22 // the License. |
|
|
23 // |
|
|
24 // The Original Code is eCos - Embedded Cygnus Operating System, released |
|
|
25 // September 30, 1998. |
|
|
26 // |
|
|
27 // The Initial Developer of the Original Code is Cygnus. Portions created |
|
|
28 // by Cygnus are Copyright (C) 1998 Cygnus Solutions. All Rights Reserved. |
|
|
29 // ------------------------------------------- |
|
|
30 // |
|
|
31 //####COPYRIGHTEND#### |
|
|
32 //=========================================================================== |
|
|
33 //#####DESCRIPTIONBEGIN#### |
|
|
34 // |
|
|
35 // Author(s): jlarmour |
|
|
36 // Contributors: jlarmour@cygnus.co.uk |
|
|
37 // Date: 1998-02-13 |
|
|
38 // Purpose: |
|
|
39 // Description: |
|
|
40 // Usage: #include <stdlib.h> - do not include this file directly |
|
|
41 // |
|
|
42 //####DESCRIPTIONEND#### |
|
|
43 // |
|
|
44 //=========================================================================== |
|
|
45 // |
|
|
46 // The div() and ldiv() functions in this file are based on original |
|
|
47 // code with the following copyright: |
|
|
48 // |
|
|
49 // Note that only div() and ldiv() have this copyright |
|
|
50 // |
|
|
51 /* |
|
|
52 * Copyright (c) 1990 Regents of the University of California. |
|
|
53 * All rights reserved. |
|
|
54 * |
|
|
55 * This code is derived from software contributed to Berkeley by |
|
|
56 * Chris Torek. |
|
|
57 * |
|
|
58 * Redistribution and use in source and binary forms, with or without |
|
|
59 * modification, are permitted provided that the following conditions |
|
|
60 * are met: |
|
|
61 * 1. Redistributions of source code must retain the above copyright |
|
|
62 * notice, this list of conditions and the following disclaimer. |
|
|
63 * 2. Redistributions in binary form must reproduce the above copyright |
|
|
64 * notice, this list of conditions and the following disclaimer in the |
|
|
65 * documentation and/or other materials provided with the distribution. |
|
|
66 * 3. All advertising materials mentioning features or use of this software |
|
|
67 * must display the following acknowledgement: |
|
|
68 * This product includes software developed by the University of |
|
|
69 * California, Berkeley and its contributors. |
|
|
70 * 4. Neither the name of the University nor the names of its contributors |
|
|
71 * may be used to endorse or promote products derived from this software |
|
|
72 * without specific prior written permission. |
|
|
73 * |
|
|
74 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
|
|
75 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
|
76 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
|
77 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
|
|
78 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|
|
79 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
|
|
80 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|
|
81 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|
|
82 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
|
|
83 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
|
|
84 * SUCH DAMAGE. |
|
|
85 */ |
|
|
86 |
|
|
87 // CONFIGURATION |
|
|
88 |
|
|
89 #include <pkgconf/libc.h> // Configuration header |
|
|
90 |
|
|
91 // Include the C library? |
|
|
92 #ifdef CYGPKG_LIBC |
|
|
93 |
|
|
94 // INCLUDES |
|
|
95 |
|
|
96 #include <stddef.h> // NULL, wchar_t and size_t from compiler |
|
|
97 #include <stdlib.h> // Just to be sure that the main header is there |
|
|
98 |
|
|
99 // FUNCTIONS |
|
|
100 |
|
|
101 //=========================================================================== |
|
|
102 |
|
|
103 // 7.10.1 String conversion functions |
|
|
104 |
|
|
105 |
|
|
106 CYGPRI_LIBC_INLINE double |
|
|
107 atof( const char *nptr ) |
|
|
108 { |
|
|
109 return strtod( nptr, (char **)NULL ); |
|
|
110 } // atof() |
|
|
111 |
|
|
112 CYGPRI_LIBC_INLINE int |
|
|
113 atoi( const char *nptr ) |
|
|
114 { |
|
|
115 return (int)strtol( nptr, (char **)NULL, 10 ); |
|
|
116 } // atoi() |
|
|
117 |
|
|
118 |
|
|
119 CYGPRI_LIBC_INLINE long |
|
|
120 atol( const char *nptr ) |
|
|
121 { |
|
|
122 return strtol( nptr, (char **)NULL, 10 ); |
|
|
123 } // atol() |
|
|
124 |
|
|
125 //=========================================================================== |
|
|
126 |
|
|
127 // 7.10.6 Integer arithmetic functions |
|
|
128 |
|
|
129 |
|
|
130 CYGPRI_LIBC_INLINE int |
|
|
131 abs( int j ) |
|
|
132 { |
|
|
133 return (j<0) ? -j : j; |
|
|
134 } // abs() |
|
|
135 |
|
|
136 |
|
|
137 CYGPRI_LIBC_INLINE div_t |
|
|
138 div( int numer, int denom ) |
|
|
139 { |
|
|
140 div_t result; |
|
|
141 |
|
|
142 // The ANSI standard says that |r.quot| <= |n/d|, where |
|
|
143 // n/d is to be computed in infinite precision. In other |
|
|
144 // words, we should always truncate the quotient towards |
|
|
145 // 0, never -infinity. |
|
|
146 // |
|
|
147 // Machine division and remainer may work either way when |
|
|
148 // one or both of n or d is negative. If only one is |
|
|
149 // negative and r.quot has been truncated towards -inf, |
|
|
150 // r.rem will have the same sign as denom and the opposite |
|
|
151 // sign of num; if both are negative and r.quot has been |
|
|
152 // truncated towards -inf, r.rem will be positive (will |
|
|
153 // have the opposite sign of num). These are considered |
|
|
154 // `wrong'. |
|
|
155 // |
|
|
156 // If both are num and denom are positive, r will always |
|
|
157 // be positive. |
|
|
158 // |
|
|
159 // This all boils down to: |
|
|
160 // if num >= 0, but r.rem < 0, we got the wrong answer. |
|
|
161 // In that case, to get the right answer, add 1 to r.quot and |
|
|
162 // subtract denom from r.rem. |
|
|
163 |
|
|
164 result.quot = numer / denom; |
|
|
165 result.rem = numer % denom; |
|
|
166 |
|
|
167 if ( (numer >= 0) && (result.rem < 0) ) |
|
|
168 { |
|
|
169 result.quot++; |
|
|
170 result.rem -= denom; |
|
|
171 } // if |
|
|
172 |
|
|
173 return result; |
|
|
174 } // div() |
|
|
175 |
|
|
176 |
|
|
177 CYGPRI_LIBC_INLINE long |
|
|
178 labs( long j ) |
|
|
179 { |
|
|
180 return (j<0) ? -j : j; |
|
|
181 } // labs() |
|
|
182 |
|
|
183 |
|
|
184 CYGPRI_LIBC_INLINE ldiv_t |
|
|
185 ldiv( long numer, long denom ) |
|
|
186 { |
|
|
187 ldiv_t result; |
|
|
188 |
|
|
189 result.quot = numer / denom; |
|
|
190 result.rem = numer % denom; |
|
|
191 |
|
|
192 if ( (numer >= 0) && (result.rem < 0) ) |
|
|
193 { |
|
|
194 result.quot++; |
|
|
195 result.rem -= denom; |
|
|
196 } // if |
|
|
197 |
|
|
198 return result; |
|
|
199 } // ldiv() |
|
|
200 |
|
|
201 |
|
|
202 #endif // ifdef CYGPKG_LIBC |
|
|
203 |
|
|
204 #endif // CYGONCE_LIBC_STDLIB_INL multiple inclusion protection |
|
|
205 |
|
|
206 // EOF stdlib.inl |