-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from milankl/mk/conversion
Match SoftPosit with 2022 posit standard
- Loading branch information
Showing
40 changed files
with
1,129 additions
and
1,086 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,17 @@ | ||
module SoftPosit | ||
|
||
import SoftPosit_jll | ||
# For compatibility with previous versions of SofPosit.jl which used the name | ||
# `SoftPositPath`. | ||
const SoftPositPath = SoftPosit_jll.softposit | ||
# import SoftPosit_jll | ||
# # For compatibility with previous versions of SofPosit.jl which used the name | ||
# # `SoftPositPath`. | ||
# const SoftPositPath = SoftPosit_jll.softposit | ||
|
||
export AbstractPosit, Posit8, Posit16, Posit32, | ||
Posit8_1, Posit16_1, Posit24_1, | ||
Posit8_2, Posit16_2, Posit24_2, | ||
notareal, minusone, | ||
AbstractQuire, Quire8, Quire16, Quire32, fms, | ||
Posit16_old, Float32_old | ||
export AbstractPosit, Posit8, Posit16, Posit32, Posit16_1, | ||
notareal | ||
|
||
import Base: Float64, Float32, Float16, Int32, Int64, | ||
UInt8, UInt16, UInt32, | ||
(+), (-), (*), (/), (<), (<=), (==), sqrt, | ||
bitstring, round, one, zero, promote_rule, eps, | ||
floatmin, floatmax, signbit, sign, isfinite, | ||
nextfloat, prevfloat, fma, | ||
exp, exp2, exp10, log, log2, log10, cos, sin, tan, | ||
expm1,log1p | ||
|
||
include("typedef.jl") | ||
include("conversionFloatToPosit.jl") | ||
include("conversionPositToFloat.jl") | ||
include("conversionPositToPosit.jl") | ||
include("conversionIntToPosit.jl") | ||
include("conversionPositToInt.jl") | ||
include("conversionHexBinToPosit.jl") | ||
include("conversionBoolToPosit.jl") | ||
include("conversionQuire.jl") | ||
include("arithmetic.jl") | ||
include("comparison.jl") | ||
include("type_definitions.jl") | ||
include("comparisons.jl") | ||
include("constants.jl") | ||
include("round.jl") | ||
include("conversions.jl") | ||
include("arithmetics.jl") | ||
include("print.jl") | ||
include("nextprevfloat.jl") | ||
include("eps.jl") | ||
include("quire.jl") | ||
include("explog_trigonometric.jl") | ||
|
||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# NEGATION via two's complement | ||
Base.:(-)(x::T) where {T<:AbstractPosit} = reinterpret(T,-unsigned(x)) | ||
|
||
# SIGNBIT from the corresponding Int | ||
Base.signbit(x::AbstractPosit) = signbit(signed(x)) | ||
|
||
# SIGN redefine, not x/|x| as in Base for floats | ||
function Base.sign(x::T) where {T<:AbstractPosit} | ||
iszero(x) && return zero(T) | ||
isnan(x) && return notareal(T) | ||
return signbit(x) ? minusone(T) : one(T) | ||
end | ||
|
||
# TWO ARGUMENT ARITHMETIC, addition, subtraction, multiplication, division via conversion | ||
for op in (:(+), :(-), :(*), :(/)) | ||
@eval begin | ||
Base.$op(x::T,y::T) where {T<:AbstractPosit} = convert(T,$op(float(x),float(y))) | ||
end | ||
end | ||
|
||
# ONE ARGUMENT ARITHMETIC, sqrt, exp, log, etc. via conversion | ||
for op in (:sqrt, :exp, :exp2, :exp10, :expm1, :log, :log2, :log10, :log1p, | ||
:sin, :cos, :tan) | ||
@eval begin | ||
Base.$op(x::T) where {T<:AbstractPosit} = convert(T,$op(float(x))) | ||
end | ||
end | ||
|
||
Base.sincos(x::AbstractPosit) = sin(x),cos(x) # not in eval loop because of convert | ||
|
||
# complex trigonometric functions | ||
for P in (:Posit8, :Posit16, :Posit16_1, :Posit32) | ||
@eval begin | ||
sin(x::Complex{$P}) = Complex{$P}(sin(Complex{Base.floattype($P)}(x))) | ||
cos(x::Complex{$P}) = Complex{$P}(cos(Complex{Base.floattype($P)}(x))) | ||
exp(x::Complex{$P}) = cos(im*x) - im*sin(im*x) | ||
end | ||
end | ||
|
||
# nextfloat, prevfloat have a wrap-around behaviour nextfloat(maxpos) = NaR, nextfloat(NaR) = -maxpos | ||
Base.nextfloat(x::T) where {T<:AbstractPosit} = reinterpret(T,reinterpret(Base.uinttype(T),x)+one(Base.uinttype(T))) | ||
Base.prevfloat(x::T) where {T<:AbstractPosit} = reinterpret(T,reinterpret(Base.uinttype(T),x)-one(Base.uinttype(T))) | ||
|
||
# precision (taken from lookup table) | ||
eps(::Type{Posit8}) = reinterpret(Posit8,0x28) | ||
eps(::Type{Posit16}) = reinterpret(Posit16,0x0a00) | ||
eps(::Type{Posit16_1}) = reinterpret(Posit16_1,0x0100) | ||
eps(::Type{Posit32}) = reinterpret(Posit32,0x00a0_0000) | ||
eps(x::AbstractPosit) = max(x-prevfloat(x),nextfloat(x)-x) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# == for posits only true if and only if types and bits match exactly (===, egality) | ||
Base.:(==)(x::AbstractPosit,y::AbstractPosit) = x === y | ||
Base.isnan(x::AbstractPosit) = x == notareal(x) # use isnan for "is NaR?" check | ||
Base.isfinite(x::AbstractPosit) = ~isnan(x) # finite if not NaR | ||
Base.iszero(x::AbstractPosit) = x == zero(x) | ||
|
||
# COMPARISONS via two's complement (- of uints) | ||
Base.:(<)(x::T,y::T) where {T<:AbstractPosit} = -unsigned(x) > -unsigned(y) | ||
Base.:(<=)(x::T,y::T) where {T<:AbstractPosit} = -unsigned(x) >= -unsigned(y) |
Oops, something went wrong.