Boost logo

Boost Users :

Subject: Re: [Boost-users] [proto] Matching grammars in the presence of adapted types
From: Joel Falcou (joel.falcou_at_[hidden])
Date: 2010-06-04 11:13:25


Sebastian Redl wrote:
> Hi,
>
> First off, this whole thing uses Boost 1.42.
>
> I'm trying to define arithmetic types where some expression patterns will
> be matched and replaced by something else (e.g. a & ~b, the andnot
> pattern). The whole thing is a proof-of-concept and teach-myself-proto
> thing that should eventually do SIMD operations. Right now, though, it
> simply wraps integers.
>

Very laudable goal ;) I think i can give you some hints :p

> What can I do? Can I teach matches<> to handle adapted types? Is there a
> "right" way to implement smartint so that it needn't be an adapted type,
> which would work around the whole issue? Is this a bug in Proto that is
> fixed in trunk, or can be fixed?

You need a grammar transform, not a context.

struct optimize_andnot :
or_<
  where< proto::bitwise_and<proto::_, proto::complement<proto::_>>
       , your_optimized_andnot_expression<type>( left_(_),child0_(right_(_))
>
, otherwise<_>
> {};

(more or less typos)

Basically if yopu match a & ~b, you extarct left_ of the expression (ie
a) then the child of the right part (ie b) and you construct a new node
from that.

Applyting this transform is done like:

optimize_andnot optimizer;
auto optimized = optimizer(your_expression);

should return you the modified AST where a & ~b have been replaced
everywhere by andnot(a,b).

That's how we handle all the fused operations in NT² including vec_madd
on altivec and such.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net