in reply to Complicated Search and Replace

I'm sort of wondering what you need all the parentheses for, given that order of operations makes them unnecessary. But here you are:
use strict; use warnings; $_ = join '', <DATA>; s/\@MAC\((\w+),(\w+),(\w+),(\d+)\)/"$1 =\n".MAC($2,$3,$4)/eg; print; sub MAC { my ($x, $y, $n) = @_; $_ = ':'; $_ = "($_)+($_)" while ($n /= 2) != .5; $n = 0; s/:/"$x$n*$y".$n++/eg; return $_; } __DATA__ @MAC(Result,X,Y,1); @MAC(Result,X,Y,2); @MAC(Result,X,Y,4); @MAC(Result,X,Y,8);

Replies are listed 'Best First'.
Re^2: Complicated Search and Replace
by gantlord (Initiate) on Jun 28, 2005 at 10:58 UTC
    Yeah, if I was just running this as ordinary C code the parentheses would be superfluous. What's happening is that the parentheses are used to let a C-to-VHDL compiler get the idea of what kind of adder structure I want. I have a series of floating point multipliers at the base level. The output of two multipliers is summed together in a 2-input floating-point addition unit. The output of these additions are summed again in pairs in a further layer of adders. This continues with the adders halving each time until you finish up with a single adder and your output. This allows all the multiplication and addition to take place in parallel. Sweet, eh?

    A0 B0 A1 B1 A2 B2 A3 B3 \ / \ / \ / \ / (*) (*) (*) (*) \ / \ / \ / \ / (+) (+) \ / \ / \ / \ / \ / \ / (+) | Output