comparison truffle/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/examples/MathPow.java @ 21951:9c8c0937da41

Moving all sources into truffle subdirectory
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Wed, 17 Jun 2015 10:58:08 +0200
parents graal/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/examples/MathPow.java@9e5947d24e63
children dc83cc1f94f2
comparison
equal deleted inserted replaced
21950:2a5011c7e641 21951:9c8c0937da41
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 double doPowCachedExponent(double base, int exponent, @Cached("exponent") int cachedExponent) {
109 doPowCachedExponent++;
110 double result = 1.0;
111 for (int i = 0; i < cachedExponent; i++) {
112 result *= base;
113 }
114 return result;
115 }
116
117 @Specialization(contains = "doPowCachedExponent", guards = "exponent >= 0")
118 double doPowDoubleInt(double base, int exponent) {
119 doPowDoubleInt++;
120 // Uses binary decomposition to limit the number of
121 // multiplications; see the discussion in "Hacker's Delight" by Henry
122 // S. Warren, Jr., figure 11-6, page 213.
123 double b = base;
124 int e = exponent;
125 double result = 1;
126 while (e > 0) {
127 if ((e & 1) == 1) {
128 result *= b;
129 }
130 e >>= 1;
131 b *= b;
132 }
133 return result;
134 }
135
136 @Specialization(contains = {"doPowCached", "doPowDoubleInt"})
137 double doPow(double base, double exponent) {
138 doPow++;
139 return Math.pow(base, exponent);
140 }
141 }
142
143 }