blob: ad552c115cac016d443e3ced22129c723bd76093 [file] [log] [blame]
Austin Schuhdace2a62020-08-18 10:56:48 -07001/* mpf_div_2exp -- Divide a float by 2^n.
2
3Copyright 1993, 1994, 1996, 2000-2002, 2004 Free Software Foundation, Inc.
4
5This file is part of the GNU MP Library.
6
7The GNU MP Library is free software; you can redistribute it and/or modify
8it under the terms of either:
9
10 * the GNU Lesser General Public License as published by the Free
11 Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
13
14or
15
16 * the GNU General Public License as published by the Free Software
17 Foundation; either version 2 of the License, or (at your option) any
18 later version.
19
20or both in parallel, as here.
21
22The GNU MP Library is distributed in the hope that it will be useful, but
23WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25for more details.
26
27You should have received copies of the GNU General Public License and the
28GNU Lesser General Public License along with the GNU MP Library. If not,
29see https://www.gnu.org/licenses/. */
30
31#include "gmp-impl.h"
32
33
34/* Multiples of GMP_NUMB_BITS in exp simply mean an amount subtracted from
35 EXP(u) to set EXP(r). The remainder exp%GMP_NUMB_BITS is then a right
36 shift for the limb data.
37
38 If exp%GMP_NUMB_BITS == 0 then there's no shifting, we effectively just
39 do an mpz_set with changed EXP(r). Like mpz_set we take prec+1 limbs in
40 this case. Although just prec would suffice, it's nice to have
41 mpf_div_2exp with exp==0 come out the same as mpz_set.
42
43 When shifting we take up to prec many limbs from the input. Our shift is
44 cy = mpn_rshift (PTR(r)+1, PTR(u)+k, ...), where k is the number of low
45 limbs dropped from u, and the carry out is stored to PTR(r)[0]. We don't
46 try to work extra bits from PTR(u)[k-1] (when k>=1 makes it available)
47 into that low carry limb. Just prec limbs (with the high non-zero) from
48 the input is enough bits for the application requested precision, no need
49 to do extra work.
50
51 If r==u the shift will have overlapping operands. When k>=1 (ie. when
52 usize > prec), the overlap is in the style supported by rshift (ie. dst
53 <= src).
54
55 But when r==u and k==0 (ie. usize <= prec), we would have an invalid
56 overlap (mpn_rshift (rp+1, rp, ...)). In this case we must instead use
57 mpn_lshift (PTR(r), PTR(u), size, NUMB-shift). An lshift by NUMB-shift
58 bits gives identical data of course, it's just its overlap restrictions
59 which differ.
60
61 In both shift cases, the resulting data is abs_usize+1 limbs. "adj" is
62 used to add +1 to that size if the high is non-zero (it may of course
63 have become zero by the shifting). EXP(u) is the exponent just above
64 those abs_usize+1 limbs, so it gets -1+adj, which means -1 if the high is
65 zero, or no change if the high is non-zero.
66
67 Enhancements:
68
69 The way mpn_lshift is used means successive mpf_div_2exp calls on the
70 same operand will accumulate low zero limbs, until prec+1 limbs is
71 reached. This is wasteful for subsequent operations. When abs_usize <=
72 prec, we should test the low exp%GMP_NUMB_BITS many bits of PTR(u)[0],
73 ie. those which would be shifted out by an mpn_rshift. If they're zero
74 then use that mpn_rshift. */
75
76void
77mpf_div_2exp (mpf_ptr r, mpf_srcptr u, mp_bitcnt_t exp)
78{
79 mp_srcptr up;
80 mp_ptr rp = r->_mp_d;
81 mp_size_t usize;
82 mp_size_t abs_usize;
83 mp_size_t prec = r->_mp_prec;
84 mp_exp_t uexp = u->_mp_exp;
85
86 usize = u->_mp_size;
87
88 if (UNLIKELY (usize == 0))
89 {
90 r->_mp_size = 0;
91 r->_mp_exp = 0;
92 return;
93 }
94
95 abs_usize = ABS (usize);
96 up = u->_mp_d;
97
98 if (exp % GMP_NUMB_BITS == 0)
99 {
100 prec++; /* retain more precision here as we don't need
101 to account for carry-out here */
102 if (abs_usize > prec)
103 {
104 up += abs_usize - prec;
105 abs_usize = prec;
106 }
107 if (rp != up)
108 MPN_COPY_INCR (rp, up, abs_usize);
109 r->_mp_exp = uexp - exp / GMP_NUMB_BITS;
110 }
111 else
112 {
113 mp_limb_t cy_limb;
114 mp_size_t adj;
115 if (abs_usize > prec)
116 {
117 up += abs_usize - prec;
118 abs_usize = prec;
119 /* Use mpn_rshift since mpn_lshift operates downwards, and we
120 therefore would clobber part of U before using that part, in case
121 R is the same variable as U. */
122 cy_limb = mpn_rshift (rp + 1, up, abs_usize, exp % GMP_NUMB_BITS);
123 rp[0] = cy_limb;
124 adj = rp[abs_usize] != 0;
125 }
126 else
127 {
128 cy_limb = mpn_lshift (rp, up, abs_usize,
129 GMP_NUMB_BITS - exp % GMP_NUMB_BITS);
130 rp[abs_usize] = cy_limb;
131 adj = cy_limb != 0;
132 }
133
134 abs_usize += adj;
135 r->_mp_exp = uexp - exp / GMP_NUMB_BITS - 1 + adj;
136 }
137 r->_mp_size = usize >= 0 ? abs_usize : -abs_usize;
138}