[Writeup] Shared pseudocode: part 28 of 36 ========================================== Section: Silicon & Architectures Started: 2026-11-28 17:47 UTC by admin Replies: 0, last post 2026-11-28 17:47 UTC Page: 1 of 1 Web: https://romdump.net/t/120 ------------------------------------------------------------------------------ #140 admin, 2026-11-28 17:47 UTC [OP] Source: https://romdump.net/raw/140

Shared pseudocode from the offline Arm documentation snapshot. Part 28 of 36.

func FP8MatMulAddFP{N}(addend : bits(N), op1 : bits(N), op2 : bits(N), E : integer{4, 8}, fpcr : FPCR_Type, fpmr : FPMR_Type) => bits(N) begin assert N IN {64, 128}; assert N == E*16; let M : integer{} = N DIV 4; var result : bits(N);

for i = 0 to 1 do
    for j = 0 to 1 do
        let elt1 : bits(2*M) = op1[i*:(2*M)];
        let elt2 : bits(2*M) = op2[j*:(2*M)];
        let sum : bits(M) = addend[(2*i + j)*:M];
        result[(2*i + j)*:M] = FP8DotAddFP{M, N DIV 2}(sum, elt1, elt2, E, fpcr, fpmr);
    end;
end;

return result;

end;


## Library pseudocode for shared/functions/float/fp8float/FP8MaxNormal

// FP8MaxNormal // ==============

func FP8MaxNormal{N}(fp8type : FP8Type, sign : bit) => bits(N) begin assert N == 8; assert fp8type IN {FP8Type_OFP8_E5M2, FP8Type_OFP8_E4M3}; let E : integer{} = if fp8type == FP8Type_OFP8_E4M3 then 4 else 5; let F : integer{} = N - (E + 1); var exp : bits(E); var frac : bits(F);

case fp8type of
    when FP8Type_OFP8_E4M3 =>
        exp  = Ones{E};
        frac = Ones{F-1}::'0';
    when FP8Type_OFP8_E5M2 =>
        exp  = Ones{E-1}::'0';
        frac = Ones{F};
end;

return sign :: exp :: frac;

end;


## Library pseudocode for shared/functions/float/fp8float/FP8MulAddFP

// FP8MulAddFP // =============

func FP8MulAddFP{M}(addend : bits(M), op1 : bits(8), op2 : bits(8), fpcr : FPCR_Type, fpmr : FPMR_Type) => bits(M) begin assert M IN {16,32}; let E : integer{} = 1; return FP8DotAddFP{M, 8}(addend, op1, op2, E, fpcr, fpmr); end;


## Library pseudocode for shared/functions/float/fp8float/FP8Round

// FP8Round // ========== // Used by FP8 downconvert instructions which observe FPMR.OSC // to convert a real number OP into an FP8 value.

func FP8Round{N}(op : real, fp8type : FP8Type, fpcr : FPCR_Type, fpmr : FPMR_Type) => bits(N) begin assert N == 8; assert fp8type IN {FP8Type_OFP8_E5M2, FP8Type_OFP8_E4M3}; assert op != 0.0; var result : bits(N);

// Format parameters - minimum exponent, numbers of exponent and
fraction bits.
let (F, minimum_exp) = FP8Bits(fp8type);
let E : integer{} = (N - F) - 1;

// Split value into sign, unrounded mantissa and exponent.
var sign : bit;
var exponent : integer;
var mantissa : real;
if op < 0.0 then
    sign = '1';  mantissa = -op;
else
    sign = '0';  mantissa = op;
end;

(mantissa, exponent) = NormalizeReal(mantissa);
// When TRUE, detection of underflow occurs after rounding.
let altfp : boolean = IsFeatureImplemented(FEAT_AFP) && fpcr.AH ==
'1';

var biased_exp_unconstrained : integer = (exponent - minimum_exp) + 1;
var int_mant_unconstrained : integer= RoundDown(mantissa * 2.0^F);
let error_unconstrained : real = mantissa * 2.0^F -
Real(int_mant_unconstrained);

// Start creating the exponent value for the result. Start by biasing the
actual exponent
// so that the minimum exponent becomes 1, lower values 0 (indicating possible
underflow).
var biased_exp : integer = Max((exponent - minimum_exp) + 1, 0);
if biased_exp == 0 then mantissa = mantissa / 2.0^(minimum_exp - exponent);
end;

// Get the unrounded mantissa as an integer, and the "units in last place"
rounding error.
// < 2.0^F if biased_exp == 0, >= 2.0^F if not
var int_mant : integer = RoundDown(mantissa * 2.0^F);
var error : real = mantissa * 2.0^F - Real(int_mant);

let trapped_UF : boolean = fpcr.UFE == '1' && (() ||
IsFullA64Enabled());

var round_up_unconstrained : boolean;
var round_up : boolean;

if altfp then
    // Round to Nearest Even
    round_up_unconstrained = (error_unconstrained > 0.5 ||
               (error_unconstrained == 0.5 && int_mant_unconstrained[0] == '1'));
    round_up = (error > 0.5 || (error == 0.5 && int_mant[0] == '1'));

    if round_up_unconstrained then
        int_mant_unconstrained = int_mant_unconstrained + 1;
        if int_mant_unconstrained == 2^(F+1) then    // Rounded up to next exponent
            biased_exp_unconstrained = biased_exp_unconstrained + 1;
            int_mant_unconstrained   = int_mant_unconstrained DIV 2;
        end;
    end;

    // Follow alternate floating-point behavior of underflow after rounding
    if (biased_exp_unconstrained < 1 && int_mant_unconstrained != 0 &&
        (error != 0.0 || trapped_UF)) then
        FPProcessException(FPExc_Underflow, fpcr);
    end;
else // altfp == FALSE
    // Underflow occurs if exponent is too small before rounding, and result is inexact or
    // the Underflow exception is trapped. This applies before rounding if FPCR.AH != '1'.
    if biased_exp == 0 && (error != 0.0 || trapped_UF) then
        FPProcessException(FPExc_Underflow, fpcr);
    end;

    // Round to Nearest Even
    round_up = (error > 0.5 || (error == 0.5 && int_mant[0] == '1'));
end;

if round_up then
    int_mant = int_mant + 1;
    if int_mant == 2^F then      // Rounded up from denormalized to normalized
        biased_exp = 1;
    end;
    if int_mant == 2^(F+1) then  // Rounded up to next exponent
        biased_exp = biased_exp + 1;
        int_mant = int_mant DIV 2;
    end;
end;

// Deal with overflow and generate result.
var overflow : boolean;
case fp8type of
    when FP8Type_OFP8_E4M3 =>
        overflow = biased_exp >= 2^E || (biased_exp == 2^E - 1 && int_mant == 2^(F+1) - 1);
    when FP8Type_OFP8_E5M2 =>
        overflow = biased_exp >= 2^E - 1;
end;

if overflow then
    result = (if fpmr.OSC == '0' then FP8Infinity{N}(fp8type, sign)
              else FP8MaxNormal{N}(fp8type, sign));
    // Flag Overflow exception regardless of FPMR.OSC
    FPProcessException(FPExc_Overflow, fpcr);
    error = 1.0;  // Ensure that an Inexact exception occurs
else
    result = sign :: biased_exp[E-1:0] :: int_mant[F-1:0];
end;

// Deal with Inexact exception.
if error != 0.0 then
    FPProcessException(FPExc_Inexact, fpcr);
end;

return result;

end;


## Library pseudocode for shared/functions/float/fp8float/FP8Type

// FP8Type // =======

type FP8Type of enumeration {FP8Type_OFP8_E5M2, FP8Type_OFP8_E4M3, FP8Type_UNSUPPORTED};


## Library pseudocode for shared/functions/float/fp8float/FP8Unpack

