-
Notifications
You must be signed in to change notification settings - Fork 326
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
Implement Number.round as a builtin #7460
Merged
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
cf4d188
decimal
GregoryTravis cc78415
integer
GregoryTravis ba63c03
bench size
GregoryTravis cf8d9ef
Merge branch 'develop' into wip/gmt/7401-round-builtin
GregoryTravis e80e6b6
move stuff into tb methods
GregoryTravis ee2cc72
bench builder api
GregoryTravis 6aee4a4
Merge branch 'develop' into wip/gmt/7401-round-builtin
GregoryTravis 6849e36
additional optimizations, handling one error
GregoryTravis d9499c6
cleanup
GregoryTravis f4d2014
Merge branch 'develop' into wip/gmt/7401-round-builtin
mergify[bot] 665786c
Allow invocation of Numeric benchmarks via JMH
JaroslavTulach dc9ebe0
Have to use PrimitiveValueProfile when profiling primitive values
JaroslavTulach efeb2d7
Make sure compiler knows a branch yields an exception
JaroslavTulach b57862b
Double.NaN => ROUND_MIN_DOUBLE is false. Also Double.Infinity <= ROUN…
JaroslavTulach 94dac30
Profile to avoid outOfRange exception code
JaroslavTulach 239c25b
Some optimizations for smallInteger.RoundNode
JaroslavTulach 843f8ef
Check decimalPlaces first to finish as soon as possible
JaroslavTulach 249a36b
Merge branch 'develop' into wip/gmt/7401-round-builtin
GregoryTravis 3034fad
fix up error handling, tests
GregoryTravis 87a1b40
cleanup
GregoryTravis 62063cc
cleanup
GregoryTravis e7e67e8
rename builtin stubs
GregoryTravis d004233
handle bigints, do range check in enso
GregoryTravis 64f0da6
bigint tests
GregoryTravis 5fbeb1a
remove old, restore bench
GregoryTravis 70d23ae
added test for type errors
GregoryTravis 92cab6e
fix meta_spec
GregoryTravis 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
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
74 changes: 74 additions & 0 deletions
74
.../src/main/java/org/enso/interpreter/node/expression/builtin/number/decimal/RoundNode.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,74 @@ | ||
package org.enso.interpreter.node.expression.builtin.number.decimal; | ||
|
||
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; | ||
import com.oracle.truffle.api.nodes.Node; | ||
import com.oracle.truffle.api.profiles.BranchProfile; | ||
import com.oracle.truffle.api.profiles.CountingConditionProfile; | ||
import com.oracle.truffle.api.profiles.PrimitiveValueProfile; | ||
import java.math.BigDecimal; | ||
import java.math.BigInteger; | ||
import org.enso.interpreter.dsl.BuiltinMethod; | ||
import org.enso.interpreter.node.expression.builtin.number.utils.BigIntegerOps; | ||
import org.enso.interpreter.node.expression.builtin.number.utils.RoundHelpers; | ||
import org.enso.interpreter.runtime.number.EnsoBigInteger; | ||
|
||
@BuiltinMethod( | ||
type = "Decimal", | ||
name = "round", | ||
description = "Decimal ceiling, converting to a small or big integer depending on size.") | ||
public class RoundNode extends Node { | ||
private final CountingConditionProfile fitsProfile = CountingConditionProfile.create(); | ||
|
||
private final PrimitiveValueProfile constantPlacesDecimalPlaces = PrimitiveValueProfile.create(); | ||
|
||
private final PrimitiveValueProfile constantPlacesUseBankers = PrimitiveValueProfile.create(); | ||
|
||
private final BranchProfile decimalPlacesOutOfRangeProfile = BranchProfile.create(); | ||
|
||
private final BranchProfile outOfRangeProfile = BranchProfile.create(); | ||
|
||
Object execute(double n, long dp, boolean ub) { | ||
long decimalPlaces = constantPlacesDecimalPlaces.profile(dp); | ||
boolean useBankers = constantPlacesUseBankers.profile(ub); | ||
|
||
if (decimalPlaces < RoundHelpers.ROUND_MIN_DECIMAL_PLACES | ||
|| decimalPlaces > RoundHelpers.ROUND_MAX_DECIMAL_PLACES) { | ||
decimalPlacesOutOfRangeProfile.enter(); | ||
RoundHelpers.decimalPlacesOutOfRangePanic(this, decimalPlaces); | ||
} | ||
|
||
boolean inRange = n >= RoundHelpers.ROUND_MIN_DOUBLE && n <= RoundHelpers.ROUND_MAX_DOUBLE; | ||
if (!inRange) { | ||
outOfRangeProfile.enter(); | ||
if (Double.isNaN(n) || Double.isInfinite(n)) { | ||
RoundHelpers.specialValuePanic(this, n); | ||
} else { | ||
RoundHelpers.argumentOutOfRangePanic(this, n); | ||
} | ||
} | ||
|
||
// Algorithm taken from https://stackoverflow.com/a/7211688. | ||
double scale = Math.pow(10.0, decimalPlaces); | ||
double scaled = n * scale; | ||
double roundBase = Math.floor(scaled); | ||
double roundMidpoint = (roundBase + 0.5) / scale; | ||
boolean evenIsUp = n >= 0 ? (((long) scaled) % 2) != 0 : (((long) scaled) % 2) == 0; | ||
boolean halfGoesUp = useBankers ? evenIsUp : n >= 0; | ||
boolean doRoundUp = halfGoesUp ? n >= roundMidpoint : n > roundMidpoint; | ||
double resultUncast = doRoundUp ? ((roundBase + 1.0) / scale) : (roundBase / scale); | ||
if (decimalPlaces > 0) { | ||
return resultUncast; | ||
} else { | ||
if (fitsProfile.profile(BigIntegerOps.fitsInLong(resultUncast))) { | ||
return (long) resultUncast; | ||
} else { | ||
return new EnsoBigInteger(toBigInteger(resultUncast)); | ||
} | ||
} | ||
} | ||
|
||
@TruffleBoundary | ||
private BigInteger toBigInteger(double n) { | ||
return BigDecimal.valueOf(n).toBigIntegerExact(); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...main/java/org/enso/interpreter/node/expression/builtin/number/smallInteger/RoundNode.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,54 @@ | ||
package org.enso.interpreter.node.expression.builtin.number.smallInteger; | ||
|
||
import com.oracle.truffle.api.nodes.Node; | ||
import com.oracle.truffle.api.profiles.BranchProfile; | ||
import com.oracle.truffle.api.profiles.CountingConditionProfile; | ||
import com.oracle.truffle.api.profiles.PrimitiveValueProfile; | ||
import org.enso.interpreter.dsl.BuiltinMethod; | ||
import org.enso.interpreter.node.expression.builtin.number.utils.RoundHelpers; | ||
|
||
@BuiltinMethod( | ||
type = "Integer", | ||
name = "round", | ||
description = "Decimal ceiling, converting to a small or big integer depending on size.") | ||
public class RoundNode extends Node { | ||
private final CountingConditionProfile fitsProfile = CountingConditionProfile.create(); | ||
|
||
private final PrimitiveValueProfile constantPlacesDecimalPlaces = PrimitiveValueProfile.create(); | ||
|
||
private final PrimitiveValueProfile constantPlacesUseBankers = PrimitiveValueProfile.create(); | ||
|
||
private final BranchProfile decimalPlacesOutOfRangeProfile = BranchProfile.create(); | ||
|
||
Object execute(long n, long dp, boolean ub) { | ||
var decimalPlaces = constantPlacesDecimalPlaces.profile(dp); | ||
|
||
// We don't check if `n` is out of range here, since the Enso wrapper does. | ||
if (decimalPlaces < RoundHelpers.ROUND_MIN_DECIMAL_PLACES | ||
|| decimalPlaces > RoundHelpers.ROUND_MAX_DECIMAL_PLACES) { | ||
decimalPlacesOutOfRangeProfile.enter(); | ||
RoundHelpers.decimalPlacesOutOfRangePanic(this, decimalPlaces); | ||
} | ||
|
||
if (decimalPlaces >= 0) { | ||
return n; | ||
} | ||
|
||
var useBankers = constantPlacesUseBankers.profile(ub); | ||
long scale = (long) Math.pow(10, -decimalPlaces); | ||
long halfway = scale / 2; | ||
long remainder = n % scale; | ||
long scaledDown = n / scale; | ||
long resultUnnudged = scaledDown * scale; | ||
|
||
if (n >= 0) { | ||
boolean halfGoesUp = useBankers ? (scaledDown % 2) != 0 : true; | ||
boolean roundUp = halfGoesUp ? remainder >= halfway : remainder > halfway; | ||
return roundUp ? resultUnnudged + scale : resultUnnudged; | ||
} else { | ||
boolean halfGoesUp = useBankers ? (scaledDown % 2) == 0 : false; | ||
boolean roundUp = halfGoesUp ? remainder < -halfway : remainder <= -halfway; | ||
return roundUp ? resultUnnudged - scale : resultUnnudged; | ||
} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...src/main/java/org/enso/interpreter/node/expression/builtin/number/utils/RoundHelpers.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,63 @@ | ||
package org.enso.interpreter.node.expression.builtin.number.utils; | ||
|
||
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; | ||
import com.oracle.truffle.api.nodes.Node; | ||
import org.enso.interpreter.runtime.EnsoContext; | ||
import org.enso.interpreter.runtime.builtin.Builtins; | ||
import org.enso.interpreter.runtime.error.PanicException; | ||
|
||
public class RoundHelpers { | ||
/** Minimum value for the `decimal_places` parameter to `roundDouble`. */ | ||
public static final double ROUND_MIN_DECIMAL_PLACES = -15; | ||
|
||
/** Maximum value for the `decimal_places` parameter to `roundDouble`. */ | ||
public static final double ROUND_MAX_DECIMAL_PLACES = 15; | ||
|
||
/** Minimum value for the `n` parameter to `roundDouble`. */ | ||
public static final double ROUND_MIN_DOUBLE = -99999999999999.0; | ||
|
||
/** Minimum value for the `n` parameter to `roundDouble`. */ | ||
public static final double ROUND_MAX_DOUBLE = 99999999999999.0; | ||
|
||
public static final double ROUND_MIN_LONG = -99999999999999L; | ||
|
||
/** Minimum value for the `n` parameter to `roundDouble`. */ | ||
public static final double ROUND_MAX_LONG = 99999999999999L; | ||
|
||
@TruffleBoundary | ||
public static void decimalPlacesOutOfRangePanic(Node node, long decimalPlaces) | ||
throws PanicException { | ||
String msg = | ||
"round: decimalPlaces must be between " | ||
+ ROUND_MIN_DECIMAL_PLACES | ||
+ " and " | ||
+ ROUND_MAX_DECIMAL_PLACES | ||
+ " (inclusive), but was " | ||
+ decimalPlaces; | ||
Builtins builtins = EnsoContext.get(node).getBuiltins(); | ||
throw new PanicException( | ||
builtins.error().makeUnsupportedArgumentsError(new Object[] {decimalPlaces}, msg), node); | ||
} | ||
|
||
@TruffleBoundary | ||
public static void argumentOutOfRangePanic(Node node, double n) throws PanicException { | ||
String msg = | ||
"Error: `round` can only accept values between " | ||
+ ROUND_MIN_DOUBLE | ||
+ " and " | ||
+ ROUND_MAX_DOUBLE | ||
+ " (inclusive), but was " | ||
+ n; | ||
Builtins builtins = EnsoContext.get(node).getBuiltins(); | ||
throw new PanicException( | ||
builtins.error().makeUnsupportedArgumentsError(new Object[] {n}, msg), node); | ||
} | ||
|
||
@TruffleBoundary | ||
public static void specialValuePanic(Node node, double n) throws PanicException { | ||
String msg = "Error: `round` cannot accept " + (Double.isNaN(n) ? "NaN" : "Inf") + " values "; | ||
Builtins builtins = EnsoContext.get(node).getBuiltins(); | ||
throw new PanicException( | ||
builtins.error().makeUnsupportedArgumentsError(new Object[] {n}, msg), node); | ||
} | ||
} |
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
Oops, something went wrong.
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.
to_text.replace
could be removed when https://github.com/enso-org/enso/pull/7519/files#r1288361395 is in.