-
Notifications
You must be signed in to change notification settings - Fork 199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for pow (^) #16
Open
tmysik
wants to merge
1
commit into
graalvm:master
Choose a base branch
from
tmysik:support-pow
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
src/main/java/com/oracle/truffle/sl/nodes/expression/SLPowNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* To change this license header, choose License Headers in Project Properties. | ||
* To change this template file, choose Tools | Templates | ||
* and open the template in the editor. | ||
*/ | ||
package com.oracle.truffle.sl.nodes.expression; | ||
|
||
import com.oracle.truffle.api.CompilerDirectives; | ||
import com.oracle.truffle.api.dsl.Specialization; | ||
import com.oracle.truffle.api.nodes.NodeInfo; | ||
import com.oracle.truffle.api.profiles.BranchProfile; | ||
import com.oracle.truffle.sl.nodes.SLBinaryNode; | ||
import java.math.BigDecimal; | ||
import java.math.BigInteger; | ||
|
||
/** | ||
* Performs the "^" operation - returns the value of the first argument | ||
* raised to the power of the second argument. | ||
*/ | ||
@NodeInfo(shortName = "^") | ||
public abstract class SLPowNode extends SLBinaryNode { | ||
|
||
private final BranchProfile bigNumbers = BranchProfile.create(); | ||
|
||
|
||
@Specialization(rewriteOn = ArithmeticException.class) | ||
protected long pow(long left, long right) { | ||
double pow = Math.pow(left, right); | ||
if (pow > Long.MAX_VALUE) { | ||
bigNumbers.enter(); | ||
throw new ArithmeticException(); | ||
} | ||
return (long) pow; | ||
} | ||
|
||
@Specialization | ||
@CompilerDirectives.TruffleBoundary | ||
protected BigInteger pow(BigInteger left, BigInteger right) { | ||
return new BigDecimal(Math.pow(left.doubleValue(), right.doubleValue())).toBigIntegerExact(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* To change this license header, choose License Headers in Project Properties. | ||
* To change this template file, choose Tools | Templates | ||
* and open the template in the editor. | ||
*/ | ||
package com.oracle.truffle.sl.test; | ||
|
||
import com.oracle.truffle.api.interop.UnsupportedTypeException; | ||
import com.oracle.truffle.api.source.Source; | ||
import com.oracle.truffle.api.vm.PolyglotEngine; | ||
import java.math.BigDecimal; | ||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class SLPowTest { | ||
|
||
private PolyglotEngine engine; | ||
private PolyglotEngine.Value pow; | ||
|
||
@Before | ||
public void initEngine() throws Exception { | ||
engine = PolyglotEngine.newBuilder().build(); | ||
engine.eval( | ||
Source.newBuilder("\n" | ||
+ "function pow(a, b) {\n" | ||
+ " return a ^ b;\n" | ||
+ "}\n"). | ||
name("pow.sl"). | ||
mimeType("application/x-sl"). | ||
build() | ||
); | ||
pow = engine.findGlobalSymbol("pow"); | ||
} | ||
|
||
@After | ||
public void dispose() { | ||
engine.dispose(); | ||
} | ||
|
||
@Test | ||
public void powOf2And2() throws Exception { | ||
Number ret = pow.execute(2, 2).as(Number.class); | ||
assertEquals(4, ret.intValue()); | ||
} | ||
|
||
@Test | ||
public void pow2And3() throws Exception { | ||
Number ret = pow.execute(2, 3).as(Number.class); | ||
assertEquals(8, ret.intValue()); | ||
} | ||
|
||
@Test | ||
public void powBigIntegers() throws Exception { | ||
Number ret = pow.execute(Long.MAX_VALUE, 2).as(Number.class); | ||
assertEquals(new BigDecimal(Math.pow(Long.MAX_VALUE, 2)).toBigIntegerExact(), ret); | ||
} | ||
|
||
@Test | ||
public void pow2AndString() throws Exception { | ||
Exception exc = null; | ||
try { | ||
pow.execute(2, "test"); | ||
Assert.fail("should not get here"); | ||
} catch (RuntimeException ex) { | ||
Throwable cause = ex.getCause(); | ||
Assert.assertTrue(cause.getClass().getName(), cause instanceof UnsupportedTypeException); | ||
exc = ex; | ||
} | ||
Assert.assertNotNull(exc); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
function main() { | ||
println(2 ^ 2); | ||
println(2 ^ 3); | ||
println(2 ^ 2 ^ 2); | ||
println(3 + 2 ^ 2); | ||
println(6465467984651316454646 ^ 4); | ||
//println(2 ^ "test"); | ||
//println(6465467984651316454646 ^ "test"); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrong license header.