Skip to content
Snippets Groups Projects
Commit 59f3dc84 authored by Lukas Stadler's avatar Lukas Stadler
Browse files

adapt octsize nodes

parent dbf9e8d6
No related branches found
No related tags found
No related merge requests found
...@@ -43,7 +43,6 @@ import com.oracle.truffle.r.nodes.attributes.SpecialAttributesFunctions.SetNames ...@@ -43,7 +43,6 @@ import com.oracle.truffle.r.nodes.attributes.SpecialAttributesFunctions.SetNames
import com.oracle.truffle.r.nodes.attributes.SpecialAttributesFunctionsFactory.SetNamesAttributeNodeGen; import com.oracle.truffle.r.nodes.attributes.SpecialAttributesFunctionsFactory.SetNamesAttributeNodeGen;
import com.oracle.truffle.r.nodes.builtin.EnvironmentNodes.GetFunctionEnvironmentNode; import com.oracle.truffle.r.nodes.builtin.EnvironmentNodes.GetFunctionEnvironmentNode;
import com.oracle.truffle.r.nodes.builtin.casts.fluent.CastNodeBuilder; import com.oracle.truffle.r.nodes.builtin.casts.fluent.CastNodeBuilder;
import com.oracle.truffle.r.nodes.builtin.casts.fluent.HeadPhaseBuilder;
import com.oracle.truffle.r.nodes.objects.NewObject; import com.oracle.truffle.r.nodes.objects.NewObject;
import com.oracle.truffle.r.nodes.objects.NewObjectNodeGen; import com.oracle.truffle.r.nodes.objects.NewObjectNodeGen;
import com.oracle.truffle.r.nodes.unary.CastNode; import com.oracle.truffle.r.nodes.unary.CastNode;
...@@ -57,8 +56,6 @@ import com.oracle.truffle.r.runtime.data.RRawVector; ...@@ -57,8 +56,6 @@ import com.oracle.truffle.r.runtime.data.RRawVector;
import com.oracle.truffle.r.runtime.data.RSymbol; import com.oracle.truffle.r.runtime.data.RSymbol;
import com.oracle.truffle.r.runtime.data.RTypes; import com.oracle.truffle.r.runtime.data.RTypes;
import com.oracle.truffle.r.runtime.data.model.RAbstractContainer; import com.oracle.truffle.r.runtime.data.model.RAbstractContainer;
import com.oracle.truffle.r.runtime.data.model.RAbstractDoubleVector;
import com.oracle.truffle.r.runtime.data.nodes.GetDataAt;
import com.oracle.truffle.r.runtime.env.REnvironment; import com.oracle.truffle.r.runtime.env.REnvironment;
import com.oracle.truffle.r.runtime.gnur.SEXPTYPE; import com.oracle.truffle.r.runtime.gnur.SEXPTYPE;
...@@ -266,26 +263,17 @@ public final class MiscNodes { ...@@ -266,26 +263,17 @@ public final class MiscNodes {
@Specialization @Specialization
protected RRawVector octSize(Object size, protected RRawVector octSize(Object size,
@Cached("create()") SizeToOctalRawNode sizeToOctal, @Cached("create()") SizeToOctalRawNode sizeToOctal,
@Cached("createCast()") CastNode castToDoubleNode, @Cached("createCast()") CastNode castToDoubleNode) {
@Cached("create()") GetDataAt.Double getDataNode) { return sizeToOctal.execute(castToDoubleNode.doCast(size));
Object val = castToDoubleNode.doCast(size);
if (val instanceof RAbstractDoubleVector) {
RAbstractDoubleVector vec = (RAbstractDoubleVector) val;
return sizeToOctal.execute(getDataNode.get(vec, vec.getInternalStore(), 0));
}
return sizeToOctal.execute(val);
} }
protected CastNode createCast() { protected CastNode createCast() {
HeadPhaseBuilder<Double> findFirst = CastNodeBuilder.newCastBuilder().mustNotBeMissing().allowNull().asDoubleVector().findFirst(); return CastNodeBuilder.newCastBuilder().mustNotBeMissing().allowNull().asDoubleVector().findFirst().buildCastNode();
return findFirst.buildCastNode();
} }
public static OctSizeNode create() { public static OctSizeNode create() {
return OctSizeNodeGen.create(); return OctSizeNodeGen.create();
} }
} }
} }
/* /*
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. * This material is distributed under the GNU General Public License
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * Version 2. You may review the terms of this license at
* http://www.gnu.org/licenses/gpl-2.0.html
* *
* This code is free software; you can redistribute it and/or modify it * Copyright (c) 2000-2013, The R Core Team
* under the terms of the GNU General Public License version 2 only, as * Copyright (c) 2017, Oracle and/or its affiliates
* published by the Free Software Foundation.
* *
* This code is distributed in the hope that it will be useful, but WITHOUT * All rights reserved.
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/ */
package com.oracle.truffle.r.nodes.unary; package com.oracle.truffle.r.nodes.unary;
import java.nio.ByteBuffer; import com.oracle.truffle.api.dsl.Fallback;
import java.nio.charset.Charset; import com.oracle.truffle.api.dsl.ImportStatic;
import java.util.Arrays;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.Specialization; import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.r.runtime.RError; import com.oracle.truffle.r.runtime.RError;
import com.oracle.truffle.r.runtime.RRuntime; import com.oracle.truffle.r.runtime.RRuntime;
import com.oracle.truffle.r.runtime.RType;
import com.oracle.truffle.r.runtime.data.RDataFactory; import com.oracle.truffle.r.runtime.data.RDataFactory;
import com.oracle.truffle.r.runtime.data.RNull; import com.oracle.truffle.r.runtime.data.RDataFactory.VectorFactory;
import com.oracle.truffle.r.runtime.data.RRawVector; import com.oracle.truffle.r.runtime.data.RRawVector;
import com.oracle.truffle.r.runtime.data.nodes.SetDataAt; import com.oracle.truffle.r.runtime.data.nodes.VectorAccess;
import com.oracle.truffle.r.runtime.data.nodes.VectorAccess.RandomIterator;
@ImportStatic(RRuntime.class)
public abstract class SizeToOctalRawNode extends UnaryNode { public abstract class SizeToOctalRawNode extends UnaryNode {
private Charset asciiCharset; @Child private VectorFactory factory = VectorFactory.create();
@Child private VectorAccess resultAccess = VectorAccess.createNew(RType.Raw);
public abstract RRawVector execute(Object size); public abstract RRawVector execute(Object size);
@Specialization @Specialization(guards = "!isNA(size)")
protected RRawVector doInt(int s) { protected RRawVector doInt(int size) {
return RDataFactory.createRawVector(toOctalAsciiString(s)); if (size < 0) {
} throw RError.error(RError.SHOW_CALLER, RError.Message.GENERIC, "size must be finite, >= 0 and < 2^33");
@TruffleBoundary
private byte[] toOctalAsciiString(int s) {
if (asciiCharset == null) {
asciiCharset = Charset.forName("US-ASCII");
} }
return toOctal(size);
ByteBuffer encode = asciiCharset.encode(Integer.toOctalString(s));
// reverse
byte[] result = new byte[11];
Arrays.fill(result, (byte) '0');
for (int i = result.length - 1; i >= 0 && encode.hasRemaining(); i--) {
result[i] = encode.get();
}
return result;
} }
// Transcribed from ".../utils/src/stubs.c" // Transcribed from ".../utils/src/stubs.c"
@Specialization @Specialization(guards = "!isNAorNaN(size)")
protected RRawVector doDouble(double size, protected RRawVector doDouble(double size) {
@Cached("create()") SetDataAt.Raw setDataNode) { if (!RRuntime.isFinite(size) || size < 0 || size >= 8589934592d /* 2^33 */) {
throw RError.error(RError.SHOW_CALLER, RError.Message.GENERIC, "size must be finite, >= 0 and < 2^33");
double s = size;
if (!RRuntime.isFinite(s) && s >= 0) {
throw RError.error(RError.SHOW_CALLER, RError.Message.GENERIC, "size must be finite and >= 0");
} }
return toOctal((long) size);
}
RRawVector ans = RDataFactory.createRawVector(11); private RRawVector toOctal(long size) {
byte[] store = ans.getInternalStore(); RRawVector ans = factory.createRawVector(11);
try (RandomIterator iter = resultAccess.randomAccess(ans)) {
for (int i = 0; i < 11; i++) { long s = size;
double s2 = Math.floor(s / 8.0); for (int i = 0; i < 11; i++) {
double t = s - 8.0 * s2; resultAccess.setRaw(iter, 10 - i, (byte) (48.0 + (s % 8)));
s = s2; s /= 8;
setDataNode.setDataAtAsObject(ans, store, 10 - i, (byte) (48.0 + t)); }
} }
return ans; return ans;
} }
@Specialization @Fallback
protected RRawVector doNull(@SuppressWarnings("unused") RNull n) { protected RRawVector doNull(@SuppressWarnings("unused") Object obj) {
return RDataFactory.createRawVector(11); return RDataFactory.createRawVector(11);
} }
public static SizeToOctalRawNode create() { public static SizeToOctalRawNode create() {
return SizeToOctalRawNodeGen.create(); return SizeToOctalRawNodeGen.create();
} }
} }
...@@ -195,6 +195,7 @@ com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/objects/LoadMethod.jav ...@@ -195,6 +195,7 @@ com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/objects/LoadMethod.jav
com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/objects/NewObject.java,gnu_r_gentleman_ihaka.copyright com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/objects/NewObject.java,gnu_r_gentleman_ihaka.copyright
com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/unary/InheritsNode.java,purdue.copyright com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/unary/InheritsNode.java,purdue.copyright
com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/unary/IsFactorNode.java,purdue.copyright com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/unary/IsFactorNode.java,purdue.copyright
com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/unary/SizeToOctalRawNode.java,gnu_r.core.copyright
com.oracle.truffle.r.parser/src/com/oracle/truffle/r/parser/R.g,purdue.copyright com.oracle.truffle.r.parser/src/com/oracle/truffle/r/parser/R.g,purdue.copyright
com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RTypedValue.java,gnu_r_gentleman_ihaka.copyright com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RTypedValue.java,gnu_r_gentleman_ihaka.copyright
com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/ffi/DLL.java,gnu_r.copyright com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/ffi/DLL.java,gnu_r.copyright
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment