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