~ubuntu-branches/ubuntu/lucid/silo/lucid

« back to all changes in this revision

Viewing changes to common/sdiv.S

  • Committer: Bazaar Package Importer
  • Author(s): Fabio M. Di Nitto
  • Date: 2007-10-25 09:28:08 UTC
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: james.westby@ubuntu.com-20071025092808-pnva6x2ggmlkd65e
Tags: upstream-1.4.13a+git20070930
ImportĀ upstreamĀ versionĀ 1.4.13a+git20070930

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: sdiv.S,v 1.1 2003/04/14 03:24:43 bencollins Exp $
 
2
 * sdiv.S:      This routine was taken from glibc-1.09 and is covered
 
3
 *              by the GNU Library General Public License Version 2.
 
4
 */
 
5
 
 
6
 
 
7
/* This file is generated from divrem.m4; DO NOT EDIT! */
 
8
/*
 
9
 * Division and remainder, from Appendix E of the Sparc Version 8
 
10
 * Architecture Manual, with fixes from Gordon Irlam.
 
11
 */
 
12
 
 
13
/*
 
14
 * Input: dividend and divisor in %o0 and %o1 respectively.
 
15
 *
 
16
 * m4 parameters:
 
17
 *  .div        name of function to generate
 
18
 *  div         div=div => %o0 / %o1; div=rem => %o0 % %o1
 
19
 *  true                true=true => signed; true=false => unsigned
 
20
 *
 
21
 * Algorithm parameters:
 
22
 *  N           how many bits per iteration we try to get (4)
 
23
 *  WORDSIZE    total number of bits (32)
 
24
 *
 
25
 * Derived constants:
 
26
 *  TOPBITS     number of bits in the top decade of a number
 
27
 *
 
28
 * Important variables:
 
29
 *  Q           the partial quotient under development (initially 0)
 
30
 *  R           the remainder so far, initially the dividend
 
31
 *  ITER        number of main division loop iterations required;
 
32
 *              equal to ceil(log2(quotient) / N).  Note that this
 
33
 *              is the log base (2^N) of the quotient.
 
34
 *  V           the current comparand, initially divisor*2^(ITER*N-1)
 
35
 *
 
36
 * Cost:
 
37
 *  Current estimate for non-large dividend is
 
38
 *      ceil(log2(quotient) / N) * (10 + 7N/2) + C
 
39
 *  A large dividend is one greater than 2^(31-TOPBITS) and takes a
 
40
 *  different path, as the upper bits of the quotient must be developed
 
41
 *  one bit at a time.
 
42
 */
 
43
 
 
44
.register %g2,#scratch
 
45
 
 
46
        .globl .div
 
47
.div:
 
48
        ! compute sign of result; if neither is negative, no problem
 
49
        orcc    %o1, %o0, %g0   ! either negative?
 
50
        bge     2f                      ! no, go do the divide
 
51
         xor    %o1, %o0, %g2   ! compute sign in any case
 
52
 
 
53
        tst     %o1
 
54
        bge     1f
 
55
         tst    %o0
 
56
        ! %o1 is definitely negative; %o0 might also be negative
 
57
        bge     2f                      ! if %o0 not negative...
 
58
         sub    %g0, %o1, %o1   ! in any case, make %o1 nonneg
 
59
1:      ! %o0 is negative, %o1 is nonnegative
 
60
        sub     %g0, %o0, %o0   ! make %o0 nonnegative
 
61
2:
 
62
 
 
63
        ! Ready to divide.  Compute size of quotient; scale comparand.
 
64
        orcc    %o1, %g0, %o5
 
65
        bne     1f
 
66
         mov    %o0, %o3
 
67
 
 
68
                ! Divide by zero trap.  If it returns, return 0 (about as
 
69
                ! wrong as possible, but that is what SunOS does...).
 
70
                ! ta    ST_DIV0 -- Not for SILO
 
71
                retl
 
72
                 clr    %o0
 
73
 
 
74
1:
 
75
        cmp     %o3, %o5                        ! if %o1 exceeds %o0, done
 
