Skip to content

Commit 9de6008

Browse files
committed
make bit_width return u64, consistently with other sizes in the compiler
1 parent 1ddbdc6 commit 9de6008

File tree

3 files changed

+25
-28
lines changed

3 files changed

+25
-28
lines changed

src/librustc_ast/ast.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,7 +1614,7 @@ impl FloatTy {
16141614
}
16151615
}
16161616

1617-
pub fn bit_width(self) -> usize {
1617+
pub fn bit_width(self) -> u64 {
16181618
match self {
16191619
FloatTy::F32 => 32,
16201620
FloatTy::F64 => 64,
@@ -1663,7 +1663,7 @@ impl IntTy {
16631663
format!("{}{}", val as u128, self.name_str())
16641664
}
16651665

1666-
pub fn bit_width(&self) -> Option<usize> {
1666+
pub fn bit_width(&self) -> Option<u64> {
16671667
Some(match *self {
16681668
IntTy::Isize => return None,
16691669
IntTy::I8 => 8,
@@ -1725,7 +1725,7 @@ impl UintTy {
17251725
format!("{}{}", val, self.name_str())
17261726
}
17271727

1728-
pub fn bit_width(&self) -> Option<usize> {
1728+
pub fn bit_width(&self) -> Option<u64> {
17291729
Some(match *self {
17301730
UintTy::Usize => return None,
17311731
UintTy::U8 => 8,

src/librustc_codegen_llvm/intrinsic.rs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,8 +1172,8 @@ fn generic_simd_intrinsic(
11721172
let m_len = match in_ty.kind {
11731173
// Note that this `.unwrap()` crashes for isize/usize, that's sort
11741174
// of intentional as there's not currently a use case for that.
1175-
ty::Int(i) => i.bit_width().unwrap() as u64,
1176-
ty::Uint(i) => i.bit_width().unwrap() as u64,
1175+
ty::Int(i) => i.bit_width().unwrap(),
1176+
ty::Uint(i) => i.bit_width().unwrap(),
11771177
_ => return_error!("`{}` is not an integral type", in_ty),
11781178
};
11791179
require_simd!(arg_tys[1], "argument");
@@ -1354,20 +1354,18 @@ fn generic_simd_intrinsic(
13541354
// trailing bits.
13551355
let expected_int_bits = in_len.max(8);
13561356
match ret_ty.kind {
1357-
ty::Uint(i) if i.bit_width() == Some(expected_int_bits as usize) => (),
1357+
ty::Uint(i) if i.bit_width() == Some(expected_int_bits) => (),
13581358
_ => return_error!("bitmask `{}`, expected `u{}`", ret_ty, expected_int_bits),
13591359
}
13601360

13611361
// Integer vector <i{in_bitwidth} x in_len>:
13621362
let (i_xn, in_elem_bitwidth) = match in_elem.kind {
1363-
ty::Int(i) => (
1364-
args[0].immediate(),
1365-
i.bit_width().unwrap_or(bx.data_layout().pointer_size.bits() as _),
1366-
),
1367-
ty::Uint(i) => (
1368-
args[0].immediate(),
1369-
i.bit_width().unwrap_or(bx.data_layout().pointer_size.bits() as _),
1370-
),
1363+
ty::Int(i) => {
1364+
(args[0].immediate(), i.bit_width().unwrap_or(bx.data_layout().pointer_size.bits()))
1365+
}
1366+
ty::Uint(i) => {
1367+
(args[0].immediate(), i.bit_width().unwrap_or(bx.data_layout().pointer_size.bits()))
1368+
}
13711369
_ => return_error!(
13721370
"vector argument `{}`'s element type `{}`, expected integer element type",
13731371
in_ty,
@@ -1378,16 +1376,16 @@ fn generic_simd_intrinsic(
13781376
// Shift the MSB to the right by "in_elem_bitwidth - 1" into the first bit position.
13791377
let shift_indices =
13801378
vec![
1381-
bx.cx.const_int(bx.type_ix(in_elem_bitwidth as _), (in_elem_bitwidth - 1) as _);
1379+
bx.cx.const_int(bx.type_ix(in_elem_bitwidth), (in_elem_bitwidth - 1) as _);
13821380
in_len as _
13831381
];
13841382
let i_xn_msb = bx.lshr(i_xn, bx.const_vector(shift_indices.as_slice()));
13851383
// Truncate vector to an <i1 x N>
1386-
let i1xn = bx.trunc(i_xn_msb, bx.type_vector(bx.type_i1(), in_len as _));
1384+
let i1xn = bx.trunc(i_xn_msb, bx.type_vector(bx.type_i1(), in_len));
13871385
// Bitcast <i1 x N> to iN:
1388-
let i_ = bx.bitcast(i1xn, bx.type_ix(in_len as _));
1386+
let i_ = bx.bitcast(i1xn, bx.type_ix(in_len));
13891387
// Zero-extend iN to the bitmask type:
1390-
return Ok(bx.zext(i_, bx.type_ix(expected_int_bits as _)));
1388+
return Ok(bx.zext(i_, bx.type_ix(expected_int_bits)));
13911389
}
13921390

13931391
fn simd_simple_float_intrinsic(
@@ -2099,7 +2097,7 @@ fn int_type_width_signed(ty: Ty<'_>, cx: &CodegenCx<'_, '_>) -> Option<(u64, boo
20992097
match ty.kind {
21002098
ty::Int(t) => Some((
21012099
match t {
2102-
ast::IntTy::Isize => cx.tcx.sess.target.ptr_width as u64,
2100+
ast::IntTy::Isize => u64::from(cx.tcx.sess.target.ptr_width),
21032101
ast::IntTy::I8 => 8,
21042102
ast::IntTy::I16 => 16,
21052103
ast::IntTy::I32 => 32,
@@ -2110,7 +2108,7 @@ fn int_type_width_signed(ty: Ty<'_>, cx: &CodegenCx<'_, '_>) -> Option<(u64, boo
21102108
)),
21112109
ty::Uint(t) => Some((
21122110
match t {
2113-
ast::UintTy::Usize => cx.tcx.sess.target.ptr_width as u64,
2111+
ast::UintTy::Usize => u64::from(cx.tcx.sess.target.ptr_width),
21142112
ast::UintTy::U8 => 8,
21152113
ast::UintTy::U16 => 16,
21162114
ast::UintTy::U32 => 32,
@@ -2127,7 +2125,7 @@ fn int_type_width_signed(ty: Ty<'_>, cx: &CodegenCx<'_, '_>) -> Option<(u64, boo
21272125
// Returns None if the type is not a float
21282126
fn float_type_width(ty: Ty<'_>) -> Option<u64> {
21292127
match ty.kind {
2130-
ty::Float(t) => Some(t.bit_width() as u64),
2128+
ty::Float(t) => Some(t.bit_width()),
21312129
_ => None,
21322130
}
21332131
}

src/librustc_mir/interpret/cast.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -228,17 +228,16 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
228228
match dest_ty.kind {
229229
// float -> uint
230230
Uint(t) => {
231-
// FIXME: can we make `bit_width` return a type more compatible with `Size::bits`?
232-
let width = t.bit_width().unwrap_or_else(|| self.pointer_size().bits() as usize);
233-
let v = f.to_u128(width).value;
231+
let width = t.bit_width().unwrap_or_else(|| self.pointer_size().bits());
232+
let v = f.to_u128(usize::try_from(width).unwrap()).value;
234233
// This should already fit the bit width
235-
Ok(Scalar::from_uint(v, Size::from_bits(width as u64)))
234+
Ok(Scalar::from_uint(v, Size::from_bits(width)))
236235
}
237236
// float -> int
238237
Int(t) => {
239-
let width = t.bit_width().unwrap_or_else(|| self.pointer_size().bits() as usize);
240-
let v = f.to_i128(width).value;
241-
Ok(Scalar::from_int(v, Size::from_bits(width as u64)))
238+
let width = t.bit_width().unwrap_or_else(|| self.pointer_size().bits());
239+
let v = f.to_i128(usize::try_from(width).unwrap()).value;
240+
Ok(Scalar::from_int(v, Size::from_bits(width)))
242241
}
243242
// float -> f32
244243
Float(FloatTy::F32) => Ok(Scalar::from_f32(f.convert(&mut false).value)),

0 commit comments

Comments
 (0)