1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
/* mpq_mul_2exp, mpq_div_2exp - multiply or divide by 2^N */
/*
Copyright 2000, 2002 Free Software Foundation, Inc.
This file is part of the GNU MP Library.
The GNU MP Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or (at your
option) any later version.
The GNU MP Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the GNU MP Library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
*/
#include "gmp.h"
#include "gmp-impl.h"
#include "longlong.h"
/* The multiplier/divisor "n", representing 2^n, is applied by right shifting
"r" until it's odd (if it isn't already), and left shifting "l" for the
rest. */
static void
mord_2exp (mpz_ptr ldst, mpz_ptr rdst, mpz_srcptr lsrc, mpz_srcptr rsrc,
unsigned long n)
{
mp_size_t rsrc_size = SIZ(rsrc);
mp_size_t len = ABS (rsrc_size);
mp_ptr rsrc_ptr = PTR(rsrc);
mp_ptr p, rdst_ptr;
mp_limb_t plow;
p = rsrc_ptr;
plow = *p;
while (n >= GMP_NUMB_BITS && plow == 0)
{
n -= GMP_NUMB_BITS;
p++;
plow = *p;
}
/* no realloc here if rsrc==rdst, so p and rsrc_ptr remain valid */
len -= (p - rsrc_ptr);
MPZ_REALLOC (rdst, len);
rdst_ptr = PTR(rdst);
if ((plow & 1) || n == 0)
{
/* need DECR when src==dst */
if (p != rdst_ptr)
MPN_COPY_DECR (rdst_ptr, p, len);
}
else
{
unsigned long shift;
if (plow == 0)
shift = n;
else
{
count_trailing_zeros (shift, plow);
shift = MIN (shift, n);
}
mpn_rshift (rdst_ptr, p, len, shift);
len -= (rdst_ptr[len-1] == 0);
n -= shift;
}
SIZ(rdst) = (rsrc_size >= 0) ? len : -len;
if (n)
mpz_mul_2exp (ldst, lsrc, n);
else if (ldst != lsrc)
mpz_set (ldst, lsrc);
}
void
mpq_mul_2exp (mpq_ptr dst, mpq_srcptr src, unsigned long n)
{
mord_2exp (mpq_numref (dst), mpq_denref (dst),
mpq_numref (src), mpq_denref (src), n);
}
void
mpq_div_2exp (mpq_ptr dst, mpq_srcptr src, unsigned long n)
{
if (SIZ (mpq_numref(src)) == 0)
{
dst->_mp_num._mp_size = 0;
dst->_mp_den._mp_size = 1;
dst->_mp_den._mp_d[0] = 1;
return;
}
mord_2exp (mpq_denref (dst), mpq_numref (dst),
mpq_denref (src), mpq_numref (src), n);
}
|