// FP8Unpack // =========== // Unpacks an FP8 value into its type, sign bit and real number that // it represents.

func FP8Unpack{N}(fpval : bits(N), fp8type : FP8Type) => (FPType, bit, real) begin assert N == 8; let E : integer{} = if fp8type == FP8Type_OFP8_E4M3 then 4 else 5; let F : integer{} = N - (E + 1);

let sign : bit = fpval[N-1];
let exp : bits(E) = fpval[(E+F)-1:F];
let frac : bits(F) = fpval[F-1:0];

var value : real;
var fptype : FPType;

if fp8type == FP8Type_OFP8_E4M3 then
    if IsZero(exp) then
        if IsZero(frac) then
            fptype = FPType_Zero;  value = 0.0;
        else
            fptype = FPType_Denormal;  value = 2.0^-6 * (Real(UInt(frac)) * 2.0^-3);
        end;
    elsif IsOnes(exp) && IsOnes(frac) then
        fptype = FPType_SNaN;
        value = 0.0;
    else
        fptype = FPType_Nonzero;
        value = 2.0^(UInt(exp)-7) * (1.0 + Real(UInt(frac)) * 2.0^-3);
    end;

elsif fp8type == FP8Type_OFP8_E5M2 then
    if IsZero(exp) then
        if IsZero(frac) then
            fptype = FPType_Zero;  value = 0.0;
        else
            fptype = FPType_Denormal;  value = 2.0^-14 * (Real(UInt(frac)) * 2.0^-2);
        end;
    elsif IsOnes(exp) then
        if IsZero(frac) then
            fptype = FPType_Infinity;  value = 2.0^1000000;
        else
            fptype = if frac[1] == '1' then FPType_QNaN else FPType_SNaN;
            value = 0.0;
        end;
    else
        fptype = FPType_Nonzero;
        value = 2.0^(UInt(exp)-15) * (1.0 + Real(UInt(frac)) * 2.0^-2);
    end;

else // fp8type == FP8Type_UNSUPPORTED
    fptype = FPType_SNaN;
    value = 0.0;
end;

if sign == '1' then value = -value; end;

return (fptype, sign, value);

end;


## Library pseudocode for shared/functions/float/fp8float/FP8Zero

// FP8Zero // =========

func FP8Zero{N}(fp8type : FP8Type, sign : bit) => bits(N) begin assert N == 8; assert fp8type IN {FP8Type_OFP8_E5M2, FP8Type_OFP8_E4M3}; let E : integer{} = if fp8type == FP8Type_OFP8_E4M3 then 4 else 5; let F : integer{} = N - (E + 1); return sign :: Zeros{E} :: Zeros{F}; end;


## Library pseudocode for shared/functions/float/fp8float/FPConvertFP8

// FPConvertFP8 // ============== // Converts a half-precision or single-precision OP to FP8 value. // The scaling factor in FPMR.NSCALE is applied to the value before rounding.

func FPConvertFP8{M, N}(op : bits(N), fpcr_in : FPCR_Type, fpmr : FPMR_Type) => bits(M) begin assert N IN {16,32} && M == 8; var result : bits(M);

let fpexc : boolean = TRUE;
var fpcr : FPCR_Type = fpcr_in;
fpcr.[FIZ,FZ,FZ16] = '000'; // Do not flush denormal inputs and outputs to
zero
let fp8type : FP8Type = FP8DecodeType(fpmr.F8D);

let (fptype,sign,value) = FPUnpack{N}(op, fpcr, fpexc);

if fp8type == FP8Type_UNSUPPORTED then
    result = Ones{M};
    FPProcessException(FPExc_InvalidOp, fpcr);
elsif fptype == FPType_SNaN || fptype == FPType_QNaN then
    result = FP8DefaultNaN{M}(fp8type, fpcr);     // Always generate default NaN as result
    if fptype == FPType_SNaN then
        FPProcessException(FPExc_InvalidOp, fpcr);
    end;
elsif fptype == FPType_Infinity then
    result = (if fpmr.OSC == '0' then FP8Infinity{M}(fp8type, sign)
              else FP8MaxNormal{M}(fp8type, sign));
elsif fptype == FPType_Zero then
    result = FP8Zero{M}(fp8type, sign);
else
    let scale : integer = if N == 16 then SInt(fpmr.NSCALE[4:0]) else SInt(fpmr.NSCALE);
    let result_value : real = value * (2.0^scale);
    result = FP8Round{M}(result_value, fp8type, fpcr, fpmr);
end;

return result;

end;


## Library pseudocode for shared/functions/float/fpabs/FPAbs

// FPAbs // =======

func FPAbs{N}(op : bits(N), fpcr : FPCR_Type) => bits(N) begin assert N IN {16,32,64}; if () && IsFeatureImplemented(FEAT_AFP) then if fpcr.AH == '1' then let (fptype, -, -) = FPUnpack{N}(op, fpcr, FALSE); if fptype IN {FPType_SNaN, FPType_QNaN} then return op; // When fpcr.AH=1, sign of NaN has no consequence end; end; end; return '0' :: op[N-2:0]; end;


## Library pseudocode for shared/functions/float/fpabsmax/FPAbsMax

// FPAbsMax // ========== // Compare absolute value of two operands and return the larger absolute // value without rounding.

func FPAbsMax{N}(op1_in : bits(N), op2_in : bits(N), fpcr_in : FPCR_Type) => bits(N) begin assert N IN {16,32,64}; var done : boolean; var result : bits(N); var fpcr : FPCR_Type = fpcr_in; fpcr.[AH,FIZ,FZ,FZ16] = '0000';

let op1 = '0'::op1_in[N-2:0];
let op2 = '0'::op2_in[N-2:0];
let (type1,-,value1) = FPUnpack{N}(op1, fpcr);
let (type2,-,value2) = FPUnpack{N}(op2, fpcr);

(done,result) = FPProcessNaNs{N}(type1, type2, op1_in, op2_in, fpcr);

if !done then
    // This condition covers all results other than NaNs,
    // including Zero & Infinity
    result = if value1 > value2 then op1 else op2;
end;

return result;

end;


## Library pseudocode for shared/functions/float/fpabsmin/FPAbsMin

// FPAbsMin // ========== // Compare absolute value of two operands and return the smaller absolute // value without rounding.

func FPAbsMin{N}(op1_in : bits(N), op2_in : bits(N), fpcr_in : FPCR_Type) => bits(N) begin assert N IN {16,32,64}; var done : boolean; var result : bits(N); var fpcr : FPCR_Type = fpcr_in; fpcr.[AH,FIZ,FZ,FZ16] = '0000';

let op1 = '0'::op1_in[N-2:0];
let op2 = '0'::op2_in[N-2:0];
let (type1,-,value1) = FPUnpack{N}(op1, fpcr);
let (type2,-,value2) = FPUnpack{N}(op2, fpcr);

(done,result) = FPProcessNaNs{N}(type1, type2, op1_in, op2_in, fpcr);

if !done then
    // This condition covers all results other than NaNs,
    // including Zero & Infinity
    result = if value1 < value2 then op1 else op2;
end;

return result;

end;


## Library pseudocode for shared/functions/float/fpadd/FPAdd

// FPAdd // =======

func FPAdd{N}(op1 : bits(N), op2 : bits(N), fpcr : FPCR_Type) => bits(N) begin let fpexc : boolean = TRUE; // Generate floating-point exceptions return FPAdd{N}(op1, op2, fpcr, fpexc); end;

// FPAdd // =======

func FPAdd{N}(op1 : bits(N), op2 : bits(N), fpcr : FPCR_Type, fpexc : boolean) => bits(N) begin

assert N IN {16,32,64};
let rounding : FPRounding = FPRoundingMode(fpcr);

