Skip to content

Instantly share code, notes, and snippets.

@ityonemo
ityonemo / tablethis-xp.js
Created April 9, 2015 23:26
readable and marginally commented version of http://scripts.shpg.org/TableThis.js
var $$;
if(!Array.prototype.forEach){
Array.prototype.forEach = function(fun){
var len=this.length;
if(typeof fun!=="function"){
throw new TypeError();
}
var thisp = arguments[1];
@ityonemo
ityonemo / testing-stuff-from-the-book.jl
Created August 14, 2015 01:41
testing things from end of error
#test-things-from-the-book.jl
include("unum.jl")
using Unums
using Base.Test
#test various things from the book.
#are our representations of unums correct?
one16 = one(Uint16)
@ityonemo
ityonemo / unum-constructor-proposal.jl
Created November 3, 2015 22:57
squaring the circle to make variadic unums have both sane construction and high performance
abstract Unum{ESS, FSS}
#define an outer constructor on this type.
type Unum_Small{ESS, FSS} <: Unum{ESS, FSS}
fsize::UInt16
esize::UInt16
flags::UInt16
fraction::UInt64
exponent::UInt64
@ityonemo
ityonemo / cleaninject.jl
Last active November 6, 2015 03:36
macros that cleanly Injecting code into a function's AST in julia
#cleaninject.jl
#cleanly injects some code into the AST of a function.
function code_to_inject()
println("this code is injected")
end
function code_to_inject(a,b)
println("injected code handles $a and $b")
doc"""
`gen_code` rejigs the standard julia `@generate` macro so that it creates a `code`
expression variable that can be extended using the `@code` macro. At the end of
the function it automatically outputs the result.
"""
macro gen_code(f)
#make sure this macro precedes a function definition.
isa(f, Expr) || error("gen_code macro must precede a function definition")
(f.head == :function) || error("gen_code macro must precede a function definition")
using Base.Test
module X
macro id(v)
quote
$v
end
end
export @id
end
@ityonemo
ityonemo / funwithsign.jl
Created December 15, 2015 09:24
sign is faster without branches.
code_native(sign,(Float64,))
## note two jbe opcodes. There are two jumps!
arry = rand(10000000) - 0.5
@time map(sign, arry)
# 0.38s
function sgn2(x::Float64)
xp::UInt64 = reinterpret(UInt64, x)
yp::UInt64 = ((xp & 0x7FFF_FFFF_0000_0000) >> 32) | (xp & 0x0000_0000_FFFF_FFFF)
yp = (yp & 0xFFFF_0000 >> 16) | (yp & 0x0000_FFFF)
yp = (yp & 0xFF00 >> 8) | (yp & 0x00FF)
@ityonemo
ityonemo / printlndebug.jl
Created January 30, 2016 00:23
Println debugging in julia
@macro printlndebug
println("println")
end
#use:
function donothing()
@printlndebug
nothing
end
@ityonemo
ityonemo / 64to16.jl
Last active June 5, 2016 01:18
converting to unsigned 16 bit integers in julia
# converting from Int64 to UInt16 in Julia can result in a minefield of inexacterrors,
# which could be performance-debilitating (if you care about that).
code_native(Int16, (Int64,))
# ==>(edited)
# pushq %rbp
# movq %rsp, %rbp
# movswq %si, %rax
# cmpq %rsi, %rax
@ityonemo
ityonemo / lookup_table_lemma.md
Last active August 18, 2016 01:16
Proof that lattice lookups only need exact values.

Definitions:

ℝᵖ - projective reals. ℝᵖ/L - projective reals represented by a Type2 Unum Lattice L. The elements are intervals, which will be denoted as x̅. An element of may also be an exact value or an ulp value, which will be denoted as ẋ.

O:(ℝᵖ/L)ⁿ → ℝᵖ/L is an implementation of an operation o:(ℝᵖ)ⁿ → ℝᵖ. The implementation must have the property o(x) ∈ O(ẋ), where ẋ is the unique ulp