Skip to content

Commit

Permalink
Showing 2 changed files with 53 additions and 3 deletions.
40 changes: 40 additions & 0 deletions spec/truffle/specs/truffle/fixnum/and_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright (c) 2015 Oracle and/or its affiliates. All rights reserved. This
# code is released under a tri EPL/GPL/LGPL license. You can use it,
# redistribute it and/or modify it under the terms of the:
#
# Eclipse Public License version 1.0
# GNU General Public License version 2
# GNU Lesser General Public License version 2.1

require_relative '../../../../ruby/spec_helper'

describe "Fixnum#&" do
before :each do
@long = (1 << 48) + 1
@mask = (1 << 30) - 1
end

it "returns an int for (int, int)" do
result = (1 & 3)
result.should == 1
Truffle::Runtime.java_class_of(result).should == 'Integer'
end

it "returns an int for (long, int)" do
Truffle::Runtime.java_class_of(@long).should == 'Long'
Truffle::Runtime.java_class_of(@mask).should == 'Integer'

result = (@long & @mask)
result.should == 1
Truffle::Runtime.java_class_of(result).should == 'Integer'
end

it "returns an int for (int, long)" do
Truffle::Runtime.java_class_of(@long).should == 'Long'
Truffle::Runtime.java_class_of(@mask).should == 'Integer'

result = (@mask & @long)
result.should == 1
Truffle::Runtime.java_class_of(result).should == 'Integer'
end
end
Original file line number Diff line number Diff line change
@@ -756,17 +756,27 @@ public BitAndNode(RubyContext context, SourceSection sourceSection) {
}

@Specialization
public int bitAnd(int a, int b) {
public int bitAndIntInt(int a, int b) {
return a & b;
}

@Specialization
public long bitAnd(long a, long b) {
public int bitAndIntLong(int a, long b) {
return (int) (a & b);
}

@Specialization
public int bitAndLongInt(long a, int b) {
return (int) (a & b);
}

@Specialization(contains = { "bitAndIntInt", "bitAndIntLong", "bitAndLongInt" })
public long bitAndLongLong(long a, long b) {
return a & b;
}

@Specialization(guards = "isRubyBignum(b)")
public Object bitAnd(long a, DynamicObject b) {
public Object bitAndBignum(long a, DynamicObject b) {
return fixnumOrBignum(BigInteger.valueOf(a).and(Layouts.BIGNUM.getValue(b)));
}
}

0 comments on commit 7457dde

Please sign in to comment.