Skip to content

Commit

Permalink
Profile some arithmetic operations
Browse files Browse the repository at this point in the history
  • Loading branch information
fniephaus committed Nov 4, 2024
1 parent 159e66d commit 16e6186
Showing 1 changed file with 33 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,18 @@ protected static final Object doLongDouble(final long lhs, final double rhs,
@GenerateNodeFactory
@SqueakPrimitive(indices = 11)
protected abstract static class PrimFloorModNode extends AbstractArithmeticPrimitiveNode implements BinaryPrimitiveFallback {
/** Profiled version of {@link Math#floorMod(long, long)}. */
@Specialization(guards = "rhs != 0")
protected static final long doLong(final long lhs, final long rhs) {
return Math.floorMod(lhs, rhs);
protected static final long doLong(final long lhs, final long rhs,
@Bind("this") final Node node,
@Cached final InlinedConditionProfile profile) {
final long r = lhs % rhs;
// if the signs are different and modulo not zero, adjust result
if (profile.profile(node, (lhs ^ rhs) < 0 && r != 0)) {
return r + rhs;
} else {
return r;
}
}

@Specialization(guards = "!rhs.isZero()")
Expand All @@ -344,9 +353,18 @@ protected static final Object doLongLargeInteger(final long lhs, final LargeInte
@GenerateNodeFactory
@SqueakPrimitive(indices = 12)
protected abstract static class PrimFloorDivideNode extends AbstractArithmeticPrimitiveNode implements BinaryPrimitiveFallback {
/** Profiled version of {@link Math#floorDiv(long, long)}. */
@Specialization(guards = {"rhs != 0", "!isOverflowDivision(lhs, rhs)"})
protected static final long doLong(final long lhs, final long rhs) {
return Math.floorDiv(lhs, rhs);
protected static final long doLong(final long lhs, final long rhs,
@Bind("this") final Node node,
@Cached final InlinedConditionProfile profile) {
final long q = lhs / rhs;
// if the signs are different and modulo not zero, round down
if (profile.profile(node, (lhs ^ rhs) < 0 && (q * rhs != lhs))) {
return q - 1;
} else {
return q;
}
}

@SuppressWarnings("unused")
Expand Down Expand Up @@ -1020,10 +1038,11 @@ protected static final Object doFloat(final FloatObject lhs, final FloatObject r
@SqueakPrimitive(indices = 51)
protected abstract static class PrimFloatTruncatedNode extends AbstractArithmeticPrimitiveNode implements UnaryPrimitiveFallback {
@Specialization(guards = "inSafeIntegerRange(receiver.getValue())")
protected static final long doFloat(final FloatObject receiver) {
protected static final long doFloat(final FloatObject receiver,
@Bind("this") final Node node,
@Cached final InlinedConditionProfile isNegativeProfile) {
assert receiver.isFinite();
final double value = receiver.getValue();
return (long) ExactMath.truncate(value);
return truncate(node, isNegativeProfile, receiver.getValue());
}

@Specialization(guards = {"!inSafeIntegerRange(receiver.getValue())", "receiver.isFinite()"})
Expand Down Expand Up @@ -1463,8 +1482,8 @@ protected abstract static class PrimSmallFloatTruncatedNode extends AbstractArit
@Specialization(guards = "inSafeIntegerRange(receiver)")
protected static final long doDouble(final double receiver,
@Bind("this") final Node node,
@Cached final InlinedConditionProfile positiveProfile) {
return (long) (positiveProfile.profile(node, receiver >= 0) ? Math.floor(receiver) : Math.ceil(receiver));
@Cached final InlinedConditionProfile isNegativeProfile) {
return truncate(node, isNegativeProfile, receiver);
}

@Specialization(guards = "!inSafeIntegerRange(receiver)")
Expand Down Expand Up @@ -1618,6 +1637,11 @@ protected static final double ensureFinite(final double value) throws Respeciali
}
}

/** Profiled version of {@link ExactMath#truncate(double)}. */
protected static final long truncate(final Node node, final InlinedConditionProfile profile, final double value) {
return (long) (profile.profile(node, value < 0.0) ? Math.ceil(value) : Math.floor(value));
}

@TruffleBoundary
protected static final int compareNotExact(final double lhs, final long rhs) {
return new BigDecimal(lhs).compareTo(new BigDecimal(rhs));
Expand Down

1 comment on commit 16e6186

@TruffleSqueak-Bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Performance Report (16e6186)

Benchmarks ran on 23.0.1-graal.

Steady (after 100 iterations)

Benchmark Name Min Geomean Median Mean Max Total (ms) Total (min)
Bounce 522 541 526.44 523 526.41 105288 1.75
CD 475 501 481.18 477 481.11 96236 1.6
DeltaBlue 285 451 403.53 406 402.48 80705 1.35
Havlak 1086 1139 1114.9 1114 1114.85 222980 3.72
Json 343 363 346.49 344 346.45 69297 1.15
List 312 348 325.54 325 325.48 65107 1.09
Mandelbrot 129 154 130.95 130 130.91 26190 0.44
NBody 280 318 286.69 281 286.57 57338 0.96
Permute 155 169 156.24 156 156.22 31248 0.52
Queens 216 243 219.29 218 219.23 43857 0.73
Richards 1276 1293 1282.09 1279 1282.08 256417 4.27
Sieve 177 199 178.28 178 178.25 35655 0.59
Storage 138 153 140.94 139 140.88 28188 0.47
Towers 178 200 180.31 179 180.25 36061 0.6
5572 6072 5772.83 5749 5771.16 1154567 19.24

16e6186-2-steady.svg

Warmup (first 100 iterations)

16e6186-3-warmup.svg

Please sign in to comment.