Path: blob/master/test/hotspot/jtreg/compiler/c2/PolynomialRoot.java
41149 views
/*1* (C) Vladislav Malyshkin 20102* This file is under GPL version 3.3*4*/56/** Polynomial root.7* @version $Id: PolynomialRoot.java,v 1.105 2012/08/18 00:00:05 mal Exp $8* @author Vladislav Malyshkin [email protected]9*/1011/**12* @test13* @key randomness14* @bug 800595615* @summary C2: assert(!def_outside->member(r)) failed: Use of external LRG overlaps the same LRG defined in this block16* @library /test/lib17* @modules java.base/jdk.internal.misc18* java.management19*20* @run main/timeout=300 compiler.c2.PolynomialRoot21*/2223package compiler.c2;2425import jdk.test.lib.Utils;2627import java.util.Arrays;28import java.util.Random;2930public class PolynomialRoot {313233public static int findPolynomialRoots(final int n,34final double [] p,35final double [] re_root,36final double [] im_root)37{38if(n==4)39{40return root4(p,re_root,im_root);41}42else if(n==3)43{44return root3(p,re_root,im_root);45}46else if(n==2)47{48return root2(p,re_root,im_root);49}50else if(n==1)51{52return root1(p,re_root,im_root);53}54else55{56throw new RuntimeException("n="+n+" is not supported yet");57}58}59606162static final double SQRT3=Math.sqrt(3.0),SQRT2=Math.sqrt(2.0);636465private static final boolean PRINT_DEBUG=false;6667public static int root4(final double [] p,final double [] re_root,final double [] im_root)68{69if (PRINT_DEBUG) { System.err.println("=====================root4:p=" + Arrays.toString(p)); }70final double vs=p[4];71if(PRINT_DEBUG) System.err.println("p[4]="+p[4]);72if(!(Math.abs(vs)>EPS))73{74re_root[0]=re_root[1]=re_root[2]=re_root[3]=75im_root[0]=im_root[1]=im_root[2]=im_root[3]=Double.NaN;76return -1;77}7879/* zsolve_quartic.c - finds the complex roots of80* x^4 + a x^3 + b x^2 + c x + d = 081*/82final double a=p[3]/vs,b=p[2]/vs,c=p[1]/vs,d=p[0]/vs;83if(PRINT_DEBUG) System.err.println("input a="+a+" b="+b+" c="+c+" d="+d);848586final double r4 = 1.0 / 4.0;87final double q2 = 1.0 / 2.0, q4 = 1.0 / 4.0, q8 = 1.0 / 8.0;88final double q1 = 3.0 / 8.0, q3 = 3.0 / 16.0;89final int mt;9091/* Deal easily with the cases where the quartic is degenerate. The92* ordering of solutions is done explicitly. */93if (0 == b && 0 == c)94{95if (0 == d)96{97re_root[0]=-a;98im_root[0]=im_root[1]=im_root[2]=im_root[3]=0;99re_root[1]=re_root[2]=re_root[3]=0;100return 4;101}102else if (0 == a)103{104if (d > 0)105{106final double sq4 = Math.sqrt(Math.sqrt(d));107re_root[0]=sq4*SQRT2/2;108im_root[0]=re_root[0];109re_root[1]=-re_root[0];110im_root[1]=re_root[0];111re_root[2]=-re_root[0];112im_root[2]=-re_root[0];113re_root[3]=re_root[0];114im_root[3]=-re_root[0];115if(PRINT_DEBUG) System.err.println("Path a=0 d>0");116}117else118{119final double sq4 = Math.sqrt(Math.sqrt(-d));120re_root[0]=sq4;121im_root[0]=0;122re_root[1]=0;123im_root[1]=sq4;124re_root[2]=0;125im_root[2]=-sq4;126re_root[3]=-sq4;127im_root[3]=0;128if(PRINT_DEBUG) System.err.println("Path a=0 d<0");129}130return 4;131}132}133134if (0.0 == c && 0.0 == d)135{136root2(new double []{p[2],p[3],p[4]},re_root,im_root);137re_root[2]=im_root[2]=re_root[3]=im_root[3]=0;138return 4;139}140141if(PRINT_DEBUG) System.err.println("G Path c="+c+" d="+d);142final double [] u=new double[3];143144if(PRINT_DEBUG) System.err.println("Generic Path");145/* For non-degenerate solutions, proceed by constructing and146* solving the resolvent cubic */147final double aa = a * a;148final double pp = b - q1 * aa;149final double qq = c - q2 * a * (b - q4 * aa);150final double rr = d - q4 * a * (c - q4 * a * (b - q3 * aa));151final double rc = q2 * pp , rc3 = rc / 3;152final double sc = q4 * (q4 * pp * pp - rr);153final double tc = -(q8 * qq * q8 * qq);154if(PRINT_DEBUG) System.err.println("aa="+aa+" pp="+pp+" qq="+qq+" rr="+rr+" rc="+rc+" sc="+sc+" tc="+tc);155final boolean flag_realroots;156157/* This code solves the resolvent cubic in a convenient fashion158* for this implementation of the quartic. If there are three real159* roots, then they are placed directly into u[]. If two are160* complex, then the real root is put into u[0] and the real161* and imaginary part of the complex roots are placed into162* u[1] and u[2], respectively. */163{164final double qcub = (rc * rc - 3 * sc);165final double rcub = (rc*(2 * rc * rc - 9 * sc) + 27 * tc);166167final double Q = qcub / 9;168final double R = rcub / 54;169170final double Q3 = Q * Q * Q;171final double R2 = R * R;172173final double CR2 = 729 * rcub * rcub;174final double CQ3 = 2916 * qcub * qcub * qcub;175176if(PRINT_DEBUG) System.err.println("CR2="+CR2+" CQ3="+CQ3+" R="+R+" Q="+Q);177178if (0 == R && 0 == Q)179{180flag_realroots=true;181u[0] = -rc3;182u[1] = -rc3;183u[2] = -rc3;184}185else if (CR2 == CQ3)186{187flag_realroots=true;188final double sqrtQ = Math.sqrt (Q);189if (R > 0)190{191u[0] = -2 * sqrtQ - rc3;192u[1] = sqrtQ - rc3;193u[2] = sqrtQ - rc3;194}195else196{197u[0] = -sqrtQ - rc3;198u[1] = -sqrtQ - rc3;199u[2] = 2 * sqrtQ - rc3;200}201}202else if (R2 < Q3)203{204flag_realroots=true;205final double ratio = (R >= 0?1:-1) * Math.sqrt (R2 / Q3);206final double theta = Math.acos (ratio);207final double norm = -2 * Math.sqrt (Q);208209u[0] = norm * Math.cos (theta / 3) - rc3;210u[1] = norm * Math.cos ((theta + 2.0 * Math.PI) / 3) - rc3;211u[2] = norm * Math.cos ((theta - 2.0 * Math.PI) / 3) - rc3;212}213else214{215flag_realroots=false;216final double A = -(R >= 0?1:-1)*Math.pow(Math.abs(R)+Math.sqrt(R2-Q3),1.0/3.0);217final double B = Q / A;218219u[0] = A + B - rc3;220u[1] = -0.5 * (A + B) - rc3;221u[2] = -(SQRT3*0.5) * Math.abs (A - B);222}223if(PRINT_DEBUG) System.err.println("u[0]="+u[0]+" u[1]="+u[1]+" u[2]="+u[2]+" qq="+qq+" disc="+((CR2 - CQ3) / 2125764.0));224}225/* End of solution to resolvent cubic */226227/* Combine the square roots of the roots of the cubic228* resolvent appropriately. Also, calculate 'mt' which229* designates the nature of the roots:230* mt=1 : 4 real roots231* mt=2 : 0 real roots232* mt=3 : 2 real roots233*/234235236final double w1_re,w1_im,w2_re,w2_im,w3_re,w3_im,mod_w1w2,mod_w1w2_squared;237if (flag_realroots)238{239mod_w1w2=-1;240mt = 2;241int jmin=0;242double vmin=Math.abs(u[jmin]);243for(int j=1;j<3;j++)244{245final double vx=Math.abs(u[j]);246if(vx<vmin)247{248vmin=vx;249jmin=j;250}251}252final double u1=u[(jmin+1)%3],u2=u[(jmin+2)%3];253mod_w1w2_squared=Math.abs(u1*u2);254if(u1>=0)255{256w1_re=Math.sqrt(u1);257w1_im=0;258}259else260{261w1_re=0;262w1_im=Math.sqrt(-u1);263}264if(u2>=0)265{266w2_re=Math.sqrt(u2);267w2_im=0;268}269else270{271w2_re=0;272w2_im=Math.sqrt(-u2);273}274if(PRINT_DEBUG) System.err.println("u1="+u1+" u2="+u2+" jmin="+jmin);275}276else277{278mt = 3;279final double w_mod2_sq=u[1]*u[1]+u[2]*u[2],w_mod2=Math.sqrt(w_mod2_sq),w_mod=Math.sqrt(w_mod2);280if(w_mod2_sq<=0)281{282w1_re=w1_im=0;283}284else285{286// calculate square root of a complex number (u[1],u[2])287// the result is in the (w1_re,w1_im)288final double absu1=Math.abs(u[1]),absu2=Math.abs(u[2]),w;289if(absu1>=absu2)290{291final double t=absu2/absu1;292w=Math.sqrt(absu1*0.5 * (1.0 + Math.sqrt(1.0 + t * t)));293if(PRINT_DEBUG) System.err.println(" Path1 ");294}295else296{297final double t=absu1/absu2;298w=Math.sqrt(absu2*0.5 * (t + Math.sqrt(1.0 + t * t)));299if(PRINT_DEBUG) System.err.println(" Path1a ");300}301if(u[1]>=0)302{303w1_re=w;304w1_im=u[2]/(2*w);305if(PRINT_DEBUG) System.err.println(" Path2 ");306}307else308{309final double vi = (u[2] >= 0) ? w : -w;310w1_re=u[2]/(2*vi);311w1_im=vi;312if(PRINT_DEBUG) System.err.println(" Path2a ");313}314}315final double absu0=Math.abs(u[0]);316if(w_mod2>=absu0)317{318mod_w1w2=w_mod2;319mod_w1w2_squared=w_mod2_sq;320w2_re=w1_re;321w2_im=-w1_im;322}323else324{325mod_w1w2=-1;326mod_w1w2_squared=w_mod2*absu0;327if(u[0]>=0)328{329w2_re=Math.sqrt(absu0);330w2_im=0;331}332else333{334w2_re=0;335w2_im=Math.sqrt(absu0);336}337}338if(PRINT_DEBUG) System.err.println("u[0]="+u[0]+"u[1]="+u[1]+" u[2]="+u[2]+" absu0="+absu0+" w_mod="+w_mod+" w_mod2="+w_mod2);339}340341/* Solve the quadratic in order to obtain the roots342* to the quartic */343if(mod_w1w2>0)344{345// a shorcut to reduce rounding error346w3_re=qq/(-8)/mod_w1w2;347w3_im=0;348}349else if(mod_w1w2_squared>0)350{351// regular path352final double mqq8n=qq/(-8)/mod_w1w2_squared;353w3_re=mqq8n*(w1_re*w2_re-w1_im*w2_im);354w3_im=-mqq8n*(w1_re*w2_im+w2_re*w1_im);355}356else357{358// typically occur when qq==0359w3_re=w3_im=0;360}361362final double h = r4 * a;363if(PRINT_DEBUG) System.err.println("w1_re="+w1_re+" w1_im="+w1_im+" w2_re="+w2_re+" w2_im="+w2_im+" w3_re="+w3_re+" w3_im="+w3_im+" h="+h);364365re_root[0]=w1_re+w2_re+w3_re-h;366im_root[0]=w1_im+w2_im+w3_im;367re_root[1]=-(w1_re+w2_re)+w3_re-h;368im_root[1]=-(w1_im+w2_im)+w3_im;369re_root[2]=w2_re-w1_re-w3_re-h;370im_root[2]=w2_im-w1_im-w3_im;371re_root[3]=w1_re-w2_re-w3_re-h;372im_root[3]=w1_im-w2_im-w3_im;373374return 4;375}376377378379static void setRandomP(final double [] p, final int n, Random r)380{381if(r.nextDouble()<0.1)382{383// integer coefficiens384for(int j=0;j<p.length;j++)385{386if(j<=n)387{388p[j]=(r.nextInt(2)<=0?-1:1)*r.nextInt(10);389}390else391{392p[j]=0;393}394}395}396else397{398// real coefficiens399for(int j=0;j<p.length;j++)400{401if(j<=n)402{403p[j]=-1+2*r.nextDouble();404}405else406{407p[j]=0;408}409}410}411if(Math.abs(p[n])<1e-2)412{413p[n]=(r.nextInt(2)<=0?-1:1)*(0.1+r.nextDouble());414}415}416417418static void checkValues(final double [] p,419final int n,420final double rex,421final double imx,422final double eps,423final String txt)424{425double res=0,ims=0,sabs=0;426final double xabs=Math.abs(rex)+Math.abs(imx);427for(int k=n;k>=0;k--)428{429final double res1=(res*rex-ims*imx)+p[k];430final double ims1=(ims*rex+res*imx);431res=res1;432ims=ims1;433sabs+=xabs*sabs+p[k];434}435sabs=Math.abs(sabs);436if(false && sabs>1/eps?437(!(Math.abs(res/sabs)<=eps)||!(Math.abs(ims/sabs)<=eps))438:439(!(Math.abs(res)<=eps)||!(Math.abs(ims)<=eps)))440{441throw new RuntimeException(442getPolinomTXT(p)+"\n"+443"\t x.r="+rex+" x.i="+imx+"\n"+444"res/sabs="+(res/sabs)+" ims/sabs="+(ims/sabs)+445" sabs="+sabs+446"\nres="+res+" ims="+ims+" n="+n+" eps="+eps+" "+447" sabs>1/eps="+(sabs>1/eps)+448" f1="+(!(Math.abs(res/sabs)<=eps)||!(Math.abs(ims/sabs)<=eps))+449" f2="+(!(Math.abs(res)<=eps)||!(Math.abs(ims)<=eps))+450" "+txt);451}452}453454static String getPolinomTXT(final double [] p)455{456final StringBuilder buf=new StringBuilder();457buf.append("order="+(p.length-1)+"\t");458for(int k=0;k<p.length;k++)459{460buf.append("p["+k+"]="+p[k]+";");461}462return buf.toString();463}464465static String getRootsTXT(int nr,final double [] re,final double [] im)466{467final StringBuilder buf=new StringBuilder();468for(int k=0;k<nr;k++)469{470buf.append("x."+k+"("+re[k]+","+im[k]+")\n");471}472return buf.toString();473}474475static void testRoots(final int n,476final int n_tests,477final Random rn,478final double eps)479{480final double [] p=new double [n+1];481final double [] rex=new double [n],imx=new double [n];482for(int i=0;i<n_tests;i++)483{484for(int dg=n;dg-->-1;)485{486for(int dr=3;dr-->0;)487{488setRandomP(p,n,rn);489for(int j=0;j<=dg;j++)490{491p[j]=0;492}493if(dr==0)494{495p[0]=-1+2.0*rn.nextDouble();496}497else if(dr==1)498{499p[0]=p[1]=0;500}501502findPolynomialRoots(n,p,rex,imx);503504for(int j=0;j<n;j++)505{506//System.err.println("j="+j);507checkValues(p,n,rex[j],imx[j],eps," t="+i);508}509}510}511}512System.err.println("testRoots(): n_tests="+n_tests+" OK, dim="+n);513}514515516517518static final double EPS=0;519520public static int root1(final double [] p,final double [] re_root,final double [] im_root)521{522if(!(Math.abs(p[1])>EPS))523{524re_root[0]=im_root[0]=Double.NaN;525return -1;526}527re_root[0]=-p[0]/p[1];528im_root[0]=0;529return 1;530}531532public static int root2(final double [] p,final double [] re_root,final double [] im_root)533{534if(!(Math.abs(p[2])>EPS))535{536re_root[0]=re_root[1]=im_root[0]=im_root[1]=Double.NaN;537return -1;538}539final double b2=0.5*(p[1]/p[2]),c=p[0]/p[2],d=b2*b2-c;540if(d>=0)541{542final double sq=Math.sqrt(d);543if(b2<0)544{545re_root[1]=-b2+sq;546re_root[0]=c/re_root[1];547}548else if(b2>0)549{550re_root[0]=-b2-sq;551re_root[1]=c/re_root[0];552}553else554{555re_root[0]=-b2-sq;556re_root[1]=-b2+sq;557}558im_root[0]=im_root[1]=0;559}560else561{562final double sq=Math.sqrt(-d);563re_root[0]=re_root[1]=-b2;564im_root[0]=sq;565im_root[1]=-sq;566}567return 2;568}569570public static int root3(final double [] p,final double [] re_root,final double [] im_root)571{572final double vs=p[3];573if(!(Math.abs(vs)>EPS))574{575re_root[0]=re_root[1]=re_root[2]=576im_root[0]=im_root[1]=im_root[2]=Double.NaN;577return -1;578}579final double a=p[2]/vs,b=p[1]/vs,c=p[0]/vs;580/* zsolve_cubic.c - finds the complex roots of x^3 + a x^2 + b x + c = 0581*/582final double q = (a * a - 3 * b);583final double r = (a*(2 * a * a - 9 * b) + 27 * c);584585final double Q = q / 9;586final double R = r / 54;587588final double Q3 = Q * Q * Q;589final double R2 = R * R;590591final double CR2 = 729 * r * r;592final double CQ3 = 2916 * q * q * q;593final double a3=a/3;594595if (R == 0 && Q == 0)596{597re_root[0]=re_root[1]=re_root[2]=-a3;598im_root[0]=im_root[1]=im_root[2]=0;599return 3;600}601else if (CR2 == CQ3)602{603/* this test is actually R2 == Q3, written in a form suitable604for exact computation with integers */605606/* Due to finite precision some double roots may be missed, and607will be considered to be a pair of complex roots z = x +/-608epsilon i close to the real axis. */609610final double sqrtQ = Math.sqrt (Q);611612if (R > 0)613{614re_root[0] = -2 * sqrtQ - a3;615re_root[1]=re_root[2]=sqrtQ - a3;616im_root[0]=im_root[1]=im_root[2]=0;617}618else619{620re_root[0]=re_root[1] = -sqrtQ - a3;621re_root[2]=2 * sqrtQ - a3;622im_root[0]=im_root[1]=im_root[2]=0;623}624return 3;625}626else if (R2 < Q3)627{628final double sgnR = (R >= 0 ? 1 : -1);629final double ratio = sgnR * Math.sqrt (R2 / Q3);630final double theta = Math.acos (ratio);631final double norm = -2 * Math.sqrt (Q);632final double r0 = norm * Math.cos (theta/3) - a3;633final double r1 = norm * Math.cos ((theta + 2.0 * Math.PI) / 3) - a3;634final double r2 = norm * Math.cos ((theta - 2.0 * Math.PI) / 3) - a3;635636re_root[0]=r0;637re_root[1]=r1;638re_root[2]=r2;639im_root[0]=im_root[1]=im_root[2]=0;640return 3;641}642else643{644final double sgnR = (R >= 0 ? 1 : -1);645final double A = -sgnR * Math.pow (Math.abs (R) + Math.sqrt (R2 - Q3), 1.0 / 3.0);646final double B = Q / A;647648re_root[0]=A + B - a3;649im_root[0]=0;650re_root[1]=-0.5 * (A + B) - a3;651im_root[1]=-(SQRT3*0.5) * Math.abs(A - B);652re_root[2]=re_root[1];653im_root[2]=-im_root[1];654return 3;655}656657}658659660static void root3a(final double [] p,final double [] re_root,final double [] im_root)661{662if(Math.abs(p[3])>EPS)663{664final double v=p[3],665a=p[2]/v,b=p[1]/v,c=p[0]/v,666a3=a/3,a3a=a3*a,667pd3=(b-a3a)/3,668qd2=a3*(a3a/3-0.5*b)+0.5*c,669Q=pd3*pd3*pd3+qd2*qd2;670if(Q<0)671{672// three real roots673final double SQ=Math.sqrt(-Q);674final double th=Math.atan2(SQ,-qd2);675im_root[0]=im_root[1]=im_root[2]=0;676final double f=2*Math.sqrt(-pd3);677re_root[0]=f*Math.cos(th/3)-a3;678re_root[1]=f*Math.cos((th+2*Math.PI)/3)-a3;679re_root[2]=f*Math.cos((th+4*Math.PI)/3)-a3;680//System.err.println("3r");681}682else683{684// one real & two complex roots685final double SQ=Math.sqrt(Q);686final double r1=-qd2+SQ,r2=-qd2-SQ;687final double v1=Math.signum(r1)*Math.pow(Math.abs(r1),1.0/3),688v2=Math.signum(r2)*Math.pow(Math.abs(r2),1.0/3),689sv=v1+v2;690// real root691re_root[0]=sv-a3;692im_root[0]=0;693// complex roots694re_root[1]=re_root[2]=-0.5*sv-a3;695im_root[1]=(v1-v2)*(SQRT3*0.5);696im_root[2]=-im_root[1];697//System.err.println("1r2c");698}699}700else701{702re_root[0]=re_root[1]=re_root[2]=im_root[0]=im_root[1]=im_root[2]=Double.NaN;703}704}705706707static void printSpecialValues()708{709for(int st=0;st<6;st++)710{711//final double [] p=new double []{8,1,3,3.6,1};712final double [] re_root=new double [4],im_root=new double [4];713final double [] p;714final int n;715if(st<=3)716{717if(st<=0)718{719p=new double []{2,-4,6,-4,1};720//p=new double []{-6,6,-6,8,-2};721}722else if(st==1)723{724p=new double []{0,-4,8,3,-9};725}726else if(st==2)727{728p=new double []{-1,0,2,0,-1};729}730else731{732p=new double []{-5,2,8,-2,-3};733}734root4(p,re_root,im_root);735n=4;736}737else738{739p=new double []{0,2,0,1};740if(st==4)741{742p[1]=-p[1];743}744root3(p,re_root,im_root);745n=3;746}747System.err.println("======== n="+n);748for(int i=0;i<=n;i++)749{750if(i<n)751{752System.err.println(String.valueOf(i)+"\t"+753p[i]+"\t"+754re_root[i]+"\t"+755im_root[i]);756}757else758{759System.err.println(String.valueOf(i)+"\t"+p[i]+"\t");760}761}762}763}764765766767public static void main(final String [] args)768{769if (System.getProperty("os.arch").equals("x86") ||770System.getProperty("os.arch").equals("amd64") ||771System.getProperty("os.arch").equals("x86_64")){772final long t0=System.currentTimeMillis();773final double eps=1e-6;774//checkRoots();775final Random r = Utils.getRandomInstance();776printSpecialValues();777778final int n_tests=100000;779//testRoots(2,n_tests,r,eps);780//testRoots(3,n_tests,r,eps);781testRoots(4,n_tests,r,eps);782final long t1=System.currentTimeMillis();783System.err.println("PolynomialRoot.main: "+n_tests+" tests OK done in "+(t1-t0)+" milliseconds. ver=$Id: PolynomialRoot.java,v 1.105 2012/08/18 00:00:05 mal Exp $");784System.out.println("PASSED");785} else {786System.out.println("PASS test for non-x86");787}788}789790791792}793794795