let (type1,sign1,value1) = FPUnpack{N}(op1, fpcr, fpexc);
let (type2,sign2,value2) = FPUnpack{N}(op2, fpcr, fpexc);

var (done,result) = FPProcessNaNs{N}(type1, type2, op1, op2, fpcr, fpexc);
if !done then
    let inf1 : boolean  = (type1 == FPType_Infinity);
    let inf2 : boolean  = (type2 == FPType_Infinity);
    let zero1 : boolean = (type1 == FPType_Zero);
    let zero2 : boolean = (type2 == FPType_Zero);
    if inf1 && inf2 && sign1 == NOT(sign2) then
        result = FPDefaultNaN{N}(fpcr);
        if fpexc then FPProcessException(FPExc_InvalidOp, fpcr); end;
    elsif (inf1 && sign1 == '0') || (inf2 && sign2 == '0') then
        result = FPInfinity{N}('0');
    elsif (inf1 && sign1 == '1') || (inf2 && sign2 == '1') then
        result = FPInfinity{N}('1');
    elsif zero1 && zero2 && sign1 == sign2 then
        result = FPZero{N}(sign1);
    else
        let result_value : real = value1 + value2;
        if result_value == 0.0 then  // Sign of exact zero result depends on rounding mode
            let result_sign : bit = if rounding == FPRounding_NEGINF then '1' else '0';
            result = FPZero{N}(result_sign);
        else
            result = FPRound{N}(result_value, fpcr, rounding, fpexc);
        end;
    end;

    if fpexc then FPProcessDenorms(type1, type2, N, fpcr); end;
end;
return result;

end;


## Library pseudocode for shared/functions/float/fpadd/FPAdd\_ZA

// FPAdd_ZA // ========== // Calculates op1+op2 for SME2 ZA-targeting instructions.

func FPAdd_ZA{N}(op1 : bits(N), op2 : bits(N), fpcr_in : FPCR_Type) => bits(N) begin var fpcr : FPCR_Type = fpcr_in; let fpexc : boolean = FALSE; // Do not generate floating-point exceptions fpcr.DN = '1'; // Generate default NaN values return FPAdd{N}(op1, op2, fpcr, fpexc); end;


## Library pseudocode for shared/functions/float/fpbits/FPBits

// FPBits // ======== // Returns the minimum exponent, numbers of exponent and fraction bits.

func FPBits(N : integer, isbfloat16 : boolean) => FPBitsType begin var F : FPFracBits; var minimum_exp : integer; if N == 16 then minimum_exp = -14; F = 10; elsif N == 32 && isbfloat16 then minimum_exp = -126; F = 7; elsif N == 32 then minimum_exp = -126; F = 23; else // N == 64 minimum_exp = -1022; F = 52; end;

return (F, minimum_exp);

end;


## Library pseudocode for shared/functions/float/fpbits/FPBitsType

// FPBitsType // ==========

type FPBitsType of (FPFracBits, integer);


## Library pseudocode for shared/functions/float/fpbits/FPFracBits

// FPFracBits // ==========

type FPFracBits of integer{2, 3, 7, 10, 23, 52};


## Library pseudocode for shared/functions/float/fpcompare/FPCompare

// FPCompare // ===========

func FPCompare{N}(op1 : bits(N), op2 : bits(N), signal_nans : boolean, fpcr : FPCR_Type) => bits(4) begin assert N IN {16,32,64}; let (type1,sign1,value1) : (FPType, bit, real) = FPUnpack{N}(op1, fpcr); let (type2,sign2,value2) : (FPType, bit, real) = FPUnpack{N}(op2, fpcr);

var result : bits(4);
if type1 IN {FPType_SNaN, FPType_QNaN} || type2 IN {FPType_SNaN, FPType_QNaN}
then
    result = '0011';
    if type1 == FPType_SNaN || type2 == FPType_SNaN || signal_nans then
        FPProcessException(FPExc_InvalidOp, fpcr);
    end;
else
    // All non-NaN cases can be evaluated on the values produced by FPUnpack
    if value1 == value2 then
        result = '0110';
    elsif value1 < value2 then
        result = '1000';
    else  // value1 > value2
        result = '0010';
    end;

    FPProcessDenorms(type1, type2, N, fpcr);
end;
return result;

end;


## Library pseudocode for shared/functions/float/fpcompareeq/FPCompareEQ

// FPCompareEQ // =============

func FPCompareEQ{N}(op1 : bits(N), op2 : bits(N), fpcr : FPCR_Type) => boolean begin assert N IN {16,32,64}; let (type1,sign1,value1) : (FPType, bit, real) = FPUnpack{N}(op1, fpcr); let (type2,sign2,value2) : (FPType, bit, real) = FPUnpack{N}(op2, fpcr);

var result : boolean;
if type1 IN {FPType_SNaN, FPType_QNaN} || type2 IN {FPType_SNaN, FPType_QNaN}
then
    result = FALSE;
    if type1 == FPType_SNaN || type2 == FPType_SNaN then
        FPProcessException(FPExc_InvalidOp, fpcr);
    end;
else
    // All non-NaN cases can be evaluated on the values produced by FPUnpack
    result = (value1 == value2);
    FPProcessDenorms(type1, type2, N, fpcr);

end;
return result;

end;


## Library pseudocode for shared/functions/float/fpcomparege/FPCompareGE

// FPCompareGE // =============

func FPCompareGE{N}(op1 : bits(N), op2 : bits(N), fpcr : FPCR_Type) => boolean begin assert N IN {16,32,64}; let (type1,sign1,value1) : (FPType, bit, real) = FPUnpack{N}(op1, fpcr); let (type2,sign2,value2) : (FPType, bit, real) = FPUnpack{N}(op2, fpcr);

var result : boolean;
if type1 IN {FPType_SNaN, FPType_QNaN} || type2 IN {FPType_SNaN, FPType_QNaN}
then
    result = FALSE;
    FPProcessException(FPExc_InvalidOp, fpcr);
else
    // All non-NaN cases can be evaluated on the values produced by FPUnpack
    result = (value1 >= value2);
    FPProcessDenorms(type1, type2, N, fpcr);

end;
return result;

end;


## Library pseudocode for shared/functions/float/fpcomparegt/FPCompareGT

// FPCompareGT // =============

func FPCompareGT{N}(op1 : bits(N), op2 : bits(N), fpcr : FPCR_Type) => boolean begin assert N IN {16,32,64}; let (type1,sign1,value1) : (FPType, bit, real) = FPUnpack{N}(op1, fpcr); let (type2,sign2,value2) : (FPType, bit, real) = FPUnpack{N}(op2, fpcr);

var result : boolean;
if type1 IN {FPType_SNaN, FPType_QNaN} || type2 IN {FPType_SNaN, FPType_QNaN}
then
    result = FALSE;
    FPProcessException(FPExc_InvalidOp, fpcr);
else
    // All non-NaN cases can be evaluated on the values produced by FPUnpack
    result = (value1 > value2);
    FPProcessDenorms(type1, type2, N, fpcr);

end;
return result;

end;


## Library pseudocode for shared/functions/float/fpconvert/FPConvert

// FPConvert // ===========

// Convert floating point 'op' with N-bit precision to M-bit precision, // with rounding controlled by ROUNDING. // This is used by the FP-to-FP conversion instructions and so for // half-precision data ignores FZ16, but observes AHP.

func FPConvert{M, N}(op : bits(N), fpcr : FPCR_Type, rounding : FPRounding) => bits(M) begin

assert M IN {16,32,64};
assert N IN {16,32,64};
var result : bits(M);

// Unpack floating-point operand optionally with flush-to-zero.
let (fptype,sign,value) : (FPType, bit, real) = FPUnpackCV{N}(op, fpcr);

