diff options
| author | bors <bors@rust-lang.org> | 2015-02-27 23:15:17 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2015-02-27 23:15:17 +0000 |
| commit | e233987ce1de88a48db2ce612019ba644d3cf5dd (patch) | |
| tree | f776ae018199c9ff2350a29dc569630f64455f87 /src/libstd/sys | |
| parent | bd0d8e47e53f25bbd50418a0f117973c366c1b08 (diff) | |
| parent | bde4c1d6fbefcd914a06b5eab6ef6f9a6f26f271 (diff) | |
| download | rust-e233987ce1de88a48db2ce612019ba644d3cf5dd.tar.gz rust-e233987ce1de88a48db2ce612019ba644d3cf5dd.zip | |
Auto merge of #22860 - Manishearth:rollup, r=alexcrichton
Passes check-stage1, check-stage2
Diffstat (limited to 'src/libstd/sys')
| -rw-r--r-- | src/libstd/sys/unix/fs.rs | 15 | ||||
| -rw-r--r-- | src/libstd/sys/unix/process2.rs | 1 | ||||
| -rw-r--r-- | src/libstd/sys/windows/time.rs | 27 |
3 files changed, 26 insertions, 17 deletions
diff --git a/src/libstd/sys/unix/fs.rs b/src/libstd/sys/unix/fs.rs index 1c28d629d40..71b6214460f 100644 --- a/src/libstd/sys/unix/fs.rs +++ b/src/libstd/sys/unix/fs.rs @@ -291,29 +291,14 @@ fn mkstat(stat: &libc::stat) -> FileStat { // FileStat times are in milliseconds fn mktime(secs: u64, nsecs: u64) -> u64 { secs * 1000 + nsecs / 1000000 } - #[cfg(target_os = "bitrig")] - fn ctime(stat: &libc::stat) -> u64 { - mktime(stat.st_ctim.tv_sec as u64, stat.st_ctim.tv_nsec as u64) - } - #[cfg(not(target_os = "bitrig"))] fn ctime(stat: &libc::stat) -> u64 { mktime(stat.st_ctime as u64, stat.st_ctime_nsec as u64) } - #[cfg(target_os = "bitrig")] - fn atime(stat: &libc::stat) -> u64 { - mktime(stat.st_atim.tv_sec as u64, stat.st_atim.tv_nsec as u64) - } - #[cfg(not(target_os = "bitrig"))] fn atime(stat: &libc::stat) -> u64 { mktime(stat.st_atime as u64, stat.st_atime_nsec as u64) } - #[cfg(target_os = "bitrig")] - fn mtime(stat: &libc::stat) -> u64 { - mktime(stat.st_mtim.tv_sec as u64, stat.st_mtim.tv_nsec as u64) - } - #[cfg(not(target_os = "bitrig"))] fn mtime(stat: &libc::stat) -> u64 { mktime(stat.st_mtime as u64, stat.st_mtime_nsec as u64) } diff --git a/src/libstd/sys/unix/process2.rs b/src/libstd/sys/unix/process2.rs index b7a1b002f55..a7d0a864a08 100644 --- a/src/libstd/sys/unix/process2.rs +++ b/src/libstd/sys/unix/process2.rs @@ -439,6 +439,7 @@ fn translate_status(status: c_int) -> ExitStatus { target_os = "ios", target_os = "freebsd", target_os = "dragonfly", + target_os = "bitrig", target_os = "openbsd"))] mod imp { pub fn WIFEXITED(status: i32) -> bool { (status & 0x7f) == 0 } diff --git a/src/libstd/sys/windows/time.rs b/src/libstd/sys/windows/time.rs index 20ceff0aa69..209460df10b 100644 --- a/src/libstd/sys/windows/time.rs +++ b/src/libstd/sys/windows/time.rs @@ -12,6 +12,8 @@ use ops::Sub; use time::Duration; use sync::{Once, ONCE_INIT}; +const NANOS_PER_SEC: i64 = 1_000_000_000; + pub struct SteadyTime { t: libc::LARGE_INTEGER, } @@ -24,7 +26,7 @@ impl SteadyTime { } pub fn ns(&self) -> u64 { - self.t as u64 * 1_000_000_000 / frequency() as u64 + mul_div_i64(self.t as i64, NANOS_PER_SEC, frequency() as i64) as u64 } } @@ -45,6 +47,27 @@ impl<'a> Sub for &'a SteadyTime { fn sub(self, other: &SteadyTime) -> Duration { let diff = self.t as i64 - other.t as i64; - Duration::microseconds(diff * 1_000_000 / frequency() as i64) + Duration::nanoseconds(mul_div_i64(diff, NANOS_PER_SEC, frequency() as i64)) } } + +// Computes (value*numer)/denom without overflow, as long as both +// (numer*denom) and the overall result fit into i64 (which is the case +// for our time conversions). +fn mul_div_i64(value: i64, numer: i64, denom: i64) -> i64 { + let q = value / denom; + let r = value % denom; + // Decompose value as (value/denom*denom + value%denom), + // substitute into (value*numer)/denom and simplify. + // r < denom, so (denom*numer) is the upper bound of (r*numer) + q * numer + r * numer / denom +} + +#[test] +fn test_muldiv() { + assert_eq!(mul_div_i64( 1_000_000_000_001, 1_000_000_000, 1_000_000), 1_000_000_000_001_000); + assert_eq!(mul_div_i64(-1_000_000_000_001, 1_000_000_000, 1_000_000), -1_000_000_000_001_000); + assert_eq!(mul_div_i64(-1_000_000_000_001,-1_000_000_000, 1_000_000), 1_000_000_000_001_000); + assert_eq!(mul_div_i64( 1_000_000_000_001, 1_000_000_000,-1_000_000), -1_000_000_000_001_000); + assert_eq!(mul_div_i64( 1_000_000_000_001,-1_000_000_000,-1_000_000), 1_000_000_000_001_000); +} |
