Skip to content

Instantly share code, notes, and snippets.

@fcard
Created December 6, 2016 20:26
Show Gist options
  • Select an option

  • Save fcard/31b6807a2b7698536a10dfd08b72aa13 to your computer and use it in GitHub Desktop.

Select an option

Save fcard/31b6807a2b7698536a10dfd08b72aa13 to your computer and use it in GitHub Desktop.
Insert muladds in `a1*a1 + a2*b2 + ...` expressions
module Muladd
macro muladd(ex)
@assert ex.head == :call
@assert ex.args != [:+]
@assert ex.args[1] == :+
esc(_muladd_meta(ex))
end
function _muladd_meta(ex)
if length(ex.args) == 2
return ex.args[2]
else
a, b = ex.args[2].args[2:end]
rest = _muladd_meta(Expr(ex.head, :+, ex.args[3:end]...))
return :($(Base.muladd)($a, $b, $rest))
end
end
end
@fcard

fcard commented Dec 6, 2016

Copy link
Copy Markdown
Author

Version with informative names/functions, error handling, tests and all that: https://gist.github.com/fcard/12a49827cc26a197d4d1e75481216176

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment