1 |
27 |
unneback |
#ifndef CYGONCE_LIBC_STDLIB_DIV_INL
|
2 |
|
|
#define CYGONCE_LIBC_STDLIB_DIV_INL
|
3 |
|
|
/*===========================================================================
|
4 |
|
|
//
|
5 |
|
|
// div.inl
|
6 |
|
|
//
|
7 |
|
|
// Inline implementations for the ISO standard utility functions
|
8 |
|
|
// div() and ldiv()
|
9 |
|
|
//
|
10 |
|
|
//===========================================================================
|
11 |
|
|
//####ECOSGPLCOPYRIGHTBEGIN####
|
12 |
|
|
// -------------------------------------------
|
13 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
14 |
|
|
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
|
15 |
|
|
//
|
16 |
|
|
// eCos is free software; you can redistribute it and/or modify it under
|
17 |
|
|
// the terms of the GNU General Public License as published by the Free
|
18 |
|
|
// Software Foundation; either version 2 or (at your option) any later version.
|
19 |
|
|
//
|
20 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
|
21 |
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
22 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
23 |
|
|
// for more details.
|
24 |
|
|
//
|
25 |
|
|
// You should have received a copy of the GNU General Public License along
|
26 |
|
|
// with eCos; if not, write to the Free Software Foundation, Inc.,
|
27 |
|
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
|
28 |
|
|
//
|
29 |
|
|
// As a special exception, if other files instantiate templates or use macros
|
30 |
|
|
// or inline functions from this file, or you compile this file and link it
|
31 |
|
|
// with other works to produce a work based on this file, this file does not
|
32 |
|
|
// by itself cause the resulting work to be covered by the GNU General Public
|
33 |
|
|
// License. However the source code for this file must still be made available
|
34 |
|
|
// in accordance with section (3) of the GNU General Public License.
|
35 |
|
|
//
|
36 |
|
|
// This exception does not invalidate any other reasons why a work based on
|
37 |
|
|
// this file might be covered by the GNU General Public License.
|
38 |
|
|
//
|
39 |
|
|
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
|
40 |
|
|
// at http://sources.redhat.com/ecos/ecos-license/
|
41 |
|
|
// -------------------------------------------
|
42 |
|
|
//####ECOSGPLCOPYRIGHTEND####
|
43 |
|
|
//===========================================================================
|
44 |
|
|
//#####DESCRIPTIONBEGIN####
|
45 |
|
|
//
|
46 |
|
|
// Author(s): jlarmour
|
47 |
|
|
// Contributors:
|
48 |
|
|
// Date: 2000-04-28
|
49 |
|
|
// Purpose:
|
50 |
|
|
// Description:
|
51 |
|
|
// Usage: Do not include this file directly - include instead
|
52 |
|
|
//
|
53 |
|
|
//####DESCRIPTIONEND####
|
54 |
|
|
//
|
55 |
|
|
//=========================================================================*/
|
56 |
|
|
|
57 |
|
|
// CONFIGURATION
|
58 |
|
|
|
59 |
|
|
#include // Configuration header
|
60 |
|
|
|
61 |
|
|
// INCLUDES
|
62 |
|
|
|
63 |
|
|
#include // Assertion support
|
64 |
|
|
#include // Tracing support
|
65 |
|
|
|
66 |
|
|
/* TYPE DEFINITIONS */
|
67 |
|
|
|
68 |
|
|
/* return type of the div() function */
|
69 |
|
|
|
70 |
|
|
typedef struct {
|
71 |
|
|
int quot; /* quotient */
|
72 |
|
|
int rem; /* remainder */
|
73 |
|
|
} div_t;
|
74 |
|
|
|
75 |
|
|
|
76 |
|
|
/* return type of the ldiv() function */
|
77 |
|
|
|
78 |
|
|
typedef struct {
|
79 |
|
|
long quot; /* quotient */
|
80 |
|
|
long rem; /* remainder */
|
81 |
|
|
} ldiv_t;
|
82 |
|
|
|
83 |
|
|
/* FUNCTION PROTOTYPES */
|
84 |
|
|
|
85 |
|
|
#ifdef __cplusplus
|
86 |
|
|
extern "C" {
|
87 |
|
|
#endif
|
88 |
|
|
|
89 |
|
|
extern div_t
|
90 |
|
|
div( int /* numerator */, int /* denominator */ ) __attribute__((__const__));
|
91 |
|
|
|
92 |
|
|
extern ldiv_t
|
93 |
|
|
ldiv( long /* numerator */, long /* denominator */ ) __attribute__((__const__));
|
94 |
|
|
|
95 |
|
|
#ifdef __cplusplus
|
96 |
|
|
} /* extern "C" */
|
97 |
|
|
#endif
|
98 |
|
|
|
99 |
|
|
/* FUNCTIONS */
|
100 |
|
|
|
101 |
|
|
#ifndef CYGPRI_LIBC_STDLIB_DIV_INLINE
|
102 |
|
|
# define CYGPRI_LIBC_STDLIB_DIV_INLINE extern __inline__
|
103 |
|
|
#endif
|
104 |
|
|
|
105 |
|
|
CYGPRI_LIBC_STDLIB_DIV_INLINE div_t
|
106 |
|
|
div( int __numer, int __denom )
|
107 |
|
|
{
|
108 |
|
|
div_t __ret;
|
109 |
|
|
|
110 |
|
|
CYG_REPORT_FUNCNAMETYPE( "div", "quotient: %d");
|
111 |
|
|
CYG_REPORT_FUNCARG2DV( __numer, __denom );
|
112 |
|
|
// FIXME: what if they want it handled with SIGFPE? Should have option
|
113 |
|
|
CYG_PRECONDITION(__denom != 0, "division by zero attempted!");
|
114 |
|
|
|
115 |
|
|
__ret.quot = __numer / __denom;
|
116 |
|
|
__ret.rem = __numer % __denom;
|
117 |
|
|
|
118 |
|
|
// But the modulo is implementation-defined for -ve numbers (ISO C 6.3.5)
|
119 |
|
|
// and we are required to "round" to zero (ISO C 7.10.6.2)
|
120 |
|
|
//
|
121 |
|
|
// The cases we have to deal with are inexact division of:
|
122 |
|
|
// a) + div +
|
123 |
|
|
// b) + div -
|
124 |
|
|
// c) - div +
|
125 |
|
|
// d) - div -
|
126 |
|
|
//
|
127 |
|
|
// a) can never go wrong and the quotient and remainder are always positive
|
128 |
|
|
// b) only goes wrong if the negative quotient has been "rounded" to
|
129 |
|
|
// -infinity - if so then the remainder will be negative when it
|
130 |
|
|
// should be positive or zero
|
131 |
|
|
// c) only goes wrong if the negative quotient has been "rounded" to
|
132 |
|
|
// -infinity - if so then the remainder will be positive when it
|
133 |
|
|
// should be negative or zero
|
134 |
|
|
// d) only goes wrong if the positive quotient has been rounded to
|
135 |
|
|
// +infinity - if so then the remainder will be positive when it
|
136 |
|
|
// should be negative or zero
|
137 |
|
|
//
|
138 |
|
|
// So the correct sign of the remainder corresponds to the sign of the
|
139 |
|
|
// numerator. Which means we can say that the result needs adjusting
|
140 |
|
|
// iff the sign of the numerator is different from the sign of the
|
141 |
|
|
// remainder.
|
142 |
|
|
//
|
143 |
|
|
// You may be interested to know that the Berkeley version of div()
|
144 |
|
|
// would get this wrong for e.g. (c) and (d) on some targets.
|
145 |
|
|
// e.g. for (-5)/4 it could leave the result as -2R3
|
146 |
|
|
|
147 |
|
|
if ((__ret.rem < 0) && (__numer > 0)) {
|
148 |
|
|
++__ret.quot;
|
149 |
|
|
__ret.rem -= __denom;
|
150 |
|
|
} else if ((__ret.rem > 0) && (__numer < 0)) {
|
151 |
|
|
--__ret.quot;
|
152 |
|
|
__ret.rem += __denom;
|
153 |
|
|
} // else
|
154 |
|
|
|
155 |
|
|
CYG_REPORT_RETVAL( __ret.quot );
|
156 |
|
|
|
157 |
|
|
return __ret;
|
158 |
|
|
} // div()
|
159 |
|
|
|
160 |
|
|
CYGPRI_LIBC_STDLIB_DIV_INLINE ldiv_t
|
161 |
|
|
ldiv( long __numer, long __denom )
|
162 |
|
|
{
|
163 |
|
|
ldiv_t __ret;
|
164 |
|
|
|
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;
|
172 |
|
|
|
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
|
201 |
|
|
|
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;
|
213 |
|
|
} // ldiv()
|
214 |
|
|
|
215 |
|
|
|
216 |
|
|
#endif // CYGONCE_LIBC_STDLIB_DIV_INL multiple inclusion protection
|
217 |
|
|
|
218 |
|
|
// EOF div.inl
|