Back to index

glibc  2.9
slowpow.c
Go to the documentation of this file.
00001 /*
00002  * IBM Accurate Mathematical Library
00003  * written by International Business Machines Corp.
00004  * Copyright (C) 2001, 2006 Free Software Foundation
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU Lesser General Public License as published by
00008  * the Free Software Foundation; either version 2.1 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00019  */
00020 /*************************************************************************/
00021 /* MODULE_NAME:slowpow.c                                                 */
00022 /*                                                                       */
00023 /* FUNCTION:slowpow                                                      */
00024 /*                                                                       */
00025 /*FILES NEEDED:mpa.h                                                     */
00026 /*             mpa.c mpexp.c mplog.c halfulp.c                           */
00027 /*                                                                       */
00028 /* Given two IEEE double machine numbers y,x , routine  computes the     */
00029 /* correctly  rounded (to nearest) value of x^y. Result calculated  by   */
00030 /* multiplication (in halfulp.c) or if result isn't accurate enough      */
00031 /* then routine converts x and y into multi-precision doubles and        */
00032 /* recompute.                                                            */
00033 /*************************************************************************/
00034 
00035 #include "mpa.h"
00036 #include "math_private.h"
00037 
00038 void __mpexp (mp_no * x, mp_no * y, int p);
00039 void __mplog (mp_no * x, mp_no * y, int p);
00040 double ulog (double);
00041 double __halfulp (double x, double y);
00042 
00043 double
00044 __slowpow (double x, double y, double z)
00045 {
00046   double res, res1;
00047   long double ldw, ldz, ldpp;
00048   static const long double ldeps = 0x4.0p-96;
00049 
00050   res = __halfulp (x, y);   /* halfulp() returns -10 or x^y             */
00051   if (res >= 0)
00052     return res;                    /* if result was really computed by halfulp */
00053   /*  else, if result was not really computed by halfulp */
00054 
00055   /* Compute pow as long double, 106 bits */
00056   ldz = __ieee754_logl ((long double) x);
00057   ldw = (long double) y *ldz;
00058   ldpp = __ieee754_expl (ldw);
00059   res = (double) (ldpp + ldeps);
00060   res1 = (double) (ldpp - ldeps);
00061 
00062   if (res != res1)          /* if result still not accurate enough */
00063     {                       /* use mpa for higher persision.  */
00064       mp_no mpx, mpy, mpz, mpw, mpp, mpr, mpr1;
00065       static const mp_no eps = { -3, {1.0, 4.0} };
00066       int p;
00067 
00068       p = 10;               /*  p=precision 240 bits  */
00069       __dbl_mp (x, &mpx, p);
00070       __dbl_mp (y, &mpy, p);
00071       __dbl_mp (z, &mpz, p);
00072       __mplog (&mpx, &mpz, p);            /* log(x) = z   */
00073       __mul (&mpy, &mpz, &mpw, p); /*  y * z =w    */
00074       __mpexp (&mpw, &mpp, p);            /*  e^w =pp     */
00075       __add (&mpp, &eps, &mpr, p); /*  pp+eps =r   */
00076       __mp_dbl (&mpr, &res, p);
00077       __sub (&mpp, &eps, &mpr1, p);       /*  pp -eps =r1 */
00078       __mp_dbl (&mpr1, &res1, p);  /*  converting into double precision */
00079       if (res == res1)
00080        return res;
00081 
00082       /* if we get here result wasn't calculated exactly, continue for
00083          more exact calculation using 768 bits.  */
00084       p = 32;
00085       __dbl_mp (x, &mpx, p);
00086       __dbl_mp (y, &mpy, p);
00087       __dbl_mp (z, &mpz, p);
00088       __mplog (&mpx, &mpz, p);            /* log(c)=z  */
00089       __mul (&mpy, &mpz, &mpw, p); /* y*z =w    */
00090       __mpexp (&mpw, &mpp, p);            /* e^w=pp    */
00091       __mp_dbl (&mpp, &res, p);           /* converting into double precision */
00092     }
00093   return res;
00094 }