1 /* Copyright (C) 2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library 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 GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 /* 64-bit signed long divide. These are not normal C functions. Argument
23 registers are t10 and t11, the result goes in t12. Only t12 and AT may
26 Theory of operation here is that we can use the FPU divider for virtually
27 all operands that we see: all dividend values between -2**53 and 2**53-1
28 can be computed directly. Note that divisor values need not be checked
29 against that range because the rounded fp value will be close enough such
30 that the quotient is < 1, which will properly be truncated to zero when we
31 convert back to integer.
33 When the dividend is outside the range for which we can compute exact
34 results, we use the fp quotent as an estimate from which we begin refining
35 an exact integral value. This reduces the number of iterations in the
36 shift-and-subtract loop significantly.
38 The FPCR save/restore is due to the fact that the EV6 _will_ set FPCR_INE
39 for cvttq/c even without /sui being set. It will not, however, properly
40 raise the exception, so we don't have to worry about FPCR_INED being clear
41 and so dying by SIGFPE. */
46 .type __divq, @funcnoplt
50 cfi_return_column (RA)
53 cfi_def_cfa_offset (FRAME)
56 /* Get the fp divide insn issued as quickly as possible. After
57 that's done, we have at least 22 cycles until its results are
58 ready -- all the time in the world to figure out how we're
59 going to use the results. */
66 cfi_rel_offset ($f0, 0)
67 cfi_rel_offset ($f1, 8)
68 cfi_rel_offset ($f3, 48)
71 _ITOFT2 X, $f0, 16, Y, $f1, 24
76 /* Check to see if X fit in the double as an exact value. */
83 /* If we get here, we're expecting exact results from the division.
84 Do nothing else besides convert and clean up. */
96 cfi_def_cfa_offset (0)
103 /* If we get here, X is large enough that we don't expect exact
104 results, and neither X nor Y got mis-translated for the fp
105 division. Our task is to take the fp result, figure out how
106 far it's off from the correct result and compute a fixup. */
111 cfi_rel_offset (t0, 16)
112 cfi_rel_offset (t1, 24)
113 cfi_rel_offset (t2, 32)
114 cfi_rel_offset (t5, 40)
116 #define Q RV /* quotient */
117 #define R t0 /* remainder */
118 #define SY t1 /* scaled Y */
119 #define S t2 /* scalar */
120 #define QY t3 /* Q*Y */
122 /* The fixup code below can only handle unsigned values. */
135 cfi_rel_offset (t3, 0)
141 cfi_rel_offset (t4, 8)
158 bne t5, $fix_sign_out
174 cfi_def_cfa_offset (0)
179 /* The quotient that we computed was too large. We need to reduce
180 it by S such that Y*S >= R. Obviously the closer we get to the
181 correct value the better, but overshooting high is ok, as we'll
182 fix that up later. */
196 /* The quotient that we computed was too small. Divide Y by the
197 current remainder (R) and add that to the existing quotient (Q).
198 The expectation, of course, is that R is much smaller than X. */
199 /* Begin with a shift-up loop. Compute S such that Y*S >= R. We
200 already have a copy of Y in SY and the value 1 in S. */
208 /* Shift-down and subtract loop. Each iteration compares our scaled
209 Y (SY) with the remainder (R); if SY <= R then X is divisible by
210 Y's scalar (S) so add it to the quotient (Q). */
225 /* If we got here, then X|Y is negative. Need to adjust everything
226 such that we're doing unsigned division in the fixup loop. */
227 /* T5 records the changes we had to make:
228 bit 0: set if result should be negative.
229 bit 2: set if X was negated.
230 bit 3: set if Y was negated.
245 blbc t5, $fix_sign_in_ret1
255 /* Now we get to undo what we did above. */
256 /* ??? Is this really faster than just increasing the size of
257 the stack frame and storing X and Y in memory? */
272 .size __divq, .-__divq