<commit_before>// Translated from C to Rust. The original C code can be found at
// https://github.com/ulfjack/ryu and carries the following license:
//
// Copyright 2018 <NAME>
//
// The contents of this file may be used under the terms of the Apache License,
// Version 2.0.
//
//    (See accompanying file LICENSE-Apache or copy at
//     http://www.apache.org/licenses/LICENSE-2.0)
//
// Alternatively, the contents of this file may be used under the terms of
// the Boost Software License, Version 1.0.
//    (See accompanying file LICENSE-Boost or copy at
//     https://www.boost.org/LICENSE_1_0.txt)
//
// Unless required by applicable law or agreed to in writing, this software
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.

mod common;
mod d2s_full_table;
mod digit_table;
mod mulshift128;

use std::{mem, ptr};

use common::*;
use d2s_full_table::*;
use digit_table::*;
use mulshift128::*;

fn pow5_factor(mut value: u64) -> i32 {
    let mut count = 0i32;
    loop {
        if value == 0 {
            return 0;
        }
        if value % 5 != 0 {
            return count;
        }
        value /= 5;
        count += 1;
    }
}

// Returns true if value is divisible by 5^p.
fn multiple_of_power_of_5(value: u64, p: i32) -> bool {
    // I tried a case distinction on p, but there was no performance difference.
    pow5_factor(value) >= p
}

unsafe fn mul_shift_all(
    mut m: u64,
    mul: &[u64; 2],
    j: u32,
    vp: *mut u64,
    vm: *mut u64,
    mm_shift: u32,
) -> u64 {
    m <<= 1;
    // m is maximum 55 bits
    let (lo, tmp) = umul128(m, mul[0]);
    let (mut mid, mut hi) = umul128(m, mul[1]);
    mid = mid.wrapping_add(tmp);
    hi = hi.wrapping_add((mid < tmp) as u64); // overflow into hi

    let lo2 = lo.wrapping_add(mul[0]);
    let mid2 = mid.wrapping_add(mul[1]).wrapping_add((lo2 < lo) as u64);
    let hi2 = hi.wrapping_add((mid2 < mid) as u64);
    *vp = shiftright128(mid2, hi2, j - 64 - 1);

    if mm_shift == 1 {
        let lo3 = lo.wrapping_sub(mul[0]);
        let mid3 = mid.wrapping_sub(mul[1]).wrapping_sub((lo3 > lo) as u64);
        let hi3 = hi - (mid3 > mid) as u64;
        *vm = shiftright128(mid3, hi3, j - 64 - 1);
    } else {
        let lo3 = lo + lo;
        let mid3 = mid + mid + (lo3 < lo) as u64;
        let hi3 = hi + hi + (mid3 < mid) as u64;
        let lo4 = lo3 - mul[0];
        let mid4 = mid3 - mul[1] - (lo4 > lo3) as u64;
        let hi4 = hi3 - (mid4 > mid3) as u64;
        *vm = shiftright128(mid4, hi4, j - 64);<commit_msg>Make mul_shift_all safe<commit_after>// Translated from C to Rust. The original C code can be found at
// https://github.com/ulfjack/ryu and carries the following license:
//
// Copyright 2018 <NAME>
//
// The contents of this file may be used under the terms of the Apache License,
// Version 2.0.
//
//    (See accompanying file LICENSE-Apache or copy at
//     http://www.apache.org/licenses/LICENSE-2.0)
//
// Alternatively, the contents of this file may be used under the terms of
// the Boost Software License, Version 1.0.
//    (See accompanying file LICENSE-Boost or copy at
//     https://www.boost.org/LICENSE_1_0.txt)
//
// Unless required by applicable law or agreed to in writing, this software
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.

mod common;
mod d2s_full_table;
mod digit_table;
mod mulshift128;

use std::{mem, ptr};

use common::*;
use d2s_full_table::*;
use digit_table::*;
use mulshift128::*;

fn pow5_factor(mut value: u64) -> i32 {
    let mut count = 0i32;
    loop {
        if value == 0 {
            return 0;
        }
        if value % 5 != 0 {
            return count;
        }
        value /= 5;
        count += 1;
    }
}

// Returns true if value is divisible by 5^p.
fn multiple_of_power_of_5(value: u64, p: i32) -> bool {
    // I tried a case distinction on p, but there was no performance difference.
    pow5_factor(value) >= p
}

fn mul_shift_all(
    mut m: u64,
    mul: &[u64; 2],
    j: u32,
    vp: &mut u64,
    vm: &mut u64,
    mm_shift: u32,
) -> u64 {
    m <<= 1;
    // m is maximum 55 bits
    let (lo, tmp) = umul128(m, mul[0]);
    let (mut mid, mut hi) = umul128(m, mul[1]);
    mid = mid.wrapping_add(tmp);
    hi = hi.wrapping_add((mid < tmp) as u64); // overflow into hi

    let lo2 = lo.wrapping_add(mul[0]);
    let mid2 = mid.wrapping_add(mul[1]).wrapping_add((lo2 < lo) as u64);
    let hi2 = hi.wrapping_add((mid2 < mid) as u64);
    *vp = shiftright128(mid2, hi2, j - 64 - 1);

    if mm_shift == 1 {
        let lo3 = lo.wrapping_sub(mul[0]);
        let mid3 = mid.wrapping_sub(mul[1]).wrapping_sub((lo3 > lo) as u64);
        let hi3 = hi - (mid3 > mid) as u64;
        *vm = shiftright128(mid3, hi3, j - 64 - 1);
    } else {
        let lo3 = lo + lo;
        let mid3 = mid + mid + (lo3 < lo) as u64;
        let hi3 = hi + hi + (mid3 < mid) as u64;
        let lo4 = lo3 - mul[0];
        let mid4 = mid3 - mul[1] - (lo4 > lo3) as u64;
        let hi4 = hi3 - (mid4 > mid3) as u64;
        *vm = shiftright128(mid4, hi4, j - 64);
{'fraction_non_alphanumeric': 0.1343139152312754, 'fraction_numerical': 0.05419005225469325, 'mean_word_length': 2.3043478260869565, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, 'https://': 4, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4770471', 'n_tokens_mistral': 1997, 'n_tokens_neox': 1865, 'n_words': 636}