function cos_out = hdlcordiccos(theta, niters)
% HDLCORDICCOS(theta, niters) Compute the CORDIC-based approximation to COS(theta).
% this implementation is setup to map to a proxy system object and
% implementation supplied by hdldefaults.SinCosCordic.
%
% Copyright 2015 The MathWorks, Inc.

%#codegen
coder.extrinsic('hdlfeature');
coder.allowpcode('plain');
eml_prefer_const(theta);
eml_prefer_const(niters);

assert( coder.target('hdl') );

flag = coder.nullcopy( false(1,1)  );
flag = coder.internal.const( true(1,1) );

if ( flag )
    if (nargin == 1)
        % niters = -1 % to indicate unspecified for SysObj
        [~, createErr, newObj] = eml_const(feval('eml_try_catch', ...
            'hdl.internal.PIRHDLFunctionObject', ...
            'FunctionName','cos', 'Prop2', -1));
        [~,cos_out] = cordicsincos(theta);
        cos_out = eml_sea_method_call('step_hdl', newObj, 1, theta, cos_out);
        eml_assert(isempty(createErr), createErr);
    else        
        [~, createErr, newObj] = eml_const(feval('eml_try_catch', ...
            'hdl.internal.PIRHDLFunctionObject', ...
            'FunctionName','cos', 'Prop2', niters));
        [~,cos_out] = cordicsincos(theta, niters);
        cos_out = eml_sea_method_call('step_hdl', newObj, 1, theta, cos_out);
        eml_assert(isempty(createErr), createErr);
    end
else
    if (nargin == 1)
        [~, cos_out] = cordicsincos(theta);
    else
        [~,cos_out] = cordicsincos(theta, niters);
    end
end
