Optimizer - Replace constant inputs with constants

This commit is contained in:
Emmanuel BENOîT 2017-12-01 23:08:05 +01:00
parent 8f21177058
commit 3de17b918d
3 changed files with 47 additions and 6 deletions

View file

@ -135,12 +135,23 @@ P_ExpressionNode T_ConstantFolder_::checkExpression(
// Replace inputs with value if no curve/constant curve // Replace inputs with value if no curve/constant curve
if ( node.type( ) == A_Node::EXPR_INPUT ) { if ( node.type( ) == A_Node::EXPR_INPUT ) {
// TODO: may be replaced with either a constant or a variable. if ( !curves ) {
// * If the curve exists and describes a constant, it's a return {};
// constant }
// * If there is no curve and only one default value in the
// whole program then it's also a constant auto& n{ (T_InputExprNode&) node };
// * No curve, multiple defaults -> variable auto const* const curve{ curves->curves.get( n.id( ) ) };
if ( curve ) {
// Curve present, check if it's constant
const auto cval{ curve->isConstant( ) };
if ( !cval ) {
return {};
}
return NewOwned< T_ConstantExprNode >( node.parent( ) , *cval );
}
// TODO: check whether there's only one default value; if that's the case,
// well, we got ourselves a constant.
return {}; return {};
} }

View file

@ -214,6 +214,17 @@ float T_SyncSegment::computeValue(
return v * ( values[ part + 1 ] - sv ) + sv; return v * ( values[ part + 1 ] - sv ) + sv;
} }
T_Optional< float > T_SyncSegment::isConstant( ) const noexcept
{
const float f{ values[ 0 ] };
for ( auto v : values ) {
if ( f != v ) {
return {};
}
}
return f;
}
M_LSHIFT_OP( T_StringBuilder , T_SyncSegment::E_SegmentType ) M_LSHIFT_OP( T_StringBuilder , T_SyncSegment::E_SegmentType )
{ {
switch ( value ) { switch ( value ) {
@ -297,6 +308,18 @@ uint32_t T_SyncCurve::points( ) const noexcept
return np; return np;
} }
T_Optional< float > T_SyncCurve::isConstant( ) const noexcept
{
T_Optional< float > v;
for ( auto const& s : segments ) {
v = s.isConstant( );
if ( !v ) {
break;
}
}
return v;
}
/*= T_SyncCurves =============================================================*/ /*= T_SyncCurves =============================================================*/

View file

@ -48,6 +48,9 @@ struct T_SyncSegment
// Compute the value for the specified position, which is relative // Compute the value for the specified position, which is relative
// to the start of the segment. // to the start of the segment.
float computeValue( float relTimeUnits ) const noexcept; float computeValue( float relTimeUnits ) const noexcept;
// Is the segment constant? If it is, return the value.
T_Optional< float > isConstant( ) const noexcept;
}; };
M_LSHIFT_OP( T_StringBuilder , T_SyncSegment::E_SegmentType ); M_LSHIFT_OP( T_StringBuilder , T_SyncSegment::E_SegmentType );
@ -106,6 +109,10 @@ struct T_SyncCurve
// inside a segment, or the first and last value in the curve, or // inside a segment, or the first and last value in the curve, or
// a junction between segments. // a junction between segments.
uint32_t points( ) const noexcept; uint32_t points( ) const noexcept;
// Does the curve represent a constant? It does if all values in
// all segments are identical; in that case the value is returned.
T_Optional< float > isConstant( ) const noexcept;
}; };
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/