Skip to content

Made the logical shift / artihmetic shift not rely on signs of intigers #749

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ use crate::type_of::LayoutGccExt;

// TODO(antoyo)
type Funclet = ();

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ShiftKind {
Logical,
Arithmetic,
}
enum ExtremumOperation {
Max,
Min,
Expand Down Expand Up @@ -827,13 +831,13 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
}

fn lshr(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
self.gcc_lshr(a, b)
self.gcc_shr(a, b, ShiftKind::Logical)
}

fn ashr(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
// TODO(antoyo): check whether behavior is an arithmetic shift for >> .
// It seems to be if the value is signed.
self.gcc_lshr(a, b)
self.gcc_shr(a, b, ShiftKind::Arithmetic)
}

fn and(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
Expand Down
70 changes: 48 additions & 22 deletions src/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use rustc_middle::ty::{self, Ty};
use rustc_target::callconv::{ArgAbi, ArgAttributes, FnAbi, PassMode};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be easy to add a test for this fix?

use rustc_type_ir::{Interner, TyKind};

use crate::builder::{Builder, ToGccComp};
use crate::builder::{Builder, ShiftKind, ToGccComp};
use crate::common::{SignType, TypeReflection};
use crate::context::CodegenCx;

Expand Down Expand Up @@ -68,36 +68,59 @@ impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
self.cx.bitwise_operation(BinaryOp::BitwiseAnd, a, b, self.location)
}

