Use bbp crate.
This commit is contained in:
79
Cargo.lock
generated
79
Cargo.lock
generated
@@ -1,21 +1,15 @@
|
||||
[root]
|
||||
name = "piar"
|
||||
[[package]]
|
||||
name = "bbp"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"byteorder 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"getopts 0.2.15 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"lazy_static 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"pbr 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "byteorder"
|
||||
version = "1.1.0"
|
||||
version = "1.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "getopts"
|
||||
version = "0.2.15"
|
||||
version = "0.2.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
@@ -27,14 +21,9 @@ dependencies = [
|
||||
"winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "0.2.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.30"
|
||||
version = "0.2.40"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
@@ -43,25 +32,34 @@ version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.30 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"time 0.1.38 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"time 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "piar"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"bbp 0.1.0",
|
||||
"byteorder 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"getopts 0.2.17 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"pbr 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "redox_syscall"
|
||||
version = "0.1.31"
|
||||
version = "0.1.37"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "time"
|
||||
version = "0.1.38"
|
||||
version = "0.1.39"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.30 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"redox_syscall 0.1.31 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"redox_syscall 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -69,19 +67,40 @@ name = "winapi"
|
||||
version = "0.2.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "winapi"
|
||||
version = "0.3.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi-build"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "winapi-i686-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "winapi-x86_64-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[metadata]
|
||||
"checksum byteorder 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ff81738b726f5d099632ceaffe7fb65b90212e8dce59d518729e7e8634032d3d"
|
||||
"checksum getopts 0.2.15 (registry+https://github.com/rust-lang/crates.io-index)" = "65922871abd2f101a2eb0eaebadc66668e54a87ad9c3dd82520b5f86ede5eff9"
|
||||
"checksum byteorder 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "73b5bdfe7ee3ad0b99c9801d58807a9dbc9e09196365b0203853b99889ab3c87"
|
||||
"checksum getopts 0.2.17 (registry+https://github.com/rust-lang/crates.io-index)" = "b900c08c1939860ce8b54dc6a89e26e00c04c380fd0e09796799bd7f12861e05"
|
||||
"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
|
||||
"checksum lazy_static 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "3b37545ab726dd833ec6420aaba8231c5b320814b9029ad585555d2a03e94fbf"
|
||||
"checksum libc 0.2.30 (registry+https://github.com/rust-lang/crates.io-index)" = "2370ca07ec338939e356443dac2296f581453c35fe1e3a3ed06023c49435f915"
|
||||
"checksum libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)" = "6fd41f331ac7c5b8ac259b8bf82c75c0fb2e469bbf37d2becbba9a6a2221965b"
|
||||
"checksum pbr 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e048e3afebb6c454bb1c5d0fe73fda54698b4715d78ed8e7302447c37736d23a"
|
||||
"checksum redox_syscall 0.1.31 (registry+https://github.com/rust-lang/crates.io-index)" = "8dde11f18c108289bef24469638a04dce49da56084f2d50618b226e47eb04509"
|
||||
"checksum time 0.1.38 (registry+https://github.com/rust-lang/crates.io-index)" = "d5d788d3aa77bc0ef3e9621256885555368b47bd495c13dd2e7413c89f845520"
|
||||
"checksum redox_syscall 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)" = "0d92eecebad22b767915e4d529f89f28ee96dbbf5a4810d2b844373f136417fd"
|
||||
"checksum time 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)" = "a15375f1df02096fb3317256ce2cee6a1f42fc84ea5ad5fc8c421cfe40c73098"
|
||||
"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
|
||||
"checksum winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "04e3bd221fcbe8a271359c04f21a76db7d0c6028862d1bb5512d85e1e2eb5bb3"
|
||||
"checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc"
|
||||
"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||
"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
||||
|
||||
@@ -7,7 +7,7 @@ authors = ["logaritmisk <anders.e.olsson@gmail.com>"]
|
||||
getopts = "0.2"
|
||||
byteorder = "1.1"
|
||||
pbr = "1.0"
|
||||
lazy_static = "0.2"
|
||||
bbp = { path = "../bbp" }
|
||||
|
||||
[profile.release]
|
||||
lto = true
|
||||
|
||||
102
src/bbp.rs
102
src/bbp.rs
@@ -1,102 +0,0 @@
|
||||
pub fn get_byte(id: i32) -> u8 {
|
||||
let pid: f64 = 4.0 * series(1, id) - 2.0 * series(4, id) - series(5, id) - series(6, id);
|
||||
|
||||
let y: f64 = (pid - f64::from(pid as i32) + 1.0).abs();
|
||||
let y = 16.0 * (y - y.floor());
|
||||
|
||||
((y as u8) << 4) | (16.0 * (y - y.floor())) as u8
|
||||
}
|
||||
|
||||
fn series(m: i32, id: i32) -> f64 {
|
||||
const EPS: f64 = 1e-17;
|
||||
|
||||
let mut ak: f64;
|
||||
let mut t: f64;
|
||||
let mut s: f64 = 0.0;
|
||||
|
||||
for k in 0..id {
|
||||
ak = f64::from(8 * k + m);
|
||||
t = expm(f64::from(id - k), ak);
|
||||
|
||||
s += t / ak;
|
||||
s -= s.trunc();
|
||||
}
|
||||
|
||||
for k in id..id + 101 {
|
||||
ak = f64::from(8 * k + m);
|
||||
t = 16f64.powi(id - k) / ak;
|
||||
|
||||
if t < EPS {
|
||||
break;
|
||||
}
|
||||
|
||||
s += t;
|
||||
s -= s.trunc();
|
||||
}
|
||||
|
||||
s
|
||||
}
|
||||
|
||||
fn expm(p: f64, ak: f64) -> f64 {
|
||||
if ak == 1.0 {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
const NTP: usize = 25;
|
||||
|
||||
lazy_static! {
|
||||
static ref TP: [f64; NTP] = {
|
||||
let mut v = [1f64; NTP];
|
||||
|
||||
for i in 1..NTP {
|
||||
v[i] = 2.0 * v[i - 1];
|
||||
}
|
||||
|
||||
v
|
||||
};
|
||||
}
|
||||
|
||||
let mut i: usize = 0;
|
||||
|
||||
while TP[i] <= p {
|
||||
i += 1;
|
||||
}
|
||||
|
||||
let mut pt = TP[i - 1];
|
||||
let mut p = p;
|
||||
let mut r: f64 = 1.0;
|
||||
|
||||
for _ in 1..i + 1 {
|
||||
if p >= pt {
|
||||
r *= 16.0;
|
||||
r -= (r / ak).trunc() * ak;
|
||||
|
||||
p -= pt;
|
||||
}
|
||||
|
||||
pt *= 0.5;
|
||||
|
||||
if pt >= 1.0 {
|
||||
r *= r;
|
||||
r -= (r / ak).trunc() * ak;
|
||||
}
|
||||
}
|
||||
|
||||
r
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_get_byte() {
|
||||
assert_eq!(36, get_byte(0));
|
||||
assert_eq!(67, get_byte(1));
|
||||
assert_eq!(63, get_byte(2));
|
||||
|
||||
assert_eq!(163, get_byte(10));
|
||||
|
||||
assert_eq!(41, get_byte(100));
|
||||
}
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
|
||||
14
src/main.rs
14
src/main.rs
@@ -1,11 +1,8 @@
|
||||
|
||||
extern crate bbp;
|
||||
extern crate byteorder;
|
||||
extern crate getopts;
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
extern crate pbr;
|
||||
|
||||
|
||||
use std::env;
|
||||
use std::fs;
|
||||
use std::io::BufReader;
|
||||
@@ -18,10 +15,6 @@ use getopts::Options;
|
||||
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
|
||||
use pbr::{ProgressBar, Units};
|
||||
|
||||
|
||||
mod bbp;
|
||||
|
||||
|
||||
struct Pi {
|
||||
next: i32,
|
||||
map: BTreeMap<u8, i32>,
|
||||
@@ -40,7 +33,7 @@ impl Pi {
|
||||
self.map[&byte]
|
||||
} else {
|
||||
for i in self.next..i32::MAX {
|
||||
if byte == bbp::get_byte(i) {
|
||||
if byte == bbp::byte_at(i) {
|
||||
self.map.insert(byte, i);
|
||||
|
||||
return i;
|
||||
@@ -52,7 +45,6 @@ impl Pi {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn print_usage(program: &str, opts: &Options) {
|
||||
let brief = format!("Usage: {} [option] INPUT OUTPUT", program);
|
||||
print!("{}", opts.usage(&brief));
|
||||
@@ -156,7 +148,7 @@ fn extract(input: &str, output: &str) {
|
||||
let mut c = Cursor::new(&buf);
|
||||
let index = c.read_i32::<LittleEndian>().unwrap();
|
||||
|
||||
output_file.write_all(&[bbp::get_byte(index)]).unwrap();
|
||||
output_file.write_all(&[bbp::byte_at(index)]).unwrap();
|
||||
|
||||
pb.add(size as u64);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user