Skip to content

Commit

Permalink
zero allocation in normalize.
Browse files Browse the repository at this point in the history
  • Loading branch information
pvazteixeira committed Sep 7, 2018
1 parent bb15351 commit 5ce33b7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
32 changes: 17 additions & 15 deletions src/TransformUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,10 @@ function normalize!(q::Quaternion, tol=1e-6)
s += q.v[i]^2
end
@fastmath mag1 = 1.0/sqrt(s)
@fastmath @inbounds for i in 1:3
q.v[i] .*= mag1
end
# @fastmath @inbounds for i in 1:3
# q.v[i] *= mag1
# end
@fastmath @inbounds q.v .*= mag1
@fastmath q.s *= mag1
nothing
end
Expand Down Expand Up @@ -345,17 +346,18 @@ function convert!(R::SO3, q::Quaternion)
q.s = -q.s
q.v[1:3] = -q.v[1:3]
end
nrm = sqrt(q.s^2+sum(q.v[1:3].^2))
if (nrm < 0.999)
println("q2C -- not a unit quaternion nrm = $(nrm)")
R = eye(3)
else
nrm = 1.0/nrm
q.s *= nrm
q.v[1:3] .*= nrm
# x = x*nrm
# y = y*nrm
# z = z*nrm
# nrm = sqrt(q.s^2+sum(q.v[1:3].^2))
# if (nrm < 0.999)
# println("q2C -- not a unit quaternion nrm = $(nrm)")
# R = eye(3)
# else
normalize!(q)
# nrm = 1.0/nrm
# q.s *= nrm
# q.v .*= nrm
# # x = x*nrm
# # y = y*nrm
# # z = z*nrm
w2, x2, y2, z2 = q.s*q.s, q.v[1]*q.v[1], q.v[2]*q.v[2], q.v[3]*q.v[3]
# y2 = y*y
# z2 = z*z
Expand All @@ -377,7 +379,7 @@ function convert!(R::SO3, q::Quaternion)
R.R[3,1] = xz-wy
R.R[3,2] = yz+wx
R.R[3,3] = w2-x2-y2+z2
end
# end
end
# return SO3(R)
nothing
Expand Down
18 changes: 16 additions & 2 deletions test/testInPlaceConvert.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ function normalize!(q::Quaternion, tol=1e-6)
end


q = convert(Quaternion, so3(randn(3)))

@code_warntype normalize!(q)

Expand All @@ -94,8 +93,23 @@ q = convert(Quaternion, so3(randn(3)))


q = convert(Quaternion, so3(randn(3)))
E = convert(Euler, convert(SO3, so3(randn(3))))



function convert!(q::Quaternion, E::Euler)
@btime convert!(q, E)

#
Profile.clear()
@profiler convert!(q, E)


Profile.clear()
@profiler for i in 1:10^6 convert!(q, E); end




@code_warntype convert!(q, E)

# julia --track-allocation=user testInPlaceConvert.jl

0 comments on commit 5ce33b7

Please sign in to comment.