2 * IBM Accurate Mathematical Library
3 * written by International Business Machines Corp.
4 * Copyright (C) 2001, 2006 Free Software Foundation
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 /*************************************************************************/
21 /* MODULE_NAME:slowpow.c */
23 /* FUNCTION:slowpow */
25 /*FILES NEEDED:mpa.h */
26 /* mpa.c mpexp.c mplog.c halfulp.c */
28 /* Given two IEEE double machine numbers y,x , routine computes the */
29 /* correctly rounded (to nearest) value of x^y. Result calculated by */
30 /* multiplication (in halfulp.c) or if result isn't accurate enough */
31 /* then routine converts x and y into multi-precision doubles and */
33 /*************************************************************************/
36 #include "math_private.h"
38 void __mpexp (mp_no
* x
, mp_no
* y
, int p
);
39 void __mplog (mp_no
* x
, mp_no
* y
, int p
);
41 double __halfulp (double x
, double y
);
44 __slowpow (double x
, double y
, double z
)
47 long double ldw
, ldz
, ldpp
;
48 static const long double ldeps
= 0x4.0p
-96;
50 res
= __halfulp (x
, y
); /* halfulp() returns -10 or x^y */
52 return res
; /* if result was really computed by halfulp */
53 /* else, if result was not really computed by halfulp */
55 /* Compute pow as long double, 106 bits */
56 ldz
= __ieee754_logl ((long double) x
);
57 ldw
= (long double) y
*ldz
;
58 ldpp
= __ieee754_expl (ldw
);
59 res
= (double) (ldpp
+ ldeps
);
60 res1
= (double) (ldpp
- ldeps
);
62 if (res
!= res1
) /* if result still not accurate enough */
63 { /* use mpa for higher persision. */
64 mp_no mpx
, mpy
, mpz
, mpw
, mpp
, mpr
, mpr1
;
65 static const mp_no eps
= { -3, {1.0, 4.0} };
68 p
= 10; /* p=precision 240 bits */
69 __dbl_mp (x
, &mpx
, p
);
70 __dbl_mp (y
, &mpy
, p
);
71 __dbl_mp (z
, &mpz
, p
);
72 __mplog (&mpx
, &mpz
, p
); /* log(x) = z */
73 __mul (&mpy
, &mpz
, &mpw
, p
); /* y * z =w */
74 __mpexp (&mpw
, &mpp
, p
); /* e^w =pp */
75 __add (&mpp
, &eps
, &mpr
, p
); /* pp+eps =r */
76 __mp_dbl (&mpr
, &res
, p
);
77 __sub (&mpp
, &eps
, &mpr1
, p
); /* pp -eps =r1 */
78 __mp_dbl (&mpr1
, &res1
, p
); /* converting into double precision */
82 /* if we get here result wasn't calculated exactly, continue for
83 more exact calculation using 768 bits. */
85 __dbl_mp (x
, &mpx
, p
);
86 __dbl_mp (y
, &mpy
, p
);
87 __dbl_mp (z
, &mpz
, p
);
88 __mplog (&mpx
, &mpz
, p
); /* log(c)=z */
89 __mul (&mpy
, &mpz
, &mpw
, p
); /* y*z =w */
90 __mpexp (&mpw
, &mpp
, p
); /* e^w=pp */
91 __mp_dbl (&mpp
, &res
, p
); /* converting into double precision */