function [throughput,latency] = systolicQRMatrixSolverBlockTiming(A,B)
%SYSTOLICQRMATRIXSOLVERBLOCKTIMING compute the throughput and latency of
%Systolic Matrix Solve using QR Decomposition block.
    %   This computation is based on the datatype, complexity, and
    %   dimension of sample matrices A and B.
    %   Throughput is the clock cycles between adjacent ready signals.
    %   Latency is the clock cycles between the input and corresponding output.
    %   Example:
    %   A = fi(complex(randn(4,4),randn(4,4)),1,17,10)
    %   B = fi(complex(randn(4,1),randn(4,1)),1,17,10)
    %   [throughput,latency] = embblk.latency.systolicQRMatrixSolverBlockTiming(A,B)
    
    %   Copyright 2023 The MathWorks, Inc.
    if ~isequal(size(A,1),size(B,1))
        error(message('fixed:emblib:SameNumberOfRows','A','B'));
    end
    
    m = size(A,1);
    n = size(A,2);
    
    % If any input is complex, assume both A and B are complex
    input = [A B];

    [UA,~] = fixed.internal.type.upcastWordlength(input(1),input(2));
    if isreal(UA)
        complexity = 'real';
    else
        complexity = 'complex';
    end
    if isfi(UA) && (isfixed(UA) || isscaleddouble(UA))
        wl = UA.WordLength;
    else
        switch class(UA)
            case 'double'
                wl = 53;
            case 'single'
                wl = 24;
            otherwise
                error(message('fixed:fi:invalidDataType', class(A),'A and B',mfilename));
        end
    end

    [QRThroughput, QRLatency] = embblk.latency.systolicQRBlockTiming(A,B);
    backSubThroughput = 2 +(n-1)*4;
    throughput = max(QRThroughput,backSubThroughput);

    pairs = fixed.emblib.internal.qr.systolicQRPattern(m,n,complexity);
    nStages = numel(pairs);
    if isfi(UA) && (isfixed(UA))
        if QRThroughput < backSubThroughput
            latency = (throughput-1)*(nStages+3);
        else
            latency = QRLatency+nextpow2(wl)+1+(throughput-1)+(1 +(n-1)*4);
        end
    else
        if QRThroughput < backSubThroughput
            latency = (throughput-1)*(nStages+3)+1;
        else
            latency = QRLatency+1+(throughput-1)+(1 +(n-1)*4);
        end
    end
end
