summaryrefslogtreecommitdiffstats
path: root/third_party/rust/num-integer/benches/roots.rs
blob: 7f672786a572b88d740118b4f62423503a76179b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//! Benchmark sqrt and cbrt

#![feature(test)]

extern crate num_integer;
extern crate num_traits;
extern crate test;

use num_integer::Integer;
use num_traits::checked_pow;
use num_traits::{AsPrimitive, PrimInt, WrappingAdd, WrappingMul};
use test::{black_box, Bencher};

trait BenchInteger: Integer + PrimInt + WrappingAdd + WrappingMul + 'static {}

impl<T> BenchInteger for T where T: Integer + PrimInt + WrappingAdd + WrappingMul + 'static {}

fn bench<T, F>(b: &mut Bencher, v: &[T], f: F, n: u32)
where
    T: BenchInteger,
    F: Fn(&T) -> T,
{
    // Pre-validate the results...
    for i in v {
        let rt = f(i);
        if *i >= T::zero() {
            let rt1 = rt + T::one();
            assert!(rt.pow(n) <= *i);
            if let Some(x) = checked_pow(rt1, n as usize) {
                assert!(*i < x);
            }
        } else {
            let rt1 = rt - T::one();
            assert!(rt < T::zero());
            assert!(*i <= rt.pow(n));
            if let Some(x) = checked_pow(rt1, n as usize) {
                assert!(x < *i);
            }
        };
    }

    // Now just run as fast as we can!
    b.iter(|| {
        for i in v {
            black_box(f(i));
        }
    });
}

// Simple PRNG so we don't have to worry about rand compatibility
fn lcg<T>(x: T) -> T
where
    u32: AsPrimitive<T>,
    T: BenchInteger,
{
    // LCG parameters from Numerical Recipes
    // (but we're applying it to arbitrary sizes)
    const LCG_A: u32 = 1664525;
    const LCG_C: u32 = 1013904223;
    x.wrapping_mul(&LCG_A.as_()).wrapping_add(&LCG_C.as_())
}

fn bench_rand<T, F>(b: &mut Bencher, f: F, n: u32)
where
    u32: AsPrimitive<T>,
    T: BenchInteger,
    F: Fn(&T) -> T,
{
    let mut x: T = 3u32.as_();
    let v: Vec<T> = (0..1000)
        .map(|_| {
            x = lcg(x);
            x
        })
        .collect();
    bench(b, &v, f, n);
}

fn bench_rand_pos<T, F>(b: &mut Bencher, f: F, n: u32)
where
    u32: AsPrimitive<T>,
    T: BenchInteger,
    F: Fn(&T) -> T,
{
    let mut x: T = 3u32.as_();
    let v: Vec<T> = (0..1000)
        .map(|_| {
            x = lcg(x);
            while x < T::zero() {
                x = lcg(x);
            }
            x
        })
        .collect();
    bench(b, &v, f, n);
}

fn bench_small<T, F>(b: &mut Bencher, f: F, n: u32)
where
    u32: AsPrimitive<T>,
    T: BenchInteger,
    F: Fn(&T) -> T,
{
    let v: Vec<T> = (0..1000).map(|i| i.as_()).collect();
    bench(b, &v, f, n);
}

fn bench_small_pos<T, F>(b: &mut Bencher, f: F, n: u32)
where
    u32: AsPrimitive<T>,
    T: BenchInteger,
    F: Fn(&T) -> T,
{
    let v: Vec<T> = (0..1000)
        .map(|i| i.as_().mod_floor(&T::max_value()))
        .collect();
    bench(b, &v, f, n);
}

macro_rules! bench_roots {
    ($($T:ident),*) => {$(
        mod $T {
            use test::Bencher;
            use num_integer::Roots;

            #[bench]
            fn sqrt_rand(b: &mut Bencher) {
                ::bench_rand_pos(b, $T::sqrt, 2);
            }

            #[bench]
            fn sqrt_small(b: &mut Bencher) {
                ::bench_small_pos(b, $T::sqrt, 2);
            }

            #[bench]
            fn cbrt_rand(b: &mut Bencher) {
                ::bench_rand(b, $T::cbrt, 3);
            }

            #[bench]
            fn cbrt_small(b: &mut Bencher) {
                ::bench_small(b, $T::cbrt, 3);
            }

            #[bench]
            fn fourth_root_rand(b: &mut Bencher) {
                ::bench_rand_pos(b, |x: &$T| x.nth_root(4), 4);
            }

            #[bench]
            fn fourth_root_small(b: &mut Bencher) {
                ::bench_small_pos(b, |x: &$T| x.nth_root(4), 4);
            }

            #[bench]
            fn fifth_root_rand(b: &mut Bencher) {
                ::bench_rand(b, |x: &$T| x.nth_root(5), 5);
            }

            #[bench]
            fn fifth_root_small(b: &mut Bencher) {
                ::bench_small(b, |x: &$T| x.nth_root(5), 5);
            }
        }
    )*}
}

bench_roots!(i8, i16, i32, i64, i128);
bench_roots!(u8, u16, u32, u64, u128);