Skip to content

Commit

Permalink
Add bitstring for FixedPoint numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
kimikage committed Jul 8, 2020
1 parent 56760f3 commit 7d881ec
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/FixedPointNumbers.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module FixedPointNumbers

import Base: ==, <, <=, -, +, *, /, ~, isapprox,
convert, promote_rule, show, isinteger, abs, decompose,
isnan, isinf, isfinite,
convert, promote_rule, show, bitstring, abs, decompose,
isnan, isinf, isfinite, isinteger,
zero, oneunit, one, typemin, typemax, floatmin, floatmax, eps, sizeof, reinterpret,
float, trunc, round, floor, ceil, bswap,
div, fld, rem, mod, mod1, fld1, min, max, minmax,
Expand Down Expand Up @@ -171,6 +171,8 @@ function minmax(x::X, y::X) where {X <: FixedPoint}
X(a,0), X(b,0)
end

bitstring(x::FixedPoint) = bitstring(x.i)

bswap(x::X) where {X <: FixedPoint} = sizeof(X) == 1 ? x : X(bswap(x.i), 0)

for f in (:zero, :oneunit, :one, :eps, :rawone, :rawtype, :floattype)
Expand Down
5 changes: 4 additions & 1 deletion test/fixed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,17 @@ end
@test_throws DomainError zero(Fixed{Int16,17})
end

@testset "reinterpret" begin
@testset "reinterpret/bitstring" begin
@test reinterpret(Q0f7, signed(0xa2)) === -0.734375Q0f7
@test reinterpret(Q5f10, signed(0x00a2)) === 0.158203125Q5f10

@test reinterpret(reinterpret(Q0f7, signed(0xa2))) === signed(0xa2)
@test reinterpret(reinterpret(Q5f10, signed(0x00a2))) === signed(0x00a2)

@test reinterpret(Int8, 0.5Q0f7) === signed(0x40)

@test bitstring(reinterpret(Q0f7, signed(0xa2))) === "10100010"
@test bitstring(reinterpret(Q5f10, signed(0x00a2))) === "0000000010100010"
end

@testset "masks" begin
Expand Down
5 changes: 4 additions & 1 deletion test/normed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ using FixedPointNumbers: bitwidth
@test_throws DomainError zero(Normed{UInt16,17})
end

@testset "reinterpret" begin
@testset "reinterpret/bitstring" begin
@test reinterpret(N0f8, 0xa2).i === 0xa2
@test reinterpret(N6f10, 0x1fa2).i === 0x1fa2
@test reinterpret(N4f12, 0x1fa2).i === 0x1fa2
Expand All @@ -23,6 +23,9 @@ end

@test reinterpret(UInt8, 1N0f8) === 0xff

@test bitstring(reinterpret(N0f8, 0xa2)) === "10100010"
@test bitstring(reinterpret(N6f10, 0x00a2)) === "0000000010100010"

@test 0.635N0f8 == N0f8(0.635)
@test 0.635N6f10 == N6f10(0.635)
@test 0.635N4f12 == N4f12(0.635)
Expand Down

0 comments on commit 7d881ec

Please sign in to comment.