let alt_hp : boolean = (M == 16) && (fpcr.AHP == '1');

if fptype == FPType_SNaN || fptype == FPType_QNaN then
    if alt_hp then
        result = FPZero{M}(sign);
    elsif fpcr.DN == '1' then
        result = FPDefaultNaN{M}(fpcr);
    else
        result = FPConvertNaN{M, N}(op);
    end;
    if fptype == FPType_SNaN || alt_hp then
        FPProcessException(FPExc_InvalidOp,fpcr);
    end;
elsif fptype == FPType_Infinity then
    if alt_hp then
        result = sign::Ones{M-1};
        FPProcessException(FPExc_InvalidOp, fpcr);
    else
        result = FPInfinity{M}(sign);
    end;
elsif fptype == FPType_Zero then
    result = FPZero{M}(sign);
else
    result = FPRoundCV{M}(value, fpcr, rounding);
    FPProcessDenorm(fptype, N, fpcr);

end;
return result;

end;

// FPConvert // ===========

func FPConvert{M, N}(op : bits(N), fpcr : FPCR_Type) => bits(M) begin return FPConvert{M, N}(op, fpcr, FPRoundingMode(fpcr)); end;


## Library pseudocode for shared/functions/float/fpconvertnan/FPConvertNaN

// FPConvertNaN // ============== // Converts a NaN of one floating-point type to another

func FPConvertNaN{M, N}(op : bits(N)) => bits(M) begin assert N IN {16,32,64}; assert M IN {16,32,64}; var result : bits(M); var frac : bits(51);

let sign : bit = op[N-1];

// Unpack payload from input NaN
case N of
    when 64 => frac = op[50:0];
    when 32 => frac = op[21:0]::Zeros{29};
    when 16 => frac = op[8:0]::Zeros{42};
end;

// Repack payload into output NaN, while
// converting an SNaN to a QNaN.
case M of
    when 64 => result = sign::Ones{M-52}::frac;
    when 32 => result = sign::Ones{M-23}::frac[50:29];
    when 16 => result = sign::Ones{M-10}::frac[50:42];
end;

return result;

end;


## Library pseudocode for shared/functions/float/fpdecoderm/FPDecodeRM

// FPDecodeRM // ============

// Decode most common AArch32 floating-point rounding encoding.

func FPDecodeRM(rm : bits(2)) => FPRounding begin var result : FPRounding; case rm of when '00' => result = FPRounding_TIEAWAY; // A when '01' => result = FPRounding_TIEEVEN; // N when '10' => result = FPRounding_POSINF; // P when '11' => result = FPRounding_NEGINF; // M end;

return result;

end;


## Library pseudocode for
shared/functions/float/fpdecoderounding/FPDecodeRounding

// FPDecodeRounding // ==================

// Decode floating-point rounding mode and common AArch64 encoding.

func FPDecodeRounding(rmode : bits(2)) => FPRounding begin case rmode of when '00' => return FPRounding_TIEEVEN; // N when '01' => return FPRounding_POSINF; // P when '10' => return FPRounding_NEGINF; // M when '11' => return FPRounding_ZERO; // Z end; end;


## Library pseudocode for shared/functions/float/fpdefaultnan/FPDefaultNaN

// FPDefaultNaN // ==============

func FPDefaultNaN{N}(fpcr : FPCR_Type) => bits(N) begin assert N IN {16,32,64}; let E : integer{} = (if N == 16 then 5 else (if N == 32 then 8 else 11)); let F : integer{} = N - (E + 1); let sign : bit = if IsFeatureImplemented(FEAT_AFP) && () then fpcr.AH else '0';

let exp : bits(E)  = Ones{};
let frac : bits(F) = '1'::Zeros{F-1};
return sign :: exp :: frac;

end;


## Library pseudocode for shared/functions/float/fpdiv/FPDiv

// FPDiv // =======

func FPDiv{N}(op1 : bits(N), op2 : bits(N), fpcr : FPCR_Type) => bits(N) begin assert N IN {16,32,64}; let (type1,sign1,value1) : (FPType, bit, real) = FPUnpack{N}(op1, fpcr); let (type2,sign2,value2) : (FPType, bit, real) = FPUnpack{N}(op2, fpcr); var (done,result) : (boolean, bits(N)) = FPProcessNaNs{N}(type1, type2, op1, op2, fpcr);

if !done then
    let inf1 : boolean  = type1 == FPType_Infinity;
    let inf2 : boolean  = type2 == FPType_Infinity;
    let zero1 : boolean = type1 == FPType_Zero;
    let zero2 : boolean = type2 == FPType_Zero;

    if (inf1 && inf2) || (zero1 && zero2) then
        result = FPDefaultNaN{N}(fpcr);
        FPProcessException(FPExc_InvalidOp, fpcr);
    elsif inf1 || zero2 then
        result = FPInfinity{N}(sign1 XOR sign2);
        if !inf1 then FPProcessException(FPExc_DivideByZero, fpcr); end;
    elsif zero1 || inf2 then
        result = FPZero{N}(sign1 XOR sign2);
    else
        result = FPRound{N}(value1/value2, fpcr);
    end;

    if !zero2 then
        FPProcessDenorms(type1, type2, N, fpcr);
    end;
end;
return result;

end;


## Library pseudocode for shared/functions/float/fpdot/FPDot

// FPDot // ======= // Calculates single-precision result of 2-way 16-bit floating-point dot-product // with a single rounding. // The 'fpcr' argument supplies the FPCR control bits and 'isbfloat16' // determines whether input operands are BFloat16 or half-precision type. // and 'fpexc' controls the generation of floating-point exceptions.

func FPDot(op1_a : bits(16), op1_b : bits(16), op2_a : bits(16), op2_b : bits(16), fpcr : FPCR_Type, isbfloat16 : boolean) => bits(32) begin let fpexc : boolean = TRUE; // Generate floating-point exceptions return FPDot(op1_a, op1_b, op2_a, op2_b, fpcr, isbfloat16, fpexc); end;

func FPDot(op1_a : bits(16), op1_b : bits(16), op2_a : bits(16), op2_b : bits(16), fpcr_in : FPCR_Type, isbfloat16 : boolean, fpexc : boolean) => bits(32) begin var fpcr : FPCR_Type = fpcr_in; var result : bits(32); var done : boolean; fpcr.AHP = '0'; // Ignore alternative half-precision option let rounding : FPRounding = FPRoundingMode(fpcr);

let (type1_a,sign1_a,value1_a) = FPUnpackBase{16}(op1_a, fpcr,
fpexc, isbfloat16);
let (type1_b,sign1_b,value1_b) = FPUnpackBase{16}(op1_b, fpcr, fpexc,
isbfloat16);
let (type2_a,sign2_a,value2_a) = FPUnpackBase{16}(op2_a, fpcr, fpexc,
isbfloat16);
let (type2_b,sign2_b,value2_b) = FPUnpackBase{16}(op2_b, fpcr, fpexc,
isbfloat16);

let inf1_a = (type1_a == FPType_Infinity); let zero1_a = (type1_a ==
FPType_Zero);
let inf1_b = (type1_b == FPType_Infinity); let zero1_b = (type1_b ==
FPType_Zero);
let inf2_a = (type2_a == FPType_Infinity); let zero2_a = (type2_a ==
FPType_Zero);
let inf2_b = (type2_b == FPType_Infinity); let zero2_b = (type2_b ==
FPType_Zero);

(done,result) = FPProcessNaNs4(type1_a, type1_b, type2_a, type2_b,
                               op1_a, op1_b, op2_a, op2_b, fpcr, fpexc);

