Skip to content

Commit 1bc6857

Browse files
committed
Merge pull request #700 from yledu/shootout
Shootout pidigits code, new shootout directory in examples and moved existing shootout code to it
2 parents 480e719 + 721ab71 commit 1bc6857

File tree

6 files changed

+120
-1
lines changed

6 files changed

+120
-1
lines changed

examples/shootout/README

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
This directory contains the Julia version of the "The
2+
Computer Language Benchmarks Game":
3+
http://shootout.alioth.debian.org/
4+
5+
The source code for all the benchmarks are available there:
6+
http://alioth.debian.org/scm/viewvc.php/shootout/bench/?root=shootout
7+
8+
See specific Julia discussion here:
9+
https://github.com/JuliaLang/julia/issues/660
File renamed without changes.

examples/shootout/pidigits.jl

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
load("timing.jl")
2+
load("bigint.jl")
3+
4+
function pidigits(N::Int, printOut::Bool)
5+
"""
6+
See http://shootout.alioth.debian.org/u64q/performance.php?test=pidigits#about
7+
8+
Transliterated from Mario Pernici Python's program
9+
10+
11+
INPUT:
12+
13+
- N -- a positive integer giving the number of digits of pi to be computed
14+
15+
- printOut -- a boolean specifying if we want intermediate printouts of digits in packets of 10
16+
17+
OUTPUT:
18+
19+
- returns the last ten digits anyway
20+
21+
- prints all the digits in packets of 10 iff printOut == true
22+
23+
"""
24+
25+
i = k = ns = 0
26+
k1 = 1
27+
n,a,d,t,u = map(BigInt,(1,0,1,0,0))
28+
29+
while true
30+
k += 1
31+
t = lshift(n,uint(1))
32+
n *= k
33+
a += t
34+
k1 += 2
35+
a *= k1
36+
d *= k1
37+
38+
if a >= n
39+
t,u = divmod(n*3 +a, d)
40+
u += n
41+
if d > u
42+
ns = ns*10 + t
43+
i += 1
44+
if mod(i,10) == 0
45+
if printOut
46+
show(ns)
47+
printf("\t:%d\n", i)
48+
end
49+
if i >= N
50+
return ns
51+
end
52+
ns = 0
53+
end
54+
a -= d*t
55+
a *= 10
56+
n *= 10
57+
58+
end
59+
end
60+
end
61+
end
62+
63+
function pidigits(N::Int)
64+
pidigits(N,false)
65+
end
66+
67+
68+
@assert pidigits(1000) == 9216420198
69+
70+
if length(ARGS)==1
71+
N = int(ARGS[1])
72+
else
73+
N = 1000
74+
end
75+
@timeit pidigits(N) "pidigits"
76+
77+

examples/shootout/timing.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
macro timeit(ex,name)
2+
t, i = gensym(2)
3+
quote
4+
$t = Inf
5+
for $i=1:5
6+
$t = min($t, @elapsed $ex)
7+
end
8+
printf("julia, %s: best of 5 took %.1f ms\n", $name, $t*1000)
9+
end
10+
end
11+
12+

external/gmp_wrapper.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ extern void _jl_mpz_div(mpz_t* rop, mpz_t* op1, mpz_t* op2) {
4545
mpz_fdiv_q(*rop, *op1, *op2);
4646
}
4747

48+
extern void _jl_mpz_divmod(mpz_t* rop1, mpz_t* rop2, mpz_t* op1, mpz_t* op2) {
49+
mpz_divmod(*rop1, *rop2, *op1, *op2);
50+
}
51+
4852
extern void _jl_mpz_rem(mpz_t* rop, mpz_t* op1, mpz_t* op2) {
4953
mpz_fdiv_r(*rop, *op1, *op2);
5054
}
@@ -61,6 +65,10 @@ extern int _jl_mpz_cmp(mpz_t* op1, mpz_t* op2) {
6165
return mpz_cmp(*op1, *op2);
6266
}
6367

68+
extern void _jl_mpz_lshift(mpz_t* rop, mpz_t* base, unsigned long int count) {
69+
mpz_mul_2exp(*rop, *base, count);
70+
}
71+
6472
extern void _jl_mpz_pow_ui(mpz_t* rop, mpz_t* base, unsigned long int exp) {
6573
mpz_pow_ui(*rop, *base, exp);
6674
}

extras/bigint.jl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
_jl_libgmp_wrapper = dlopen("libgmp_wrapper")
1+
_jl_libgmp_wrapper = dlopen("libgmp_wrapper.so")
22

33
type BigInt <: Integer
44
mpz::Ptr{Void}
@@ -57,6 +57,12 @@ function -(x::BigInt)
5757
BigInt(z)
5858
end
5959

60+
function lshift(x::BigInt, c::Uint)
61+
z= _jl_bigint_init()
62+
ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_lshift), Void, (Ptr{Void}, Ptr{Void}, Uint), z, x.mpz, c)
63+
BigInt(z)
64+
end
65+
6066
function -(x::BigInt, y::BigInt)
6167
z= _jl_bigint_init()
6268
ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_sub), Void, (Ptr{Void}, Ptr{Void}, Ptr{Void}),z,x.mpz,y.mpz)
@@ -75,6 +81,13 @@ function div (x::BigInt, y::BigInt)
7581
BigInt(z)
7682
end
7783

84+
function divmod(x::BigInt, y::BigInt)
85+
z1= _jl_bigint_init()
86+
z2= _jl_bigint_init()
87+
ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_divmod), Void, (Ptr{Void}, Ptr{Void}, Ptr{Void}, Ptr{Void}), z1, z2, x.mpz, y.mpz)
88+
BigInt(z1),BigInt(z2)
89+
end
90+
7891
function rem (x::BigInt, y::BigInt)
7992
z= _jl_bigint_init()
8093
ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_rem), Void, (Ptr{Void}, Ptr{Void}, Ptr{Void}),z,x.mpz,y.mpz)

0 commit comments

Comments
 (0)