1// Translated from C to Rust. The original C code can be found at
2// https://github.com/ulfjack/ryu and carries the following license:
3//
4// Copyright 2018 Ulf Adams
5//
6// The contents of this file may be used under the terms of the Apache License,
7// Version 2.0.
8//
9// (See accompanying file LICENSE-Apache or copy at
10// http://www.apache.org/licenses/LICENSE-2.0)
11//
12// Alternatively, the contents of this file may be used under the terms of
13// the Boost Software License, Version 1.0.
14// (See accompanying file LICENSE-Boost or copy at
15// https://www.boost.org/LICENSE_1_0.txt)
16//
17// Unless required by applicable law or agreed to in writing, this software
18// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19// KIND, either express or implied.
2021// Returns (lo, hi).
22#[cfg(not(integer128))]
23#[cfg_attr(feature = "no-panic", inline)]
24pub fn umul128(a: u64, b: u64) -> (u64, u64) {
25let a_lo = a as u32;
26let a_hi = (a >> 32) as u32;
27let b_lo = b as u32;
28let b_hi = (b >> 32) as u32;
2930let b00 = a_lo as u64 * b_lo as u64;
31let b01 = a_lo as u64 * b_hi as u64;
32let b10 = a_hi as u64 * b_lo as u64;
33let b11 = a_hi as u64 * b_hi as u64;
3435let b00_lo = b00 as u32;
36let b00_hi = (b00 >> 32) as u32;
3738let mid1 = b10 + b00_hi as u64;
39let mid1_lo = mid1 as u32;
40let mid1_hi = (mid1 >> 32) as u32;
4142let mid2 = b01 + mid1_lo as u64;
43let mid2_lo = mid2 as u32;
44let mid2_hi = (mid2 >> 32) as u32;
4546let p_hi = b11 + mid1_hi as u64 + mid2_hi as u64;
47let p_lo = ((mid2_lo as u64) << 32) | b00_lo as u64;
4849 (p_lo, p_hi)
50}
5152#[cfg(not(integer128))]
53#[cfg_attr(feature = "no-panic", inline)]
54pub fn shiftright128(lo: u64, hi: u64, dist: u32) -> u64 {
55// We don't need to handle the case dist >= 64 here (see above).
56debug_assert!(dist > 0);
57debug_assert!(dist < 64);
58 (hi << (64 - dist)) | (lo >> dist)
59}
6061#[cfg_attr(feature = "no-panic", inline)]
62pub fn div5(x: u64) -> u64 {
63 x / 5
64}
6566#[cfg_attr(feature = "no-panic", inline)]
67pub fn div10(x: u64) -> u64 {
68 x / 10
69}
7071#[cfg_attr(feature = "no-panic", inline)]
72pub fn div100(x: u64) -> u64 {
73 x / 100
74}
7576#[cfg_attr(feature = "no-panic", inline)]
77fn pow5_factor(mut value: u64) -> u32 {
78let mut count = 0u32;
79loop {
80debug_assert!(value != 0);
81let q = div5(value);
82let r = (value as u32).wrapping_sub(5u32.wrapping_mul(q as u32));
83if r != 0 {
84break;
85 }
86 value = q;
87 count += 1;
88 }
89 count
90}
9192// Returns true if value is divisible by 5^p.
93#[cfg_attr(feature = "no-panic", inline)]
94pub fn multiple_of_power_of_5(value: u64, p: u32) -> bool {
95// I tried a case distinction on p, but there was no performance difference.
96pow5_factor(value) >= p
97}
9899// Returns true if value is divisible by 2^p.
100#[cfg_attr(feature = "no-panic", inline)]
101pub fn multiple_of_power_of_2(value: u64, p: u32) -> bool {
102debug_assert!(value != 0);
103// return __builtin_ctzll(value) >= p;
104(value & ((1u64 << p) - 1)) == 0
105}