76
        blu     Lgot_result             ! (and algorithm fails otherwise)
 
77
         clr    %o2
 
78
 
 
79
        sethi   %hi(1 << (32 - 4 - 1)), %g1
 
80
 
 
81
        cmp     %o3, %g1
 
82
        blu     Lnot_really_big
 
83
         clr    %o4
 
84
 
 
85
        ! Here the dividend is >= 2**(31-N) or so.  We must be careful here,
 
86
        ! as our usual N-at-a-shot divide step will cause overflow and havoc.
 
87
        ! The number of bits in the result here is N*ITER+SC, where SC <= N.
 
88
        ! Compute ITER in an unorthodox manner: know we need to shift V into
 
89
        ! the top decade: so do not even bother to compare to R.
 
90
        1:
 
91
                cmp     %o5, %g1
 
92
                bgeu    3f
 
93
                 mov    1, %g7
 
94
 
 
95
                sll     %o5, 4, %o5
 
96
 
 
97
                b       1b
 
98
                 add    %o4, 1, %o4
 
99
 
 
100
        ! Now compute %g7.
 
101
        2:
 
102
                addcc   %o5, %o5, %o5
 
103
                bcc     Lnot_too_big
 
104
                 add    %g7, 1, %g7
 
105
 
 
106
                ! We get here if the %o1 overflowed while shifting.
 
107
                ! This means that %o3 has the high-order bit set.
 
108
                ! Restore %o5 and subtract from %o3.
 
109
                sll     %g1, 4, %g1     ! high order bit
 
110
                srl     %o5, 1, %o5             ! rest of %o5
 
111
                add     %o5, %g1, %o5
 
112
 
 
113
                b       Ldo_single_div
 
114
                 sub    %g7, 1, %g7
 
115
 
 
116
        Lnot_too_big:
 
117
        3:
 
118
                cmp     %o5, %o3
 
119
                blu     2b
 
120
                 nop
 
121
 
 
122
                be      Ldo_single_div
 
123
                 nop
 
124
        /* NB: these are commented out in the V8-Sparc manual as well */
 
125
        /* (I do not understand this) */
 
126
        ! %o5 > %o3: went too far: back up 1 step
 
127
        !       srl     %o5, 1, %o5
 
128
        !       dec     %g7
 
129
        ! do single-bit divide steps
 
130
        !
 
131
        ! We have to be careful here.  We know that %o3 >= %o5, so we can do the
 
132
        ! first divide step without thinking.  BUT, the others are conditional,
 
133
        ! and are only done if %o3 >= 0.  Because both %o3 and %o5 may have the high-
 
134
        ! order bit set in the first step, just falling into the regular
 
135
        ! division loop will mess up the first time around.
 
136
        ! So we unroll slightly...
 
137
        Ldo_single_div:
 
138
                subcc   %g7, 1, %g7
 
139
                bl      Lend_regular_divide
 
140
                 nop
 
141
 
 
142
                sub     %o3, %o5, %o3
 
143
                mov     1, %o2
 
144
 
 
145
                b       Lend_single_divloop
 
146
                 nop
 
147
        Lsingle_divloop:
 
148
                sll     %o2, 1, %o2
 
149
 
 
150
                bl      1f
 
151
                 srl    %o5, 1, %o5
 
152
                ! %o3 >= 0
 
153
                sub     %o3, %o5, %o3
 
154
 
 
155
                b       2f
 
156
                 add    %o2, 1, %o2
 
157
        1:      ! %o3 < 0
 
158
                add     %o3, %o5, %o3
 
159
                sub     %o2, 1, %o2
 
160
        2:
 
161
        Lend_single_divloop:
 
162
                subcc   %g7, 1, %g7
 
163
                bge     Lsingle_divloop
 
164
                 tst    %o3
 
165
 
 
166
                b,a     Lend_regular_divide
 
167
 
 
168
Lnot_really_big:
 
169
1:
 
170
        sll     %o5, 4, %o5
 
171
        cmp     %o5, %o3
 
