Skip to content

Commit

Permalink
Rollup merge of #66410 - RalfJung:miri-machine-max, r=oli-obk
Browse files Browse the repository at this point in the history
miri: helper methods for max values of machine's usize/isize

We recently wanted this in Miri.

r? @oli-obk
  • Loading branch information
tmandry committed Nov 14, 2019
2 parents 853af77 + 6c9ba97 commit e3bbc0d
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/librustc/mir/interpret/pointer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::fmt::{self, Display};
use std::convert::TryFrom;

use crate::mir;
use crate::ty::layout::{self, HasDataLayout, Size};
Expand Down Expand Up @@ -40,6 +41,18 @@ pub trait PointerArithmetic: layout::HasDataLayout {
self.data_layout().pointer_size
}

#[inline]
fn usize_max(&self) -> u64 {
let max_usize_plus_1 = 1u128 << self.pointer_size().bits();
u64::try_from(max_usize_plus_1-1).unwrap()
}

#[inline]
fn isize_max(&self) -> i64 {
let max_isize_plus_1 = 1u128 << (self.pointer_size().bits()-1);
i64::try_from(max_isize_plus_1-1).unwrap()
}

/// Helper function: truncate given value-"overflowed flag" pair to pointer size and
/// update "overflowed flag" if there was an overflow.
/// This should be called by all the other methods before returning!
Expand Down

0 comments on commit e3bbc0d

Please sign in to comment.