Skip to content

Commit 830c8e6

Browse files
committed
[Truffle] Fixnum#& and #bit_length.
1 parent 0e47d16 commit 830c8e6

File tree

4 files changed

+36
-23
lines changed

4 files changed

+36
-23
lines changed

core/src/main/java/org/jruby/truffle/nodes/core/FixnumNodes.java

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.oracle.truffle.api.nodes.UnexpectedResultException;
1717
import com.oracle.truffle.api.source.SourceSection;
1818
import com.oracle.truffle.api.utilities.BranchProfile;
19+
import com.oracle.truffle.api.utilities.ConditionProfile;
1920
import org.jruby.truffle.runtime.RubyContext;
2021
import org.jruby.truffle.runtime.core.RubyArray;
2122
import org.jruby.truffle.runtime.core.RubyBignum;
@@ -1207,49 +1208,59 @@ public long rightShift(long a, int b) {
12071208

12081209
}
12091210

1210-
@CoreMethod(names = "[]", required = 1)
1211-
public abstract static class GetIndexNode extends CoreMethodNode {
1211+
@CoreMethod(names = "abs")
1212+
public abstract static class AbsNode extends CoreMethodNode {
12121213

1213-
public GetIndexNode(RubyContext context, SourceSection sourceSection) {
1214+
public AbsNode(RubyContext context, SourceSection sourceSection) {
12141215
super(context, sourceSection);
12151216
}
12161217

1217-
public GetIndexNode(GetIndexNode prev) {
1218+
public AbsNode(AbsNode prev) {
12181219
super(prev);
12191220
}
12201221

12211222
@Specialization
1222-
public int getIndex(int self, int index) {
1223-
notDesignedForCompilation();
1223+
public int abs(int n) {
1224+
return Math.abs(n);
1225+
}
12241226

1225-
if ((self & (1 << index)) == 0) {
1226-
return 0;
1227-
} else {
1228-
return 1;
1229-
}
1227+
@Specialization
1228+
public long abs(long n) {
1229+
return Math.abs(n);
12301230
}
12311231

12321232
}
12331233

1234-
@CoreMethod(names = "abs")
1235-
public abstract static class AbsNode extends CoreMethodNode {
1234+
@CoreMethod(names = "bit_length")
1235+
public abstract static class BitLengthNode extends CoreMethodNode {
12361236

1237-
public AbsNode(RubyContext context, SourceSection sourceSection) {
1237+
private static final int INTEGER_BITS = Integer.numberOfLeadingZeros(0);
1238+
private static final int LONG_BITS = Long.numberOfLeadingZeros(0);
1239+
1240+
public BitLengthNode(RubyContext context, SourceSection sourceSection) {
12381241
super(context, sourceSection);
12391242
}
12401243

1241-
public AbsNode(AbsNode prev) {
1244+
public BitLengthNode(BitLengthNode prev) {
12421245
super(prev);
12431246
}
12441247

12451248
@Specialization
1246-
public int abs(int n) {
1247-
return Math.abs(n);
1249+
public int bitLength(int n) {
1250+
return bitLength((long) n);
12481251
}
12491252

12501253
@Specialization
1251-
public long abs(long n) {
1252-
return Math.abs(n);
1254+
public int bitLength(long n) {
1255+
if (n < 0) {
1256+
n = ~n;
1257+
}
1258+
1259+
if (n == Long.MAX_VALUE) {
1260+
return LONG_BITS - 1;
1261+
}
1262+
1263+
return LONG_BITS - Long.numberOfLeadingZeros(n);
12531264
}
12541265

12551266
}

core/src/main/ruby/jruby/truffle/core/rubinius/kernel/common/integer.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,10 @@ def to_r
4444
Rational(self, 1)
4545
end
4646

47+
def [](index)
48+
index = Rubinius::Type.coerce_to(index, Integer, :to_int)
49+
return 0 if index.is_a?(Bignum)
50+
index < 0 ? 0 : (self >> index) & 1
51+
end
52+
4753
end

spec/truffle/tags/core/fixnum/bit_and_tags.txt

Lines changed: 0 additions & 2 deletions
This file was deleted.

spec/truffle/tags/core/fixnum/bit_length_tags.txt

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)