if !done then
    // Determine sign and type products will have if it does not cause an Invalid
    // Operation.
    let signPa = sign1_a XOR sign2_a;
    let signPb = sign1_b XOR sign2_b;
    let infPa  = inf1_a || inf2_a;
    let infPb  = inf1_b || inf2_b;
    let zeroPa = zero1_a || zero2_a;
    let zeroPb = zero1_b || zero2_b;

    // Non SNaN-generated Invalid Operation cases are multiplies of zero
    // by infinity and additions of opposite-signed infinities.
    let invalidop = ((inf1_a && zero2_a) || (zero1_a && inf2_a) ||
                     (inf1_b && zero2_b) || (zero1_b && inf2_b) ||
                     (infPa && infPb && signPa != signPb));

    if invalidop then
        result = FPDefaultNaN{32}(fpcr);
        if fpexc then FPProcessException(FPExc_InvalidOp, fpcr); end;

   // Other cases involving infinities produce an infinity of the same sign.
    elsif (infPa && signPa == '0') || (infPb && signPb == '0') then
        result = FPInfinity{32}('0');
    elsif (infPa && signPa == '1') || (infPb && signPb == '1') then
        result = FPInfinity{32}('1');

    // Cases where the result is exactly zero and its sign is not determined by the
    // rounding mode are additions of same-signed zeros.
    elsif zeroPa && zeroPb && signPa == signPb then
        result = FPZero{32}(signPa);

    // Otherwise calculate fused sum of products and round it.
    else
        let result_value = (value1_a * value2_a) + (value1_b * value2_b);
        if result_value == 0.0 then  // Sign of exact zero result depends on rounding mode
            let result_sign = if rounding == FPRounding_NEGINF then '1' else '0';
            result = FPZero{32}(result_sign);
        else
            result = FPRound{32}(result_value, fpcr, rounding, fpexc);
        end;
    end;
end;

return result;

end;


## Library pseudocode for shared/functions/float/fpdot/FPDotAdd

// FPDotAdd // ========== // Half-precision 2-way dot-product and add to single-precision.

func FPDotAdd(addend : bits(32), op1_a : bits(16), op1_b : bits(16), op2_a : bits(16), op2_b : bits(16), fpcr : FPCR_Type) => bits(32) begin var prod : bits(32); let isbfloat16 : boolean = FALSE; let fpexc : boolean = TRUE; // Generate floating-point exceptions prod = FPDot(op1_a, op1_b, op2_a, op2_b, fpcr, isbfloat16, fpexc); let result : bits(32) = FPAdd{}(addend, prod, fpcr, fpexc);

return result;

end;


## Library pseudocode for shared/functions/float/fpdot/FPDotAdd\_ZA

// FPDotAdd_ZA // ============= // Half-precision 2-way dot-product and add to single-precision // for SME ZA-targeting instructions.

func FPDotAdd_ZA(addend : bits(32), op1_a : bits(16), op1_b : bits(16), op2_a : bits(16), op2_b : bits(16), fpcr_in : FPCR_Type) => bits(32) begin var fpcr : FPCR_Type = fpcr_in; var prod : bits(32); let isbfloat16 : boolean = FALSE; let fpexc : boolean = FALSE; // Do not generate floating-point exceptions fpcr.DN = '1'; // Generate default NaN values prod = FPDot(op1_a, op1_b, op2_a, op2_b, fpcr, isbfloat16, fpexc); let result : bits(32) = FPAdd{}(addend, prod, fpcr, fpexc);

return result;

end;


## Library pseudocode for shared/functions/float/fpexc/FPExc

// FPExc // =====

type FPExc of enumeration {FPExc_InvalidOp, FPExc_DivideByZero, FPExc_Overflow, FPExc_Underflow, FPExc_Inexact, FPExc_InputDenorm};


## Library pseudocode for
shared/functions/float/fpexceptrapsenabled/FPExcepTrapsEnabled

// FPExcepTrapsEnabled // =====================

func FPExcepTrapsEnabled() => boolean begin return (ImpDefBool("IMPLEMENTED_trapping of Input Denormal floating-point exceptions") || ImpDefBool("IMPLEMENTED_trapping of Inexact floating-point exceptions") || ImpDefBool("IMPLEMENTED_trapping of Underflow floating-point exceptions") || ImpDefBool("IMPLEMENTED_trapping of Overflow floating-point exceptions") || ImpDefBool("IMPLEMENTED_trapping of Divide by Zero floating-point exceptions") || ImpDefBool("IMPLEMENTED_trapping of Invalid Operation floating-point exceptions")); end;


## Library pseudocode for shared/functions/float/fpinfinity/FPInfinity

// FPInfinity // ============

func FPInfinity{N}(sign : bit) => bits(N) begin assert N IN {16,32,64}; let E : integer{} = (if N == 16 then 5 else (if N == 32 then 8 else 11)); let F : integer{} = N - (E + 1); let exp : bits(E) = Ones{}; let frac : bits(F) = Zeros{}; return sign :: exp :: frac; end;


## Library pseudocode for shared/functions/float/fpmatmul/FPMatMulAdd

// FPMatMulAdd // ============= // // Floating point matrix multiply and add to same precision matrix // result[2, 2] = addend[2, 2] + (op1[2, 2] * op2[2, 2])

func FPMatMulAdd{N}(addend : bits(N), op1 : bits(N), op2 : bits(N), esize : ESize, fpcr : FPCR_Type) => bits(N) begin assert N == esize * 2 * 2; var result : bits(N); var prod0, prod1, sum : bits(esize);

for i = 0 to 1 do
    for j = 0 to 1 do
        sum   = addend[(2*i + j)*:esize];
        prod0 = FPMul{esize}(op1[(2*i + 0)*:esize],
                      op2[(2*j + 0)*:esize], fpcr);
        prod1 = FPMul{esize}(op1[(2*i + 1)*:esize],
                      op2[(2*j + 1)*:esize], fpcr);
        sum   = FPAdd{esize}(sum, FPAdd{esize}(prod0, prod1, fpcr), fpcr);
        result[(2*i + j)*:esize] = sum;
    end;
end;

return result;

end;


## Library pseudocode for shared/functions/float/fpmatmulh/FPMatMulAddH

// FPMatMulAddH // ============== // Half-precision matrix multiply and add to single-precision matrix // result[2, 2] = addend[2, 2] + (op1[2, 4] * op2[4, 2])

func FPMatMulAddH{N}(addend : bits(N), op1 : bits(N), op2 : bits(N), fpcr : FPCR_Type) => bits(N) begin assert N == 128; let M : integer{} = 32; var result : bits(N);

let isbfloat16 : boolean = FALSE;
for i = 0 to 1 do
    for j = 0 to 1 do
        var sum : bits(M) = addend[(2*i + j)*:M];
        var prod : array[[2]] of bits(M);
        for k = 0 to 1 do
            let elt1_a : bits(M DIV 2) = op1[(4*i + 2*k + 0)*:(M DIV 2)];
            let elt1_b : bits(M DIV 2) = op1[(4*i + 2*k + 1)*:(M DIV 2)];
            let elt2_a : bits(M DIV 2) = op2[(4*j + 2*k + 0)*:(M DIV 2)];
            let elt2_b : bits(M DIV 2) = op2[(4*j + 2*k + 1)*:(M DIV 2)];
            prod[[k]] = FPDot(elt1_a, elt1_b, elt2_a, elt2_b, fpcr, isbfloat16);
        end;
        sum = FPAdd{M}(sum, FPAdd{M}(prod[[0]], prod[[1]], fpcr), fpcr);
        result[(2*i + j)*:M] = sum;
    end;
end;

return result;

end;


## Library pseudocode for shared/functions/float/fpmax/FPMax

// FPMax // =======

func FPMax{N}(op1 : bits(N), op2 : bits(N), fpcr : FPCR_Type) => bits(N) begin let altfp : boolean = IsFeatureImplemented(FEAT_AFP) && () && fpcr.AH == '1'; let fpexc : boolean = TRUE; return FPMax{N}(op1, op2, fpcr, altfp, fpexc); end;

