-
Notifications
You must be signed in to change notification settings - Fork 543
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Inline a handful of functions from num-integer
The motivation for this is that we use a very small portion of what num-integer provides, but it's a relatively heavy dependency -- it brings in several crates with build scripts. The goal here is to reduce compilation time for projects using chrono.
- Loading branch information
Showing
10 changed files
with
93 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// All code cribbed from num-integer. We do this to minimize dependencies and | ||
// build-time. | ||
|
||
pub(crate) trait Integer: Copy { | ||
fn div_rem(self, other: Self) -> (Self, Self); | ||
fn div_floor(self, other: Self) -> Self; | ||
fn mod_floor(self, other: Self) -> Self; | ||
} | ||
|
||
macro_rules! impl_integer_signed { | ||
($t: ty) => { | ||
impl Integer for $t { | ||
#[inline] | ||
fn div_rem(self, other: Self) -> (Self, Self) { | ||
(self / other, self % other) | ||
} | ||
|
||
#[inline] | ||
fn div_floor(self, other: Self) -> Self { | ||
// Algorithm from [Daan Leijen. _Division and Modulus for Computer Scientists_, | ||
// December 2001](http://research.microsoft.com/pubs/151917/divmodnote-letter.pdf) | ||
let (d, r) = self.div_rem(other); | ||
if (r > 0 && other < 0) || (r < 0 && other > 0) { | ||
d - 1 | ||
} else { | ||
d | ||
} | ||
} | ||
|
||
#[inline] | ||
fn mod_floor(self, other: Self) -> Self { | ||
// Algorithm from [Daan Leijen. _Division and Modulus for Computer Scientists_, | ||
// December 2001](http://research.microsoft.com/pubs/151917/divmodnote-letter.pdf) | ||
let r = self % other; | ||
if (r > 0 && other < 0) || (r < 0 && other > 0) { | ||
r + other | ||
} else { | ||
r | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
|
||
macro_rules! impl_integer_unsigned { | ||
($t: ty) => { | ||
impl Integer for $t { | ||
#[inline] | ||
fn div_rem(self, other: Self) -> (Self, Self) { | ||
(self / other, self % other) | ||
} | ||
|
||
#[inline] | ||
fn div_floor(self, other: Self) -> Self { | ||
self / other | ||
} | ||
|
||
#[inline] | ||
fn mod_floor(self, other: Self) -> Self { | ||
self % other | ||
} | ||
} | ||
}; | ||
} | ||
|
||
impl_integer_signed!(i32); | ||
impl_integer_signed!(i64); | ||
impl_integer_unsigned!(u32); | ||
|
||
pub(crate) fn div_mod_floor<T: Integer>(x: T, y: T) -> (T, T) { | ||
(div_floor(x, y), mod_floor(x, y)) | ||
} | ||
|
||
pub(crate) fn div_rem<T: Integer>(x: T, y: T) -> (T, T) { | ||
x.div_rem(y) | ||
} | ||
|
||
pub(crate) fn div_floor<T: Integer>(x: T, y: T) -> T { | ||
x.div_floor(y) | ||
} | ||
|
||
pub(crate) fn mod_floor<T: Integer>(x: T, y: T) -> T { | ||
x.mod_floor(y) | ||
} |