172
        bleu    1b
 
173
         addcc  %o4, 1, %o4
 
174
 
 
175
        be      Lgot_result
 
176
         sub    %o4, 1, %o4
 
177
 
 
178
        tst     %o3     ! set up for initial iteration
 
179
Ldivloop:
 
180
        sll     %o2, 4, %o2
 
181
                ! depth 1, accumulated bits 0
 
182
        bl      L.1.16
 
183
         srl    %o5,1,%o5
 
184
        ! remainder is positive
 
185
        subcc   %o3,%o5,%o3
 
186
                        ! depth 2, accumulated bits 1
 
187
        bl      L.2.17
 
188
         srl    %o5,1,%o5
 
189
        ! remainder is positive
 
190
        subcc   %o3,%o5,%o3
 
191
                        ! depth 3, accumulated bits 3
 
192
        bl      L.3.19
 
193
         srl    %o5,1,%o5
 
194
        ! remainder is positive
 
195
        subcc   %o3,%o5,%o3
 
196
                        ! depth 4, accumulated bits 7
 
197
        bl      L.4.23
 
198
         srl    %o5,1,%o5
 
199
        ! remainder is positive
 
200
        subcc   %o3,%o5,%o3
 
201
        b       9f
 
202
         add    %o2, (7*2+1), %o2
 
203
 
 
204
L.4.23:
 
205
        ! remainder is negative
 
206
        addcc   %o3,%o5,%o3
 
207
        b       9f
 
208
         add    %o2, (7*2-1), %o2
 
209
 
 
210
L.3.19:
 
211
        ! remainder is negative
 
212
        addcc   %o3,%o5,%o3
 
213
                        ! depth 4, accumulated bits 5
 
214
        bl      L.4.21
 
215
         srl    %o5,1,%o5
 
216
        ! remainder is positive
 
217
        subcc   %o3,%o5,%o3
 
218
        b       9f
 
219
         add    %o2, (5*2+1), %o2
 
220
 
 
221
L.4.21:
 
222
        ! remainder is negative
 
223
        addcc   %o3,%o5,%o3
 
224
        b       9f
 
225
         add    %o2, (5*2-1), %o2
 
226
 
 
227
L.2.17:
 
228
        ! remainder is negative
 
229
        addcc   %o3,%o5,%o3
 
230
                        ! depth 3, accumulated bits 1
 
231
        bl      L.3.17
 
232
         srl    %o5,1,%o5
 
233
        ! remainder is positive
 
234
        subcc   %o3,%o5,%o3
 
235
                        ! depth 4, accumulated bits 3
 
236
        bl      L.4.19
 
237
         srl    %o5,1,%o5
 
238
        ! remainder is positive
 
239
        subcc   %o3,%o5,%o3
 
240
        b       9f
 
241
         add    %o2, (3*2+1), %o2
 
242
 
 
243
L.4.19:
 
244
        ! remainder is negative
 
245
        addcc   %o3,%o5,%o3
 
246
        b       9f
 
247
         add    %o2, (3*2-1), %o2
 
248
        
 
249
        
 
250
L.3.17:
 
251
        ! remainder is negative
 
252
        addcc   %o3,%o5,%o3
 
253
                        ! depth 4, accumulated bits 1
 
254
        bl      L.4.17
 
255
         srl    %o5,1,%o5
 
256
        ! remainder is positive
 
257
        subcc   %o3,%o5,%o3
 
258
        b       9f
 
259
         add    %o2, (1*2+1), %o2
 
260
 
 
261
L.4.17:
 
262
        ! remainder is negative
 
263
        addcc   %o3,%o5,%o3
 
264
        b       9f
 
265
         add    %o2, (1*2-1), %o2
 
266
 
 
267
L.1.16:
 
268
        ! remainder is negative
 
269
        addcc   %o3,%o5,%o3
 
270
                        ! depth 2, accumulated bits -1
 
271
        bl      L.2.15
 
272
         srl    %o5,1,%o5
 
273
        ! remainder is positive
 
