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

« back to all changes in this revision

Viewing changes to common/rem.S

  • Committer: Bazaar Package Importer
  • Author(s): Fabio M. Di Nitto
  • Date: 2007-10-25 09:28:08 UTC
  • mfrom: (15.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20071025092808-1yhj12t7s4zqsfu5
Tags: 1.4.13a+git20070930-1ubuntu1
* Merge from debian unstable, remaining changes:
  - Build with -fno-stack-protector.
  - Change silo.postinst to automatically update the boot block without
    invoking siloconfig and keep asking questions on upgrades.
  - Convert silo.conf to use /dev/disk/by-uuid.
  - Ubuntu maintainer foobar.
  - Fix debian/rules call to dh_installdocs.
  - Drop the requirement of gcc-4.1 and start using default gcc.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: rem.S,v 1.1 2003/04/14 03:24:43 bencollins Exp $
 
2
 * rem.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
 *  .rem        name of function to generate
 
18
 *  rem         rem=div => %o0 / %o1; rem=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 .rem
 
47
.rem:
 
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
         mov    %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
 
 
104
                bcc     Lnot_too_big
 
105
                 add    %g7, 1, %g7
 
106
 
 
107
                ! We get here if the %o1 overflowed while shifting.
 
108
                ! This means that %o3 has the high-order bit set.
 
109
                ! Restore %o5 and subtract from %o3.
 
110
                sll     %g1, 4, %g1     ! high order bit
 
111
                srl     %o5, 1, %o5             ! rest of %o5
 
112
                add     %o5, %g1, %o5
 
113
 
 
114
                b       Ldo_single_div
 
115
                 sub    %g7, 1, %g7
 
116
 
 
117
        Lnot_too_big:
 
118
        3:
 
119
                cmp     %o5, %o3
 
120
                blu     2b
 
121
                 nop
 
122
 
 
123
                be      Ldo_single_div
 
124
                 nop
 
125
        /* NB: these are commented out in the V8-Sparc manual as well */
 
126
        /* (I do not understand this) */
 
127
        ! %o5 > %o3: went too far: back up 1 step
 
128
        !       srl     %o5, 1, %o5
 
129
        !       dec     %g7
 
130
        ! do single-bit divide steps
 
131
        !
 
132
        ! We have to be careful here.  We know that %o3 >= %o5, so we can do the
 
133
        ! first divide step without thinking.  BUT, the others are conditional,
 
134
        ! and are only done if %o3 >= 0.  Because both %o3 and %o5 may have the high-
 
135
        ! order bit set in the first step, just falling into the regular
 
136
        ! division loop will mess up the first time around.
 
137
        ! So we unroll slightly...
 
138
        Ldo_single_div:
 
139
                subcc   %g7, 1, %g7
 
140
                bl      Lend_regular_divide
 
141
                 nop
 
142
 
 
143
                sub     %o3, %o5, %o3
 
144
                mov     1, %o2
 
145
 
 
146
                b       Lend_single_divloop
 
147
                 nop
 
148
        Lsingle_divloop:
 
149
                sll     %o2, 1, %o2
 
150
 
 
151
                bl      1f
 
152
                 srl    %o5, 1, %o5
 
153
                ! %o3 >= 0
 
154
                sub     %o3, %o5, %o3
 
155
 
 
156
                b       2f
 
157
                 add    %o2, 1, %o2
 
158
        1:      ! %o3 < 0
 
159
                add     %o3, %o5, %o3
 
160
                sub     %o2, 1, %o2
 
161
        2:
 
162
        Lend_single_divloop:
 
163
                subcc   %g7, 1, %g7
 
164
                bge     Lsingle_divloop
 
165
                 tst    %o3
 
166
 
 
167
                b,a     Lend_regular_divide
 
168
 
 
169
Lnot_really_big:
 
170
1:
 
171
        sll     %o5, 4, %o5
 
172
        cmp     %o5, %o3
 
173
        bleu    1b
 
174
         addcc  %o4, 1, %o4
 
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
 
 
202
        b       9f
 
203
         add    %o2, (7*2+1), %o2
 
204
        
 
205
L.4.23:
 
206
        ! remainder is negative
 
207
        addcc   %o3,%o5,%o3
 
208
        b       9f
 
209
         add    %o2, (7*2-1), %o2
 
210
        
 
211
L.3.19:
 
212
        ! remainder is negative
 
213
        addcc   %o3,%o5,%o3
 
214
                        ! depth 4, accumulated bits 5
 
215
        bl      L.4.21
 
216
         srl    %o5,1,%o5
 
217
        ! remainder is positive
 
218
        subcc   %o3,%o5,%o3
 
219
        b       9f
 
220
         add    %o2, (5*2+1), %o2
 
221
        
 
222
L.4.21:
 
223
        ! remainder is negative
 
224
        addcc   %o3,%o5,%o3
 
225
        b       9f
 
226
         add    %o2, (5*2-1), %o2
 
227
        
 
228
L.2.17:
 
229
        ! remainder is negative
 
230
        addcc   %o3,%o5,%o3
 
231
                        ! depth 3, accumulated bits 1
 
232
        bl      L.3.17
 
233
         srl    %o5,1,%o5
 
234
        ! remainder is positive
 
235
        subcc   %o3,%o5,%o3
 
236
                        ! depth 4, accumulated bits 3
 
237
        bl      L.4.19
 
238
         srl    %o5,1,%o5
 
239
        ! remainder is positive
 
240
        subcc   %o3,%o5,%o3
 
241
        b       9f
 
242
         add    %o2, (3*2+1), %o2
 
243
 
 
244
L.4.19:
 
245
        ! remainder is negative
 
246
        addcc   %o3,%o5,%o3
 
247
        b       9f
 
248
         add    %o2, (3*2-1), %o2
 
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
 
 
334
L.3.13:
 
335
        ! remainder is negative
 
336
        addcc   %o3,%o5,%o3
 
337
                        ! depth 4, accumulated bits -7
 
338
        bl      L.4.9
 
339
         srl    %o5,1,%o5
 
340
        ! remainder is positive
 
341
        subcc   %o3,%o5,%o3
 
342
        b       9f
 
343
         add    %o2, (-7*2+1), %o2
 
344
 
 
345
L.4.9:
 
346
        ! remainder is negative
 
347
        addcc   %o3,%o5,%o3
 
348
        b       9f
 
349
         add    %o2, (-7*2-1), %o2
 
350
 
 
351
        9:
 
352
Lend_regular_divide:
 
353
        subcc   %o4, 1, %o4
 
354
        bge     Ldivloop
 
355
         tst    %o3
 
356
 
 
357
        bl,a    Lgot_result
 
358
        ! non-restoring fixup here (one instruction only!)
 
359
        add     %o3, %o1, %o3
 
360
 
 
361
Lgot_result:
 
362
        ! check to see if answer should be < 0
 
363
        tst     %g2
 
364
        bl,a    1f
 
365
         sub %g0, %o3, %o3
 
366
1:
 
367
        retl
 
368
         mov %o3, %o0
 
369
 
 
370
        .globl  .rem_patch
 
371
.rem_patch:
 
372
        sra     %o0, 0x1f, %o4
 
373
        wr      %o4, 0x0, %y
 
374
        nop
 
375
        nop
 
376
        nop
 
377
        sdivcc  %o0, %o1, %o2
 
378
        bvs,a   1f
 
379
         xnor   %o2, %g0, %o2
 
380
1:      smul    %o2, %o1, %o2
 
381
        retl
 
382
         sub    %o0, %o2, %o0
 
383
        nop