// FPMax // =======

func FPMax{N}(op1 : bits(N), op2 : bits(N), fpcr : FPCR_Type, altfp : boolean) => bits(N) begin let fpexc : boolean = TRUE; return FPMax{N}(op1, op2, fpcr, altfp, fpexc); end;

// FPMax // ======= // Compare two inputs and return the larger value after rounding. The // 'fpcr' argument supplies the FPCR control bits and 'altfp' determines // if the function should use alternative floating-point behavior.

func FPMax{N}(op1 : bits(N), op2 : bits(N), fpcr_in : FPCR_Type, altfp : boolean, fpexc : boolean) => bits(N) begin assert N IN {16,32,64}; var done : boolean; var result : bits(N); var fpcr : FPCR_Type = fpcr_in; let (type1,sign1,value1) : (FPType, bit, real) = FPUnpack{N}(op1, fpcr, fpexc); let (type2,sign2,value2) : (FPType, bit, real) = FPUnpack{N}(op2, fpcr, fpexc);

if altfp && type1 == FPType_Zero && type2 ==
FPType_Zero && sign1 != sign2 then
    // Alternate handling of zeros with differing sign
    return FPZero{N}(sign2);
elsif altfp && (type1 IN {FPType_SNaN, FPType_QNaN} || type2 IN
{FPType_SNaN, FPType_QNaN}) then
    // Alternate handling of NaN inputs
    if fpexc then FPProcessException(FPExc_InvalidOp, fpcr); end;
    return (if type2 == FPType_Zero then FPZero{N}(sign2) else op2);
end;

(done,result) = FPProcessNaNs{N}(type1, type2, op1, op2, fpcr, fpexc);
if !done then
    var fptype : FPType;
    var sign : bit;
    var value : real;
    if value1 > value2 then
        (fptype,sign,value) = (type1,sign1,value1);
    else
        (fptype,sign,value) = (type2,sign2,value2);
    end;
    if fptype == FPType_Infinity then
        result = FPInfinity{N}(sign);
    elsif fptype == FPType_Zero then
        sign = sign1 AND sign2;         // Use most positive sign
        result = FPZero{N}(sign);
    else
        // The use of FPRound() covers the case where there is a trapped underflow exception
        // for a denormalized number even though the result is exact.
        let rounding : FPRounding = FPRoundingMode(fpcr);
        if altfp then    // Denormal output is not flushed to zero
            fpcr.FZ = '0';
            fpcr.FZ16 = '0';
        end;

        result = FPRound{N}(value, fpcr, rounding, fpexc);
    end;
    if fpexc then FPProcessDenorms(type1, type2, N, fpcr); end;
end;

return result;

end;


## Library pseudocode for shared/functions/float/fpmaxnormal/FPMaxNormal

// FPMaxNormal // =============

func FPMaxNormal{N}(sign : bit) => bits(N) begin assert N IN {16,32,64}; let E : integer{} = (if N == 16 then 5 else (if N == 32 then 8 else 11)); let F : integer{} = N - (E + 1); let exp : bits(E) = Ones{E-1}::'0'; let frac : bits(F) = Ones{}; return sign :: exp :: frac; end;


## Library pseudocode for shared/functions/float/fpmaxnum/FPMaxNum

// FPMaxNum // ==========

func FPMaxNum{N}(op1 : bits(N), op2 : bits(N), fpcr : FPCR_Type) => bits(N) begin let fpexc : boolean = TRUE; return FPMaxNum{N}(op1, op2, fpcr, fpexc); end;

// FPMaxNum // ==========

func FPMaxNum{N}(op1_in : bits(N), op2_in : bits(N), fpcr : FPCR_Type, fpexc : boolean) => bits(N) begin assert N IN {16,32,64}; var op1 : bits(N) = op1_in; var op2 : bits(N) = op2_in; let (type1,-,-) = FPUnpack{N}(op1, fpcr, fpexc); let (type2,-,-) = FPUnpack{N}(op2, fpcr, fpexc);

let type1_nan : boolean = type1 IN {FPType_QNaN, FPType_SNaN};
let type2_nan : boolean = type2 IN {FPType_QNaN, FPType_SNaN};
let altfp : boolean = IsFeatureImplemented(FEAT_AFP) && () &&
fpcr.AH == '1';

if !(altfp && type1_nan && type2_nan) then
    // Treat a single quiet-NaN as -Infinity.
    if type1 == FPType_QNaN && type2 != FPType_QNaN then
        op1 = FPInfinity{N}('1');
    elsif type1 != FPType_QNaN && type2 == FPType_QNaN then
        op2 = FPInfinity{N}('1');
    end;
end;

let altfmaxfmin : boolean = FALSE; // Restrict use of FMAX/FMIN NaN
propagation rules
let result : bits(N) = FPMax{}(op1, op2, fpcr, altfmaxfmin, fpexc);

return result;

end;


## Library pseudocode for shared/functions/float/fpmerge/IsMerging

// IsMerging // =========== // Returns TRUE if the output elements other than the lowest are taken from // the destination register.

func IsMerging(fpcr : FPCR_Type) => boolean begin let nep : bit = (if IsFeatureImplemented(FEAT_SME) && PSTATE.SM == '1' && () then '0' else fpcr.NEP); return IsFeatureImplemented(FEAT_AFP) && () && nep == '1'; end;


## Library pseudocode for shared/functions/float/fpmin/FPMin

// FPMin // =======

func FPMin{N}(op1 : bits(N), op2 : bits(N), fpcr : FPCR_Type) => bits(N) begin let altfp : boolean = IsFeatureImplemented(FEAT_AFP) && () && fpcr.AH == '1'; let fpexc : boolean = TRUE; return FPMin{N}(op1, op2, fpcr, altfp, fpexc); end;

// FPMin // =======

func FPMin{N}(op1 : bits(N), op2 : bits(N), fpcr : FPCR_Type, altfp : boolean) => bits(N) begin let fpexc : boolean = TRUE; return FPMin{N}(op1, op2, fpcr, altfp, fpexc); end;

// FPMin // ======= // Compare two inputs and return the smaller operand after rounding. The // 'fpcr' argument supplies the FPCR control bits and 'altfp' determines // if the function should use alternative floating-point behavior.

func FPMin{N}(op1 : bits(N), op2 : bits(N), fpcr_in : FPCR_Type, altfp : boolean, fpexc : boolean) => bits(N) begin assert N IN {16,32,64}; var done : boolean; var result : bits(N); var fpcr : FPCR_Type = fpcr_in; let (type1,sign1,value1) : (FPType, bit, real) = FPUnpack{N}(op1, fpcr, fpexc); let (type2,sign2,value2) : (FPType, bit, real) = FPUnpack{N}(op2, fpcr, fpexc);

if altfp && type1 == FPType_Zero && type2 ==
FPType_Zero && sign1 != sign2 then
    // Alternate handling of zeros with differing sign
    return FPZero{N}(sign2);
elsif altfp && (type1 IN {FPType_SNaN, FPType_QNaN} || type2 IN
{FPType_SNaN, FPType_QNaN}) then
    // Alternate handling of NaN inputs
    if fpexc then FPProcessException(FPExc_InvalidOp, fpcr); end;
    return (if type2 == FPType_Zero then FPZero{N}(sign2) else op2);
end;