274
        subcc   %o3,%o5,%o3
 
275
                        ! depth 3, accumulated bits -1
 
276
        bl      L.3.15
 
277
         srl    %o5,1,%o5
 
278
        ! remainder is positive
 
279
        subcc   %o3,%o5,%o3
 
280
                        ! depth 4, accumulated bits -1
 
281
        bl      L.4.15
 
282
         srl    %o5,1,%o5
 
283
        ! remainder is positive
 
284
        subcc   %o3,%o5,%o3
 
285
        b       9f
 
286
         add    %o2, (-1*2+1), %o2
 
287
 
 
288
L.4.15:
 
289
        ! remainder is negative
 
290
        addcc   %o3,%o5,%o3
 
291
        b       9f
 
292
         add    %o2, (-1*2-1), %o2
 
293
 
 
294
L.3.15:
 
295
        ! remainder is negative
 
296
        addcc   %o3,%o5,%o3
 
297
                        ! depth 4, accumulated bits -3
 
298
        bl      L.4.13
 
299
         srl    %o5,1,%o5
 
300
        ! remainder is positive
 
301
        subcc   %o3,%o5,%o3
 
302
        b       9f
 
303
         add    %o2, (-3*2+1), %o2
 
304
 
 
305
L.4.13:
 
306
        ! remainder is negative
 
307
        addcc   %o3,%o5,%o3
 
308
        b       9f
 
309
         add    %o2, (-3*2-1), %o2
 
310
 
 
311
L.2.15:
 
312
        ! remainder is negative
 
313
        addcc   %o3,%o5,%o3
 
314
                        ! depth 3, accumulated bits -3
 
315
        bl      L.3.13
 
316
         srl    %o5,1,%o5
 
317
        ! remainder is positive
 
318
        subcc   %o3,%o5,%o3
 
319
                        ! depth 4, accumulated bits -5
 
320
        bl      L.4.11
 
321
         srl    %o5,1,%o5
 
322
        ! remainder is positive
 
323
        subcc   %o3,%o5,%o3
 
324
        b       9f
 
325
         add    %o2, (-5*2+1), %o2
 
326
 
 
327
L.4.11:
 
328
        ! remainder is negative
 
329
        addcc   %o3,%o5,%o3
 
330
        b       9f
 
331
         add    %o2, (-5*2-1), %o2
 
332
 
 
333
L.3.13:
 
334
        ! remainder is negative
 
335
        addcc   %o3,%o5,%o3
 
336
                        ! depth 4, accumulated bits -7
 
337
        bl      L.4.9
 
338
         srl    %o5,1,%o5
 
339
        ! remainder is positive
 
340
        subcc   %o3,%o5,%o3
 
341
        b       9f
 
342
         add    %o2, (-7*2+1), %o2
 
343
 
 
344
L.4.9:
 
345
        ! remainder is negative
 
346
        addcc   %o3,%o5,%o3
 
347
        b       9f
 
348
         add    %o2, (-7*2-1), %o2
 
349
 
 
350
        9:
 
351
Lend_regular_divide:
 
352
        subcc   %o4, 1, %o4
 
353
        bge     Ldivloop
 
354
         tst    %o3
 
355
 
 
356
        bl,a    Lgot_result
 
357
        ! non-restoring fixup here (one instruction only!)
 
358
        sub     %o2, 1, %o2
 
359
 
 
360
Lgot_result:
 
361
        ! check to see if answer should be < 0
 
362
        tst     %g2
 
363
        bl,a    1f
 
364
         sub %g0, %o2, %o2
 
365
1:
 
366
        retl
 
367
         mov %o2, %o0
 
368
 
 
369
        .globl  .div_patch
 
370
.div_patch:
 
371
        sra     %o0, 0x1f, %o2
 
372
        wr      %o2, 0x0, %y
 
373
        nop
 
374
        nop
 
375
        nop
 
376
        sdivcc  %o0, %o1, %o0
 
377
        bvs,a   1f
 
378
         xnor   %o0, %g0, %o0
 
379
1:      retl
 
380
         nop