Merge branch 'master' of git://github.com/illumos/illumos-gate
[unleashed.git] / usr / src / grub / grub-0.97 / stage2 / moddiv.c
blob78460cfc716bf295e79298e915bd1553b31b1520
1 /* Header file for libgcc2.c. */
2 /* Copyright (C) 2000, 2001, 2004, 2005, 2009
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 typedef int SItype __attribute__ ((mode (SI)));
27 typedef unsigned int USItype __attribute__ ((mode (SI)));
28 typedef int DItype __attribute__ ((mode (DI)));
29 typedef unsigned int UDItype __attribute__ ((mode (DI)));
31 #define BITS_PER_UNIT 8
32 #define W_TYPE_SIZE (4 * BITS_PER_UNIT)
33 #define Wtype SItype
34 #define DWtype DItype
35 #define UWtype USItype
36 #define UDWtype UDItype
38 struct DWstruct {Wtype low, high;};
40 typedef union
42 struct DWstruct s;
43 DWtype ll;
44 } DWunion;
46 #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
47 __asm__ ("sub{l} {%5,%1|%1,%5}\n\tsbb{l} {%3,%0|%0,%3}" \
48 : "=r" ((USItype) (sh)), \
49 "=&r" ((USItype) (sl)) \
50 : "0" ((USItype) (ah)), \
51 "g" ((USItype) (bh)), \
52 "1" ((USItype) (al)), \
53 "g" ((USItype) (bl)))
55 #define umul_ppmm(w1, w0, u, v) \
56 __asm__ ("mul{l} %3" \
57 : "=a" ((USItype) (w0)), \
58 "=d" ((USItype) (w1)) \
59 : "%0" ((USItype) (u)), \
60 "rm" ((USItype) (v)))
61 #define udiv_qrnnd(q, r, n1, n0, dv) \
62 __asm__ ("div{l} %4" \
63 : "=a" ((USItype) (q)), \
64 "=d" ((USItype) (r)) \
65 : "0" ((USItype) (n0)), \
66 "1" ((USItype) (n1)), \
67 "rm" ((USItype) (dv)))
69 #define count_leading_zeros(count, x) ((count) = __builtin_clz (x))
71 UDWtype
72 __udivmoddi4 (UDWtype n, UDWtype d, UDWtype *rp)
74 const DWunion nn = {.ll = n};
75 const DWunion dd = {.ll = d};
76 DWunion rr;
77 UWtype d0, d1, n0, n1, n2;
78 UWtype q0, q1;
79 UWtype b, bm;
81 d0 = dd.s.low;
82 d1 = dd.s.high;
83 n0 = nn.s.low;
84 n1 = nn.s.high;
86 if (d1 == 0)
88 if (d0 > n1)
90 /* 0q = nn / 0D */
92 udiv_qrnnd (q0, n0, n1, n0, d0);
93 q1 = 0;
95 /* Remainder in n0. */
97 else
99 /* qq = NN / 0d */
101 if (d0 == 0)
102 d0 = 1 / d0; /* Divide intentionally by zero. */
104 udiv_qrnnd (q1, n1, 0, n1, d0);
105 udiv_qrnnd (q0, n0, n1, n0, d0);
107 /* Remainder in n0. */
110 if (rp != 0)
112 rr.s.low = n0;
113 rr.s.high = 0;
114 *rp = rr.ll;
119 else
121 if (d1 > n1)
123 /* 00 = nn / DD */
125 q0 = 0;
126 q1 = 0;
128 /* Remainder in n1n0. */
129 if (rp != 0)
131 rr.s.low = n0;
132 rr.s.high = n1;
133 *rp = rr.ll;
136 else
138 /* 0q = NN / dd */
140 count_leading_zeros (bm, d1);
141 if (bm == 0)
143 /* From (n1 >= d1) /\ (the most significant bit of d1 is set),
144 conclude (the most significant bit of n1 is set) /\ (the
145 quotient digit q0 = 0 or 1).
147 This special case is necessary, not an optimization. */
149 /* The condition on the next line takes advantage of that
150 n1 >= d1 (true due to program flow). */
151 if (n1 > d1 || n0 >= d0)
153 q0 = 1;
154 sub_ddmmss (n1, n0, n1, n0, d1, d0);
156 else
157 q0 = 0;
159 q1 = 0;
161 if (rp != 0)
163 rr.s.low = n0;
164 rr.s.high = n1;
165 *rp = rr.ll;
168 else
170 UWtype m1, m0;
171 /* Normalize. */
173 b = W_TYPE_SIZE - bm;
175 d1 = (d1 << bm) | (d0 >> b);
176 d0 = d0 << bm;
177 n2 = n1 >> b;
178 n1 = (n1 << bm) | (n0 >> b);
179 n0 = n0 << bm;
181 udiv_qrnnd (q0, n1, n2, n1, d1);
182 umul_ppmm (m1, m0, q0, d0);
184 if (m1 > n1 || (m1 == n1 && m0 > n0))
186 q0--;
187 sub_ddmmss (m1, m0, m1, m0, d1, d0);
190 q1 = 0;
192 /* Remainder in (n1n0 - m1m0) >> bm. */
193 if (rp != 0)
195 sub_ddmmss (n1, n0, n1, n0, m1, m0);
196 rr.s.low = (n1 << b) | (n0 >> bm);
197 rr.s.high = n1 >> bm;
198 *rp = rr.ll;
204 const DWunion ww = {{.low = q0, .high = q1}};
205 return ww.ll;
208 UDWtype
209 __udivdi3 (UDWtype n, UDWtype d)
211 return __udivmoddi4 (n, d, (UDWtype *) 0);
214 UDWtype
215 __umoddi3 (UDWtype u, UDWtype v)
217 UDWtype w;
219 (void) __udivmoddi4 (u, v, &w);
221 return w;