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