Skip to content

Commit

Permalink
[Truffle] Fixnum#& and #bit_length.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisseaton committed Jan 7, 2015
1 parent 0e47d16 commit 830c8e6
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 23 deletions.
49 changes: 30 additions & 19 deletions core/src/main/java/org/jruby/truffle/nodes/core/FixnumNodes.java
Expand Up @@ -16,6 +16,7 @@
import com.oracle.truffle.api.nodes.UnexpectedResultException;
import com.oracle.truffle.api.source.SourceSection;
import com.oracle.truffle.api.utilities.BranchProfile;
import com.oracle.truffle.api.utilities.ConditionProfile;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.core.RubyArray;
import org.jruby.truffle.runtime.core.RubyBignum;
Expand Down Expand Up @@ -1207,49 +1208,59 @@ public long rightShift(long a, int b) {

}

@CoreMethod(names = "[]", required = 1)
public abstract static class GetIndexNode extends CoreMethodNode {
@CoreMethod(names = "abs")
public abstract static class AbsNode extends CoreMethodNode {

public GetIndexNode(RubyContext context, SourceSection sourceSection) {
public AbsNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

public GetIndexNode(GetIndexNode prev) {
public AbsNode(AbsNode prev) {
super(prev);
}

@Specialization
public int getIndex(int self, int index) {
notDesignedForCompilation();
public int abs(int n) {
return Math.abs(n);
}

if ((self & (1 << index)) == 0) {
return 0;
} else {
return 1;
}
@Specialization
public long abs(long n) {
return Math.abs(n);
}

}

@CoreMethod(names = "abs")
public abstract static class AbsNode extends CoreMethodNode {
@CoreMethod(names = "bit_length")
public abstract static class BitLengthNode extends CoreMethodNode {

This comment has been minimized.

Copy link
@chrisseaton

chrisseaton Jan 7, 2015

Author Contributor

There's no Rbx version of this method, and to implement it myself in Ruby would be to copy and paste the code from the JVM implementation and translate it into Ruby, so I think this is the easier way in this case.


public AbsNode(RubyContext context, SourceSection sourceSection) {
private static final int INTEGER_BITS = Integer.numberOfLeadingZeros(0);
private static final int LONG_BITS = Long.numberOfLeadingZeros(0);

public BitLengthNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

public AbsNode(AbsNode prev) {
public BitLengthNode(BitLengthNode prev) {
super(prev);
}

@Specialization
public int abs(int n) {
return Math.abs(n);
public int bitLength(int n) {
return bitLength((long) n);
}

@Specialization
public long abs(long n) {
return Math.abs(n);
public int bitLength(long n) {
if (n < 0) {
n = ~n;
}

if (n == Long.MAX_VALUE) {
return LONG_BITS - 1;
}

return LONG_BITS - Long.numberOfLeadingZeros(n);
}

}
Expand Down
Expand Up @@ -44,4 +44,10 @@ def to_r
Rational(self, 1)
end

def [](index)
index = Rubinius::Type.coerce_to(index, Integer, :to_int)
return 0 if index.is_a?(Bignum)
index < 0 ? 0 : (self >> index) & 1
end

end
2 changes: 0 additions & 2 deletions spec/truffle/tags/core/fixnum/bit_and_tags.txt

This file was deleted.

2 changes: 0 additions & 2 deletions spec/truffle/tags/core/fixnum/bit_length_tags.txt

This file was deleted.

0 comments on commit 830c8e6

Please sign in to comment.