001/* 002 * Copyright (c) 2007, 2012, Oracle and/or its affiliates. All rights reserved. 003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 004 * 005 * This code is free software; you can redistribute it and/or modify it 006 * under the terms of the GNU General Public License version 2 only, as 007 * published by the Free Software Foundation. 008 * 009 * This code is distributed in the hope that it will be useful, but WITHOUT 010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 011 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 012 * version 2 for more details (a copy is included in the LICENSE file that 013 * accompanied this code). 014 * 015 * You should have received a copy of the GNU General Public License version 016 * 2 along with this work; if not, write to the Free Software Foundation, 017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 018 * 019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 020 * or visit www.oracle.com if you need additional information or have any 021 * questions. 022 */ 023// Checkstyle: stop 024 025package com.oracle.graal.jtt.hotpath; 026 027import org.junit.*; 028 029import com.oracle.graal.jtt.*; 030 031/* 032 */ 033public class HP_series extends JTTTest { 034 035 public static double test(int count) { 036 final int arrayRows = count; 037 final double[][] testArray = new double[2][arrayRows]; 038 double omega; // Fundamental frequency. 039 testArray[0][0] = TrapezoidIntegrate(0.0, // Lower bound. 040 2.0, // Upper bound. 041 1000, // # of steps. 042 0.0, // No omega*n needed. 043 0) / 2.0; // 0 = term A[0]. 044 omega = 3.1415926535897932; 045 for (int i = 1; i < arrayRows; i++) { 046 testArray[0][i] = TrapezoidIntegrate(0.0, 2.0, 1000, omega * i, 1); // 1 = cosine 047 // term. 048 testArray[1][i] = TrapezoidIntegrate(0.0, 2.0, 1000, omega * i, 2); // 2 = sine 049 // term. 050 } 051 final double ref[][] = {{2.8729524964837996, 0.0}, {1.1161046676147888, -1.8819691893398025}, {0.34429060398168704, -1.1645642623320958}, {0.15238898702519288, -0.8143461113044298}}; 052 double error = 0.0; 053 double sum = 0.0; 054 for (int i = 0; i < 4; i++) { 055 for (int j = 0; j < 2; j++) { 056 error += Math.abs(testArray[j][i] - ref[i][j]); 057 sum += testArray[j][i]; 058 } 059 } 060 return sum + error; 061 } 062 063 private static double TrapezoidIntegrate(double x0, // Lower bound. 064 double x1, // Upper bound. 065 int ns, // # of steps. 066 double omegan, // omega * n. 067 int select) // Term type. 068 { 069 int nsteps = ns; 070 double x; // Independent variable. 071 double dx; // Step size. 072 double rvalue; // Return value. 073 074 x = x0; 075 dx = (x1 - x0) / nsteps; 076 rvalue = thefunction(x0, omegan, select) / 2.0; 077 if (nsteps != 1) { 078 --nsteps; // Already done 1 step. 079 while (--nsteps > 0) { 080 x += dx; 081 rvalue += thefunction(x, omegan, select); 082 } 083 } 084 rvalue = (rvalue + thefunction(x1, omegan, select) / 2.0) * dx; 085 return (rvalue); 086 } 087 088 private static double thefunction(double x, // Independent variable. 089 double omegan, // Omega * term. 090 int select) // Choose type. 091 { 092 switch (select) { 093 case 0: 094 return (Math.pow(x + 1.0, x)); 095 case 1: 096 return (Math.pow(x + 1.0, x) * Math.cos(omegan * x)); 097 case 2: 098 return (Math.pow(x + 1.0, x) * Math.sin(omegan * x)); 099 } 100 return (0.0); 101 } 102 103 /* 104 * This test is sensible to the implementation of Math.pow, cos and sin. Since for these 105 * functions, the specs says "The computed result must be within 1 ulp of the exact result", 106 * different implementation may return different results. The 11 ulp delta allowed for test(100) 107 * tries to account for that but is not guaranteed to work forever. 108 */ 109 @Ignore("failure-prone because of the variabiliy of pow/cos/sin") 110 @Test 111 public void run0() throws Throwable { 112 double expected = 0.6248571921291398d; 113 runTestWithDelta(11 * Math.ulp(expected), "test", 100); 114 } 115 116}