Created
November 14, 2012 00:18
-
-
Save JeffreySarnoff/4069329 to your computer and use it in GitHub Desktop.
An always unsigned Unsigned Integer Type for Julia
This file contains hidden or 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
| >>>> please see https://gist.github.com/4070761 | |
| >>>> and ignore this gist | |
| # Unsigned32.jl | |
| # | |
| # A very preliminary cut at an always unsigned fixed bitwidth type | |
| # | |
| # use : Just require this file, it self imports. | |
| # myUnsigned32value = unsd32(number) | |
| # typeof(myUnsigned32value) == Unsd32 | |
| # | |
| # exports: Unsd32, unsd32, | |
| # ~, &, |, $, <<, >>, >>>, | |
| # ==, !=, >=, <, >, <=, | |
| # - , +, *, /, %, ^, | |
| # mod, rem, div, fld, | |
| # leading_zeros, leading_ones, trailing_zeros, trailing_ones | |
| # | |
| # author: Jeffrey Sarnoff | |
| # date : 2012-11-13 19:10:00 America/New_York | |
| # | |
| # status: Mostly Untested. Suggestions Are Welcome. | |
| module Unsigned32 | |
| using Base | |
| import Base.~ , | |
| Base.& , | |
| Base.| , | |
| Base.$ , | |
| Base.>> , | |
| Base.<< , | |
| Base.>>> , | |
| Base.== , | |
| Base.!= , | |
| Base.<= , | |
| Base.> , | |
| Base.< , | |
| Base.>= , | |
| Base.- , | |
| Base.+ , | |
| Base.* , | |
| Base./ , | |
| Base.% , | |
| Base.mod , | |
| Base.rem , | |
| Base.div , | |
| Base.fld , | |
| Base.^ , | |
| Base.leading_zeros , | |
| Base.leading_ones , | |
| Base.trailing_zeros , | |
| Base.trailing_ones , | |
| Base.Uint8 , | |
| Base.Uint16 , | |
| Base.Uint32 , | |
| Base.Uint64 , | |
| Base.Uint128 , | |
| Base.uint8 , | |
| Base.uint16 , | |
| Base.uint32 , | |
| Base.uint64 , | |
| Base.uint128 , | |
| Base.Int8 , | |
| Base.Int16 , | |
| Base.Int32 , | |
| Base.Int64 , | |
| Base.Int128 , | |
| Base.convert , | |
| Base.promote_rule | |
| export | |
| Unsd32, unsd32, show, print, convert, | |
| ~, &, |, $, <<, >>, >>>, ==, !=, >=, <, >, <=, | |
| - , +, *, /, %, mod, rem, div, fld, ^, | |
| leading_zeros, leading_ones, trailing_zeros, trailing_ones | |
| # PROBLEM DEFINING AND EXPORTING === | |
| bitstype 32 Unsd32 <: Unsigned | |
| convert(::Type{Unsd32} , u::Uint8) = reinterpret(Unsd32,u) | |
| convert(::Type{Uint8} , u::Unsd32) = reinterpret(Uint32,u) | |
| convert(::Type{Uint16} , u::Unsd32) = uint16 (reinterpret(Uint32, u)) | |
| convert(::Type{Uint32} , u::Unsd32) = uint32 (reinterpret(Uint32, u)) | |
| convert(::Type{Uint64} , u::Unsd32) = uint64 (reinterpret(Uint32, u)) | |
| convert(::Type{Uint128}, u::Unsd32) = uint128(reinterpret(Uint32, u)) | |
| convert(::Type{Unsd32} , u::Uint16) = reinterpret(Unsd32, uint32(u)) | |
| convert(::Type{Unsd32} , u::Uint32) = reinterpret(Unsd32, uint32(u)) | |
| convert(::Type{Unsd32} , u::Uint64) = reinterpret(Unsd32, uint32(u)) | |
| convert(::Type{Unsd32} , u::Uint128) = reinterpret(Unsd32, uint32(u)) | |
| convert(::Type{Unsd32} , s::Int8) = reinterpret(Unsd32, reinterpret(Uint32,s)) | |
| convert(::Type{Unsd32} , s::Int16) = reinterpret(Unsd32, uint8(reinterpret(Uint16,s))) | |
| convert(::Type{Unsd32} , s::Int32) = reinterpret(Unsd32, uint8(reinterpret(Uint32,s))) | |
| convert(::Type{Unsd32} , s::Int64) = reinterpret(Unsd32, uint8(reinterpret(Uint64,s))) | |
| convert(::Type{Unsd32} , s::Int128) = reinterpret(Unsd32, uint8(reinterpret(Uint128,s))) | |
| unsd32 (u::Unsd32) = u | |
| unsd32 (u::Uint8) = convert(Unsd32, u) | |
| unsd32 (u::Uint16) = convert(Unsd32, u) | |
| unsd32 (u::Uint32) = convert(Unsd32, u) | |
| unsd32 (u::Uint64) = convert(Unsd32, u) | |
| unsd32 (u::Uint128) = convert(Unsd32, u) | |
| unsd32 (s::Int8) = convert(Unsd32, s) | |
| unsd32 (s::Int16) = convert(Unsd32, s) | |
| unsd32 (s::Int32) = convert(Unsd32, s) | |
| unsd32 (s::Int64) = convert(Unsd32, s) | |
| unsd32 (s::Int128) = convert(Unsd32, s) | |
| promote_rule(::Type{Unsd32}, ::Type{Uint8}) = Unsd32 | |
| promote_rule(::Type{Unsd32}, ::Type{Uint16 }) = Unsd32 # Uint16 | |
| promote_rule(::Type{Unsd32}, ::Type{Uint32 }) = Unsd32 # Uint32 | |
| promote_rule(::Type{Unsd32}, ::Type{Uint64 }) = Unsd32 # Uint64 | |
| promote_rule(::Type{Unsd32}, ::Type{Uint128}) = Unsd32 # Uint128 | |
| promote_rule(::Type{Unsd32}, ::Type{Int8}) = Unsd32 | |
| promote_rule(::Type{Unsd32}, ::Type{Int16 }) = Unsd32 # Int16 | |
| promote_rule(::Type{Unsd32}, ::Type{Int32 }) = Unsd32 # Int32 | |
| promote_rule(::Type{Unsd32}, ::Type{Int64 }) = Unsd32 # Int64 | |
| promote_rule(::Type{Unsd32}, ::Type{Int128}) = Unsd32 # Int128 | |
| leading_zeros (u::Unsd32) = leading_zeros (reinterpret(Uint32,u)) | |
| leading_ones (u::Unsd32) = leading_ones (reinterpret(Uint32,u)) | |
| trailing_zeros(u::Unsd32) = trailing_zeros(reinterpret(Uint32,u)) | |
| trailing_ones (u::Unsd32) = trailing_ones (reinterpret(Uint32,u)) | |
| show (io::IO, u::Unsd32) = show (io, reinterpret(Uint32,u) ) | |
| print(io::IO, u::Unsd32) = print(io, reinterpret(Uint32,u) ) | |
| (~)(u::Unsd32 ) = box(Unsd32,not_int(unbox(Unit8,box(Uint8,u)))) | |
| (-)(u::Unsd32 ) = box(Unsd32,neg_int(unbox(Unsd32,u))) | |
| (&)(u::Unsd32, v::Unsd32 ) = box(Unsd32,and_int(unbox(Unsd32,u),unbox(Unsd32,v))) | |
| (|)(u::Unsd32, v::Unsd32 ) = box(Unsd32,or_int(unbox(Unsd32,u),unbox(Unsd32,v))) | |
| ($)(u::Unsd32, v::Unsd32 ) = box(Unsd32,xor_int(unbox(Unsd32,u),unbox(Unsd32,v))) | |
| (<<) (u::Unsd32, v::Unsd32 ) = box(Unsd32, shl_int(unbox(Unsd32,u), unbox(Unsd32,v))) | |
| (>>) (u::Unsd32, v::Unsd32 ) = box(Unsd32, lshr_int(unbox(Unsd32,u), unbox(Unsd32,v))) | |
| (>>>)(u::Unsd32, v::Unsd32 ) = box(Unsd32, lshr_int(unbox(Unsd32,u), unbox(Unsd32,v))) | |
| (<<) (u::Unsd32, v::Uint8 ) = box(Unsd32, shl_int(unbox(Unsd32,u), unbox(Uint8 ,v))) | |
| (>>) (u::Unsd32, v::Uint8 ) = box(Unsd32, lshr_int(unbox(Unsd32,u), unbox(Uint8 ,v))) | |
| (>>>)(u::Unsd32, v::Uint8 ) = box(Unsd32, lshr_int(unbox(Unsd32,u), unbox(Uint8 ,v))) | |
| (<<) (u::Unsd32, v::Uint32 ) = box(Unsd32, shl_int(unbox(Unsd32,u), unbox(Uint32,v))) | |
| (>>) (u::Unsd32, v::Uint32 ) = box(Unsd32, lshr_int(unbox(Unsd32,u), unbox(Uint32,v))) | |
| (>>>)(u::Unsd32, v::Uint32 ) = box(Unsd32, lshr_int(unbox(Unsd32,u), unbox(Uint32,v))) | |
| (<<) (u::Unsd32, v::Uint64 ) = box(Unsd32, shl_int(unbox(Unsd32,u), unbox(Uint64,v))) | |
| (>>) (u::Unsd32, v::Uint64 ) = box(Unsd32, lshr_int(unbox(Unsd32,u), unbox(Uint64,v))) | |
| (>>>)(u::Unsd32, v::Uint64 ) = box(Unsd32, lshr_int(unbox(Unsd32,u), unbox(Uint64,v))) | |
| (<<) (u::Unsd32, v::Int32 ) = box(Unsd32, shl_int(unbox(Unsd32,u), unbox( Int32,v))) | |
| (>>) (u::Unsd32, v::Int32 ) = box(Unsd32, lshr_int(unbox(Unsd32,u), unbox( Int32,v))) | |
| (>>>)(u::Unsd32, v::Int32 ) = box(Unsd32, lshr_int(unbox(Unsd32,u), unbox( Int32,v))) | |
| (<<) (u::Unsd32, v::Int64 ) = box(Unsd32, shl_int(unbox(Unsd32,u), unbox( Int64,v))) | |
| (>>) (u::Unsd32, v::Int64 ) = box(Unsd32, lshr_int(unbox(Unsd32,u), unbox( Int64,v))) | |
| (>>>)(u::Unsd32, v::Int64 ) = box(Unsd32, lshr_int(unbox(Unsd32,u), unbox( Int64,v))) | |
| (==)(u::Unsd32, v::Unsd32) = | |
| eq_int(unbox(Uint8,box(Uint8,unbox(Unsd32,u))), unbox(Uint8,box(Uint8,unbox(Unsd32,v)))) | |
| (!=)(u::Unsd32, v::Unsd32) = | |
| neq_int(unbox(Uint8,box(Uint8,unbox(Unsd32,u))), unbox(Uint8,box(Uint8,unbox(Unsd32,v)))) | |
| (<)(u::Unsd32, v::Unsd32) = | |
| ult_int(unbox(Uint8,box(Uint8,unbox(Unsd32,u))), unbox(Uint8,box(Uint8,unbox(Unsd32,v)))) | |
| (<=)(u::Unsd32, v::Unsd32) = | |
| ule_int(unbox(Uint8,box(Uint8,unbox(Unsd32,u))), unbox(Uint8,box(Uint8,unbox(Unsd32,v)))) | |
| (>)(u::Unsd32, v::Unsd32) = !(u <= v) | |
| (>=)(u::Unsd32, v::Unsd32) = !(u < v) | |
| # (===)(u::Unsd32, v::Unsd32) = (===)(box(Uint8,unbox(Unsd32,u)),box(Uint8,unbox(Unsd32,v))) | |
| #invalid method definition: not a generic function | |
| doubbits(Uint32) = Uint64 | |
| quadbits(Uint32) = Uint128 | |
| (+)(u::Unsd32, v::Unsd32 ) = box(Unsd32,add_int(unbox(Unsd32,u),unbox(Unsd32,v))) | |
| (-)(u::Unsd32, v::Unsd32 ) = box(Unsd32,sub_int(unbox(Unsd32,u),unbox(Unsd32,v))) | |
| (*)(u::Unsd32, v::Unsd32) = convert(Unsd32, (*)(convert(doubbits(Uint8),u), convert(doubbits(Uint8),v))) | |
| (/)(u::Unsd32, v::Unsd32) = convert(Unsd32, (div)(convert(Uint8,u), convert(Uint8,v))) | |
| (%)(u::Unsd32, v::Unsd32) = convert(Unsd32, (%)(convert(Uint8,u), convert(Uint8,v))) | |
| (^)(u::Unsd32, v::Unsd32) = convert(Unsd32, (^)(convert(quadbits(Uint8),u), convert(quadbits(Uint8),v))) | |
| (div)(u::Unsd32, v::Unsd32) = convert(Unsd32, (div)(convert(Uint8,u), convert(Uint8,v))) | |
| (mod)(u::Unsd32, v::Unsd32) = convert(Unsd32, (mod)(convert(Uint8,u), convert(Uint8,v))) | |
| (rem)(u::Unsd32, v::Unsd32) = convert(Unsd32, (rem)(convert(Uint8,u), convert(Uint8,v))) | |
| (fld)(u::Unsd32, v::Unsd32) = convert(Unsd32, (fld)(convert(Uint8,u), convert(Uint8,v))) | |
| end # module | |
| using Unsigned32 | |
| import Unsigned32.Unsd32, Unsigned32.unsd32, | |
| Unsigned32.show, Unsigned32.print, Unsigned32.convert, | |
| Unsigned32.~, Unsigned32.&, Unsigned32.|, Unsigned32.$, | |
| Unsigned32.<<, Unsigned32.>>, Unsigned32.>>>, | |
| Unsigned32.-, Unsigned32.+, Unsigned32.*, Unsigned32./, Unsigned32.%, | |
| Unsigned32.mod, Unsigned32.div, Unsigned32.rem, Unsigned32.fld, | |
| Unsigned32.leading_zeros, Unsigned32.leading_ones, | |
| Unsigned32.trailing_zeros, Unsigned32.trailing_ones |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment