Mercurial > ecos
annotate packages/language/c/libc/current/include/stdlib.inl @ 76:435cced73e2f ecos-v1_3_1-release
eCos v1.3.1 merged from eCos master repository on 2000-03-27-23:22:51-BST
| author | jlarmour |
|---|---|
| date | Tue, 28 Mar 2000 14:10:45 +0000 |
| parents | bf00f99aec69 |
| children |
| rev | line source |
|---|---|
| 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#### | |
|
64
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
12 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
13 // ------------------------------------------- |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
14 // The contents of this file are subject to the Red Hat eCos Public License |
|
66
bf00f99aec69
Merge from eCos master repository on 2000-02-02-19:16:44-GMT
jlarmour
parents:
64
diff
changeset
|
15 // Version 1.1 (the "License"); you may not use this file except in |
|
64
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
16 // compliance with the License. You may obtain a copy of the License at |
|
66
bf00f99aec69
Merge from eCos master repository on 2000-02-02-19:16:44-GMT
jlarmour
parents:
64
diff
changeset
|
17 // http://www.redhat.com/ |
|
64
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
18 // |
|
66
bf00f99aec69
Merge from eCos master repository on 2000-02-02-19:16:44-GMT
jlarmour
parents:
64
diff
changeset
|
19 // Software distributed under the License is distributed on an "AS IS" |
|
64
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
20 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
21 // License for the specific language governing rights and limitations under |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
22 // the License. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
23 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
24 // The Original Code is eCos - Embedded Configurable Operating System, |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
25 // released September 30, 1998. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
26 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
27 // The Initial Developer of the Original Code is Red Hat. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
28 // Portions created by Red Hat are |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
29 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
30 // All Rights Reserved. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
31 // ------------------------------------------- |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
32 // |
| 0 | 33 //####COPYRIGHTEND#### |
| 34 //=========================================================================== | |
| 35 //#####DESCRIPTIONBEGIN#### | |
| 36 // | |
| 2 | 37 // Author(s): jlarmour |
| 38 // Contributors: jlarmour@ | |
| 39 // Date: 1999-03-02 | |
| 0 | 40 // Purpose: |
| 41 // Description: | |
| 2 | 42 // Usage: Do not include this file directly - include <stdlib.h> instead |
| 0 | 43 // |
| 44 //####DESCRIPTIONEND#### | |
| 45 // | |
| 46 //=========================================================================== | |
| 47 | |
| 48 // CONFIGURATION | |
| 49 | |
| 50 #include <pkgconf/libc.h> // Configuration header | |
| 51 | |
| 52 // INCLUDES | |
| 53 | |
| 2 | 54 #include <stddef.h> // NULL, wchar_t and size_t from compiler |
| 55 #include <stdlib.h> // Header for this file, just in case | |
| 56 #include <cyg/infra/cyg_ass.h> // Assertion support | |
| 57 #include <cyg/infra/cyg_trac.h> // Tracing support | |
| 0 | 58 |
| 59 // FUNCTIONS | |
| 60 | |
| 61 //=========================================================================== | |
| 62 | |
| 63 // 7.10.1 String conversion functions | |
| 64 | |
| 65 | |
| 66 CYGPRI_LIBC_INLINE double | |
| 67 atof( const char *nptr ) | |
| 68 { | |
| 69 return strtod( nptr, (char **)NULL ); | |
| 70 } // atof() | |
| 71 | |
| 72 CYGPRI_LIBC_INLINE int | |
| 73 atoi( const char *nptr ) | |
| 74 { | |
| 75 return (int)strtol( nptr, (char **)NULL, 10 ); | |
| 76 } // atoi() | |
| 77 | |
| 78 | |
| 79 CYGPRI_LIBC_INLINE long | |
| 80 atol( const char *nptr ) | |
| 81 { | |
| 82 return strtol( nptr, (char **)NULL, 10 ); | |
| 83 } // atol() | |
| 84 | |
| 85 //=========================================================================== | |
| 86 | |
| 87 // 7.10.6 Integer arithmetic functions | |
| 88 | |
| 89 | |
| 90 CYGPRI_LIBC_INLINE int | |
| 2 | 91 abs( int __j ) |
| 0 | 92 { |
| 2 | 93 return (__j<0) ? -__j : __j; |
| 0 | 94 } // abs() |
| 95 | |
| 96 | |
| 97 CYGPRI_LIBC_INLINE div_t | |
| 2 | 98 div( int __numer, int __denom ) |
| 0 | 99 { |
| 2 | 100 div_t __ret; |
| 101 | |
| 102 CYG_REPORT_FUNCNAMETYPE( "div", "quotient: %d"); | |
| 103 CYG_REPORT_FUNCARG2DV( __numer, __denom ); | |
| 104 // FIXME: what if they want it handled with SIGFPE? Should have option | |
| 105 CYG_PRECONDITION(__denom != 0, "division by zero attempted!"); | |
| 0 | 106 |
| 2 | 107 __ret.quot = __numer / __denom; |
| 108 __ret.rem = __numer % __denom; | |
| 109 | |
| 110 // But the modulo is implementation-defined for -ve numbers (ISO C 6.3.5) | |
| 111 // and we are required to "round" to zero (ISO C 7.10.6.2) | |
| 0 | 112 // |
| 2 | 113 // The cases we have to deal with are inexact division of: |
| 114 // a) + div + | |
| 115 // b) + div - | |
| 116 // c) - div + | |
| 117 // d) - div - | |
| 0 | 118 // |
| 2 | 119 // a) can never go wrong and the quotient and remainder are always positive |
| 120 // b) only goes wrong if the negative quotient has been "rounded" to | |
| 121 // -infinity - if so then the remainder will be negative when it | |
| 122 // should be positive or zero | |
| 123 // c) only goes wrong if the negative quotient has been "rounded" to | |
| 124 // -infinity - if so then the remainder will be positive when it | |
| 125 // should be negative or zero | |
| 126 // d) only goes wrong if the positive quotient has been rounded to | |
| 127 // +infinity - if so then the remainder will be positive when it | |
| 128 // should be negative or zero | |
| 0 | 129 // |
| 2 | 130 // So the correct sign of the remainder corresponds to the sign of the |
| 131 // numerator. Which means we can say that the result needs adjusting | |
| 132 // iff the sign of the numerator is different from the sign of the | |
| 133 // remainder. | |
| 134 // | |
| 135 // You may be interested to know that the Berkeley version of div() | |
| 136 // would get this wrong for e.g. (c) and (d) on some targets. | |
| 137 // e.g. for (-5)/4 it could leave the result as -2R3 | |
| 0 | 138 |
| 2 | 139 if ((__ret.rem < 0) && (__numer > 0)) { |
| 140 ++__ret.quot; | |
| 141 __ret.rem -= __denom; | |
| 142 } else if ((__ret.rem > 0) && (__numer < 0)) { | |
| 143 --__ret.quot; | |
| 144 __ret.rem += __denom; | |
| 145 } // else | |
| 0 | 146 |
| 2 | 147 CYG_REPORT_RETVAL( __ret.quot ); |
| 0 | 148 |
| 2 | 149 return __ret; |
| 0 | 150 } // div() |
| 151 | |
| 152 | |
| 153 CYGPRI_LIBC_INLINE long | |
| 2 | 154 labs( long __j ) |
| 0 | 155 { |
| 2 | 156 return (__j<0) ? -__j : __j; |
| 0 | 157 } // labs() |
| 158 | |
| 159 | |
| 160 CYGPRI_LIBC_INLINE ldiv_t | |
| 2 | 161 ldiv( long __numer, long __denom ) |
| 0 | 162 { |
| 2 | 163 ldiv_t __ret; |
| 0 | 164 |
| 2 | 165 CYG_REPORT_FUNCNAMETYPE( "ldiv", "quotient: %d"); |
| 166 CYG_REPORT_FUNCARG2DV( __numer, __denom ); | |
| 167 // FIXME: what if they want it handled with SIGFPE? Should have option | |
| 168 CYG_PRECONDITION(__denom != 0, "division by zero attempted!"); | |
| 169 | |
| 170 __ret.quot = __numer / __denom; | |
| 171 __ret.rem = __numer % __denom; | |
| 0 | 172 |
| 2 | 173 // But the modulo is implementation-defined for -ve numbers (ISO C 6.3.5) |
| 174 // and we are required to "round" to zero (ISO C 7.10.6.2) | |
| 175 // | |
| 176 // The cases we have to deal with are inexact division of: | |
| 177 // a) + div + | |
| 178 // b) + div - | |
| 179 // c) - div + | |
| 180 // d) - div - | |
| 181 // | |
| 182 // a) can never go wrong and the quotient and remainder are always positive | |
| 183 // b) only goes wrong if the negative quotient has been "rounded" to | |
| 184 // -infinity - if so then the remainder will be negative when it | |
| 185 // should be positive or zero | |
| 186 // c) only goes wrong if the negative quotient has been "rounded" to | |
| 187 // -infinity - if so then the remainder will be positive when it | |
| 188 // should be negative or zero | |
| 189 // d) only goes wrong if the positive quotient has been rounded to | |
| 190 // +infinity - if so then the remainder will be positive when it | |
| 191 // should be negative or zero | |
| 192 // | |
| 193 // So the correct sign of the remainder corresponds to the sign of the | |
| 194 // numerator. Which means we can say that the result needs adjusting | |
| 195 // iff the sign of the numerator is different from the sign of the | |
| 196 // remainder. | |
| 197 // | |
| 198 // You may be interested to know that the Berkeley version of ldiv() | |
| 199 // would get this wrong for e.g. (c) and (d) on some targets. | |
| 200 // e.g. for (-5)/4 it could leave the result as -2R3 | |
| 0 | 201 |
| 2 | 202 if ((__ret.rem < 0) && (__numer > 0)) { |
| 203 ++__ret.quot; | |
| 204 __ret.rem -= __denom; | |
| 205 } else if ((__ret.rem > 0) && (__numer < 0)) { | |
| 206 --__ret.quot; | |
| 207 __ret.rem += __denom; | |
| 208 } // else | |
| 209 | |
| 210 CYG_REPORT_RETVAL( __ret.quot ); | |
| 211 | |
| 212 return __ret; | |
| 0 | 213 } // ldiv() |
| 214 | |
| 215 | |
| 216 #endif // CYGONCE_LIBC_STDLIB_INL multiple inclusion protection | |
| 217 | |
| 218 // EOF stdlib.inl |
