auto merge of #10555 : TeXitoi/rust/pidigits-resurected, r=alexcrichton
Changes: * default value when no args * license * removed libc printing * use extra::bigint instead of handmade gmp binding The drawback is that it's 2 order of magnitude slower, the good news is that there is a good bench for extra::bigint.
This commit is contained in:
commit
3d569df41d
1 changed files with 68 additions and 139 deletions
|
@ -1,167 +1,96 @@
|
||||||
// xfail-test
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
||||||
|
// file at the top-level directory of this distribution and at
|
||||||
|
// http://rust-lang.org/COPYRIGHT.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||||
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||||
|
// option. This file may not be copied, modified, or distributed
|
||||||
|
// except according to those terms.
|
||||||
|
|
||||||
|
extern mod extra;
|
||||||
|
|
||||||
use std::cast::transmute;
|
|
||||||
use std::from_str::FromStr;
|
use std::from_str::FromStr;
|
||||||
use std::libc::{STDOUT_FILENO, c_char, c_int, c_uint, c_void, fdopen, fputc};
|
use std::num::One;
|
||||||
use std::libc::{fputs};
|
use std::num::Zero;
|
||||||
use std::ptr::null;
|
use std::num::FromPrimitive;
|
||||||
|
use extra::bigint::BigInt;
|
||||||
struct mpz_t {
|
|
||||||
_mp_alloc: c_int,
|
|
||||||
_mp_size: c_int,
|
|
||||||
_mp_limb_t: *c_void,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl mpz_t {
|
|
||||||
fn new() -> mpz_t {
|
|
||||||
mpz_t {
|
|
||||||
_mp_alloc: 0,
|
|
||||||
_mp_size: 0,
|
|
||||||
_mp_limb_t: null(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[link_args="-lgmp"]
|
|
||||||
extern {
|
|
||||||
#[link_name="__gmpz_add"]
|
|
||||||
fn mpz_add(x: *mpz_t, y: *mpz_t, z: *mpz_t);
|
|
||||||
#[link_name="__gmpz_cmp"]
|
|
||||||
fn mpz_cmp(x: *mpz_t, y: *mpz_t) -> c_int;
|
|
||||||
#[link_name="__gmpz_fdiv_qr"]
|
|
||||||
fn mpz_fdiv_qr(a: *mpz_t, b: *mpz_t, c: *mpz_t, d: *mpz_t);
|
|
||||||
#[link_name="__gmpz_get_ui"]
|
|
||||||
fn mpz_get_ui(x: *mpz_t) -> c_uint;
|
|
||||||
#[link_name="__gmpz_init"]
|
|
||||||
fn mpz_init(x: *mpz_t);
|
|
||||||
#[link_name="__gmpz_init_set_ui"]
|
|
||||||
fn mpz_init_set_ui(x: *mpz_t, y: c_uint);
|
|
||||||
#[link_name="__gmpz_mul_2exp"]
|
|
||||||
fn mpz_mul_2exp(x: *mpz_t, y: *mpz_t, z: c_uint);
|
|
||||||
#[link_name="__gmpz_mul_ui"]
|
|
||||||
fn mpz_mul_ui(x: *mpz_t, y: *mpz_t, z: c_uint);
|
|
||||||
#[link_name="__gmpz_submul_ui"]
|
|
||||||
fn mpz_submul_ui(x: *mpz_t, y: *mpz_t, z: c_uint);
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Context {
|
struct Context {
|
||||||
numer: mpz_t,
|
numer: BigInt,
|
||||||
accum: mpz_t,
|
accum: BigInt,
|
||||||
denom: mpz_t,
|
denom: BigInt,
|
||||||
tmp1: mpz_t,
|
|
||||||
tmp2: mpz_t,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Context {
|
impl Context {
|
||||||
fn new() -> Context {
|
fn new() -> Context {
|
||||||
unsafe {
|
Context {
|
||||||
let mut result = Context {
|
numer: One::one(),
|
||||||
numer: mpz_t::new(),
|
accum: Zero::zero(),
|
||||||
accum: mpz_t::new(),
|
denom: One::one(),
|
||||||
denom: mpz_t::new(),
|
|
||||||
tmp1: mpz_t::new(),
|
|
||||||
tmp2: mpz_t::new(),
|
|
||||||
};
|
|
||||||
mpz_init(&result.tmp1);
|
|
||||||
mpz_init(&result.tmp2);
|
|
||||||
mpz_init_set_ui(&result.numer, 1);
|
|
||||||
mpz_init_set_ui(&result.accum, 0);
|
|
||||||
mpz_init_set_ui(&result.denom, 1);
|
|
||||||
result
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn extract_digit(&mut self) -> i32 {
|
fn from_int(i: int) -> BigInt {
|
||||||
unsafe {
|
FromPrimitive::from_int(i).unwrap()
|
||||||
if mpz_cmp(&self.numer, &self.accum) > 0 {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Compute (numer * 3 + accum) / denom
|
|
||||||
mpz_mul_2exp(&self.tmp1, &self.numer, 1);
|
|
||||||
mpz_add(&self.tmp1, &self.tmp1, &self.numer);
|
|
||||||
mpz_add(&self.tmp1, &self.tmp1, &self.accum);
|
|
||||||
mpz_fdiv_qr(&self.tmp1, &self.tmp2, &self.tmp1, &self.denom);
|
|
||||||
|
|
||||||
// Now, if (numer * 4 + accum) % denom...
|
|
||||||
mpz_add(&self.tmp2, &self.tmp2, &self.numer);
|
|
||||||
|
|
||||||
// ... is normalized, then the two divisions have the same result.
|
|
||||||
if mpz_cmp(&self.tmp2, &self.denom) >= 0 {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
mpz_get_ui(&self.tmp1) as i32
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn next_term(&mut self, k: u32) {
|
fn extract_digit(&self) -> int {
|
||||||
unsafe {
|
if self.numer > self.accum {return -1;}
|
||||||
let y2 = k*2 + 1;
|
let (q, r) =
|
||||||
|
(self.numer * Context::from_int(3) + self.accum)
|
||||||
mpz_mul_2exp(&self.tmp1, &self.numer, 1);
|
.div_rem(&self.denom);
|
||||||
mpz_add(&self.accum, &self.accum, &self.tmp1);
|
if r + self.numer >= self.denom {return -1;}
|
||||||
mpz_mul_ui(&self.accum, &self.accum, y2);
|
q.to_int().unwrap()
|
||||||
mpz_mul_ui(&self.numer, &self.numer, k);
|
|
||||||
mpz_mul_ui(&self.denom, &self.denom, y2);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn eliminate_digit(&mut self, d: u32) {
|
fn next_term(&mut self, k: int) {
|
||||||
unsafe {
|
let y2 = Context::from_int(k * 2 + 1);
|
||||||
mpz_submul_ui(&self.accum, &self.denom, d);
|
self.accum = (self.accum + (self.numer << 1)) * y2;
|
||||||
mpz_mul_ui(&self.accum, &self.accum, 10);
|
self.numer = self.numer * Context::from_int(k);
|
||||||
mpz_mul_ui(&self.numer, &self.numer, 10);
|
self.denom = self.denom * y2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn eliminate_digit(&mut self, d: int) {
|
||||||
|
let d = Context::from_int(d);
|
||||||
|
let ten = Context::from_int(10);
|
||||||
|
self.accum = (self.accum - self.denom * d) * ten;
|
||||||
|
self.numer = self.numer * ten;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn pidigits(n: u32) {
|
fn pidigits(n: int) {
|
||||||
unsafe {
|
let mut k = 0;
|
||||||
let mode = "w";
|
let mut context = Context::new();
|
||||||
let stdout = fdopen(STDOUT_FILENO as c_int, transmute(&mode[0]));
|
|
||||||
|
|
||||||
let mut d: i32;
|
for i in range(1, n + 1) {
|
||||||
let mut i: u32 = 0, k: u32 = 0, m: u32;
|
let mut d;
|
||||||
|
|
||||||
let mut context = Context::new();
|
|
||||||
loop {
|
loop {
|
||||||
loop {
|
k += 1;
|
||||||
k += 1;
|
context.next_term(k);
|
||||||
context.next_term(k);
|
d = context.extract_digit();
|
||||||
d = context.extract_digit();
|
if d != -1 {break;}
|
||||||
if d != -1 {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fputc((d as c_int) + ('0' as c_int), stdout);
|
|
||||||
|
|
||||||
i += 1;
|
|
||||||
m = i % 10;
|
|
||||||
if m == 0 {
|
|
||||||
let res = fmt!("\t:%d\n", i as int);
|
|
||||||
fputs(transmute(&res[0]), stdout);
|
|
||||||
}
|
|
||||||
if i >= n {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
context.eliminate_digit(d as u32);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if m != 0 {
|
print!("{}", d);
|
||||||
m = 10 - m;
|
if i % 10 == 0 {print!("\t:{}\n", i);}
|
||||||
while m != 0 {
|
|
||||||
m -= 1;
|
context.eliminate_digit(d);
|
||||||
fputc(' ' as c_int, stdout);
|
}
|
||||||
}
|
|
||||||
let res = fmt!("\t:%d\n", i as int);
|
let m = n % 10;
|
||||||
fputs(transmute(&res[0]), stdout);
|
if m != 0 {
|
||||||
}
|
for _ in range(m, 10) {print(" ");}
|
||||||
|
print!("\t:{}\n", n);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let n: u32 = FromStr::from_str(os::args()[1]).get();
|
let args = std::os::args();
|
||||||
|
let n = if args.len() < 2 {
|
||||||
|
512
|
||||||
|
} else {
|
||||||
|
FromStr::from_str(args[1]).unwrap()
|
||||||
|
};
|
||||||
pidigits(n);
|
pidigits(n);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue