Skip to content

Commit

Permalink
Merge pull request #48 from dtolnay/usize
Browse files Browse the repository at this point in the history
Convert to unsigned integer pointer offsets
  • Loading branch information
dtolnay authored Nov 20, 2024
2 parents b45df1f + c4b3742 commit 9b3e148
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ macro_rules! impl_Integer {
// Convert negative number to positive by summing 1 to its two's complement.
(!(self as $large_unsigned)).wrapping_add(1)
};
let mut curr = buf.len() as isize;
let mut curr = buf.len();
let buf_ptr = buf.as_mut_ptr() as *mut u8;
let lut_ptr = DEC_DIGITS_LUT.as_ptr();

Expand All @@ -168,8 +168,8 @@ macro_rules! impl_Integer {
let d2 = (rem % 100) << 1;
curr -= 4;
unsafe {
ptr::copy_nonoverlapping(lut_ptr.offset(d1), buf_ptr.offset(curr), 2);
ptr::copy_nonoverlapping(lut_ptr.offset(d2), buf_ptr.offset(curr + 2), 2);
ptr::copy_nonoverlapping(lut_ptr.offset(d1), buf_ptr.add(curr), 2);
ptr::copy_nonoverlapping(lut_ptr.offset(d2), buf_ptr.add(curr + 2), 2);
}
}
}
Expand All @@ -183,33 +183,33 @@ macro_rules! impl_Integer {
n /= 100;
curr -= 2;
unsafe {
ptr::copy_nonoverlapping(lut_ptr.offset(d1), buf_ptr.offset(curr), 2);
ptr::copy_nonoverlapping(lut_ptr.offset(d1), buf_ptr.add(curr), 2);
}
}

// Decode last 1 or 2 digits.
if n < 10 {
curr -= 1;
unsafe {
*buf_ptr.offset(curr) = (n as u8) + b'0';
*buf_ptr.add(curr) = (n as u8) + b'0';
}
} else {
let d1 = n << 1;
curr -= 2;
unsafe {
ptr::copy_nonoverlapping(lut_ptr.offset(d1), buf_ptr.offset(curr), 2);
ptr::copy_nonoverlapping(lut_ptr.offset(d1), buf_ptr.add(curr), 2);
}
}

if !is_nonnegative {
curr -= 1;
unsafe {
*buf_ptr.offset(curr) = b'-';
*buf_ptr.add(curr) = b'-';
}
}

let len = buf.len() - curr as usize;
let bytes = unsafe { slice::from_raw_parts(buf_ptr.offset(curr), len) };
let len = buf.len() - curr;
let bytes = unsafe { slice::from_raw_parts(buf_ptr.add(curr), len) };
unsafe { str::from_utf8_unchecked(bytes) }
}
}
Expand Down Expand Up @@ -266,53 +266,53 @@ macro_rules! impl_Integer128 {
// Convert negative number to positive by summing 1 to its two's complement.
(!(self as u128)).wrapping_add(1)
};
let mut curr = buf.len() as isize;
let mut curr = buf.len();
let buf_ptr = buf.as_mut_ptr() as *mut u8;

// Divide by 10^19 which is the highest power less than 2^64.
let (n, rem) = udiv128::udivmod_1e19(n);
let buf1 = unsafe { buf_ptr.offset(curr - U64_MAX_LEN as isize) as *mut [MaybeUninit<u8>; U64_MAX_LEN] };
curr -= rem.write(unsafe { &mut *buf1 }).len() as isize;
let buf1 = unsafe { buf_ptr.add(curr - U64_MAX_LEN) as *mut [MaybeUninit<u8>; U64_MAX_LEN] };
curr -= rem.write(unsafe { &mut *buf1 }).len();

if n != 0 {
// Memset the base10 leading zeros of rem.
let target = buf.len() as isize - 19;
let target = buf.len() - 19;
unsafe {
ptr::write_bytes(buf_ptr.offset(target), b'0', (curr - target) as usize);
ptr::write_bytes(buf_ptr.add(target), b'0', curr - target);
}
curr = target;

// Divide by 10^19 again.
let (n, rem) = udiv128::udivmod_1e19(n);
let buf2 = unsafe { buf_ptr.offset(curr - U64_MAX_LEN as isize) as *mut [MaybeUninit<u8>; U64_MAX_LEN] };
curr -= rem.write(unsafe { &mut *buf2 }).len() as isize;
let buf2 = unsafe { buf_ptr.add(curr - U64_MAX_LEN) as *mut [MaybeUninit<u8>; U64_MAX_LEN] };
curr -= rem.write(unsafe { &mut *buf2 }).len();

if n != 0 {
// Memset the leading zeros.
let target = buf.len() as isize - 38;
let target = buf.len() - 38;
unsafe {
ptr::write_bytes(buf_ptr.offset(target), b'0', (curr - target) as usize);
ptr::write_bytes(buf_ptr.add(target), b'0', curr - target);
}
curr = target;

// There is at most one digit left
// because u128::MAX / 10^19 / 10^19 is 3.
curr -= 1;
unsafe {
*buf_ptr.offset(curr) = (n as u8) + b'0';
*buf_ptr.add(curr) = (n as u8) + b'0';
}
}
}

if !is_nonnegative {
curr -= 1;
unsafe {
*buf_ptr.offset(curr) = b'-';
*buf_ptr.add(curr) = b'-';
}
}

let len = buf.len() - curr as usize;
let bytes = unsafe { slice::from_raw_parts(buf_ptr.offset(curr), len) };
let len = buf.len() - curr;
let bytes = unsafe { slice::from_raw_parts(buf_ptr.add(curr), len) };
unsafe { str::from_utf8_unchecked(bytes) }
}
}
Expand Down

0 comments on commit 9b3e148

Please sign in to comment.