/* * IBM Accurate Mathematical Library * written by International Business Machines Corp. * Copyright (C) 2001-2012 Free Software Foundation, Inc. * * This program 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. * * This program 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 this program; if not, see . */ /****************************************************************************/ /* MODULE_NAME:mpsqrt.c */ /* */ /* FUNCTION:mpsqrt */ /* fastiroot */ /* */ /* FILES NEEDED:endian.h mpa.h mpsqrt.h */ /* mpa.c */ /* Multi-Precision square root function subroutine for precision p >= 4. */ /* The relative error is bounded by 3.501*r**(1-p), where r=2**24. */ /* */ /****************************************************************************/ #include "endian.h" #include "mpa.h" #ifndef SECTION # define SECTION #endif #include "mpsqrt.h" /****************************************************************************/ /* Multi-Precision square root function subroutine for precision p >= 4. */ /* The relative error is bounded by 3.501*r**(1-p), where r=2**24. */ /* Routine receives two pointers to Multi Precision numbers: */ /* x (left argument) and y (next argument). Routine also receives precision */ /* p as integer. Routine computes sqrt(*x) and stores result in *y */ /****************************************************************************/ static double fastiroot(double); void SECTION __mpsqrt(mp_no *x, mp_no *y, int p) { int i,m,ey; double dx,dy; static const mp_no mphalf = {0,{1.0,8388608.0 /* 2^23 */}}, mp3halfs = {1,{1.0,1.0,8388608.0 /* 2^23 */}}; mp_no mpxn,mpz,mpu,mpt1,mpt2; ey=EX/2; __cpy(x,&mpxn,p); mpxn.e -= (ey+ey); __mp_dbl(&mpxn,&dx,p); dy=fastiroot(dx); __dbl_mp(dy,&mpu,p); __mul(&mpxn,&mphalf,&mpz,p); m=__mpsqrt_mp[p]; for (i=0; i>1; z = ((c3*z + c2)*z + c1)*z + c0; /* 2**-7 */ z = z*(1.5 - 0.5*y*z*z); /* 2**-14 */ p.d = z*(1.5 - 0.5*y*z*z); /* 2**-28 */ p.i[HIGH_HALF] -= n; t = x*p.d; return p.d*(1.5 - 0.5*p.d*t); }