Skip to content

Instantly share code, notes, and snippets.

@giuseppe998e
Last active January 15, 2026 13:15
Show Gist options
  • Select an option

  • Save giuseppe998e/8be830738be772f5a8c0b1feacb9301c to your computer and use it in GitHub Desktop.

Select an option

Save giuseppe998e/8be830738be772f5a8c0b1feacb9301c to your computer and use it in GitHub Desktop.
Fast (scalar) u64 parse function from ASCII byte slice
// SPDX-License-Identifier: MIT OR Apache-2.0
//
// Copyright (c) 2026 Giuseppe Eletto <giuseppe@eletto.me>
//
// This source code is licensed under either the MIT license or the Apache
// License, Version 2.0, at your option. You may choose either license.
//
//
// MIT License
// -----------
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
//
// Apache License 2.0
// ------------------
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/// Parses a byte slice into a `u64`.
///
/// Returns `Some(u64)` if the entire slice consists of ASCII digits and the resulting
/// value fits within a 64-bit unsigned integer. Returns `None` if the slice is empty,
/// contains non-digit characters, or would overflow `u64`.
///
/// # Performance
///
/// This function uses a fast path for slices with 1 to 19 digits. Since $2^{64} - 1$
/// is a 20-digit number (`18,446,744,073,709,551,615`), any sequence of 19 digits
/// or fewer is guaranteed to fit in a `u64`, allowing for optimized wrapping arithmetic.
pub fn atou64(buf: &[u8]) -> Option<u64> {
/// Handles the 20-digit case where overflow is possible.
///
/// This is marked `#[cold]` to keep it out of the primary instruction cache
/// path, as most inputs are expected to be shorter than 20 digits.
#[cold]
fn slow_path(buf: &[u8]) -> Option<u64> {
let mut val = 0u64;
for &b in buf {
let d = match b.wrapping_sub(b'0').into() {
d @ 0..10 => d,
_ => return None,
};
val = val.checked_mul(10).and_then(|val| val.checked_add(d))?;
}
Some(val)
}
// fn atou64(..)
match buf.len() {
1..20 => {
// fast path (inline)
let mut val = 0u64;
for &b in buf {
let d = match b.wrapping_sub(b'0').into() {
d @ 0..10 => d,
_ => return None,
};
val = val.wrapping_mul(10).wrapping_add(d);
}
Some(val)
}
20 => slow_path(buf),
_ => None,
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment