Mercurial > ecos
annotate packages/language/c/libc/current/src/stdlib/div.cxx @ 64:c38311975d4f ecos-sw-2000-01-28
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
| author | jlarmour |
|---|---|
| date | Fri, 28 Jan 2000 04:59:39 +0000 |
| parents | 443894e2e912 |
| children | bf00f99aec69 |
| rev | line source |
|---|---|
| 0 | 1 //=========================================================================== |
| 2 // | |
| 3 // div.cxx | |
| 4 // | |
| 2 | 5 // ISO C implementation for div() utility function |
| 0 | 6 // |
| 7 //=========================================================================== | |
| 8 //####COPYRIGHTBEGIN#### | |
|
64
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
9 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
10 // ------------------------------------------- |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
11 // The contents of this file are subject to the Red Hat eCos Public License |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
12 // Version 1.0 (the "License"); you may not use this file except in |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
13 // compliance with the License. You may obtain a copy of the License at |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
14 // http://sourceware.cygnus.com/ecos |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
15 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
16 // Software distributed under the License is distributed on an |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
17 // 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
|
18 // 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
|
19 // the License. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
20 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
21 // 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
|
22 // released September 30, 1998. |
|
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 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
|
25 // 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
|
26 // 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
|
27 // All Rights Reserved. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
28 // ------------------------------------------- |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
29 // |
| 0 | 30 //####COPYRIGHTEND#### |
| 31 //=========================================================================== | |
| 32 //#####DESCRIPTIONBEGIN#### | |
| 33 // | |
| 2 | 34 // Author(s): jlarmour |
| 35 // Contributors: jlarmour | |
| 36 // Date: 1999-03-02 | |
| 37 // Purpose: Provide implementation of div() from ISO C section 7.10.6.2 | |
| 38 // Description: | |
| 39 // Usage: | |
| 0 | 40 // |
| 41 //####DESCRIPTIONEND#### | |
| 42 // | |
| 43 //=========================================================================== | |
| 44 // | |
| 45 | |
| 46 // CONFIGURATION | |
| 47 | |
| 48 #include <pkgconf/libc.h> // Configuration header | |
| 49 | |
| 50 // INCLUDES | |
| 51 | |
| 52 #include <cyg/infra/cyg_type.h> // Common type definitions and support | |
| 2 | 53 #include <cyg/infra/cyg_ass.h> // Assertion support |
| 0 | 54 #include <cyg/infra/cyg_trac.h> // Tracing support |
| 55 | |
| 56 // We don't want the inline versions of stdlib functions defined here | |
| 57 | |
| 58 #ifdef CYGIMP_LIBC_STDLIB_INLINES | |
| 2 | 59 # undef CYGIMP_LIBC_STDLIB_INLINES |
| 0 | 60 #endif |
| 61 | |
| 62 #include <stddef.h> // NULL, wchar_t and size_t from compiler | |
| 63 #include <stdlib.h> // Main header for stdlib functions | |
| 2 | 64 |
| 65 | |
| 66 // FUNCTIONS | |
| 67 | |
| 68 externC div_t | |
| 69 __div( int __numer, int __denom ) | |
| 70 { | |
| 71 div_t __ret; | |
| 72 | |
| 73 CYG_REPORT_FUNCNAMETYPE( "__div", "quotient: %d"); | |
| 74 CYG_REPORT_FUNCARG2DV( __numer, __denom ); | |
| 75 // FIXME: what if they want it handled with SIGFPE? Should have option | |
| 76 CYG_PRECONDITION(__denom != 0, "division by zero attempted!"); | |
| 77 | |
| 78 __ret.quot = __numer / __denom; | |
| 79 __ret.rem = __numer % __denom; | |
| 80 | |
| 81 // But the modulo is implementation-defined for -ve numbers (ISO C 6.3.5) | |
| 82 // and we are required to "round" to zero (ISO C 7.10.6.2) | |
| 83 // | |
| 84 // The cases we have to deal with are inexact division of: | |
| 85 // a) + div + | |
| 86 // b) + div - | |
| 87 // c) - div + | |
| 88 // d) - div - | |
| 89 // | |
| 90 // a) can never go wrong and the quotient and remainder are always positive | |
| 91 // b) only goes wrong if the negative quotient has been "rounded" to | |
| 92 // -infinity - if so then the remainder will be negative when it | |
| 93 // should be positive or zero | |
| 94 // c) only goes wrong if the negative quotient has been "rounded" to | |
| 95 // -infinity - if so then the remainder will be positive when it | |
| 96 // should be negative or zero | |
| 97 // d) only goes wrong if the positive quotient has been rounded to | |
| 98 // +infinity - if so then the remainder will be positive when it | |
| 99 // should be negative or zero | |
| 100 // | |
| 101 // So the correct sign of the remainder corresponds to the sign of the | |
| 102 // numerator. Which means we can say that the result needs adjusting | |
| 103 // iff the sign of the numerator is different from the sign of the | |
| 104 // remainder. | |
| 105 // | |
| 106 // You may be interested to know that the Berkeley version of div() | |
| 107 // would get this wrong for (c) and (d) on some targets. | |
| 108 // e.g. for (-5)/4 it could leave the result as -2R3 | |
| 109 | |
| 110 if ((__ret.rem < 0) && (__numer > 0)) { | |
| 111 ++__ret.quot; | |
| 112 __ret.rem -= __denom; | |
| 113 } else if ((__ret.rem > 0) && (__numer < 0)) { | |
| 114 --__ret.quot; | |
| 115 __ret.rem += __denom; | |
| 116 } // else | |
| 117 | |
| 118 CYG_REPORT_RETVAL( __ret.quot ); | |
| 119 | |
| 120 return __ret; | |
| 121 } // __div() | |
| 0 | 122 |
| 123 | |
| 124 // EXPORTED SYMBOLS | |
| 125 | |
| 126 externC div_t | |
| 2 | 127 div( int __numer, int __denom ) CYGBLD_ATTRIB_WEAK_ALIAS(__div); |
| 0 | 128 |
| 129 | |
| 130 // EOF div.cxx |
