Skip to content

Instantly share code, notes, and snippets.

@JeffreySarnoff
Created November 14, 2012 00:39
Show Gist options
  • Save JeffreySarnoff/4069426 to your computer and use it in GitHub Desktop.
Save JeffreySarnoff/4069426 to your computer and use it in GitHub Desktop.
An always unsigned always 8bit type
>>>> please see https://gist.github.com/4070758
>>>> and ignore this gist
# Unsigned8.jl
#
# A very preliminary cut at an always unsigned fixed bitwidth type
#
# use : Just require this file, it self imports.
# myUnsigned8value = unsd8(number)
# typeof(myUnsigned8value) == Unsd8
#
# exports: Unsd8, unsd8,
# ~, &, |, $, <<, >>, >>>,
# ==, !=, >=, <, >, <=,
# - , +, *, /, %, ^,
# 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 Unsigned8
using Base
import Base.~ ,
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 ,
export
Unsd8, unsd8, show, print, convert,
~, &, |, $, <<, >>, >>>, ==, !=, >=, <, >, <=,
-, +, *, /, ^, %, mod, rem, div, fld,
leading_zeros, leading_ones, trailing_zeros, trailing_ones
# PROBLEM DEFINING AND EXPORTING ===
bitstype 8 Unsd8 <: Unsigned
convert(::Type{Unsd8} , u::Uint8) = reinterpret(Unsd8,u)
convert(::Type{Uint8} , u::Unsd8) = reinterpret(Uint8,u)
convert(::Type{Uint16} , u::Unsd8) = uint16 (reinterpret(Uint8, u))
convert(::Type{Uint32} , u::Unsd8) = uint32 (reinterpret(Uint8, u))
convert(::Type{Uint64} , u::Unsd8) = uint64 (reinterpret(Uint8, u))
convert(::Type{Uint128}, u::Unsd8) = uint128(reinterpret(Uint8, u))
convert(::Type{Unsd8} , u::Uint16) = reinterpret(Unsd8, uint8(u))
convert(::Type{Unsd8} , u::Uint32) = reinterpret(Unsd8, uint8(u))
convert(::Type{Unsd8} , u::Uint64) = reinterpret(Unsd8, uint8(u))
convert(::Type{Unsd8} , u::Uint128) = reinterpret(Unsd8, uint8(u))
convert(::Type{Unsd8} , s::Int8) = reinterpret(Unsd8, reinterpret(Uint8,s))
convert(::Type{Unsd8} , s::Int16) = reinterpret(Unsd8, uint8(reinterpret(Uint16,s)))
convert(::Type{Unsd8} , s::Int32) = reinterpret(Unsd8, uint8(reinterpret(Uint32,s)))
convert(::Type{Unsd8} , s::Int64) = reinterpret(Unsd8, uint8(reinterpret(Uint64,s)))
convert(::Type{Unsd8} , s::Int128) = reinterpret(Unsd8, uint8(reinterpret(Uint128,s)))
unsd8 (u::Unsd8) = u
unsd8 (u::Uint8) = convert(Unsd8, u)
unsd8 (u::Uint16) = convert(Unsd8, u)
unsd8 (u::Uint32) = convert(Unsd8, u)
unsd8 (u::Uint64) = convert(Unsd8, u)
unsd8 (u::Uint128) = convert(Unsd8, u)
unsd8 (s::Int8) = convert(Unsd8, s)
unsd8 (s::Int16) = convert(Unsd8, s)
unsd8 (s::Int32) = convert(Unsd8, s)
unsd8 (s::Int64) = convert(Unsd8, s)
unsd8 (s::Int128) = convert(Unsd8, s)
promote_rule(::Type{Unsd8}, ::Type{Uint8}) = Unsd8
promote_rule(::Type{Unsd8}, ::Type{Uint16 }) = Unsd8 # Uint16
promote_rule(::Type{Unsd8}, ::Type{Uint32 }) = Unsd8 # Uint32
promote_rule(::Type{Unsd8}, ::Type{Uint64 }) = Unsd8 # Uint64
promote_rule(::Type{Unsd8}, ::Type{Uint128}) = Unsd8 # Uint128
promote_rule(::Type{Unsd8}, ::Type{Int8}) = Unsd8
promote_rule(::Type{Unsd8}, ::Type{Int16 }) = Unsd8 # Int16
promote_rule(::Type{Unsd8}, ::Type{Int32 }) = Unsd8 # Int32
promote_rule(::Type{Unsd8}, ::Type{Int64 }) = Unsd8 # Int64
promote_rule(::Type{Unsd8}, ::Type{Int128}) = Unsd8 # Int128
leading_zeros (u::Unsd8) = leading_zeros (reinterpret(Uint8,u))
leading_ones (u::Unsd8) = leading_ones (reinterpret(Uint8,u))
trailing_zeros(u::Unsd8) = trailing_zeros(reinterpret(Uint8,u))
trailing_ones (u::Unsd8) = trailing_ones (reinterpret(Uint8,u))
show (io::IO, u::Unsd8) = show (io, reinterpret(Uint8,u) )
print(io::IO, u::Unsd8) = print(io, reinterpret(Uint8,u) )
(~)(u::Unsd8 ) = box(Unsd8,not_int(unbox(Unit8,box(Uint8,u))))
(-)(u::Unsd8 ) = box(Unsd8,neg_int(unbox(Unsd8,u)))
(&)(u::Unsd8, v::Unsd8 ) = box(Unsd8,and_int(unbox(Unsd8,u),unbox(Unsd8,v)))
(|)(u::Unsd8, v::Unsd8 ) = box(Unsd8,or_int(unbox(Unsd8,u),unbox(Unsd8,v)))
($)(u::Unsd8, v::Unsd8 ) = box(Unsd8,xor_int(unbox(Unsd8,u),unbox(Unsd8,v)))
(<<) (u::Unsd8, v::Unsd8 ) = box(Unsd8, shl_int(unbox(Unsd8,u), unbox(Unsd8,v)))
(>>) (u::Unsd8, v::Unsd8 ) = box(Unsd8, lshr_int(unbox(Unsd8,u), unbox(Unsd8,v)))
(>>>)(u::Unsd8, v::Unsd8 ) = box(Unsd8, lshr_int(unbox(Unsd8,u), unbox(Unsd8,v)))
(<<) (u::Unsd8, v::Uint8 ) = box(Unsd8, shl_int(unbox(Unsd8,u), unbox(Uint8 ,v)))
(>>) (u::Unsd8, v::Uint8 ) = box(Unsd8, lshr_int(unbox(Unsd8,u), unbox(Uint8 ,v)))
(>>>)(u::Unsd8, v::Uint8 ) = box(Unsd8, lshr_int(unbox(Unsd8,u), unbox(Uint8 ,v)))
(<<) (u::Unsd8, v::Uint32 ) = box(Unsd8, shl_int(unbox(Unsd8,u), unbox(Uint32,v)))
(>>) (u::Unsd8, v::Uint32 ) = box(Unsd8, lshr_int(unbox(Unsd8,u), unbox(Uint32,v)))
(>>>)(u::Unsd8, v::Uint32 ) = box(Unsd8, lshr_int(unbox(Unsd8,u), unbox(Uint32,v)))
(<<) (u::Unsd8, v::Uint64 ) = box(Unsd8, shl_int(unbox(Unsd8,u), unbox(Uint64,v)))
(>>) (u::Unsd8, v::Uint64 ) = box(Unsd8, lshr_int(unbox(Unsd8,u), unbox(Uint64,v)))
(>>>)(u::Unsd8, v::Uint64 ) = box(Unsd8, lshr_int(unbox(Unsd8,u), unbox(Uint64,v)))
(<<) (u::Unsd8, v::Int32 ) = box(Unsd8, shl_int(unbox(Unsd8,u), unbox( Int32,v)))
(>>) (u::Unsd8, v::Int32 ) = box(Unsd8, lshr_int(unbox(Unsd8,u), unbox( Int32,v)))
(>>>)(u::Unsd8, v::Int32 ) = box(Unsd8, lshr_int(unbox(Unsd8,u), unbox( Int32,v)))
(<<) (u::Unsd8, v::Int64 ) = box(Unsd8, shl_int(unbox(Unsd8,u), unbox( Int64,v)))
(>>) (u::Unsd8, v::Int64 ) = box(Unsd8, lshr_int(unbox(Unsd8,u), unbox( Int64,v)))
(>>>)(u::Unsd8, v::Int64 ) = box(Unsd8, lshr_int(unbox(Unsd8,u), unbox( Int64,v)))
(==)(u::Unsd8, v::Unsd8) =
eq_int(unbox(Uint8,box(Uint8,unbox(Unsd8,u))), unbox(Uint8,box(Uint8,unbox(Unsd8,v))))
(!=)(u::Unsd8, v::Unsd8) =
neq_int(unbox(Uint8,box(Uint8,unbox(Unsd8,u))), unbox(Uint8,box(Uint8,unbox(Unsd8,v))))
(<)(u::Unsd8, v::Unsd8) =
ult_int(unbox(Uint8,box(Uint8,unbox(Unsd8,u))), unbox(Uint8,box(Uint8,unbox(Unsd8,v))))
(<=)(u::Unsd8, v::Unsd8) =
ule_int(unbox(Uint8,box(Uint8,unbox(Unsd8,u))), unbox(Uint8,box(Uint8,unbox(Unsd8,v))))
(>)(u::Unsd8, v::Unsd8) = !(u <= v)
(>=)(u::Unsd8, v::Unsd8) = !(u < v)
# (===)(u::Unsd8, v::Unsd8) = (===)(box(Uint8,unbox(Unsd8,u)),box(Uint8,unbox(Unsd8,v)))
#invalid method definition: not a generic function
doubbits(Uint8) = Uint16
quadbits(Uint8) = Uint32
(+)(u::Unsd8, v::Unsd8 ) = box(Unsd8,add_int(unbox(Unsd8,u),unbox(Unsd8,v)))
(-)(u::Unsd8, v::Unsd8 ) = box(Unsd8,sub_int(unbox(Unsd8,u),unbox(Unsd8,v)))
#(+)(u::Unsd8, v::Unsd8) = convert(Unsd8, (+)(convert(doubbits(Uint8),u), convert(doubbits(Uint8),v)))
#(-)(u::Unsd8, v::Unsd8) = convert(Unsd8, (-)(convert(Uint8,u), convert(Uint8,v)))
(*)(u::Unsd8, v::Unsd8) = convert(Unsd8, (*)(convert(doubbits(Uint8),u), convert(doubbits(Uint8),v)))
(/)(u::Unsd8, v::Unsd8) = convert(Unsd8, (div)(convert(Uint8,u), convert(Uint8,v)))
(%)(u::Unsd8, v::Unsd8) = convert(Unsd8, (%)(convert(Uint8,u), convert(Uint8,v)))
(^)(u::Unsd8, v::Unsd8) = convert(Unsd8, (^)(convert(quadbits(Uint8),u), convert(quadbits(Uint8),v)))
(div)(u::Unsd8, v::Unsd8) = convert(Unsd8, (div)(convert(Uint8,u), convert(Uint8,v)))
(mod)(u::Unsd8, v::Unsd8) = convert(Unsd8, (mod)(convert(Uint8,u), convert(Uint8,v)))
(rem)(u::Unsd8, v::Unsd8) = convert(Unsd8, (rem)(convert(Uint8,u), convert(Uint8,v)))
(fld)(u::Unsd8, v::Unsd8) = convert(Unsd8, (fld)(convert(Uint8,u), convert(Uint8,v)))
end # module
using Unsigned8
import Unsigned8.Unsd8, Unsigned8.unsd8, Unsigned8.show,
Unsigned8.print, Unsigned8.convert,
Unsigned8.~,Unsigned8.&,Unsigned8.|,Unsigned8.$,
Unsigned8.<<,Unsigned8.>>,Unsigned8.>>>,
Unsigned8.-,Unsigned8.+,Unsigned8.*,Unsigned8./,Unsigned8.%,
Unsigned8.mod,Unsigned8.div,Unsigned8.rem,Unsigned8.fld,
Unsigned8.leading_zeros,Unsigned8.leading_ones,
Unsigned8.trailing_zeros,Unsigned8.trailing_ones
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment