comparison graal/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/examples/MathPow.java @ 19301:a79a3e467245

Truffle-DSL: move examples package into test package for the project canonicalizer.
author Christian Humer <christian.humer@gmail.com>
date Wed, 11 Feb 2015 19:01:35 +0100
parents
children 9e5947d24e63
comparison
equal deleted inserted replaced
19300:67ab244ab689 19301:a79a3e467245
1 /*
2 * Copyright (c) 2015, 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 package com.oracle.truffle.api.dsl.test.examples;
24
25 import static com.oracle.truffle.api.dsl.test.examples.ExampleNode.*;
26 import static org.junit.Assert.*;
27
28 import org.junit.*;
29
30 import com.oracle.truffle.api.*;
31 import com.oracle.truffle.api.dsl.*;
32 import com.oracle.truffle.api.dsl.test.examples.MathPowFactory.MathPowNodeGen;
33 import com.oracle.truffle.api.nodes.*;
34
35 /**
36 * This example shows possible specializations for a simplified math pow node. It demonstrates how
37 * multiple caches can coexist within in the same node. This example does not show the best possible
38 * specializations for math.pow.
39 *
40 * Note: int values are implicitly casted to double values.
41 */
42 @SuppressWarnings("unused")
43 public class MathPow extends Node {
44
45 @Test
46 public void testPow() {
47 MathPowNode node = MathPowNodeGen.create(createArguments(2));
48 CallTarget target = createTarget(node);
49
50 // start with doPowCached
51 assertEquals(1D, target.call(1D, 1));
52 assertEquals(2D, target.call(2D, 1));
53 assertEquals(3D, target.call(3D, 1));
54 assertEquals(3, node.doPowCached);
55 assertEquals(0, node.doPowCachedExponent);
56
57 // transition to doPowCachedExponent
58 assertEquals(4D, target.call(4D, 1));
59 assertEquals(5D, target.call(5D, 1));
60 assertEquals(6D, target.call(6D, 1));
61 assertEquals(16D, target.call(4D, 2));
62 assertEquals(125D, target.call(5D, 3));
63 assertEquals(5, node.doPowCachedExponent);
64 assertEquals(0, node.doPowDoubleInt);
65
66 // transition to doPowDoubleInt
67 assertEquals(4D * 4D * 4D * 4D, target.call(4D, 4));
68 assertEquals(5D * 5D * 5D * 5D * 5D, target.call(5D, 5));
69 assertEquals(5, node.doPowCachedExponent);
70 assertEquals(2, node.doPowDoubleInt);
71
72 // transition to doPow
73 assertEquals(5D, target.call(5D, 1D));
74 assertEquals(2D, target.call(2D, 1D));
75
76 assertEquals(3, node.doPowCached);
77 assertEquals(5, node.doPowCachedExponent);
78 assertEquals(2, node.doPowDoubleInt);
79 assertEquals(2, node.doPow);
80 }
81
82 public static class MathPowNode extends ExampleNode {
83
84 // test flags
85 int doPowCached;
86 int doPowCachedExponent;
87 int doPowDoubleInt;
88 int doPow;
89
90 @Specialization(guards = {"base == cachedBase", "exponent == cachedExponent"})
91 double doPowCached(double base, int exponent, //
92 @Cached("base") double cachedBase, //
93 @Cached("exponent") int cachedExponent, //
94 @Cached("cachePow(cachedBase, cachedExponent)") double cachedResult) {
95 doPowCached++;
96 return cachedResult;
97 }
98
99 /*
100 * We could just use the doPow specialization instead. But this makes the number of doPow
101 * calls more difficult to assert.
102 */
103 protected static double cachePow(double base, int exponent) {
104 return Math.pow(base, exponent);
105 }
106
107 @Specialization(contains = "doPowCached", guards = {"exponent == cachedExponent", "cachedExponent <= 10"})
108 @ExplodeLoop
109 double doPowCachedExponent(double base, int exponent, @Cached("exponent") int cachedExponent) {
110 doPowCachedExponent++;
111 double result = 1.0;
112 for (int i = 0; i < cachedExponent; i++) {
113 result *= base;
114 }
115 return result;
116 }
117
118 @Specialization(contains = "doPowCachedExponent", guards = "exponent >= 0")
119 double doPowDoubleInt(double base, int exponent) {
120 doPowDoubleInt++;
121 // Uses binary decomposition to limit the number of
122 // multiplications; see the discussion in "Hacker's Delight" by Henry
123 // S. Warren, Jr., figure 11-6, page 213.
124 double b = base;
125 int e = exponent;
126 double result = 1;
127 while (e > 0) {
128 if ((e & 1) == 1) {
129 result *= b;
130 }
131 e >>= 1;
132 b *= b;
133 }
134 return result;
135 }
136
137 @Specialization(contains = {"doPowCached", "doPowDoubleInt"})
138 double doPow(double base, double exponent) {
139 doPow++;
140 return Math.pow(base, exponent);
141 }
142 }
143
144 }