]>
git.ipfire.org Git - people/ms/u-boot.git/blob - arch/nios2/lib/libgcc.c
2 * This file is part of GNU CC.
4 * GNU CC is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published
6 * by the Free Software Foundation; either version 2, or (at your
7 * option) any later version.
9 * GNU CC is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public
15 * License along with GNU CC; see the file COPYING. If not, write
16 * to the Free Software Foundation, 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 typedef unsigned int UWtype
;
21 typedef unsigned int UHWtype
;
22 typedef unsigned long long UDWtype
;
23 #define W_TYPE_SIZE 32
25 typedef unsigned char UQItype
;
27 typedef unsigned long USItype
;
28 typedef long long DItype
;
29 typedef unsigned long long DSItype
;
34 typedef int word_type
;
36 typedef long long DWtype
;
38 struct DWstruct
{ Wtype low
, high
;};
46 #define BITS_PER_UNIT 8
49 __udivmoddi4 (UDWtype n
, UDWtype d
, UDWtype
*rp
);
51 const UQItype __clz_tab
[256] =
53 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
54 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
55 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
56 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
57 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
58 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
59 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
60 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
65 __ashldi3 (DWtype u
, word_type b
)
70 const DWunion uu
= {.ll
= u
};
71 const word_type bm
= (sizeof (Wtype
) * BITS_PER_UNIT
) - b
;
77 w
.s
.high
= (UWtype
) uu
.s
.low
<< -bm
;
81 const UWtype carries
= (UWtype
) uu
.s
.low
>> bm
;
83 w
.s
.low
= (UWtype
) uu
.s
.low
<< b
;
84 w
.s
.high
= ((UWtype
) uu
.s
.high
<< b
) | carries
;
91 __ashrdi3 (DWtype u
, word_type b
)
96 const DWunion uu
= {.ll
= u
};
97 const word_type bm
= (sizeof (Wtype
) * BITS_PER_UNIT
) - b
;
102 /* w.s.high = 1..1 or 0..0 */
103 w
.s
.high
= uu
.s
.high
>> (sizeof (Wtype
) * BITS_PER_UNIT
- 1);
104 w
.s
.low
= uu
.s
.high
>> -bm
;
108 const UWtype carries
= (UWtype
) uu
.s
.high
<< bm
;
110 w
.s
.high
= uu
.s
.high
>> b
;
111 w
.s
.low
= ((UWtype
) uu
.s
.low
>> b
) | carries
;
118 __lshrdi3 (DWtype u
, word_type b
)
123 const DWunion uu
= {.ll
= u
};
124 const word_type bm
= (sizeof (Wtype
) * BITS_PER_UNIT
) - b
;
130 w
.s
.low
= (UWtype
) uu
.s
.high
>> -bm
;
134 const UWtype carries
= (UWtype
) uu
.s
.high
<< bm
;
136 w
.s
.high
= (UWtype
) uu
.s
.high
>> b
;
137 w
.s
.low
= ((UWtype
) uu
.s
.low
>> b
) | carries
;
144 __cmpdi2 (DWtype a
, DWtype b
)
146 const DWunion au
= {.ll
= a
};
147 const DWunion bu
= {.ll
= b
};
149 if (au
.s
.high
< bu
.s
.high
)
151 else if (au
.s
.high
> bu
.s
.high
)
153 if ((UWtype
) au
.s
.low
< (UWtype
) bu
.s
.low
)
155 else if ((UWtype
) au
.s
.low
> (UWtype
) bu
.s
.low
)
161 __udivmoddi4 (UDWtype n
, UDWtype d
, UDWtype
*rp
)
163 const DWunion nn
= {.ll
= n
};
164 const DWunion dd
= {.ll
= d
};
166 UWtype d0
, d1
, n0
, n1
, n2
;
175 #if !UDIV_NEEDS_NORMALIZATION
182 udiv_qrnnd (q0
, n0
, n1
, n0
, d0
);
185 /* Remainder in n0. */
192 d0
= 1 / d0
; /* Divide intentionally by zero. */
194 udiv_qrnnd (q1
, n1
, 0, n1
, d0
);
195 udiv_qrnnd (q0
, n0
, n1
, n0
, d0
);
197 /* Remainder in n0. */
208 #else /* UDIV_NEEDS_NORMALIZATION */
216 count_leading_zeros (bm
, d0
);
220 /* Normalize, i.e. make the most significant bit of the
224 n1
= (n1
<< bm
) | (n0
>> (W_TYPE_SIZE
- bm
));
228 udiv_qrnnd (q0
, n0
, n1
, n0
, d0
);
231 /* Remainder in n0 >> bm. */
238 d0
= 1 / d0
; /* Divide intentionally by zero. */
240 count_leading_zeros (bm
, d0
);
244 /* From (n1 >= d0) /\ (the most significant bit of d0 is set),
245 conclude (the most significant bit of n1 is set) /\ (the
246 leading quotient digit q1 = 1).
248 This special case is necessary, not an optimization.
249 (Shifts counts of W_TYPE_SIZE are undefined.) */
258 b
= W_TYPE_SIZE
- bm
;
262 n1
= (n1
<< bm
) | (n0
>> b
);
265 udiv_qrnnd (q1
, n1
, n2
, n1
, d0
);
270 udiv_qrnnd (q0
, n0
, n1
, n0
, d0
);
272 /* Remainder in n0 >> bm. */
282 #endif /* UDIV_NEEDS_NORMALIZATION */
293 /* Remainder in n1n0. */
305 count_leading_zeros (bm
, d1
);
308 /* From (n1 >= d1) /\ (the most significant bit of d1 is set),
309 conclude (the most significant bit of n1 is set) /\ (the
310 quotient digit q0 = 0 or 1).
312 This special case is necessary, not an optimization. */
314 /* The condition on the next line takes advantage of that
315 n1 >= d1 (true due to program flow). */
316 if (n1
> d1
|| n0
>= d0
)
319 sub_ddmmss (n1
, n0
, n1
, n0
, d1
, d0
);
338 b
= W_TYPE_SIZE
- bm
;
340 d1
= (d1
<< bm
) | (d0
>> b
);
343 n1
= (n1
<< bm
) | (n0
>> b
);
346 udiv_qrnnd (q0
, n1
, n2
, n1
, d1
);
347 umul_ppmm (m1
, m0
, q0
, d0
);
349 if (m1
> n1
|| (m1
== n1
&& m0
> n0
))
352 sub_ddmmss (m1
, m0
, m1
, m0
, d1
, d0
);
357 /* Remainder in (n1n0 - m1m0) >> bm. */
360 sub_ddmmss (n1
, n0
, n1
, n0
, m1
, m0
);
361 rr
.s
.low
= (n1
<< b
) | (n0
>> bm
);
362 rr
.s
.high
= n1
>> bm
;
369 const DWunion ww
= {{.low
= q0
, .high
= q1
}};
374 __divdi3 (DWtype u
, DWtype v
)
377 DWunion uu
= {.ll
= u
};
378 DWunion vv
= {.ll
= v
};
388 w
= __udivmoddi4 (uu
.ll
, vv
.ll
, (UDWtype
*) 0);
398 const DWunion uu
= {.ll
= u
};
399 const DWunion w
= { {.low
= -uu
.s
.low
,
400 .high
= -uu
.s
.high
- ((UWtype
) -uu
.s
.low
> 0) } };
407 __muldi3 (DWtype u
, DWtype v
)
409 const DWunion uu
= {.ll
= u
};
410 const DWunion vv
= {.ll
= v
};
411 DWunion w
= {.ll
= __umulsidi3 (uu
.s
.low
, vv
.s
.low
)};
413 w
.s
.high
+= ((UWtype
) uu
.s
.low
* (UWtype
) vv
.s
.high
414 + (UWtype
) uu
.s
.high
* (UWtype
) vv
.s
.low
);
420 __moddi3 (DWtype u
, DWtype v
)
423 DWunion uu
= {.ll
= u
};
424 DWunion vv
= {.ll
= v
};
433 (void) __udivmoddi4 (uu
.ll
, vv
.ll
, (UDWtype
*)&w
);
441 __ucmpdi2 (DWtype a
, DWtype b
)
443 const DWunion au
= {.ll
= a
};
444 const DWunion bu
= {.ll
= b
};
446 if ((UWtype
) au
.s
.high
< (UWtype
) bu
.s
.high
)
448 else if ((UWtype
) au
.s
.high
> (UWtype
) bu
.s
.high
)
450 if ((UWtype
) au
.s
.low
< (UWtype
) bu
.s
.low
)
452 else if ((UWtype
) au
.s
.low
> (UWtype
) bu
.s
.low
)
459 __udivdi3 (UDWtype n
, UDWtype d
)
461 return __udivmoddi4 (n
, d
, (UDWtype
*) 0);
465 __umoddi3 (UDWtype u
, UDWtype v
)
468 (void) __udivmoddi4 (u
, v
, &w
);
474 udivmodsi4(USItype num
, USItype den
, word_type modwanted
)
479 while (den
< num
&& bit
&& !(den
& (1L<<31)))
494 if (modwanted
) return num
;
499 __divsi3 (SItype a
, SItype b
)
516 res
= udivmodsi4 (a
, b
, 0);
526 __udivsi3 (SItype a
, SItype b
)
528 return udivmodsi4 (a
, b
, 0);
533 __modsi3 (SItype a
, SItype b
)
547 res
= udivmodsi4 (a
, b
, 1);
556 __mulsi3 (SItype a
, SItype b
)
575 __umodsi3 (SItype a
, SItype b
)
578 return udivmodsi4 (a
, b
, 1);
582 __gcc_bcmp (const unsigned char *s1
, const unsigned char *s2
, unsigned long size
)
586 const unsigned char c1
= *s1
++, c2
= *s2
++;