summaryrefslogtreecommitdiffstats
path: root/src/common/mod.rs
diff options
context:
space:
mode:
authorPaul Masurel <paul.masurel@gmail.com>2017-05-04 16:25:18 +0800
committerPaul Masurel <paul.masurel@gmail.com>2017-05-04 16:46:14 +0800
commit5cb5c9a8f21aad4a3cc0a10462be49f3aa66e714 (patch)
tree539f63ba5fbe3d7f55930ef162daab68ff56ecf2 /src/common/mod.rs
parent9ab92b77394a589f65938f30dd5245dd15628df5 (diff)
issues/65 Added i64 fast fields
Diffstat (limited to 'src/common/mod.rs')
-rw-r--r--src/common/mod.rs15
1 files changed, 15 insertions, 0 deletions
diff --git a/src/common/mod.rs b/src/common/mod.rs
index 86a56a7..bc31b18 100644
--- a/src/common/mod.rs
+++ b/src/common/mod.rs
@@ -43,11 +43,26 @@ pub fn allocate_vec<T>(capacity: usize) -> Vec<T> {
const HIGHEST_BIT: u64 = 1 << 63;
+
+/// Maps `i64` to `u64` so that
+/// `-2^63 .. 2^63-1` is mapped
+/// to
+/// `0 .. 2^64`
+/// in that order.
+///
+/// This is more suited than simply casting (`val as u64`)
+/// because of bitpacking.
+///
+/// Imagine a list of `i64` ranging from -10 to 10.
+/// When casting negative values, the negative values are projected
+/// to values over 2^63, and all values end up requiring 64 bits.
#[inline(always)]
pub fn i64_to_u64(val: i64) -> u64 {
(val as u64) ^ HIGHEST_BIT
}
+/// Reverse the mapping given by
+/// `i64_to_u64`.
#[inline(always)]
pub fn u64_to_i64(val: u64) -> i64 {
(val ^ HIGHEST_BIT) as i64