Skip to content

Commit

Permalink
Reduce direct access to UnsafeUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
fniephaus committed May 24, 2020
1 parent 522b010 commit 7779278
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,13 @@ public final class DSAPrims extends AbstractPrimitiveFactoryHolder {
protected abstract static class PrimExpandBlockNode extends AbstractPrimitiveNode implements TernaryPrimitive {
@Specialization(guards = {"buf.isByteType()", "expanded.isIntType()", "expanded.getIntLength() == 80", "buf.getByteLength() == 64"})
protected static final Object doExpand(final Object receiver, final NativeObject buf, final NativeObject expanded) {
final int[] words = expanded.getIntStorage();
final byte[] bytes = buf.getByteStorage();
for (int i = 0; i <= 15; i++) {
words[i] = UnsafeUtils.getIntReversed(bytes, i);
expanded.setInt(i, UnsafeUtils.getIntReversed(bytes, i));
}
for (int i = 16; i <= 79; i += 1) {
final long value = Integer.toUnsignedLong(words[i - 3] ^ words[i - 8] ^ words[i - 14] ^ words[i - 16]);
words[i] = (int) (value << 1 | value >> 31); // leftRotate:by:.
final long value = Integer.toUnsignedLong(expanded.getInt(i - 3) ^ expanded.getInt(i - 8) ^ expanded.getInt(i - 14) ^ expanded.getInt(i - 16));
expanded.setInt(i, (int) (value << 1 | value >> 31)); // leftRotate:by:.
}
return receiver;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,15 @@ protected static final class NotAsciiOrderException extends RuntimeException {
private static final long serialVersionUID = 1L;
}

@Specialization(guards = {"string1Value.isByteType()", "string2Value.isByteType()"}, rewriteOn = NotAsciiOrderException.class)
protected final long doCompareAsciiOrder(@SuppressWarnings("unused") final Object receiver, final NativeObject string1Value, final NativeObject string2Value, final NativeObject orderValue) {
@Specialization(guards = {"string1.isByteType()", "string2.isByteType()"}, rewriteOn = NotAsciiOrderException.class)
protected final long doCompareAsciiOrder(@SuppressWarnings("unused") final Object receiver, final NativeObject string1, final NativeObject string2, final NativeObject orderValue) {
ensureAsciiOrder(orderValue);
final byte[] string1 = string1Value.getByteStorage();
final byte[] string2 = string2Value.getByteStorage();
final int len1 = string1.length;
final int len2 = string2.length;
final int len1 = string1.getByteLength();
final int len2 = string2.getByteLength();
final int min = Math.min(len1, len2);
for (int i = 0; i < min; i++) {
final byte c1 = UnsafeUtils.getByte(string1, i);
final byte c2 = UnsafeUtils.getByte(string2, i);
final byte c1 = string1.getByte(i);
final byte c2 = string2.getByte(i);
if (c1 != c2) {
return (c1 & 0xff) < (c2 & 0xff) ? 1L : 3L;
}
Expand Down Expand Up @@ -88,18 +86,15 @@ private void ensureAsciiOrder(final NativeObject orderValue) {
}
}

@Specialization(guards = {"string1Value.isByteType()", "string2Value.isByteType()", "orderValue.isByteType()", "orderValue.getByteLength() >= 256"}, replaces = "doCompareAsciiOrder")
protected static final long doCompare(@SuppressWarnings("unused") final Object receiver, final NativeObject string1Value, final NativeObject string2Value,
@Specialization(guards = {"string1.isByteType()", "string2.isByteType()", "orderValue.isByteType()", "orderValue.getByteLength() >= 256"}, replaces = "doCompareAsciiOrder")
protected static final long doCompare(@SuppressWarnings("unused") final Object receiver, final NativeObject string1, final NativeObject string2,
final NativeObject orderValue) {
final byte[] string1 = string1Value.getByteStorage();
final byte[] string2 = string2Value.getByteStorage();
final byte[] order = orderValue.getByteStorage();
final int len1 = string1.length;
final int len2 = string2.length;
final int len1 = string1.getByteLength();
final int len2 = string2.getByteLength();
final int min = Math.min(len1, len2);
for (int i = 0; i < min; i++) {
final byte c1 = UnsafeUtils.getByte(order, UnsafeUtils.getByte(string1, i) & 0xff);
final byte c2 = UnsafeUtils.getByte(order, UnsafeUtils.getByte(string2, i) & 0xff);
final byte c1 = orderValue.getByte(string1.getByte(i) & 0xff);
final byte c2 = orderValue.getByte(string2.getByte(i) & 0xff);
if (c1 != c2) {
return (c1 & 0xff) < (c2 & 0xff) ? 1L : 3L;
}
Expand Down Expand Up @@ -325,11 +320,9 @@ public abstract static class PrimFindFirstInStringNode extends AbstractPrimitive

@Specialization(guards = {"start >= 1", "string.isByteType()", "inclusionMap.isByteType()", "inclusionMap.getByteLength() == 256"})
protected static final long doFind(@SuppressWarnings("unused") final Object receiver, final NativeObject string, final NativeObject inclusionMap, final long start) {
final byte[] stringBytes = string.getByteStorage();
final byte[] inclusionMapBytes = inclusionMap.getByteStorage();
final int stringSize = stringBytes.length;
final int stringSize = string.getByteLength();
long index = start - 1;
while (index < stringSize && UnsafeUtils.getByte(inclusionMapBytes, UnsafeUtils.getByte(stringBytes, index) & 0xff) == 0) {
while (index < stringSize && inclusionMap.getByte(string.getByte(index) & 0xff) == 0) {
index++;
}
return index >= stringSize ? 0L : index + 1;
Expand Down Expand Up @@ -466,21 +459,17 @@ public abstract static class PrimTranslateStringWithTableNode extends AbstractPr

@Specialization(guards = {"start >= 1", "string.isByteType()", "stop <= string.getByteLength()", "table.isByteType()", "table.getByteLength() >= 256"})
protected static final Object doNativeObject(final Object receiver, final NativeObject string, final long start, final long stop, final NativeObject table) {
final byte[] stringBytes = string.getByteStorage();
final byte[] tableBytes = table.getByteStorage();
for (int i = (int) start - 1; i < stop; i++) {
stringBytes[i] = UnsafeUtils.getByte(tableBytes, UnsafeUtils.getByte(stringBytes, i) & 0xff);
for (long i = start - 1; i < stop; i++) {
string.setByte(i, table.getByte(string.getByte(i) & 0xff));
}
return receiver;
}

@Specialization(guards = {"start >= 1", "string.isByteType()", "stop <= string.getByteLength()", "table.isIntType()", "table.getIntLength() >= 256"})
protected static final Object doNativeObjectIntTable(final Object receiver, final NativeObject string, final long start, final long stop,
final NativeObject table) {
final byte[] stringBytes = string.getByteStorage();
final int[] tableBytes = table.getIntStorage();
for (int i = (int) start - 1; i < stop; i++) {
stringBytes[i] = (byte) UnsafeUtils.getInt(tableBytes, UnsafeUtils.getByte(stringBytes, i) & 0xff);
for (long i = start - 1; i < stop; i++) {
string.setByte(i, (byte) table.getInt(string.getByte(i) & 0xff));
}
return receiver;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import de.hpi.swa.trufflesqueak.nodes.primitives.PrimitiveInterfaces.UnaryPrimitive;
import de.hpi.swa.trufflesqueak.nodes.primitives.SqueakPrimitive;
import de.hpi.swa.trufflesqueak.util.ArrayUtils;
import de.hpi.swa.trufflesqueak.util.UnsafeUtils;

public final class UUIDPlugin extends AbstractPrimitiveFactoryHolder {

Expand All @@ -31,12 +30,11 @@ public List<? extends NodeFactory<? extends AbstractPrimitiveNode>> getFactories
protected abstract static class PrimMakeUUIDNode extends AbstractPrimitiveNode implements UnaryPrimitive {
@Specialization(guards = {"receiver.isByteType()", "receiver.getByteLength() == 16"})
protected static final Object doUUID(final NativeObject receiver) {
final byte[] bytes = receiver.getByteStorage();
ArrayUtils.fillRandomly(bytes);
ArrayUtils.fillRandomly(receiver.getByteStorage());
// Version 4
UnsafeUtils.putByte(bytes, 6, (byte) (UnsafeUtils.getByte(bytes, 6) & 0x0F | 0x40));
receiver.setByte(6, (byte) (receiver.getByte(6) & 0x0F | 0x40));
// Fixed 8..b value
UnsafeUtils.putByte(bytes, 8, (byte) (UnsafeUtils.getByte(bytes, 8) & 0x3F | 0x80));
receiver.setByte(8, (byte) (receiver.getByte(8) & 0x3F | 0x80));
return receiver;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ protected abstract static class PrimIntegerAtNode extends AbstractPrimitiveNode

@Specialization(guards = {"receiver.isIntType()", "inBounds1(index, receiver.getIntLength())"})
protected static final long doNativeInt(final NativeObject receiver, final long index) {
return UnsafeUtils.getInt(receiver.getIntStorage(), index - 1);
return receiver.getInt(index - 1);
}
}

Expand All @@ -207,7 +207,7 @@ protected abstract static class PrimIntegerAtPutNode extends AbstractPrimitiveNo

@Specialization(guards = {"receiver.isIntType()", "inBounds1(index, receiver.getIntLength())", "fitsIntoInt(value)"})
protected static final long doNativeInt(final NativeObject receiver, final long index, final long value) {
UnsafeUtils.putInt(receiver.getIntStorage(), index - 1, (int) value);
receiver.setInt(index - 1, (int) value);
return value;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
import de.hpi.swa.trufflesqueak.nodes.primitives.PrimitiveInterfaces.UnaryPrimitiveWithoutFallback;
import de.hpi.swa.trufflesqueak.nodes.primitives.SqueakPrimitive;
import de.hpi.swa.trufflesqueak.util.NotProvided;
import de.hpi.swa.trufflesqueak.util.UnsafeUtils;

public final class IOPrimitives extends AbstractPrimitiveFactoryHolder {

Expand Down Expand Up @@ -266,13 +265,12 @@ protected final Object doScan(final PointersObject receiver, final long startInd
final ArrayObject stops, final long kernData) {
final ArrayObject scanXTable = pointersReadNode.executeArray(receiver, CHARACTER_SCANNER.XTABLE);
final ArrayObject scanMap = pointersReadNode.executeArray(receiver, CHARACTER_SCANNER.MAP);
final byte[] sourceBytes = sourceString.getByteStorage();

final int maxGlyph = arraySizeNode.execute(scanXTable) - 2;
long scanDestX = pointersReadNode.executeLong(receiver, CHARACTER_SCANNER.DEST_X);
long scanLastIndex = startIndex;
while (scanLastIndex <= stopIndex) {
final long ascii = UnsafeUtils.getByte(sourceBytes, scanLastIndex - 1) & 0xFF;
final long ascii = sourceString.getByte(scanLastIndex - 1) & 0xFF;
final Object stopReason = arrayReadNode.execute(stops, ascii);
if (stopReason != NilObject.SINGLETON) {
storeStateInReceiver(receiver, scanDestX, scanLastIndex);
Expand Down Expand Up @@ -395,8 +393,8 @@ protected static final FloatObject doFloat(final FloatObject rcvr, final long st
errorProfile.enter();
throw PrimitiveFailed.GENERIC_ERROR;
}
rcvr.setHigh(repl.getIntStorage()[1]);
rcvr.setLow(repl.getIntStorage()[0]);
rcvr.setHigh(repl.getInt(1));
rcvr.setLow(repl.getInt(0));
return rcvr;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,9 +405,15 @@ protected static final NativeObject doNativeInts(final NativeObject receiver, fi
return receiver;
}

@Specialization(guards = {"receiver.isIntType()", "value.lessThanOrEqualTo(INTEGER_MAX)"})
@Specialization(guards = {"receiver.isIntType()"}, rewriteOn = ArithmeticException.class)
protected static final NativeObject doNativeInts(final NativeObject receiver, final LargeIntegerObject value) {
Arrays.fill(receiver.getIntStorage(), (int) value.longValueExact());
Arrays.fill(receiver.getIntStorage(), value.intValueExact());
return receiver;
}

@Specialization(guards = {"receiver.isIntType()", "value.lessThanOrEqualTo(INTEGER_MAX)"}, replaces = "doNativeInts")
protected static final NativeObject doNativeIntsFallback(final NativeObject receiver, final LargeIntegerObject value) {
Arrays.fill(receiver.getIntStorage(), value.intValueExact());
return receiver;
}

Expand All @@ -417,11 +423,17 @@ protected static final NativeObject doNativeLongs(final NativeObject receiver, f
return receiver;
}

@Specialization(guards = {"receiver.isLongType()", "value.fitsIntoLong()"})
@Specialization(guards = {"receiver.isLongType()"}, rewriteOn = ArithmeticException.class)
protected static final NativeObject doNativeLongs(final NativeObject receiver, final LargeIntegerObject value) {
Arrays.fill(receiver.getLongStorage(), value.longValueExact());
return receiver;
}

@Specialization(guards = {"receiver.isLongType()", "value.fitsIntoLong()"}, replaces = "doNativeLongs")
protected static final NativeObject doNativeLongsFallback(final NativeObject receiver, final LargeIntegerObject value) {
Arrays.fill(receiver.getLongStorage(), value.longValueExact());
return receiver;
}
}

@NodeInfo(cost = NodeCost.NONE)
Expand Down

0 comments on commit 7779278

Please sign in to comment.