Skip to content

Commit

Permalink
more explicit convert; additonal tests; add GMP to docs
Browse files Browse the repository at this point in the history
  • Loading branch information
aviks committed Feb 27, 2012
1 parent d05446d commit 6613651
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ Julia uses the following external libraries, which are automatically downloaded
- **[ARPACK]** — a collection of subroutines designed to solve large, sparse eigenvalue problems.
- **[FFTW]** — library for computing fast Fourier transforms very quickly and efficiently.
- **[PCRE]** — Perl-compatible regular expressions library.
- **[GMP]** - the GNU multiple precision arithmetic library, needed for bigint support
- **[D3]** — JavaScript visualization library.

[GNU make]: http://www.gnu.org/software/make/
Expand All @@ -160,6 +161,7 @@ Julia uses the following external libraries, which are automatically downloaded
[LLVM]: http://www.llvm.org/
[FemtoLisp]: https://github.com/JeffBezanson/femtolisp
[GNU readline]: http://cnswww.cns.cwru.edu/php/chet/readline/rltop.html
[GMP]: http://gmplib.org/
[D3]: http://mbostock.github.com/d3/

<a name="Directories"/>
Expand Down
1 change: 1 addition & 0 deletions external/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
/Clp-*
/SuiteSparse-*
/double-conversion-*
/gmp-*
25 changes: 14 additions & 11 deletions j/bigint.j
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,21 @@ type BigInt <: Integer
end
end

#BigInt(x::Int64) = BigInt(string(x))
convert(::Type{BigInt}, x::Int8) = BigInt(int(x))
convert(::Type{BigInt}, x::Int16) = BigInt(int(x))
convert(::Type{BigInt}, x::Int) = BigInt(x)

macro define_bigint_convert ()
if WORD_SIZE == 64
:(convert(::Type{BigInt}, x::Int32) = BigInt(int(x)))

#convert(::Type{BigInt}, x::Integer) = BigInt(string(x))
function convert(::Type{BigInt}, x::Int64)
if is(Int, Int64)
BigInt(int(x))
else
BigInt(string(x))
:(convert(::Type{BigInt}, x::Int64) = BigInt(string(x)))
end

end

@define_bigint_convert

promote_rule(::Type{BigInt}, ::Type{Int8}) = BigInt
promote_rule(::Type{BigInt}, ::Type{Int16}) = BigInt
promote_rule(::Type{BigInt}, ::Type{Int32}) = BigInt
Expand Down Expand Up @@ -83,11 +86,11 @@ function cmp(x::BigInt, y::BigInt)
ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_cmp), Int, (Ptr{Void}, Ptr{Void}),x.mpz, y.mpz)
end

==(x::BigInt, y::BigInt) = cmp(x,y) == 0
==(x::BigInt, y::BigInt) = cmp(x,y) == 0
<=(x::BigInt, y::BigInt) = cmp(x,y) <= 0
>= (x::BigInt, y::BigInt) = cmp(x,y) >= 0
< (x::BigInt, y::BigInt) = cmp(x,y) < 0
> (x::BigInt, y::BigInt) = cmp(x,y) > 0
>=(x::BigInt, y::BigInt) = cmp(x,y) >= 0
<(x::BigInt, y::BigInt) = cmp(x,y) < 0
>(x::BigInt, y::BigInt) = cmp(x,y) > 0

function string(x::BigInt)
s=ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_printf), Ptr{Uint8}, (Ptr{Void},),x.mpz)
Expand Down
13 changes: 10 additions & 3 deletions test/bigint.j
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ d = BigInt("-246913578024691357802469135780")
@assert d == -c


e=2^63-1
@assert typeof(BigInt(e)) == BigInt
@assert BigInt(e)+1 == BigInt("9223372036854775808")
ee=2^63-1
@assert typeof(BigInt(ee)) == BigInt
@assert BigInt(ee)+1 == BigInt("9223372036854775808")

#Multiple calls for sanity check, since we're doing direct memory manipulation
@assert string(a) == "123456789012345678901234567890"
Expand All @@ -38,3 +38,10 @@ e=2^63-1
@assert div(BigInt(3), BigInt(2)) == BigInt(1)
@assert rem(BigInt(3), BigInt(2)) == BigInt(1)
@assert a+int(1) == b
@assert a+int8(1) == b
@assert a+int16(1) == b
@assert a+int32(1) == b
@assert a+int64(1) == b

0 comments on commit 6613651

Please sign in to comment.