1 /* mpf_div -- Divide two floats.
3 Copyright 1993, 1994, 1996, 2000-2002, 2004, 2005, 2010, 2012 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 either:
11 * the GNU Lesser General Public License as published by the Free
12 Software Foundation; either version 3 of the License, or (at your
13 option) any later version.
17 * the GNU General Public License as published by the Free Software
18 Foundation; either version 2 of the License, or (at your option) any
21 or both in parallel, as here.
23 The GNU MP Library is distributed in the hope that it will be useful, but
24 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
25 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
28 You should have received copies of the GNU General Public License and the
29 GNU Lesser General Public License along with the GNU MP Library. If not,
30 see https://www.gnu.org/licenses/. */
38 No attempt is made to identify an overlap u==v. The result will be
39 correct (1.0), but a full actual division is done whereas of course
40 x/x==1 needs no work. Such a call is not a sensible thing to make, and
41 it's left to an application to notice and optimize if it might arise
42 somehow through pointer aliasing or whatever.
46 The high quotient limb is non-zero when high{up,vsize} >= {vp,vsize}. We
47 could make that comparison and use qsize==prec instead of qsize==prec+1,
48 to save one limb in the division.
50 If r==u but the size is enough bigger than prec that there won't be an
51 overlap between quotient and dividend in mpn_div_q, then we can avoid
52 copying up,usize. This would only arise from a prec reduced with
53 mpf_set_prec_raw and will be pretty unusual, but might be worthwhile if
54 it could be worked into the copy_u decision cleanly. */
57 mpf_div (mpf_ptr r
, mpf_srcptr u
, mpf_srcptr v
)
60 mp_ptr rp
, tp
, new_vp
;
61 mp_size_t usize
, vsize
, rsize
, prospective_rsize
, tsize
, zeros
;
62 mp_size_t sign_quotient
, prec
, high_zero
, chop
;
70 if (UNLIKELY (vsize
== 0))
80 sign_quotient
= usize
^ vsize
;
86 rexp
= EXP(u
) - EXP(v
) + 1;
92 prospective_rsize
= usize
- vsize
+ 1; /* quot from using given u,v sizes */
93 rsize
= prec
+ 1; /* desired quot */
95 zeros
= rsize
- prospective_rsize
; /* padding u to give rsize */
96 copy_u
= (zeros
> 0 || rp
== up
); /* copy u if overlap or padding */
98 chop
= MAX (-zeros
, 0); /* negative zeros means shorten u */
101 zeros
+= chop
; /* now zeros >= 0 */
103 tsize
= usize
+ zeros
; /* size for possible copy of u */
105 /* copy and possibly extend u if necessary */
108 tp
= TMP_ALLOC_LIMBS (tsize
+ 1); /* +1 for mpn_div_q's scratch needs */
109 MPN_ZERO (tp
, zeros
);
110 MPN_COPY (tp
+zeros
, up
, usize
);
116 tp
= TMP_ALLOC_LIMBS (usize
+ 1);
119 /* ensure divisor doesn't overlap quotient */
122 new_vp
= TMP_ALLOC_LIMBS (vsize
);
123 MPN_COPY (new_vp
, vp
, vsize
);
127 ASSERT (usize
-vsize
+1 == rsize
);
128 mpn_div_q (rp
, up
, usize
, vp
, vsize
, tp
);
130 /* strip possible zero high limb */
131 high_zero
= (rp
[rsize
-1] == 0);
135 SIZ(r
) = sign_quotient
>= 0 ? rsize
: -rsize
;