|
16 | 16 | import com.oracle.truffle.api.nodes.UnexpectedResultException;
|
17 | 17 | import com.oracle.truffle.api.source.SourceSection;
|
18 | 18 | import com.oracle.truffle.api.utilities.BranchProfile;
|
| 19 | +import com.oracle.truffle.api.utilities.ConditionProfile; |
19 | 20 | import org.jruby.truffle.runtime.RubyContext;
|
20 | 21 | import org.jruby.truffle.runtime.core.RubyArray;
|
21 | 22 | import org.jruby.truffle.runtime.core.RubyBignum;
|
@@ -1207,49 +1208,59 @@ public long rightShift(long a, int b) {
|
1207 | 1208 |
|
1208 | 1209 | }
|
1209 | 1210 |
|
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 { |
1212 | 1213 |
|
1213 |
| - public GetIndexNode(RubyContext context, SourceSection sourceSection) { |
| 1214 | + public AbsNode(RubyContext context, SourceSection sourceSection) { |
1214 | 1215 | super(context, sourceSection);
|
1215 | 1216 | }
|
1216 | 1217 |
|
1217 |
| - public GetIndexNode(GetIndexNode prev) { |
| 1218 | + public AbsNode(AbsNode prev) { |
1218 | 1219 | super(prev);
|
1219 | 1220 | }
|
1220 | 1221 |
|
1221 | 1222 | @Specialization
|
1222 |
| - public int getIndex(int self, int index) { |
1223 |
| - notDesignedForCompilation(); |
| 1223 | + public int abs(int n) { |
| 1224 | + return Math.abs(n); |
| 1225 | + } |
1224 | 1226 |
|
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); |
1230 | 1230 | }
|
1231 | 1231 |
|
1232 | 1232 | }
|
1233 | 1233 |
|
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 { |
1236 | 1236 |
|
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) { |
1238 | 1241 | super(context, sourceSection);
|
1239 | 1242 | }
|
1240 | 1243 |
|
1241 |
| - public AbsNode(AbsNode prev) { |
| 1244 | + public BitLengthNode(BitLengthNode prev) { |
1242 | 1245 | super(prev);
|
1243 | 1246 | }
|
1244 | 1247 |
|
1245 | 1248 | @Specialization
|
1246 |
| - public int abs(int n) { |
1247 |
| - return Math.abs(n); |
| 1249 | + public int bitLength(int n) { |
| 1250 | + return bitLength((long) n); |
1248 | 1251 | }
|
1249 | 1252 |
|
1250 | 1253 | @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); |
1253 | 1264 | }
|
1254 | 1265 |
|
1255 | 1266 | }
|
|
0 commit comments