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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
/* mpfr_mul -- multiply two floating-point numbers
Copyright (C) 1999-2001 Free Software Foundation.
Contributed by Paul Zimmermann, INRIA Lorraine.
This file is part of the MPFR Library.
The MPFR Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
The MPFR 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 Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with the MPFR Library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA. */
#include <stdio.h>
#include "gmp.h"
#include "gmp-impl.h"
#include "mpfr.h"
#include "mpfr-impl.h"
/* Remains to do:
- do not use all bits of b and c when MPFR_PREC(b)>MPFR_PREC(a) or MPFR_PREC(c)>MPFR_PREC(a)
[current complexity is O(MPFR_PREC(b)*MPFR_PREC(c))]
*/
int
#if __STDC__
mpfr_mul (mpfr_ptr a, mpfr_srcptr b, mpfr_srcptr c, mp_rnd_t rnd_mode)
#else
mpfr_mul (a, b, c, rnd_mode)
mpfr_ptr a;
mpfr_srcptr b;
mpfr_srcptr c;
mp_rnd_t rnd_mode;
#endif
{
unsigned int bn, cn, an, tn, k;
int cc, inexact = 0;
mp_limb_t *ap=MPFR_MANT(a), *bp=MPFR_MANT(b), *cp=MPFR_MANT(c), *tmp, b1;
long int sign_product;
mp_prec_t prec_a=MPFR_PREC(a), prec_b=MPFR_PREC(b), prec_c=MPFR_PREC(c);
TMP_DECL(marker);
/* deal with NaN and zero */
if (MPFR_IS_NAN(b) || MPFR_IS_NAN(c))
{
MPFR_CLEAR_FLAGS(a);
MPFR_SET_NAN(a);
return 1;
}
if (MPFR_IS_INF(b))
{
if (!MPFR_NOTZERO(c))
{
MPFR_CLEAR_FLAGS(a);
MPFR_SET_NAN(a);
return 1;
}
else
{
if (MPFR_SIGN(a) != MPFR_SIGN(b) * MPFR_SIGN(c))
MPFR_CHANGE_SIGN(a);
MPFR_CLEAR_FLAGS(a);
MPFR_SET_INF(a);
return 0;
}
}
else if (MPFR_IS_INF(c))
{
if (!MPFR_NOTZERO(b))
{
MPFR_CLEAR_FLAGS(a);
MPFR_SET_NAN(a);
return 1;
}
else
{
if (MPFR_SIGN(a) != MPFR_SIGN(b) * MPFR_SIGN(c))
MPFR_CHANGE_SIGN(a);
MPFR_CLEAR_FLAGS(a);
MPFR_SET_INF(a);
return 0;
}
}
if (!MPFR_NOTZERO(b) || !MPFR_NOTZERO(c))
{
MPFR_CLEAR_FLAGS(a);
MPFR_SET_ZERO(a);
return 0;
}
sign_product = MPFR_SIGN(b) * MPFR_SIGN(c);
MPFR_CLEAR_FLAGS(a);
an = (prec_a - 1)/BITS_PER_MP_LIMB + 1; /* nb of significant limbs of a */
bn = (prec_b - 1)/BITS_PER_MP_LIMB + 1; /* nb of significant limbs of b */
cn = (prec_c - 1)/BITS_PER_MP_LIMB + 1; /* nb of significant limbs of c */
tn = (prec_c + prec_b - 1)/BITS_PER_MP_LIMB + 1;
k = bn + cn; /* effective nb of limbs used by b*c (=tn or tn+1) */
TMP_MARK(marker);
tmp = (mp_limb_t*) TMP_ALLOC(k * BYTES_PER_MP_LIMB);
/* multiplies two mantissa in temporary allocated space */
b1 = (bn >= cn) ? mpn_mul (tmp, bp, bn, cp, cn)
: mpn_mul (tmp, cp, cn, bp, bn);
/* now tmp[0]..tmp[k-1] contains the product of both mantissa,
with tmp[k-1]>=2^(BITS_PER_MP_LIMB-2) */
b1 >>= BITS_PER_MP_LIMB - 1; /* msb from the product */
tmp += k - tn;
if (b1 == 0)
mpn_lshift (tmp, tmp, tn, 1);
cc = mpfr_round_raw (ap, tmp, prec_b + prec_c, sign_product < 0, prec_a,
rnd_mode, &inexact);
if (cc) /* cc = 1 ==> result is a power of two */
ap[an-1] = (mp_limb_t) 1 << (BITS_PER_MP_LIMB-1);
TMP_FREE(marker);
MPFR_EXP(a) = MPFR_EXP(b) + MPFR_EXP(c) + b1 - 1 + cc;
if (sign_product * MPFR_SIGN(a) < 0)
MPFR_CHANGE_SIGN(a);
return inexact;
}
|