1 /*
2  * Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * @test
26  *
27  * @summary converted from VM Testbase jit/FloatingPoint/gen_math/Filtering.
28  * VM Testbase keywords: [jit, quick]
29  *
30  * @library /vmTestbase
31  *          /test/lib
32  * @run driver jdk.test.lib.FileInstaller . .
33  * @build jit.FloatingPoint.gen_math.Filtering.Filtering
34  * @run driver ExecDriver --java jit.FloatingPoint.gen_math.Filtering.Filtering
35  */
36 
37 package jit.FloatingPoint.gen_math.Filtering;
38 
39 import nsk.share.TestFailure;
40 
41 class Filtering
42 {
43    static int N = 1000;
44    static double xx[];
45    static double yy[];
46 
main(String args[])47    public static void main (String args[])
48    {
49 
50         double Error = 0.0001;
51 
52         xx = new double[N];
53         yy = new double[N];
54 
55 
56         double r1, r2, r3, r4;
57 
58         double rn = N;
59         double dx = 1/rn;
60         double A = 0.5;
61         double B = 0.75;
62 
63         for(int i = 0; i < N; i++)
64         {       r1 = i * dx;
65                 xx[i] = A * Math.sin(3 * r1) + B * Math.cos(3 * r1);
66         }
67 
68         Filtering ff;
69         ff = new Filtering();
70 
71         double normaX = ff.Norma1(N,xx);
72 
73         ff.Filter1(N);
74         double norma1 = ff.Norma1(N,yy);
75 
76         ff.Filter2(N);
77         double norma2 = ff.Norma1(N,yy);
78 
79         ff.Filter3(N);
80         double norma3 = ff.Norma1(N,yy);
81 
82         r4 = (norma1 * norma1 + norma2 * norma2 + norma3 * norma3) / 3 ;
83         r4 = Math.sqrt(r4);
84         double errrr = Math.abs(r4 - normaX);
85 
86         ff.Echeck(errrr,Error);
87 
88   }
89 
Norma1(int nn, double uu[])90    public double Norma1(int nn, double uu[])
91    {
92         double nor = 0;
93         double r1 = nn;
94         double r2;
95         for(int i = 0; i < nn; i++)
96         {       r2 = uu[i] * uu[i];
97                 nor = nor + r2;
98         }
99         nor = nor / r1;
100         return nor;
101    }
102 
103 
Filter1(int nn)104    public void Filter1 (int nn)
105    {    yy[0] = xx[0];
106         yy[nn - 1] = xx[nn - 1];
107         for(int i = 1; i < nn - 1; i++)
108         {
109                 yy[i] = 0.5 * (xx[i - 1] + xx[i + 1]);
110         }
111    }
112 
113 
Filter2(int nn)114    public void Filter2 (int nn)
115    {    yy[0] = xx[0];
116         yy[nn - 1] = xx[nn - 1];
117         for(int i = 1; i < nn - 1; i++)
118         {
119                 yy[i] = 0.25 * (xx[i - 1] + 2 * xx[i] + xx[i + 1]);
120         }
121 
122    }
123 
Filter3(int nn)124    public void Filter3 (int nn)
125    {    yy[0] = xx[0];
126         yy[nn - 1] = xx[nn - 1];
127         yy[1] = 0.5 * (xx[0] + xx[2]);
128         yy[nn - 2] = 0.5 * (xx[nn - 1] + xx[nn - 3]);
129         for(int i = 2; i < nn - 2; i++)
130         {
131                 yy[i] = 0.1 * (xx[i - 2] + 2 * xx[i - 1] + 4 * xx[i] +
132                         2 * xx[i + 1] + xx[i + 2]);
133         }
134 
135    }
136 
137 
138 
139 
140 
Echeck(double er, double ER)141    public void Echeck(double er, double ER)
142    {
143 
144         if( er < ER)
145                 System.out.println("test PASS");
146         else
147         {
148                 System.out.println("expected error: " + ER);
149                 System.out.println("   found error: " + er);
150                 throw new TestFailure("test FAIL");
151 
152         }
153 
154    }
155 
156 
157 
158 
159 
160 
161 }
162