(done,result) = FPProcessNaNs{N}(type1, type2, op1, op2, fpcr, fpexc);
if !done then
    var fptype : FPType;
    var sign : bit;
    var value : real;
    var rounding : FPRounding;
    if value1 < value2 then
        (fptype,sign,value) = (type1,sign1,value1);
    else
        (fptype,sign,value) = (type2,sign2,value2);
    end;
    if fptype == FPType_Infinity then
        result = FPInfinity{N}(sign);
    elsif fptype == FPType_Zero then
        sign = sign1 OR sign2;              // Use most negative sign
        result = FPZero{N}(sign);
    else
        // The use of FPRound() covers the case where there is a trapped underflow exception
        // for a denormalized number even though the result is exact.
        rounding = FPRoundingMode(fpcr);
        if altfp then    // Denormal output is not flushed to zero
            fpcr.FZ = '0';
            fpcr.FZ16 = '0';
        end;

        result = FPRound{N}(value, fpcr, rounding, fpexc);
    end;

    if fpexc then FPProcessDenorms(type1, type2, N, fpcr); end;
end;
return result;

end;


## Library pseudocode for shared/functions/float/fpminnum/FPMinNum

// FPMinNum // ==========

func FPMinNum{N}(op1 : bits(N), op2 : bits(N), fpcr : FPCR_Type) => bits(N) begin let fpexc : boolean = TRUE; return FPMinNum{N}(op1, op2, fpcr, fpexc); end;

// FPMinNum // ==========

func FPMinNum{N}(op1_in : bits(N), op2_in : bits(N), fpcr : FPCR_Type, fpexc : boolean) => bits(N) begin assert N IN {16,32,64}; var op1 : bits(N) = op1_in; var op2 : bits(N) = op2_in; let (type1,-,-) = FPUnpack{N}(op1, fpcr, fpexc); let (type2,-,-) = FPUnpack{N}(op2, fpcr, fpexc);

let type1_nan : boolean = type1 IN {FPType_QNaN, FPType_SNaN};
let type2_nan : boolean = type2 IN {FPType_QNaN, FPType_SNaN};
let altfp : boolean = IsFeatureImplemented(FEAT_AFP) && () &&
                                                             fpcr.AH == '1';

if !(altfp && type1_nan && type2_nan) then
    // Treat a single quiet-NaN as +Infinity.
    if type1 == FPType_QNaN && type2 != FPType_QNaN then
        op1 = FPInfinity{N}('0');
    elsif type1 != FPType_QNaN && type2 == FPType_QNaN then
        op2 = FPInfinity{N}('0');
    end;
end;

let altfmaxfmin : boolean = FALSE; // Restrict use of FMAX/FMIN NaN
propagation rules
let result : bits(N) = FPMin{}(op1, op2, fpcr, altfmaxfmin, fpexc);

return result;

end;


## Library pseudocode for shared/functions/float/fpmul/FPMul

// FPMul // =======

func FPMul{N}(op1 : bits(N), op2 : bits(N), fpcr : FPCR_Type) => bits(N) begin assert N IN {16,32,64}; let (type1,sign1,value1) : (FPType, bit, real) = FPUnpack{N}(op1, fpcr); let (type2,sign2,value2) : (FPType, bit, real) = FPUnpack{N}(op2, fpcr); var (done,result) : (boolean, bits(N)) = FPProcessNaNs{N}(type1, type2, op1, op2, fpcr); if !done then let inf1 : boolean = (type1 == FPType_Infinity); let inf2 : boolean = (type2 == FPType_Infinity); let zero1 : boolean = (type1 == FPType_Zero); let zero2 : boolean = (type2 == FPType_Zero);

    if (inf1 && zero2) || (zero1 && inf2) then
        result = FPDefaultNaN{N}(fpcr);
        FPProcessException(FPExc_InvalidOp, fpcr);
    elsif inf1 || inf2 then
        result = FPInfinity{N}(sign1 XOR sign2);
    elsif zero1 || zero2 then
        result = FPZero{N}(sign1 XOR sign2);
    else
        result = FPRound{N}(value1*value2, fpcr);
    end;

    FPProcessDenorms(type1, type2, N, fpcr);

end;
return result;

end;


## Library pseudocode for shared/functions/float/fpmuladd/FPMulAdd

// FPMulAdd // ==========

func FPMulAdd{N}(addend : bits(N), op1 : bits(N), op2 : bits(N), fpcr : FPCR_Type) => bits(N) begin let fpexc : boolean = TRUE; // Generate floating-point exceptions return FPMulAdd{N}(addend, op1, op2, fpcr, fpexc); end;

// FPMulAdd // ========== // // Calculates addend + op1*op2 with a single rounding. The 'fpcr' argument // supplies the FPCR control bits, and 'fpexc' controls the generation of // floating-point exceptions.

func FPMulAdd{N}(addend : bits(N), op1 : bits(N), op2 : bits(N), fpcr : FPCR_Type, fpexc : boolean) => bits(N) begin assert N IN {16,32,64};

let (typeA,signA,valueA) : (FPType, bit, real) =
FPUnpack{N}(addend, fpcr, fpexc);
let (type1,sign1,value1) : (FPType, bit, real) = FPUnpack{N}(op1, fpcr,
fpexc);
let (type2,sign2,value2) : (FPType, bit, real) = FPUnpack{N}(op2, fpcr,
fpexc);
let rounding : FPRounding = FPRoundingMode(fpcr);
let inf1 : boolean = (type1 == FPType_Infinity); let zero1 : boolean = (type1
== FPType_Zero);
let inf2 : boolean = (type2 == FPType_Infinity); let zero2 : boolean = (type2
== FPType_Zero);

var (done,result) = FPProcessNaNs3{N}(typeA, type1, type2, addend, op1, op2,
fpcr, fpexc);

if !(IsFeatureImplemented(FEAT_AFP) && () && fpcr.AH == '1')
then
    if typeA == FPType_QNaN && ((inf1 && zero2) || (zero1 && inf2)) then
        result = FPDefaultNaN{N}(fpcr);
        if fpexc then FPProcessException(FPExc_InvalidOp, fpcr); end;
    end;
end;

if !done then
    let infA : boolean = (typeA == FPType_Infinity);
    let zeroA : boolean = (typeA == FPType_Zero);

    // Determine sign and type product will have if it does not cause an
    // Invalid Operation.
    let signP : bit = sign1 XOR sign2;
    let infP : boolean  = inf1 || inf2;
    let zeroP : boolean = zero1 || zero2;

    // Non SNaN-generated Invalid Operation cases are multiplies of zero
    // by infinity and additions of opposite-signed infinities.
    let invalidop : boolean = ((inf1 && zero2) || (zero1 && inf2) ||
                                (infA && infP && signA != signP));

    if invalidop then
        result = FPDefaultNaN{N}(fpcr);
        if fpexc then FPProcessException(FPExc_InvalidOp, fpcr); end;
    // Other cases involving infinities produce an infinity of the same sign.
    elsif (infA && signA == '0') || (infP && signP == '0') then
        result = FPInfinity{N}('0');
    elsif (infA && signA == '1') || (infP && signP == '1') then
        result = FPInfinity{N}('1');

    // Cases where the result is exactly zero and its sign is not determined by the
    // rounding mode are additions of same-signed zeros.
    elsif zeroA && zeroP && signA == signP then
        result = FPZero{N}(signA);

    // Otherwise calculate numerical result and round it.
    else
        let result_value : real = valueA + (value1 * value2);
        if result_value == 0.0 then  // Sign of exact zero result depends on rounding mode
            let result_sign : bit = if rounding == FPRounding_NEGINF then '1' else '0';
            result = FPZero{N}(result_sign);
        else
            result = FPRound{N}(result_value, fpcr, rounding, fpexc);
        end;
    end;

    if !invalidop && fpexc then
        FPProcessDenorms3(typeA, type1, type2, N, fpcr);
    end;
end;
return result;

end;


## Library pseudocode for shared/functions/float/fpmuladd/FPMulAdd\_ZA

