Skip to content

Commit

Permalink
Add convert(Float,_), convert(Int,_), convert(Uint,_) methods.
Browse files Browse the repository at this point in the history
This moves the burden to the more general convert function from
the more ad-hoc float(), int() and uint() functions.

Also added the signed(x) function which converts any integer to
the signed type of the same size. Previously int(x) would make
unsigned integers into signed ones of a size that could safely
fit them (8->16->32->64), but now int(x) leaves all subtypes of
Int as-is, which makes more sense anyway.
  • Loading branch information
StefanKarpinski committed Jul 22, 2011
1 parent f66e816 commit 6008d6c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 25 deletions.
20 changes: 12 additions & 8 deletions j/float.j
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,20 @@ convert(::Type{Float64}, x::Char) = boxf64(uitofp64(unbox32(x)))
convert(::Type{Float64}, x::Uint64) = boxf64(uitofp64(unbox64(x)))
convert(::Type{Float64}, x::Float32) = boxf64(fpext64(unbox32(x)))

convert(::Type{Float}, x::Bool) = convert(Float32, x)
convert(::Type{Float}, x::Int8) = convert(Float32, x)
convert(::Type{Float}, x::Int16) = convert(Float32, x)
convert(::Type{Float}, x::Int32) = convert(Float64, x)
convert(::Type{Float}, x::Int64) = convert(Float64, x) # LOSSY
convert(::Type{Float}, x::Uint8) = convert(Float32, x)
convert(::Type{Float}, x::Uint16) = convert(Float32, x)
convert(::Type{Float}, x::Uint32) = convert(Float64, x)
convert(::Type{Float}, x::Char) = convert(Float32, x)
convert(::Type{Float}, x::Uint64) = convert(Float64, x) # LOSSY

float32(x) = convert(Float32, x)
float64(x) = convert(Float64, x)

int(x::Float32) = int32(x)
int(x::Float64) = int64(x)

uint(x::Float32) = uint32(x)
uint(x::Float64) = uint64(x)

float(x::Float) = x
float(x) = convert(Float, x)

## floating point promotions ##

Expand Down
38 changes: 21 additions & 17 deletions j/int.j
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,18 @@ convert(::Type{Uint64}, x::Int64 ) = boxui64(unbox64(x))
convert(::Type{Uint64}, x::Float32) = boxui64(fptoui64(unbox32(x)))
convert(::Type{Uint64}, x::Float64) = boxui64(fptoui64(unbox64(x)))

convert(::Type{Int}, x::Bool ) = convert(Int8, x)
convert(::Type{Int}, x::Float32) = convert(Int32, x)
convert(::Type{Int}, x::Float64) = convert(Int64, x)

convert(::Type{Uint}, x::Bool ) = convert(Uint8, x)
convert(::Type{Uint}, x::Int8 ) = convert(Uint16, x)
convert(::Type{Uint}, x::Int16 ) = convert(Uint32, x)
convert(::Type{Uint}, x::Int32 ) = convert(Uint64, x)
convert(::Type{Uint}, x::Int64 ) = convert(Uint64, x) # LOSSY
convert(::Type{Uint}, x::Float32) = convert(Uint32, x)
convert(::Type{Uint}, x::Float64) = convert(Uint64, x)

int8 (x) = convert(Int8, x)
uint8 (x) = convert(Uint8, x)
int16 (x) = convert(Int16, x)
Expand All @@ -105,28 +117,20 @@ uint32(x) = convert(Uint32, x)
int64 (x) = convert(Int64, x)
uint64(x) = convert(Uint64, x)

int (x) = convert(Int, x)
uint(x) = convert(Uint, x)

signed(x::Int) = x
signed(x::Uint8 ) = convert(Int8 , x)
signed(x::Uint16) = convert(Int16, x)
signed(x::Uint32) = convert(Int32, x)
signed(x::Uint64) = convert(Int64, x)

round(x::Int) = x
trunc(x::Int) = x
floor(x::Int) = x
ceil(x::Int) = x

int(x::Int) = x
int(x::Uint8 ) = int16(x)
int(x::Uint16) = int32(x)
int(x::Uint32) = int64(x)
int(x::Uint64) = int64(x) # LOSSY

uint(x::Int) = x
uint(x::Int8 ) = uint8(x)
uint(x::Int16) = uint16(x)
uint(x::Int32) = uint32(x)
uint(x::Int64) = uint64(x)

float(x::Union(Int8 ,Uint8) ) = float32(x)
float(x::Union(Int16,Uint16)) = float32(x)
float(x::Union(Int32,Uint32)) = float64(x)
float(x::Union(Int64,Uint64)) = float64(x) # TODO: should be Float80

## integer promotions ##

promote_rule(::Type{Int16}, ::Type{Int8} ) = Int16
Expand Down

0 comments on commit 6008d6c

Please sign in to comment.