annotate truffle/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/examples/MathPow.java @ 22548:6b76a24fffbd default tip

Use all variables: a, b, u,v and x, y
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Thu, 14 Jan 2016 14:20:57 +0100
parents 5309cc9668e3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19301
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1 /*
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
4 *
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
7 * published by the Free Software Foundation.
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
8 *
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
13 * accompanied this code).
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
14 *
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
18 *
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
21 * questions.
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
22 */
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
23 package com.oracle.truffle.api.dsl.test.examples;
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
24
22272
5309cc9668e3 Cleanup DSL tests from instrumentation dependencies.
Christian Humer <christian.humer@oracle.com>
parents: 22241
diff changeset
25 import static com.oracle.truffle.api.dsl.test.examples.ExampleNode.createArguments;
5309cc9668e3 Cleanup DSL tests from instrumentation dependencies.
Christian Humer <christian.humer@oracle.com>
parents: 22241
diff changeset
26 import static com.oracle.truffle.api.dsl.test.examples.ExampleNode.createTarget;
5309cc9668e3 Cleanup DSL tests from instrumentation dependencies.
Christian Humer <christian.humer@oracle.com>
parents: 22241
diff changeset
27 import static org.junit.Assert.assertEquals;
5309cc9668e3 Cleanup DSL tests from instrumentation dependencies.
Christian Humer <christian.humer@oracle.com>
parents: 22241
diff changeset
28
5309cc9668e3 Cleanup DSL tests from instrumentation dependencies.
Christian Humer <christian.humer@oracle.com>
parents: 22241
diff changeset
29 import org.junit.Test;
5309cc9668e3 Cleanup DSL tests from instrumentation dependencies.
Christian Humer <christian.humer@oracle.com>
parents: 22241
diff changeset
30
22157
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
31 import com.oracle.truffle.api.CallTarget;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
32 import com.oracle.truffle.api.dsl.Cached;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
33 import com.oracle.truffle.api.dsl.Specialization;
19301
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
34 import com.oracle.truffle.api.dsl.test.examples.MathPowFactory.MathPowNodeGen;
22157
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
35 import com.oracle.truffle.api.nodes.Node;
22241
14e6dfb1ef05 Truffle/Testing: massive rework of tests to accommodate recent instrumentation change with respect to "applyInstrumentation"
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22157
diff changeset
36
19301
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
37 /**
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
38 * This example shows possible specializations for a simplified math pow node. It demonstrates how
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
39 * multiple caches can coexist within in the same node. This example does not show the best possible
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
40 * specializations for math.pow.
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
41 *
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
42 * Note: int values are implicitly casted to double values.
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
43 */
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
44 @SuppressWarnings("unused")
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
45 public class MathPow extends Node {
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
46
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
47 @Test
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
48 public void testPow() {
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
49 MathPowNode node = MathPowNodeGen.create(createArguments(2));
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
50 CallTarget target = createTarget(node);
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
51
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
52 // start with doPowCached
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
53 assertEquals(1D, target.call(1D, 1));
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
54 assertEquals(2D, target.call(2D, 1));
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
55 assertEquals(3D, target.call(3D, 1));
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
56 assertEquals(3, node.doPowCached);
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
57 assertEquals(0, node.doPowCachedExponent);
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
58
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
59 // transition to doPowCachedExponent
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
60 assertEquals(4D, target.call(4D, 1));
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
61 assertEquals(5D, target.call(5D, 1));
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
62 assertEquals(6D, target.call(6D, 1));
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
63 assertEquals(16D, target.call(4D, 2));
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
64 assertEquals(125D, target.call(5D, 3));
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
65 assertEquals(5, node.doPowCachedExponent);
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
66 assertEquals(0, node.doPowDoubleInt);
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
67
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
68 // transition to doPowDoubleInt
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
69 assertEquals(4D * 4D * 4D * 4D, target.call(4D, 4));
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
70 assertEquals(5D * 5D * 5D * 5D * 5D, target.call(5D, 5));
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
71 assertEquals(5, node.doPowCachedExponent);
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
72 assertEquals(2, node.doPowDoubleInt);
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
73
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
74 // transition to doPow
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
75 assertEquals(5D, target.call(5D, 1D));
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
76 assertEquals(2D, target.call(2D, 1D));
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
77
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
78 assertEquals(3, node.doPowCached);
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
79 assertEquals(5, node.doPowCachedExponent);
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
80 assertEquals(2, node.doPowDoubleInt);
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
81 assertEquals(2, node.doPow);
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
82 }
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
83
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
84 public static class MathPowNode extends ExampleNode {
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
85
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
86 // test flags
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
87 int doPowCached;
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
88 int doPowCachedExponent;
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
89 int doPowDoubleInt;
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
90 int doPow;
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
91
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
92 @Specialization(guards = {"base == cachedBase", "exponent == cachedExponent"})
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
93 double doPowCached(double base, int exponent, //
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
94 @Cached("base") double cachedBase, //
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
95 @Cached("exponent") int cachedExponent, //
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
96 @Cached("cachePow(cachedBase, cachedExponent)") double cachedResult) {
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
97 doPowCached++;
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
98 return cachedResult;
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
99 }
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
100
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
101 /*
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
102 * We could just use the doPow specialization instead. But this makes the number of doPow
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
103 * calls more difficult to assert.
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
104 */
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
105 protected static double cachePow(double base, int exponent) {
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
106 return Math.pow(base, exponent);
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
107 }
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
108
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
109 @Specialization(contains = "doPowCached", guards = {"exponent == cachedExponent", "cachedExponent <= 10"})
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
110 double doPowCachedExponent(double base, int exponent, @Cached("exponent") int cachedExponent) {
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
111 doPowCachedExponent++;
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
112 double result = 1.0;
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
113 for (int i = 0; i < cachedExponent; i++) {
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
114 result *= base;
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
115 }
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
116 return result;
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
117 }
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
118
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
119 @Specialization(contains = "doPowCachedExponent", guards = "exponent >= 0")
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
120 double doPowDoubleInt(double base, int exponent) {
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
121 doPowDoubleInt++;
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
122 // Uses binary decomposition to limit the number of
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
123 // multiplications; see the discussion in "Hacker's Delight" by Henry
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
124 // S. Warren, Jr., figure 11-6, page 213.
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
125 double b = base;
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
126 int e = exponent;
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
127 double result = 1;
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
128 while (e > 0) {
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
129 if ((e & 1) == 1) {
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
130 result *= b;
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
131 }
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
132 e >>= 1;
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
133 b *= b;
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
134 }
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
135 return result;
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
136 }
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
137
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
138 @Specialization(contains = {"doPowCached", "doPowDoubleInt"})
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
139 double doPow(double base, double exponent) {
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
140 doPow++;
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
141 return Math.pow(base, exponent);
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
142 }
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
143 }
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
144
a79a3e467245 Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
145 }