// FPMulAdd_ZA // ============= // Calculates addend + op1*op2 with a single rounding for SME ZA-targeting // instructions.

func FPMulAdd_ZA{N}(addend : bits(N), op1 : bits(N), op2 : bits(N), fpcr_in : FPCR_Type) => bits(N) begin var fpcr : FPCR_Type = fpcr_in; let fpexc : boolean = FALSE; // Do not generate floating-point exceptions fpcr.DN = '1'; // Generate default NaN values return FPMulAdd{N}(addend, op1, op2, fpcr, fpexc); end;


## Library pseudocode for shared/functions/float/fpmuladdh/FPMulAddH

// FPMulAddH // =========== // Calculates addend + op1*op2.

func FPMulAddH(addend : bits(32), op1 : bits(16), op2 : bits(16), fpcr : FPCR_Type) => bits(32) begin let fpexc : boolean = TRUE; // Generate floating-point exceptions return FPMulAddH(addend, op1, op2, fpcr, fpexc); end;

// FPMulAddH // =========== // Calculates addend + op1*op2.

func FPMulAddH(addend : bits(32), op1 : bits(16), op2 : bits(16), fpcr : FPCR_Type, fpexc : boolean) => bits(32) begin

let rounding : FPRounding = FPRoundingMode(fpcr);
let (typeA,signA,valueA)  = FPUnpack{32}(addend, fpcr, fpexc);
let (type1,sign1,value1)  = FPUnpack{16}(op1, fpcr, fpexc);
let (type2,sign2,value2)  = FPUnpack{16}(op2, fpcr, fpexc);
let inf1 = (type1 == FPType_Infinity); let zero1 = (type1 == FPType_Zero);
let inf2 = (type2 == FPType_Infinity); let zero2 = (type2 == FPType_Zero);

var (done,result) = FPProcessNaNs3H(typeA, type1, type2, addend, op1, op2,
fpcr, fpexc);

if !(IsFeatureImplemented(FEAT_AFP) && () && fpcr.AH == '1')
then
    if typeA == FPType_QNaN && ((inf1 && zero2) || (zero1 && inf2)) then
        result = FPDefaultNaN{32}(fpcr);
        if fpexc then FPProcessException(FPExc_InvalidOp, fpcr); end;
    end;
end;

if !done then
    let infA = (typeA == FPType_Infinity); let zeroA = (typeA == FPType_Zero);

    // Determine sign and type product will have if it does not cause an
    // Invalid Operation.
    let signP = sign1 XOR sign2;
    let infP  = inf1 || inf2;
    let zeroP = zero1 || zero2;

    // Non SNaN-generated Invalid Operation cases are multiplies of zero by infinity and
    // additions of opposite-signed infinities.
    let invalidop = (inf1 && zero2) || (zero1 && inf2) || (infA && infP && signA != signP);

    if invalidop then
        result = FPDefaultNaN{32}(fpcr);
        if fpexc then FPProcessException(FPExc_InvalidOp, fpcr); end;

    // Other cases involving infinities produce an infinity of the same sign.
    elsif (infA && signA == '0') || (infP && signP == '0') then
        result = FPInfinity{32}('0');
    elsif (infA && signA == '1') || (infP && signP == '1') then
        result = FPInfinity{32}('1');

    // Cases where the result is exactly zero and its sign is not determined by the
    // rounding mode are additions of same-signed zeros.
    elsif zeroA && zeroP && signA == signP then
        result = FPZero{32}(signA);

    // Otherwise calculate numerical result and round it.
    else
        let result_value = valueA + (value1 * value2);
        if result_value == 0.0 then // Sign of exact zero result depends on rounding mode
            let result_sign = if rounding == FPRounding_NEGINF then '1' else '0';
            result = FPZero{32}(result_sign);
        else
            result = FPRound{32}(result_value, fpcr, rounding, fpexc);
        end;
    end;

    if !invalidop && fpexc then
        FPProcessDenorm(typeA, 32, fpcr);
    end;
end;

return result;

end;


## Library pseudocode for shared/functions/float/fpmuladdh/FPMulAddH\_ZA

// FPMulAddH_ZA // ============== // Calculates addend + op1*op2 for SME2 ZA-targeting instructions.

func FPMulAddH_ZA(addend : bits(32), op1 : bits(16), op2 : bits(16), fpcr_in : FPCR_Type) => bits(32) begin var fpcr : FPCR_Type = fpcr_in; let fpexc : boolean = FALSE; // Do not generate floating-point exceptions fpcr.DN = '1'; // Generate default NaN values return FPMulAddH(addend, op1, op2, fpcr, fpexc); end;


## Library pseudocode for shared/functions/float/fpmuladdh/FPProcessNaNs3H

// FPProcessNaNs3H // =================

func FPProcessNaNs3H(type1 : FPType, type2 : FPType, type3 : FPType, op1 : bits(32), op2 : bits(16), op3 : bits(16), fpcr : FPCR_Type, fpexc : boolean) => (boolean, bits(32)) begin

var result : bits(32);
var type_nan : FPType;
// When TRUE, use alternative NaN propagation rules.
let altfp : boolean = IsFeatureImplemented(FEAT_AFP) && () &&
fpcr.AH == '1';
let op1_nan : boolean = type1 IN {FPType_SNaN, FPType_QNaN};
let op2_nan : boolean = type2 IN {FPType_SNaN, FPType_QNaN};
let op3_nan : boolean = type3 IN {FPType_SNaN, FPType_QNaN};
if altfp then
    if (type1 == FPType_SNaN || type2 == FPType_SNaN || type3 == FPType_SNaN) then
        type_nan = FPType_SNaN;
    else
        type_nan = FPType_QNaN;
    end;
end;

var done : boolean;
if altfp && op1_nan && op2_nan && op3_nan then // [n]
register NaN selected
    done = TRUE;  result = FPConvertNaN{32, 16}(FPProcessNaN{16}(type_nan, op2, fpcr, fpexc));
elsif altfp && op2_nan && (op1_nan || op3_nan) then // [n]
register NaN selected
    done = TRUE;  result = FPConvertNaN{32, 16}(FPProcessNaN{16}(type_nan, op2, fpcr, fpexc));
elsif altfp && op3_nan && op1_nan then // [m] register NaN
selected
    done = TRUE;  result = FPConvertNaN{32, 16}(FPProcessNaN{16}(type_nan, op3, fpcr, fpexc));
elsif type1 == FPType_SNaN then
    done = TRUE; result = FPProcessNaN{32}(type1, op1, fpcr, fpexc);
elsif type2 == FPType_SNaN then
    done = TRUE; result = FPConvertNaN{32, 16}(FPProcessNaN{16}(type2, op2, fpcr, fpexc));
elsif type3 == FPType_SNaN then
    done = TRUE; result = FPConvertNaN{32, 16}(FPProcessNaN{16}(type3, op3, fpcr, fpexc));
elsif type1 == FPType_QNaN then
    done = TRUE; result = FPProcessNaN{32}(type1, op1, fpcr, fpexc);
elsif type2 == FPType_QNaN then
    done = TRUE; result = FPConvertNaN{32, 16}(FPProcessNaN{16}(type2, op2, fpcr, fpexc));
elsif type3 == FPType_QNaN then
    done = TRUE; result = FPConvertNaN{32, 16}(FPProcessNaN{16}(type3, op3, fpcr, fpexc));
else
    done = FALSE; result = Zeros{32}; // 'Don't care' result
end;
return (done, result);

end;


## Library pseudocode for shared/functions/float/fpmulx/FPMulX

// FPMulX // ========

func FPMulX{N}(op1 : bits(N), op2 : bits(N), fpcr : FPCR_Type) => bits(N) begin assert N IN {16,32,64}; var result : bits(N);

------------------------------------------------------------------------------ Page 1 of 1. End of thread.