1 /* mpq_mul_2exp, mpq_div_2exp - multiply or divide by 2^N */
2
3 /*
4 Copyright 2000, 2002, 2012, 2018 Free Software Foundation, Inc.
5
6 This file is part of the GNU MP Library.
7
8 The GNU MP Library is free software; you can redistribute it and/or modify
9 it under the terms of either:
10
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.
14
15 or
16
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
19 later version.
20
21 or both in parallel, as here.
22
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
26 for more details.
27
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/. */
31
32 #include "gmp-impl.h"
33 #include "longlong.h"
34
35
36 /* The multiplier/divisor "n", representing 2^n, is applied by right shifting
37 "r" until it's odd (if it isn't already), and left shifting "l" for the
38 rest. */
39
40 static void
mord_2exp(mpz_ptr ldst,mpz_ptr rdst,mpz_srcptr lsrc,mpz_srcptr rsrc,mp_bitcnt_t n)41 mord_2exp (mpz_ptr ldst, mpz_ptr rdst, mpz_srcptr lsrc, mpz_srcptr rsrc,
42 mp_bitcnt_t n)
43 {
44 mp_size_t rsrc_size = SIZ(rsrc);
45 mp_size_t len = ABS (rsrc_size);
46 mp_ptr rsrc_ptr = PTR(rsrc);
47 mp_ptr p, rdst_ptr;
48 mp_limb_t plow;
49
50 p = rsrc_ptr;
51 plow = *p;
52 while (n >= GMP_NUMB_BITS && plow == 0)
53 {
54 n -= GMP_NUMB_BITS;
55 p++;
56 plow = *p;
57 }
58
59 /* no realloc here if rsrc==rdst, so p and rsrc_ptr remain valid */
60 len -= (p - rsrc_ptr);
61 rdst_ptr = MPZ_REALLOC (rdst, len);
62
63 if ((plow & 1) || n == 0)
64 {
65 /* need INCR when src==dst */
66 if (p != rdst_ptr)
67 MPN_COPY_INCR (rdst_ptr, p, len);
68 }
69 else
70 {
71 unsigned long shift;
72 if (plow == 0)
73 shift = n;
74 else
75 {
76 count_trailing_zeros (shift, plow);
77 shift = MIN (shift, n);
78 }
79 mpn_rshift (rdst_ptr, p, len, shift);
80 len -= (rdst_ptr[len-1] == 0);
81 n -= shift;
82 }
83 SIZ(rdst) = (rsrc_size >= 0) ? len : -len;
84
85 if (n)
86 mpz_mul_2exp (ldst, lsrc, n);
87 else if (ldst != lsrc)
88 mpz_set (ldst, lsrc);
89 }
90
91
92 void
mpq_mul_2exp(mpq_ptr dst,mpq_srcptr src,mp_bitcnt_t n)93 mpq_mul_2exp (mpq_ptr dst, mpq_srcptr src, mp_bitcnt_t n)
94 {
95 mord_2exp (NUM(dst), DEN(dst), NUM(src), DEN(src), n);
96 }
97
98 void
mpq_div_2exp(mpq_ptr dst,mpq_srcptr src,mp_bitcnt_t n)99 mpq_div_2exp (mpq_ptr dst, mpq_srcptr src, mp_bitcnt_t n)
100 {
101 if (SIZ(NUM(src)) == 0)
102 {
103 SIZ(NUM(dst)) = 0;
104 SIZ(DEN(dst)) = 1;
105 MPZ_NEWALLOC (DEN(dst), 1)[0] = 1;
106 return;
107 }
108
109 mord_2exp (DEN(dst), NUM(dst), DEN(src), NUM(src), n);
110 }
111