1 /* Signed and unsigned multiplication and division and modulus for CRIS.
2 Contributed by Axis Communications.
3 Written by Hans-Peter Nilsson <hp@axis.se>, c:a 1992.
5 Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by the
11 Free Software Foundation; either version 2, or (at your option) any
14 In addition to the permissions in the GNU General Public License, the
15 Free Software Foundation gives you unlimited permission to link the
16 compiled version of this file with other programs, and to distribute
17 those programs without any restriction coming from the use of this
18 file. (The General Public License restrictions do apply in other
19 respects; for example, they cover modification of the file, and
20 distribution when not linked into another program.)
22 This file is distributed in the hope that it will be useful, but
23 WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program; see the file COPYING. If not, write to
29 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
30 Boston, MA 02110-1301, USA.
32 As a special exception, if you link this library with files, some of
33 which are compiled with GCC, this library does not by itself cause
34 the resulting object or executable to be covered by the GNU General
36 This exception does not however invalidate any other reasons why
37 the executable file or object might be covered by the GNU General
41 /* Note that we provide prototypes for all "const" functions, to attach
42 the const attribute. This is necessary in 2.7.2 - adding the
43 attribute to the function *definition* is a syntax error.
44 This did not work with e.g. 2.1; back then, the return type had to
49 #if defined (__CRIS_arch_version) && __CRIS_arch_version >= 3
50 #define LZ(v) __extension__ \
51 ({ int tmp_; __asm__ ("lz %1,%0" : "=r" (tmp_) : "r" (v)); tmp_; })
55 #if defined (L_udivsi3) || defined (L_divsi3) || defined (L_umodsi3) \
57 /* Result type of divmod worker function. */
64 /* This is the worker function for div and mod. It is inlined into the
65 respective library function. */
66 static __inline__
struct quot_rem
67 do_31div (unsigned long a
, unsigned long b
)
68 __attribute__ ((__const__
, __always_inline__
));
70 static __inline__
struct quot_rem
71 do_31div (unsigned long a
, unsigned long b
)
73 /* Adjust operands and result if a is 31 bits. */
80 ret
.quot
= 0xffffffff;
86 return (struct quot_rem
) { 0, a
};
91 quot_digits
= LZ (b
) - LZ (a
);
92 quot_digits
+= (a
>= (b
<< quot_digits
));
103 /* Is a 31 bits? Note that bit 31 is handled by the caller. */
106 /* Then make b:s highest bit max 0x40000000, because it must have
107 been 0x80000000 to be 1 bit higher than a. */
110 /* Adjust a to be maximum 0x3fffffff, i.e. two upper bits zero. */
114 extra
= 1 << (quot_digits
- 1);
120 /* Remember that we adjusted a by subtracting b * 2 ** Something. */
121 extra
= 1 << quot_digits
;
124 /* The number of quotient digits will be one less, because
125 we just adjusted b. */
129 /* Now do the division part. */
131 /* Subtract b and add ones to the right when a >= b
132 i.e. "a - (b - 1) == (a - b) + 1". */
135 #define DS __asm__ ("dstep %2,%0" : "=r" (a) : "0" (a), "r" (b))
139 case 32: DS
; case 31: DS
; case 30: DS
; case 29: DS
;
140 case 28: DS
; case 27: DS
; case 26: DS
; case 25: DS
;
141 case 24: DS
; case 23: DS
; case 22: DS
; case 21: DS
;
142 case 20: DS
; case 19: DS
; case 18: DS
; case 17: DS
;
143 case 16: DS
; case 15: DS
; case 14: DS
; case 13: DS
;
144 case 12: DS
; case 11: DS
; case 10: DS
; case 9: DS
;
145 case 8: DS
; case 7: DS
; case 6: DS
; case 5: DS
;
146 case 4: DS
; case 3: DS
; case 2: DS
; case 1: DS
;
152 ret
.quot
= (a
& ((1 << quot_digits
) - 1)) + extra
;
153 ret
.rem
= a
>> quot_digits
;
158 /* Note that unsigned and signed division both build when L_divsi3, but
159 the unsigned variant is then inlined, as with do_31div above. */
160 #if defined (L_udivsi3) || defined (L_divsi3)
165 __Udiv (unsigned long a
, unsigned long b
)
166 __attribute__ ((__const__
, __always_inline__
));
172 __Udiv (unsigned long a
, unsigned long b
)
176 /* Adjust operands and result, if a and/or b is 32 bits. */
177 /* Effectively: b & 0x80000000. */
181 /* Effectively: a & 0x80000000. */
191 for (tmp
= 31; (((long) b
& (1 << tmp
)) == 0); tmp
--)
199 extra
= 1 << (tmp
-1);
209 return do_31div (a
, b
).quot
+extra
;
215 __Div (long a
, long b
) __attribute__ ((__const__
));
218 __Div (long a
, long b
)
223 /* Do *not* call do_31div since abs (-2147483648) == 2147483648
224 <=> abs (-0x80000000) == 0x80000000
225 which is still 32 bits. */
228 result
= __Udiv (__builtin_labs (a
), __builtin_labs (b
));
230 return (sign
< 0) ? -result
: result
;
232 #endif /* L_divsi3 */
233 #endif /* L_udivsi3 || L_divsi3 */
236 /* Note that unsigned and signed modulus both build when L_modsi3, but
237 then the unsigned variant is inlined, as with do_31div above. */
238 #if defined (L_umodsi3) || defined (L_modsi3)
243 __Umod (unsigned long a
, unsigned long b
)
244 __attribute__ ((__const__
, __always_inline__
));
250 __Umod (unsigned long a
, unsigned long b
)
252 /* Adjust operands and result if a and/or b is 32 bits. */
254 return a
>= b
? a
- b
: a
;
265 for (tmp
= 31; (((long) b
& (1 << tmp
)) == 0); tmp
--)
280 return do_31div (a
, b
).rem
;
285 __Mod (long a
, long b
) __attribute__ ((__const__
));
288 __Mod (long a
, long b
)
292 result
= __Umod (__builtin_labs (a
), __builtin_labs (b
));
294 return (a
< 0) ? -result
: result
;
296 #endif /* L_modsi3 */
297 #endif /* L_umodsi3 || L_modsi3 */
298 #endif /* L_udivsi3 || L_divsi3 || L_umodsi3 || L_modsi3 */
302 * eval: (c-set-style "gnu")
303 * indent-tabs-mode: t