1 /* mpf_div -- Divide two floats.
3 Copyright 1993, 1994, 1996, 2000, 2001, 2002, 2004, 2005, 2010 Free Software
6 This file is part of the GNU MP Library.
8 The GNU MP Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
13 The GNU MP Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
27 No attempt is made to identify an overlap u==v. The result will be
28 correct (1.0), but a full actual division is done whereas of course
29 x/x==1 needs no work. Such a call is not a sensible thing to make, and
30 it's left to an application to notice and optimize if it might arise
31 somehow through pointer aliasing or whatever.
35 The high quotient limb is non-zero when high{up,vsize} >= {vp,vsize}. We
36 could make that comparison and use qsize==prec instead of qsize==prec+1,
37 to save one limb in the division.
39 If r==u but the size is enough bigger than prec that there won't be an
40 overlap between quotient and dividend in mpn_tdiv_qr, then we can avoid
41 copying up,usize. This would only arise from a prec reduced with
42 mpf_set_prec_raw and will be pretty unusual, but might be worthwhile if
43 it could be worked into the copy_u decision cleanly. */
46 mpf_div (mpf_ptr r
, mpf_srcptr u
, mpf_srcptr v
)
49 mp_ptr rp
, tp
, new_vp
;
50 mp_size_t usize
, vsize
, rsize
, prospective_rsize
, tsize
, zeros
;
51 mp_size_t sign_quotient
, prec
, high_zero
, chop
;
58 sign_quotient
= usize
^ vsize
;
74 rexp
= EXP(u
) - EXP(v
) + 1;
80 prospective_rsize
= usize
- vsize
+ 1; /* quot from using given u,v sizes */
81 rsize
= prec
+ 1; /* desired quot */
83 zeros
= rsize
- prospective_rsize
; /* padding u to give rsize */
84 copy_u
= (zeros
> 0 || rp
== up
); /* copy u if overlap or padding */
86 chop
= MAX (-zeros
, 0); /* negative zeros means shorten u */
89 zeros
+= chop
; /* now zeros >= 0 */
91 tsize
= usize
+ zeros
; /* size for possible copy of u */
93 /* copy and possibly extend u if necessary */
96 tp
= TMP_ALLOC_LIMBS (tsize
+ 1); /* +1 for mpn_div_q's scratch needs */
98 MPN_COPY (tp
+zeros
, up
, usize
);
104 tp
= TMP_ALLOC_LIMBS (usize
+ 1);
107 /* ensure divisor doesn't overlap quotient */
110 new_vp
= TMP_ALLOC_LIMBS (vsize
);
111 MPN_COPY (new_vp
, vp
, vsize
);
115 ASSERT (usize
-vsize
+1 == rsize
);
116 mpn_div_q (rp
, up
, usize
, vp
, vsize
, tp
);
118 /* strip possible zero high limb */
119 high_zero
= (rp
[rsize
-1] == 0);
123 SIZ(r
) = sign_quotient
>= 0 ? rsize
: -rsize
;