File tree 1 file changed +13
-4
lines changed
1 file changed +13
-4
lines changed Original file line number Diff line number Diff line change @@ -51,8 +51,6 @@ mod tests {
51
51
}
52
52
}
53
53
54
- // TODO(dp): Need a variant for 32 bit, or at least a helper function to extract u64 from limbs.
55
- #[ cfg( target_pointer_width = "64" ) ]
56
54
#[ test]
57
55
fn pi_estimates ( ) {
58
56
let pi_xs: Vec < ( u128 , u32 ) > = vec ! [
@@ -96,13 +94,24 @@ mod tests {
96
94
} else {
97
95
approx_pi_x - pi_x_wide
98
96
} ;
99
- let delta = ( delta. as_limbs ( ) [ 1 ] . 0 as u128 ) << 64 | delta . as_limbs ( ) [ 0 ] . 0 as u128 ;
100
- let approx_pi_x_128 = ( approx_pi_x. as_limbs ( ) [ 1 ] . 0 as u128 ) << 64 | approx_pi_x . as_limbs ( ) [ 0 ] . 0 as u128 ;
97
+ let delta = uint_to_u128 ( & delta) ;
98
+ let approx_pi_x_128 = uint_to_u128 ( & approx_pi_x) ;
101
99
let error = ( delta as f64 / * pi_x as f64 ) * 100.0 ;
102
100
assert ! (
103
101
error < 5.0 , // For large x, this error is much better, well below 1.
104
102
"10^{exponent}:\t {pi_x} - {approx_pi_x_128} = {delta}, err: {error:.2}"
105
103
) ;
106
104
}
107
105
}
106
+
107
+ fn uint_to_u128 < const LIMBS : usize > ( x : & Uint < LIMBS > ) -> u128 {
108
+ let limbs = x. as_limbs ( ) ;
109
+ #[ cfg( target_pointer_width = "32" ) ]
110
+ return ( limbs[ 3 ] . 0 as u128 ) << 96
111
+ | ( limbs[ 2 ] . 0 as u128 ) << 64
112
+ | ( limbs[ 1 ] . 0 as u128 ) << 32
113
+ | limbs[ 0 ] . 0 as u128 ;
114
+ #[ cfg( target_pointer_width = "64" ) ]
115
+ return ( limbs[ 1 ] . 0 as u128 ) << 64 | limbs[ 0 ] . 0 as u128 ;
116
+ }
108
117
}
You can’t perform that action at this time.
0 commit comments