OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [language/] [c/] [libc/] [stdlib/] [current/] [include/] [div.inl] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
#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 Free Software Foundation, 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
19
// version.
20
//
21
// eCos is distributed in the hope that it will be useful, but WITHOUT
22
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
24
// for more details.
25
//
26
// You should have received a copy of the GNU General Public License
27
// along with eCos; if not, write to the Free Software Foundation, Inc.,
28
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
29
//
30
// As a special exception, if other files instantiate templates or use
31
// macros or inline functions from this file, or you compile this file
32
// and link it with other works to produce a work based on this file,
33
// this file does not by itself cause the resulting work to be covered by
34
// the GNU General Public License. However the source code for this file
35
// must still be made available in accordance with section (3) of the GNU
36
// General Public License v2.
37
//
38
// This exception does not invalidate any other reasons why a work based
39
// on this file might be covered by the GNU General Public License.
40
// -------------------------------------------
41
// ####ECOSGPLCOPYRIGHTEND####
42
//===========================================================================
43
//#####DESCRIPTIONBEGIN####
44
//
45
// Author(s):    jlarmour
46
// Contributors:
47
// Date:         2000-04-28
48
// Purpose:
49
// Description:
50
// Usage:        Do not include this file directly - include  instead
51
//
52
//####DESCRIPTIONEND####
53
//
54
//=========================================================================*/
55
 
56
// CONFIGURATION
57
 
58
#include     // Configuration header
59
 
60
// INCLUDES
61
 
62
#include       // Assertion support
63
#include      // Tracing support
64
 
65
/* TYPE DEFINITIONS */
66
 
67
/* return type of the div() function */
68
 
69
typedef struct {
70
    int quot;      /* quotient  */
71
    int rem;       /* remainder */
72
} div_t;
73
 
74
 
75
/* return type of the ldiv() function */
76
 
77
typedef struct {
78
    long quot;     /* quotient  */
79
    long rem;      /* remainder */
80
} ldiv_t;
81
 
82
/* FUNCTION PROTOTYPES */
83
 
84
#ifdef __cplusplus
85
extern "C" {
86
#endif
87
 
88
extern div_t
89
div( int /* numerator */, int /* denominator */ ) __attribute__((__const__));
90
 
91
extern ldiv_t
92
ldiv( long /* numerator */, long /* denominator */ ) __attribute__((__const__));
93
 
94
#ifdef __cplusplus
95
} /* extern "C" */
96
#endif
97
 
98
/* FUNCTIONS */
99
 
100
#ifndef CYGPRI_LIBC_STDLIB_DIV_INLINE
101
# define CYGPRI_LIBC_STDLIB_DIV_INLINE extern __inline__
102
#endif
103
 
104
CYGPRI_LIBC_STDLIB_DIV_INLINE div_t
105
div( int __numer, int __denom )
106
{
107
    div_t __ret;
108
 
109
    CYG_REPORT_FUNCNAMETYPE( "div", "quotient: %d");
110
    CYG_REPORT_FUNCARG2DV( __numer, __denom );
111
    // FIXME: what if they want it handled with SIGFPE? Should have option
112
    CYG_PRECONDITION(__denom != 0, "division by zero attempted!");
113
 
114
    __ret.quot = __numer / __denom;
115
    __ret.rem  = __numer % __denom;
116
 
117
    // But the modulo is implementation-defined for -ve numbers (ISO C 6.3.5)
118
    // and we are required to "round" to zero (ISO C 7.10.6.2)
119
    //
120
    // The cases we have to deal with are inexact division of:
121
    // a) + div +
122
    // b) + div -
123
    // c) - div +
124
    // d) - div -
125
    //
126
    // a) can never go wrong and the quotient and remainder are always positive
127
    // b) only goes wrong if the negative quotient has been "rounded" to
128
    //    -infinity - if so then the remainder will be negative when it
129
    //    should be positive or zero
130
    // c) only goes wrong if the negative quotient has been "rounded" to
131
    //    -infinity - if so then the remainder will be positive when it
132
    //    should be negative or zero
133
    // d) only goes wrong if the positive quotient has been rounded to
134
    //    +infinity - if so then the remainder will be positive when it
135
    //    should be negative or zero
136
    //
137
    // So the correct sign of the remainder corresponds to the sign of the
138
    // numerator. Which means we can say that the result needs adjusting
139
    // iff the sign of the numerator is different from the sign of the
140
    // remainder.
141
    //
142
    // You may be interested to know that the Berkeley version of div()
143
    // would get this wrong for e.g. (c) and (d) on some targets.
144
    // e.g. for (-5)/4 it could leave the result as -2R3
145
 
146
    if ((__ret.rem < 0) && (__numer > 0)) {
147
        ++__ret.quot;
148
        __ret.rem -= __denom;
149
    } else if ((__ret.rem > 0) && (__numer < 0)) {
150
        --__ret.quot;
151
        __ret.rem += __denom;
152
    } // else
153
 
154
    CYG_REPORT_RETVAL( __ret.quot );
155
 
156
    return __ret;
157
} // div()
158
 
159
CYGPRI_LIBC_STDLIB_DIV_INLINE ldiv_t
160
ldiv( long __numer, long __denom )
161
{
162
    ldiv_t __ret;
163
 
164
    CYG_REPORT_FUNCNAMETYPE( "ldiv", "quotient: %d");
165
    CYG_REPORT_FUNCARG2DV( __numer, __denom );
166
    // FIXME: what if they want it handled with SIGFPE? Should have option
167
    CYG_PRECONDITION(__denom != 0, "division by zero attempted!");
168
 
169
    __ret.quot = __numer / __denom;
170
    __ret.rem  = __numer % __denom;
171
 
172
    // But the modulo is implementation-defined for -ve numbers (ISO C 6.3.5)
173
    // and we are required to "round" to zero (ISO C 7.10.6.2)
174
    //
175
    // The cases we have to deal with are inexact division of:
176
    // a) + div +
177
    // b) + div -
178
    // c) - div +
179
    // d) - div -
180
    //
181
    // a) can never go wrong and the quotient and remainder are always positive
182
    // b) only goes wrong if the negative quotient has been "rounded" to
183
    //    -infinity - if so then the remainder will be negative when it
184
    //    should be positive or zero
185
    // c) only goes wrong if the negative quotient has been "rounded" to
186
    //    -infinity - if so then the remainder will be positive when it
187
    //    should be negative or zero
188
    // d) only goes wrong if the positive quotient has been rounded to
189
    //    +infinity - if so then the remainder will be positive when it
190
    //    should be negative or zero
191
    //
192
    // So the correct sign of the remainder corresponds to the sign of the
193
    // numerator. Which means we can say that the result needs adjusting
194
    // iff the sign of the numerator is different from the sign of the
195
    // remainder.
196
    //
197
    // You may be interested to know that the Berkeley version of ldiv()
198
    // would get this wrong for e.g. (c) and (d) on some targets.
199
    // e.g. for (-5)/4 it could leave the result as -2R3
200
 
201
    if ((__ret.rem < 0) && (__numer > 0)) {
202
        ++__ret.quot;
203
        __ret.rem -= __denom;
204
    } else if ((__ret.rem > 0) && (__numer < 0)) {
205
        --__ret.quot;
206
        __ret.rem += __denom;
207
    } // else
208
 
209
    CYG_REPORT_RETVAL( __ret.quot );
210
 
211
    return __ret;
212
} // ldiv()
213
 
214
 
215
#endif // CYGONCE_LIBC_STDLIB_DIV_INL multiple inclusion protection
216
 
217
// EOF div.inl

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.