pub fn gcc_lshr(&mut self, a: RValue<'gcc>, b: RValue<'gcc>) -> RValue<'gcc> {
pub fn gcc_shr(
&mut self,
a: RValue<'gcc>,
b: RValue<'gcc>,
shift_kind: ShiftKind,
) -> RValue<'gcc> {
let a_type = a.get_type();
let b_type = b.get_type();
let a_native = self.is_native_int_type(a_type);
let b_native = self.is_native_int_type(b_type);
if a_native && b_native {
// FIXME(antoyo): remove the casts when libgccjit can shift an unsigned number by a signed number.
// TODO(antoyo): cast to unsigned to do a logical shift if that does not work.
if a_type.is_signed(self) != b_type.is_signed(self) {
let b = self.context.new_cast(self.location, b, a_type);
a >> b
} else {
let a_size = a_type.get_size();
let b_size = b_type.get_size();
match a_size.cmp(&b_size) {
std::cmp::Ordering::Less => {
let a = self.context.new_cast(self.location, a, b_type);
a >> b
}
std::cmp::Ordering::Equal => a >> b,
std::cmp::Ordering::Greater => {
let b = self.context.new_cast(self.location, b, a_type);
a >> b
}
let (a, a_type) = match (shift_kind, a_type.is_signed(self.cx)) {
(ShiftKind::Logical, true) => {
let a_type = a_type.to_unsigned(self.cx);
(self.gcc_int_cast(a, a_type), a_type)
}
(ShiftKind::Logical, false) => (a, a_type),
(ShiftKind::Arithmetic, true) => (a, a_type),
(ShiftKind::Arithmetic, false) => {
let a_type = a_type.to_signed(self.cx);
(self.gcc_int_cast(a, a_type), a_type)
}
};
let (b, b_type) = match (shift_kind, b_type.is_signed(self.cx)) {
(ShiftKind::Logical, true) => {
let b_type = b_type.to_unsigned(self.cx);
(self.gcc_int_cast(b, b_type), b_type)
}
(ShiftKind::Logical, false) => (b, b_type),
(ShiftKind::Arithmetic, true) => (b, b_type),
(ShiftKind::Arithmetic, false) => {
let b_type = b_type.to_signed(self.cx);
(self.gcc_int_cast(b, b_type), b_type)
}
};
Comment on lines -86 to +105
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add a comment to explain that this casts to unsigned if needed to do a logical shift and to signed if needed to do an arithmetic shift?


let a_size = a_type.get_size();
let b_size = b_type.get_size();
match a_size.cmp(&b_size) {
std::cmp::Ordering::Less => {
let a = self.context.new_cast(self.location, a, b_type);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a debug_assert!() that checks that the sign of a and b is the same here?

a >> b
}
std::cmp::Ordering::Equal => a >> b,
std::cmp::Ordering::Greater => {
let b = self.context.new_cast(self.location, b, a_type);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here as well.

a >> b
}
}
} else if a_type.is_vector() && b_type.is_vector() {
a >> b
} else if a_native && !b_native {
self.gcc_lshr(a, self.gcc_int_cast(b, a_type))
self.gcc_shr(a, self.gcc_int_cast(b, a_type), shift_kind)
} else {
// NOTE: we cannot use the lshr builtin because it's calling hi() (to get the most
// significant half of the number) which uses lshr.
Expand All @@ -122,7 +145,10 @@ impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {

let shift_value = self.gcc_sub(b, sixty_four);
let high = self.high(a);
let sign = if a_type.is_signed(self) { high >> sixty_three } else { zero };
let sign = match shift_kind {
ShiftKind::Logical => zero,
ShiftKind::Arithmetic => high >> sixty_three,
};
let array_value = self.concat_low_high_rvalues(a_type, high >> shift_value, sign);
then_block.add_assignment(self.location, result, array_value);
then_block.end_with_jump(self.location, after_block);
Expand Down
14 changes: 7 additions & 7 deletions src/intrinsic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
128 => {
// TODO(antoyo): find a more efficient implementation?
let sixty_four = self.gcc_int(typ, 64);
let right_shift = self.gcc_lshr(value, sixty_four);
let right_shift = self.lshr(value, sixty_four);
let high = self.gcc_int_cast(right_shift, self.u64_type);
let low = self.gcc_int_cast(value, self.u64_type);

Expand Down Expand Up @@ -998,7 +998,7 @@ impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
.new_local(None, array_type, "count_loading_zeroes_results");

let sixty_four = self.gcc_int(arg_type, 64);
let shift = self.gcc_lshr(arg, sixty_four);
let shift = self.lshr(arg, sixty_four);
let high = self.gcc_int_cast(shift, self.u64_type);
let low = self.gcc_int_cast(arg, self.u64_type);

Expand Down Expand Up @@ -1073,7 +1073,7 @@ impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
// TODO(antoyo): remove this if/when native 128-bit integers land in libgccjit
if value_type.is_u128(self.cx) && !self.cx.supports_128bit_integers {
let sixty_four = self.gcc_int(value_type, 64);
let right_shift = self.gcc_lshr(value, sixty_four);
let right_shift = self.lshr(value, sixty_four);
let high = self.gcc_int_cast(right_shift, self.cx.ulonglong_type);
let high = self.pop_count(high);
let low = self.gcc_int_cast(value, self.cx.ulonglong_type);
Expand Down Expand Up @@ -1196,12 +1196,12 @@ impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
// Return `result_type`'s maximum or minimum value on overflow
// NOTE: convert the type to unsigned to have an unsigned shift.
let unsigned_type = result_type.to_unsigned(self.cx);
let shifted = self.gcc_lshr(
let shifted = self.lshr(
self.gcc_int_cast(lhs, unsigned_type),
self.gcc_int(unsigned_type, width as i64 - 1),
);
let uint_max = self.gcc_not(self.gcc_int(unsigned_type, 0));
let int_max = self.gcc_lshr(uint_max, self.gcc_int(unsigned_type, 1));
let int_max = self.lshr(uint_max, self.gcc_int(unsigned_type, 1));
then_block.add_assignment(
self.location,
res,
Expand Down Expand Up @@ -1267,12 +1267,12 @@ impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {
// Return `result_type`'s maximum or minimum value on overflow
// NOTE: convert the type to unsigned to have an unsigned shift.
let unsigned_type = result_type.to_unsigned(self.cx);
let shifted = self.gcc_lshr(
let shifted = self.lshr(
self.gcc_int_cast(lhs, unsigned_type),
self.gcc_int(unsigned_type, width as i64 - 1),
);
let uint_max = self.gcc_not(self.gcc_int(unsigned_type, 0));
let int_max = self.gcc_lshr(uint_max, self.gcc_int(unsigned_type, 1));
let int_max = self.lshr(uint_max, self.gcc_int(unsigned_type, 1));
then_block.add_assignment(
self.location,
res,
Expand Down