[Writeup] Shared pseudocode: part 29 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/121 ------------------------------------------------------------------------------ #141 admin, 2026-11-28 17:47 UTC [OP] Source: https://romdump.net/raw/141

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

var done : boolean;
let (type1,sign1,value1) : (FPType, bit, real) = FPUnpack{N}(op1, fpcr);
let (type2,sign2,value2) : (FPType, bit, real) = FPUnpack{N}(op2, fpcr);

(done,result) = 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 = FPTwo{N}(sign1 XOR sign2);
    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/fpneg/FPNeg

// FPNeg // =======

func FPNeg{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 NOT(op[N-1]) :: op[N-2:0]; end;


## Library pseudocode for shared/functions/float/fponepointfive/FPOnePointFive

// FPOnePointFive // ================

func FPOnePointFive{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) = '0'::Ones{E-1}; let frac : bits(F) = '1'::Zeros{F-1}; let result : bits((F + E) + 1) = sign :: exp :: frac;

return result;

end;


## Library pseudocode for
shared/functions/float/fpprocessdenorms/FPProcessDenorm

// FPProcessDenorm // ================= // Handles denormal input in case of single-precision or double-precision // when using alternative floating-point mode.

func FPProcessDenorm(fptype : FPType, N : integer, fpcr : FPCR_Type) begin let altfp : boolean = IsFeatureImplemented(FEAT_AFP) && () && fpcr.AH == '1'; if altfp && N != 16 && fptype == FPType_Denormal then FPProcessException(FPExc_InputDenorm, fpcr); end; end;


## Library pseudocode for
shared/functions/float/fpprocessdenorms/FPProcessDenorms

// FPProcessDenorms // ================== // Handles denormal input in case of single-precision or double-precision // when using alternative floating-point mode.

func FPProcessDenorms(type1 : FPType, type2 : FPType, N : integer, fpcr : FPCR_Type) begin let altfp : boolean = IsFeatureImplemented(FEAT_AFP) && () && fpcr.AH == '1'; if altfp && N != 16 && (type1 == FPType_Denormal || type2 == FPType_Denormal) then FPProcessException(FPExc_InputDenorm, fpcr); end; end;


## Library pseudocode for
shared/functions/float/fpprocessdenorms/FPProcessDenorms3

// FPProcessDenorms3 // =================== // Handles denormal input in case of single-precision or double-precision // when using alternative floating-point mode.

func FPProcessDenorms3(type1 : FPType, type2 : FPType, type3 : FPType, N : integer, fpcr : FPCR_Type) begin let altfp : boolean = IsFeatureImplemented(FEAT_AFP) && () && fpcr.AH == '1'; if altfp && N != 16 && (type1 == FPType_Denormal || type2 == FPType_Denormal || type3 == FPType_Denormal) then FPProcessException(FPExc_InputDenorm, fpcr); end; end;


## Library pseudocode for
shared/functions/float/fpprocessexception/FPProcessException

// FPProcessException // ==================== // // The 'fpcr' argument supplies FPCR control bits. Status information is // updated directly in the FPSR where appropriate.

func FPProcessException(except : FPExc, fpcr : FPCR_Type) begin var cumul : integer; // Determine the cumulative exception bit number case except of when FPExc_InvalidOp => cumul = 0; when FPExc_DivideByZero => cumul = 1; when FPExc_Overflow => cumul = 2; when FPExc_Underflow => cumul = 3; when FPExc_Inexact => cumul = 4; when FPExc_InputDenorm => cumul = 7; end; // When FEAT_TRAPFPE is implemented, the control bits FPCR.{IDE, IXE, UFE, OFE, DZE, IOE} // can be independently set to 1. // Otherwise when FEAT_TRAPFPE is not implemented, bits FPCR.{IDE, IXE, UFE, OFE, DZE, IOE} // are RAZ/WI. let enable : integer = cumul + 8; if (fpcr[enable] == '1' && (!IsFeatureImplemented(FEAT_SME) || PSTATE.SM == '0' || IsFullA64Enabled())) then // Trapping of the exception enabled. // It is IMPLEMENTATION DEFINED how exceptions and in what order that they may be // accumulated before calling FPTrappedException(). var accumulated_exceptions : bits(8) = GetAccumulatedFPExceptions(); accumulated_exceptions[cumul] = '1'; if !ImpDefBool("Accumulate floating-point exceptions before trapping") then if UsingAArch32() then AArch32_FPTrappedException(accumulated_exceptions); else let is_ase : boolean = IsASEInstruction(); AArch64_FPTrappedException(is_ase, accumulated_exceptions); end; else // The exceptions generated by this instruction are accumulated by the PE and // FPTrappedException is called later during its execution, before the next // instruction is executed. This field is cleared at the start of each FP instruction. SetAccumulatedFPExceptions(accumulated_exceptions); end; elsif UsingAArch32() then // Set the cumulative exception bit FPSCR()[cumul] = '1'; else // Set the cumulative exception bit FPSR()[cumul] = '1'; end; return; end;


## Library pseudocode for shared/functions/float/fpprocessnan/FPProcessNaN

// FPProcessNaN // ==============

func FPProcessNaN{N}(fptype : FPType, op : bits(N), fpcr : FPCR_Type) => bits(N) begin let fpexc : boolean = TRUE; // Generate floating-point exceptions return FPProcessNaN{N}(fptype, op, fpcr, fpexc); end;

// FPProcessNaN // ============== // Handle NaN input operands, returning the operand or default NaN value // if fpcr.DN is selected. The 'fpcr' argument supplies the FPCR control bits. // The 'fpexc' argument controls the generation of exceptions, regardless of // whether 'fptype' is a signaling NaN or a quiet NaN.

func FPProcessNaN{N}(fptype : FPType, op : bits(N), fpcr : FPCR_Type, fpexc : boolean) => bits(N) begin assert N IN {16,32,64}; assert fptype IN {FPType_QNaN, FPType_SNaN}; var topfrac : integer;

case N of
    when 16 => topfrac =  9;
    when 32 => topfrac = 22;
    when 64 => topfrac = 51;
end;

var result : bits(N) = op;
if fptype == FPType_SNaN then
    result[topfrac] = '1';
    if fpexc then FPProcessException(FPExc_InvalidOp, fpcr); end;
end;
if fpcr.DN == '1' then  // DefaultNaN requested
    result = FPDefaultNaN{N}(fpcr);
end;
return result;

end;


## Library pseudocode for shared/functions/float/fpprocessnans/FPProcessNaNs

// FPProcessNaNs // ===============

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

// FPProcessNaNs // =============== // // The boolean part of the return value says whether a NaN has been found and // processed. The bits(N) part is only relevant if it has and supplies the // result of the operation. // // The 'fpcr' argument supplies FPCR control bits and 'altfmaxfmin' controls // alternative floating-point behavior for FMAX, FMIN and variants. 'fpexc' // controls the generation of floating-point exceptions. Status information // is updated directly in the FPSR where appropriate.

func FPProcessNaNs{N}(type1 : FPType, type2 : FPType, op1 : bits(N), op2 : bits(N), fpcr : FPCR_Type, fpexc : boolean) => (boolean, bits(N)) begin assert N IN {16,32,64}; var done : boolean; var result : bits(N); 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 any_snan : boolean = type1 == FPType_SNaN || type2 == FPType_SNaN; let type_nan : FPType = if any_snan then FPType_SNaN else FPType_QNaN;

if altfp && op1_nan && op2_nan then
    // [n] register NaN selected
    done = TRUE;  result = FPProcessNaN{N}(type_nan, op1, fpcr, fpexc);
elsif type1 == FPType_SNaN then
    done = TRUE;  result = FPProcessNaN{N}(type1, op1, fpcr, fpexc);
elsif type2 == FPType_SNaN then
    done = TRUE;  result = FPProcessNaN{N}(type2, op2, fpcr, fpexc);
elsif type1 == FPType_QNaN then
    done = TRUE;  result = FPProcessNaN{N}(type1, op1, fpcr, fpexc);
elsif type2 == FPType_QNaN then
    done = TRUE;  result = FPProcessNaN{N}(type2, op2, fpcr, fpexc);
else
    done = FALSE;  result = Zeros{N};  // 'Don't care' result
end;
return (done, result);

end;


## Library pseudocode for shared/functions/float/fpprocessnans3/FPProcessNaNs3

// FPProcessNaNs3 // ================

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

// FPProcessNaNs3 // ================ // The boolean part of the return value says whether a NaN has been found and // processed. The bits(N) part is only relevant if it has and supplies the // result of the operation. // // The 'fpcr' argument supplies FPCR control bits and 'fpexc' controls the // generation of floating-point exceptions. Status information is updated // directly in the FPSR where appropriate.

func FPProcessNaNs3{N}(type1 : FPType, type2 : FPType, type3 : FPType, op1 : bits(N), op2 : bits(N), op3 : bits(N), fpcr : FPCR_Type, fpexc : boolean) => (boolean, bits(N)) begin assert N IN {16,32,64}; var result : bits(N); 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};

let altfp : boolean = IsFeatureImplemented(FEAT_AFP) && ()
&& fpcr.AH == '1';
var type_nan : FPType;
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 = FPProcessNaN{N}(type_nan, op2, fpcr, fpexc);
elsif altfp && op2_nan && (op1_nan || op3_nan) then
    // [n] register NaN selected
    done = TRUE;  result = FPProcessNaN{N}(type_nan, op2, fpcr, fpexc);
elsif altfp && op3_nan && op1_nan then
    // [m] register NaN selected
    done = TRUE;  result = FPProcessNaN{N}(type_nan, op3, fpcr, fpexc);
elsif type1 == FPType_SNaN then
    done = TRUE;  result = FPProcessNaN{N}(type1, op1, fpcr, fpexc);
elsif type2 == FPType_SNaN then
    done = TRUE;  result = FPProcessNaN{N}(type2, op2, fpcr, fpexc);
elsif type3 == FPType_SNaN then
    done = TRUE;  result = FPProcessNaN{N}(type3, op3, fpcr, fpexc);
elsif type1 == FPType_QNaN then
    done = TRUE;  result = FPProcessNaN{N}(type1, op1, fpcr, fpexc);
elsif type2 == FPType_QNaN then
    done = TRUE;  result = FPProcessNaN{N}(type2, op2, fpcr, fpexc);
elsif type3 == FPType_QNaN then
    done = TRUE;  result = FPProcessNaN{N}(type3, op3, fpcr, fpexc);
else
    done = FALSE;  result = Zeros{N};  // 'Don't care' result
end;
return (done, result);

end;


## Library pseudocode for shared/functions/float/fpprocessnans4/FPProcessNaNs4

// FPProcessNaNs4 // ================ // The boolean part of the return value says whether a NaN has been found and // processed. The bits(N) part is only relevant if it has and supplies the // result of the operation. // // The 'fpcr' argument supplies FPCR control bits. // Status information is updated directly in the FPSR where appropriate. // The 'fpexc' controls the generation of floating-point exceptions.

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

var result : bits(32);
var done : boolean;
// The FPCR.AH control does not affect these checks
if type1 == FPType_SNaN then
    done = TRUE;  result = FPConvertNaN{32, 16}(FPProcessNaN{16}(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 type4 == FPType_SNaN then
    done = TRUE;  result = FPConvertNaN{32, 16}(FPProcessNaN{16}(type4, op4, fpcr, fpexc));
elsif type1 == FPType_QNaN then
    done = TRUE;  result = FPConvertNaN{32, 16}(FPProcessNaN{16}(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));
elsif type4 == FPType_QNaN then
    done = TRUE;  result = FPConvertNaN{32, 16}(FPProcessNaN{16}(type4, op4, fpcr, fpexc));
else
    done = FALSE;  result = Zeros{32};  // 'Don't care' result
end;

return (done, result);

end;


## Library pseudocode for
shared/functions/float/fprecipestimate/FPRecipEstimate

// FPRecipEstimate // =================

func FPRecipEstimate{N}(operand : bits(N), fpcr_in : FPCR_Type) => bits(N) begin assert N IN {16,32,64}; var fpcr : FPCR_Type = fpcr_in; var result : bits(N); var overflow_to_inf : boolean; // When using alternative floating-point behavior, do not generate // floating-point exceptions, flush denormal input and output to zero, // and use RNE rounding mode. let altfp : boolean = IsFeatureImplemented(FEAT_AFP) && () && fpcr.AH == '1'; let fpexc : boolean = !altfp; if altfp then fpcr.[FIZ,FZ] = '11'; end; if altfp then fpcr.RMode = '00'; end;

let (fptype,sign,value) : (FPType, bit, real) =
FPUnpack{N}(operand, fpcr, fpexc);

let rounding : FPRounding = FPRoundingMode(fpcr);
if fptype == FPType_SNaN || fptype == FPType_QNaN then
    result = FPProcessNaN{N}(fptype, operand, fpcr, fpexc);
elsif fptype == FPType_Infinity then
    result = FPZero{N}(sign);
elsif fptype == FPType_Zero then
    result = FPInfinity{N}(sign);
    if fpexc then FPProcessException(FPExc_DivideByZero, fpcr); end;
elsif (
        (N == 16 && Abs(value) < 2.0^-16) ||
        (N == 32 && Abs(value) < 2.0^-128) ||
        (N == 64 && Abs(value) < 2.0^-1024)
      ) then
    case rounding of
        when FPRounding_TIEEVEN =>
            overflow_to_inf = TRUE;
        when FPRounding_POSINF =>
            overflow_to_inf = (sign == '0');
        when FPRounding_NEGINF =>
            overflow_to_inf = (sign == '1');
        when FPRounding_ZERO =>
            overflow_to_inf = FALSE;
    end;
    result = if overflow_to_inf then FPInfinity{N}(sign) else FPMaxNormal{N}(sign);
    if fpexc then
        FPProcessException(FPExc_Overflow, fpcr);
        FPProcessException(FPExc_Inexact, fpcr);
    end;
elsif ((fpcr.FZ == '1' && N != 16) || (fpcr.FZ16 == '1' && N
== 16))
      && (
           (N == 16 && Abs(value) >= 2.0^14) ||
           (N == 32 && Abs(value) >= 2.0^126) ||
           (N == 64 && Abs(value) >= 2.0^1022)
         ) then
    // Result flushed to zero of correct sign
    result = FPZero{N}(sign);

    // Flush-to-zero never generates a trapped exception.
    if UsingAArch32() then
        FPSCR().UFC = '1';
    else
        if fpexc then FPSR().UFC = '1'; end;
    end;
else
    // Scale to a fixed point value in the range 0.5 <= x < 1.0 in steps of 1/512, and
    // calculate result exponent. Scaled value has copied sign bit,
    // exponent = 1022 = double-precision biased version of -1,
    // fraction = original fraction
    var fraction : bits(52);
    var exp : integer;
    case N of
        when 16 =>
            fraction = operand[9:0] :: Zeros{42};
            exp = UInt(operand[14:10]);
        when 32 =>
            fraction = operand[22:0] :: Zeros{29};
            exp = UInt(operand[30:23]);
        when 64 =>
            fraction = operand[51:0];
            exp = UInt(operand[62:52]);
    end;

    if exp == 0 then
        if fraction[51] == '0' then
            exp = -1;
            fraction = fraction[49:0]::'00';
        else
            fraction = fraction[50:0]::'0';
        end;
    end;

    var scaled : integer;
    let increasedprecision : boolean = N==32 && IsFeatureImplemented(FEAT_RPRES) && altfp;

    if !increasedprecision then
        scaled = UInt('1'::fraction[51:44]);
    else
        scaled = UInt('1'::fraction[51:41]);
    end;

    var result_exp : integer;
    case N of
        when 16 => result_exp = 29 - exp;   // In range 29-30 = -1 to 29+1 = 30
        when 32 => result_exp = 253 - exp;  // In range 253-254 = -1 to 253+1 = 254
        when 64 => result_exp = 2045 - exp; // In range 2045-2046 = -1 to 2045+1 = 2046
    end;

    // Scaled is in range 256 .. 511 or 2048 .. 4095 range representing a
    // fixed-point number in range [0.5 .. 1.0].
    let estimate : integer = RecipEstimate(scaled, increasedprecision);

    // Estimate is in the range 256 .. 511 or 4096 .. 8191 representing a
    // fixed-point result in the range [1.0 .. 2.0].
    // Convert to scaled floating point result with copied sign bit,
    // high-order bits from estimate, and exponent calculated above.
    if !increasedprecision then
        fraction = estimate[7:0] :: Zeros{44};
    else
        fraction = estimate[11:0] :: Zeros{40};
    end;

    if result_exp == 0 then
        fraction = '1' :: fraction[51:1];
    elsif result_exp == -1 then
        fraction = '01' :: fraction[51:2];
        result_exp = 0;
    end;

    case N of
        when 16 => result = sign :: result_exp[N-12:0] :: fraction[51:42];
        when 32 => result = sign :: result_exp[N-25:0] :: fraction[51:29];
        when 64 => result = sign :: result_exp[N-54:0] :: fraction[51:0];
    end;
end;

return result;

end;


## Library pseudocode for shared/functions/float/fprecipestimate/RecipEstimate

// RecipEstimate // =============== // Compute estimate of reciprocal of 9-bit fixed-point number. // // a is in range 256 .. 511 or 2048 .. 4096 representing a number in // the range 0.5 <= x < 1.0. // increasedprecision determines if the mantissa is 8-bit or 12-bit. // result is in the range 256 .. 511 or 4096 .. 8191 representing a // number in the range 1.0 to 511/256 or 1.00 to 8191/4096.

func RecipEstimate(a_in : integer, increasedprecision : boolean) => integer begin var a : integer = a_in; var r : integer; if !increasedprecision then assert 256 <= a && a < 512; a = a2+1; // Round to nearest let b : integer = (2 ^ 19) DIVRM a; r = (b+1) DIVRM 2; // Round to nearest assert 256 <= r && r < 512; else assert 2048 <= a && a < 4096; a = a2+1; // Round to nearest let b : integer = (2 ^ 26) DIVRM a; r = (b+1) DIVRM 2; // Round to nearest assert 4096 <= r && r < 8192; end; return r; end;


## Library pseudocode for shared/functions/float/fprecpx/FPRecpX

// FPRecpX // =========

func FPRecpX{N}(op : bits(N), fpcr_in : FPCR_Type) => bits(N) begin assert N IN {16,32,64}; var fpcr : FPCR_Type = fpcr_in; let isbfloat16 : boolean = FALSE; let (F, -) = FPBits(N, isbfloat16); let E = (N - F) - 1; var result : bits(N); var exp : bits(E); var max_exp : bits(E); let frac : bits(F) = Zeros{};

let altfp : boolean = IsFeatureImplemented(FEAT_AFP) &&
fpcr.AH == '1';
let fpexc : boolean = !altfp; // Generate no floating-point exceptions
if altfp then fpcr.[FIZ,FZ] = '11'; end; // Flush denormal input and output to
zero
let (fptype,sign,value) = FPUnpack{N}(op, fpcr, fpexc);
exp = op[F+:E];
max_exp = Ones{E} - 1;

if fptype == FPType_SNaN || fptype == FPType_QNaN then
    result = FPProcessNaN{N}(fptype, op, fpcr, fpexc);
else
    if IsZero(exp) then                 // Zero and denormals
        result = ZeroExtend{N}(sign::max_exp::frac);
    else                                // Infinities and normals
        result = ZeroExtend{N}(sign::NOT(exp)::frac);
    end;
end;

return result;

end;


## Library pseudocode for shared/functions/float/fpround/FPRound

// FPRound // ========= // Generic conversion from precise, unbounded real data type to IEEE format.

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

// FPRound // ========= // For directed FP conversion, includes an explicit 'rounding' argument.

func FPRound{N}(op : real, fpcr_in : FPCR_Type, rounding : FPRounding) => bits(N) begin let fpexc : boolean = TRUE; // Generate floating-point exceptions return FPRound{N}(op, fpcr_in, rounding, fpexc); end;

// FPRound // ========= // For AltFP, includes an explicit FPEXC argument to disable exception // generation and switches off Arm alternate half-precision mode.

func FPRound{N}(op : real, fpcr_in : FPCR_Type, rounding : FPRounding, fpexc : boolean) => bits(N) begin var fpcr : FPCR_Type = fpcr_in; fpcr.AHP = '0'; let isbfloat16 : boolean = FALSE; return FPRoundBase{N}(op, fpcr, rounding, isbfloat16, fpexc); end;


## Library pseudocode for shared/functions/float/fpround/FPRoundBase

// FPRoundBase // ============= // For BFloat16, includes an explicit 'isbfloat16' argument.

func FPRoundBase{N}(op : real, fpcr : FPCR_Type, rounding : FPRounding, isbfloat16 : boolean) => bits(N) begin let fpexc : boolean = TRUE; // Generate floating-point exceptions return FPRoundBase{N}(op, fpcr, rounding, isbfloat16, fpexc); end;

// FPRoundBase // ============= // For FP8 multiply-accumulate, dot product, and outer product instructions, includes // an explicit saturation overflow argument.

func FPRoundBase{N}(op : real, fpcr : FPCR_Type, rounding : FPRounding, isbfloat16 : boolean, fpexc : boolean) => bits(N) begin let satoflo : boolean = FALSE; return FPRoundBase{N}(op, fpcr, rounding, isbfloat16, fpexc, satoflo); end;

// FPRoundBase // ============= // Convert a real number 'op' into an N-bit floating-point value using the // supplied rounding mode 'rounding'. // // The 'fpcr' argument supplies FPCR control bits and 'fpexc' controls the // generation of floating-point exceptions. Status information is updated // directly in the FPSR where appropriate. The 'satoflo' argument // controls whether overflow generates Infinity or MaxNorm for 8-bit floating-point // data processing instructions.

func FPRoundBase{N}(op : real, fpcr : FPCR_Type, rounding : FPRounding, isbfloat16 : boolean, fpexc : boolean, satoflo : boolean) => bits(N) begin

assert N IN {16,32,64};
assert op != 0.0;
assert rounding != FPRounding_TIEAWAY;
var result : bits(N);

// Obtain format parameters - minimum exponent, numbers of exponent and
fraction bits.
let (F, minimum_exp) = FPBits(N, isbfloat16);
let zeros = if N == 32 && isbfloat16 then 16 else 0;
let E = N - (F + 1 + zeros);
// 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 and the test for a
// denormalized number for single and double precision values occurs after
rounding.
let altfp : boolean = IsFeatureImplemented(FEAT_AFP) && () &&
fpcr.AH == '1';

// Deal with flush-to-zero before rounding if FPCR.AH != '1'.
if (!altfp && ((fpcr.FZ == '1' && N != 16) || (fpcr.FZ16 ==
'1' && N == 16)) &&
      exponent < minimum_exp) then
    // Flush-to-zero never generates a trapped exception.
    if UsingAArch32() then
        FPSCR().UFC = '1';
    else
        if fpexc then FPSR().UFC = '1'; end;
    end;
    return FPZero{N}(sign);
end;

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);

// 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'.
let trapped_UF : boolean = fpcr.UFE == '1' && (() ||
IsFullA64Enabled());
if !altfp && biased_exp == 0 && (error != 0.0 || trapped_UF)
then
    if fpexc then FPProcessException(FPExc_Underflow, fpcr); end;
end;

// Round result according to rounding mode.
var round_up_unconstrained : boolean;
var round_up : boolean;
var overflow_to_inf : boolean;
if altfp then

    case rounding of
        when FPRounding_TIEEVEN =>
            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'));
            overflow_to_inf = !satoflo;
        when FPRounding_POSINF =>
            round_up_unconstrained = (error_unconstrained != 0.0 && sign == '0');
            round_up = (error != 0.0 && sign == '0');
            overflow_to_inf = (sign == '0');
        when FPRounding_NEGINF =>
            round_up_unconstrained = (error_unconstrained != 0.0 && sign == '1');
            round_up = (error != 0.0 && sign == '1');
            overflow_to_inf = (sign == '1');
        when FPRounding_ZERO, FPRounding_ODD =>
            round_up_unconstrained = FALSE;
            round_up = FALSE;
            overflow_to_inf = FALSE;
    end;

    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;

    // Deal with flush-to-zero and underflow after rounding if FPCR.AH == '1'.
    if biased_exp_unconstrained < 1 && int_mant_unconstrained != 0 then
        // the result of unconstrained rounding is less than the minimum normalized number
        if (fpcr.FZ == '1' && N != 16) || (fpcr.FZ16 == '1' && N == 16) then   // Flush-to-zero
            if fpexc then
                FPSR().UFC = '1';
                FPProcessException(FPExc_Inexact, fpcr);
            end;
            return FPZero{N}(sign);
        elsif error != 0.0 || trapped_UF then
            if fpexc then FPProcessException(FPExc_Underflow, fpcr); end;
        end;
    end;
else    // altfp == FALSE
    case rounding of
        when FPRounding_TIEEVEN =>
            round_up = (error > 0.5 || (error == 0.5 && int_mant[0] == '1'));
            overflow_to_inf = !satoflo;
        when FPRounding_POSINF =>
            round_up = (error != 0.0 && sign == '0');
            overflow_to_inf = (sign == '0');
        when FPRounding_NEGINF =>
            round_up = (error != 0.0 && sign == '1');
            overflow_to_inf = (sign == '1');
        when FPRounding_ZERO, FPRounding_ODD =>
            round_up = FALSE;
            overflow_to_inf = FALSE;
    end;
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;

// Handle rounding to odd
if error != 0.0 && rounding == FPRounding_ODD && int_mant[0]
== '0' then
    int_mant = int_mant + 1;
end;

// Deal with overflow and generate result.
if N != 16 || fpcr.AHP == '0' then  // Single, double or IEEE half precision
    if biased_exp >= 2^E - 1 then
        result = if overflow_to_inf then FPInfinity{N}(sign) else FPMaxNormal{N}(sign);
        if fpexc then FPProcessException(FPExc_Overflow, fpcr); end;
        error = 1.0;  // Ensure that an Inexact exception occurs
    else
        result = sign :: biased_exp[E-1:0] :: int_mant[F-1:0] :: Zeros{N-(E+F+1)};
    end;
else                                     // Alternative half precision
    if biased_exp >= 2^E then
        result = sign :: Ones{N-1};
        if fpexc then FPProcessException(FPExc_InvalidOp, fpcr); end;
        error = 0.0;  // Ensure that an Inexact exception does not occur
    else
        result = sign :: biased_exp[E-1:0] :: int_mant[F-1:0] :: Zeros{N-(E+F+1)};
    end;
end;

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

return result;

end;


## Library pseudocode for shared/functions/float/fpround/FPRoundCV

// FPRoundCV // =========== // Used for FP to FP conversion instructions. // For half-precision data ignores FZ16 and observes AHP.

func FPRoundCV{N}(op : real, fpcr_in : FPCR_Type, rounding : FPRounding) => bits(N) begin var fpcr : FPCR_Type = fpcr_in; fpcr.FZ16 = '0'; let fpexc : boolean = TRUE; // Generate floating-point exceptions let isbfloat16 : boolean = FALSE; return FPRoundBase{N}(op, fpcr, rounding, isbfloat16, fpexc); end;


## Library pseudocode for shared/functions/float/fpround/FPRound\_FP8

// FPRound_FP8 // ============= // Used by FP8 multiply-accumulate, dot product, and outer product instructions // which observe FPMR.OSM.

func FPRound_FP8{N}(op : real, fpcr_in : FPCR_Type, rounding : FPRounding, satoflo : boolean) => bits(N) begin var fpcr : FPCR_Type = fpcr_in; fpcr.AHP = '0'; let fpexc : boolean = FALSE; let isbfloat16 : boolean = FALSE; return FPRoundBase{N}(op, fpcr, rounding, isbfloat16, fpexc, satoflo); end;


## Library pseudocode for shared/functions/float/fprounding/FPRounding

// FPRounding // ========== // The conversion and rounding functions take an explicit // rounding mode enumeration instead of booleans or FPCR values.

type FPRounding of enumeration {FPRounding_TIEEVEN, FPRounding_POSINF, FPRounding_NEGINF, FPRounding_ZERO, FPRounding_TIEAWAY, FPRounding_ODD};


## Library pseudocode for shared/functions/float/fproundingmode/FPRoundingMode

// FPRoundingMode // ================ // Return the current floating-point rounding mode.

func FPRoundingMode(fpcr : FPCR_Type) => FPRounding begin return FPDecodeRounding(fpcr.RMode); end;


## Library pseudocode for shared/functions/float/fproundint/FPRoundInt

// FPRoundInt // ============

// Round op to nearest integral floating point value using rounding mode in FPCR/FPSCR. // If EXACT is TRUE, set FPSR.IXC if result is not numerically equal to op.

func FPRoundInt{N}(op : bits(N), fpcr : FPCR_Type, rounding : FPRounding, exact : boolean) => bits(N) begin assert rounding != FPRounding_ODD; assert N IN {16,32,64};

// When alternative floating-point support is TRUE, do not generate
// Input Denormal floating-point exceptions.
let altfp : boolean = IsFeatureImplemented(FEAT_AFP) && () &&
fpcr.AH == '1';
let fpexc : boolean = !altfp;

// Unpack using FPCR to determine if subnormals are flushed-to-zero.
let (fptype,sign,value) = FPUnpack{N}(op, fpcr, fpexc);

var result : bits(N);
if fptype == FPType_SNaN || fptype == FPType_QNaN then
    result = FPProcessNaN{N}(fptype, op, fpcr);
elsif fptype == FPType_Infinity then
    result = FPInfinity{N}(sign);
elsif fptype == FPType_Zero then
    result = FPZero{N}(sign);
else
    // Extract integer component.
    var int_result : integer = RoundDown(value);
    let error : real = value - Real(int_result);

    // Determine whether supplied rounding mode requires an increment.
    var round_up : boolean;
    case rounding of
        when FPRounding_TIEEVEN =>
            round_up = (error > 0.5 || (error == 0.5 && int_result[0] == '1'));
        when FPRounding_POSINF =>
            round_up = (error != 0.0);
        when FPRounding_NEGINF =>
            round_up = FALSE;
        when FPRounding_ZERO =>
            round_up = (error != 0.0 && int_result < 0);
        when FPRounding_TIEAWAY =>
            round_up = (error > 0.5 || (error == 0.5 && int_result >= 0));
    end;

    if round_up then int_result = int_result + 1; end;

    // Convert integer value into an equivalent real value.
    let real_result : real = Real(int_result);

    // Re-encode as a floating-point value, result is always exact.
    if real_result == 0.0 then
        result = FPZero{N}(sign);
    else
        result = FPRound{N}(real_result, fpcr, FPRounding_ZERO);
    end;

    // Generate inexact exceptions.
    if error != 0.0 && exact then
        FPProcessException(FPExc_Inexact, fpcr);
    end;
end;

return result;

end;


## Library pseudocode for shared/functions/float/fproundintn/FPRoundIntN

// FPRoundIntN // =============

func FPRoundIntN{N}(op : bits(N), fpcr : FPCR_Type, rounding : FPRounding, intsize : integer) => bits(N) begin assert rounding != FPRounding_ODD; assert N IN {32,64}; assert intsize IN {32, 64}; var exp : integer; var result : bits(N); var round_up : boolean; let E : integer{} = (if N == 32 then 8 else 11); let F : integer{} = N - (E + 1);

// When alternative floating-point support is TRUE, do not generate
// Input Denormal floating-point exceptions.
let altfp = IsFeatureImplemented(FEAT_AFP) && () && fpcr.AH ==
'1';
let fpexc = !altfp;

// Unpack using FPCR to determine if subnormals are flushed-to-zero.
let (fptype,sign,value) = FPUnpack{N}(op, fpcr, fpexc);

if fptype IN {FPType_SNaN, FPType_QNaN, FPType_Infinity} then
    if N == 32 then
        exp    = 126 + intsize;
        result = '1'::exp[(E-1):0]::Zeros{F};
    else
        exp    = 1022+intsize;
        result = '1'::exp[(E-1):0]::Zeros{F};
    end;
    FPProcessException(FPExc_InvalidOp, fpcr);
elsif fptype == FPType_Zero then
    result = FPZero{N}(sign);
else
    // Extract integer component.
    var int_result = RoundDown(value);
    var error      = value - Real(int_result);

    // Determine whether supplied rounding mode requires an increment.
    case rounding of
        when FPRounding_TIEEVEN =>
            round_up = error > 0.5 || (error == 0.5 && int_result[0] == '1');
        when FPRounding_POSINF =>
            round_up = error != 0.0;
        when FPRounding_NEGINF =>
            round_up = FALSE;
        when FPRounding_ZERO =>
            round_up = error != 0.0 && int_result < 0;
        when FPRounding_TIEAWAY =>
            round_up = error > 0.5 || (error == 0.5 && int_result >= 0);
    end;

    if round_up then int_result = int_result + 1; end;
    let overflow = int_result > 2^(intsize-1)-1 || int_result < -1*2^(intsize-1);

    if overflow then
        if N == 32 then
            exp    = 126 + intsize;
            result = '1'::exp[(E-1):0]::Zeros{F};
        else
            exp    = 1022 + intsize;
            result = '1'::exp[(E-1):0]::Zeros{F};
        end;
        FPProcessException(FPExc_InvalidOp, fpcr);
        // This case shouldn't set Inexact.
        error = 0.0;

    else
        // Convert integer value into an equivalent real value.
        let real_result : real = Real(int_result);

        // Re-encode as a floating-point value, result is always exact.
        if real_result == 0.0 then
            result = FPZero{N}(sign);
        else
            result = FPRound{N}(real_result, fpcr, FPRounding_ZERO);
        end;
    end;

    // Generate inexact exceptions.
    if error != 0.0 then
        FPProcessException(FPExc_Inexact, fpcr);
    end;
end;

return result;

end;


## Library pseudocode for
shared/functions/float/fprsqrtestimate/FPRSqrtEstimate

// FPRSqrtEstimate // =================

func FPRSqrtEstimate{N}(operand : bits(N), fpcr_in : FPCR_Type) => bits(N) begin assert N IN {16,32,64}; var fpcr : FPCR_Type = fpcr_in;

// When using alternative floating-point behavior, do not generate
// floating-point exceptions and flush denormal input to zero.
let altfp : boolean = IsFeatureImplemented(FEAT_AFP) && () &&
fpcr.AH == '1';
let fpexc : boolean = !altfp;
if altfp then fpcr.[FIZ,FZ] = '11'; end;

let (fptype,sign,value) : (FPType, bit, real) = FPUnpack{N}(operand, fpcr,
fpexc);

var result : bits(N);
if fptype == FPType_SNaN || fptype == FPType_QNaN then
    result = FPProcessNaN{N}(fptype, operand, fpcr, fpexc);
elsif fptype == FPType_Zero then
    result = FPInfinity{N}(sign);
    if fpexc then FPProcessException(FPExc_DivideByZero, fpcr); end;
elsif sign == '1' then
    result = FPDefaultNaN{N}(fpcr);
    if fpexc then FPProcessException(FPExc_InvalidOp, fpcr); end;
elsif fptype == FPType_Infinity then
    result = FPZero{N}('0');
else
    // Scale to a fixed-point value in the range 0.25 <= x < 1.0 in steps of 512, with the
    // evenness or oddness of the exponent unchanged, and calculate result exponent.
    // Scaled value has copied sign bit, exponent = 1022 or 1021 = double-precision
    // biased version of -1 or -2, fraction = original fraction extended with zeros.

    var fraction : bits(52);
    var exp : integer;
    case N of
        when 16 =>
            fraction = operand[9:0] :: Zeros{42};
            exp      = UInt(operand[14:10]);
        when 32 =>
            fraction = operand[22:0] :: Zeros{29};
            exp      = UInt(operand[30:23]);
        when 64 =>
            fraction = operand[51:0];
            exp      = UInt(operand[62:52]);
    end;

    if exp == 0 then
        while fraction[51] == '0' looplimit 51 do
            fraction = fraction[50:0] :: '0';
            exp      = exp - 1;
        end;
        fraction = fraction[50:0] :: '0';
    end;

    var scaled : integer;
    let increasedprecision : boolean = N==32 && IsFeatureImplemented(FEAT_RPRES) && altfp;

    if !increasedprecision then
        if exp[0] == '0' then
            scaled = UInt('1'::fraction[51:44]);
        else
            scaled = UInt('01'::fraction[51:45]);
        end;
    else
        if exp[0] == '0' then
            scaled = UInt('1'::fraction[51:41]);
        else
            scaled = UInt('01'::fraction[51:42]);
        end;
    end;

    var result_exp : integer;
    case N of
        when 16 => result_exp = (  44 - exp) DIVRM 2;
        when 32 => result_exp = ( 380 - exp) DIVRM 2;
        when 64 => result_exp = (3068 - exp) DIVRM 2;
    end;

    let estimate : integer = RecipSqrtEstimate(scaled, increasedprecision);

    // Estimate is in the range 256 .. 511 or 4096 .. 8191 representing a
    // fixed-point result in the range [1.0 .. 2.0].
    // Convert to scaled floating point result with copied sign bit and high-order
    // fraction bits, and exponent calculated above.
    case N of
        when 16 => result = '0' :: result_exp[N-12:0] :: estimate[7:0]::Zeros{2};
        when 32 =>
            if !increasedprecision then
                result = '0' :: result_exp[N-25:0] :: estimate[7:0]::Zeros{15};
            else
                result = '0' :: result_exp[N-25:0] :: estimate[11:0]::Zeros{11};
            end;
        when 64 => result = '0' :: result_exp[N-54:0] :: estimate[7:0]::Zeros{44};
    end;
end;

return result;

end;


## Library pseudocode for
shared/functions/float/fprsqrtestimate/RecipSqrtEstimate

// RecipSqrtEstimate // =================== // Compute estimate of reciprocal square root of 9-bit fixed-point number. // // a_in is in range 128 .. 511 or 1024 .. 4095, with increased precision, // representing a number in the range 0.25 <= x < 1.0. // increasedprecision determines if the mantissa is 8-bit or 12-bit. // result is in the range 256 .. 511 or 4096 .. 8191, with increased precision, // representing a number in the range 1.0 to 511/256 or 8191/4096.

func RecipSqrtEstimate(a_in : integer, increasedprecision : boolean) => integer begin var a : integer = a_in; var r : integer; if !increasedprecision then assert 128 <= a && a < 512; if a < 256 then // a in [128, 255], represents a value in [0.25, 0.5) a = a2+1; // promote to 9-bit range in units of 1/512 else // a in [256, 511], represents a value in [0.5, 1.0) a = (a >> 1) << 1; // Discard bottom bit a = (a+1)2; // round up to nearest 1/256 and convert end; var b : integer = 512; while a(b+1)(b+1) < 2^28 looplimit 510 do b = b+1; end; // b = largest b such that b < 2^14 / sqrt(a) r = (b+1) DIVRM 2; // Round to nearest assert 256 <= r && r < 512; else assert 1024 <= a && a < 4096; if a < 2048 then // a in [1024, 2047], represents a value in [0.25, 0.5) a = a2 + 1; // promote to 13-bit range in units of 1/8192 else // a in [2048, 4095], represents a value in 0.5, 1.0) a = (a >> 1) << 1; // Discard bottom bit a = (a+1)2; // round up to nearest 1/4096 and convert end; var b : integer = 8192; while a(b+1)(b+1) < 2^39 looplimit (2^39 - (2049 * 8193^2)) do b = b+1; end; r = (b+1) DIVRM 2; // Round to nearest assert 4096 <= r && r < 8192; end; return r; end;


## Library pseudocode for shared/functions/float/fpsqrt/FPSqrt

// FPSqrt // ========

func FPSqrt{N}(op : bits(N), fpcr : FPCR_Type) => bits(N) begin assert N IN {16,32,64}; var (fptype,sign,value) : ([FPType, bit, real) = FPUnpack{N}(op, fpcr);

var result : bits(N);
if fptype == FPType_SNaN || fptype == FPType_QNaN then
    result = FPProcessNaN{N}(fptype, op, fpcr);
elsif fptype == FPType_Zero then
    result = FPZero{N}(sign);
elsif fptype == FPType_Infinity && sign == '0' then
    result = FPInfinity{N}(sign);
elsif sign == '1' then
    result = FPDefaultNaN{N}(fpcr);
    FPProcessException(FPExc_InvalidOp, fpcr);
else
    var prec : integer;
    if N == 16 then
        prec = 13; // 10 fraction bits + 3
    elsif N == 32 then
        prec = 26; // 23 fraction bits + 3
    else // N == 64
        prec = 55; // 52 fraction bits + 3
    end;
    value = SqrtRounded(value, prec);
    result = FPRound{N}(value, fpcr);
    FPProcessDenorm(fptype, N, fpcr);

end;
return result;

end;


## Library pseudocode for shared/functions/float/fpsub/FPSub

// FPSub // =======

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

// FPSub // =======

func FPSub{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) : (FPType, bit, real) = FPUnpack{N}(op1, fpcr,
fpexc);
let (type2,sign2,value2) : (FPType, bit, real) = FPUnpack{N}(op2, fpcr,
fpexc);

var (done,result) : (boolean, bits(N)) = 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 == sign2 then
        result = FPDefaultNaN{N}(fpcr);
        if fpexc then FPProcessException(FPExc_InvalidOp, fpcr); end;
    elsif (inf1 && sign1 == '0') || (inf2 && sign2 == '1') then
        result = FPInfinity{N}('0');
    elsif (inf1 && sign1 == '1') || (inf2 && sign2 == '0') then
        result = FPInfinity{N}('1');
    elsif zero1 && zero2 && sign1 == NOT(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/fpsub/FPSub\_ZA

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

func FPSub_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 FPSub{N}(op1, op2, fpcr, fpexc); end;


## Library pseudocode for shared/functions/float/fpthree/FPThree

// FPThree // =========

func FPThree{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) = '1'::Zeros{E-1}; let frac : bits(F) = '1'::Zeros{F-1}; let result : bits((F + E) + 1) = sign :: exp :: frac;

return result;

end;


## Library pseudocode for shared/functions/float/fptofixed/FPToFixed

// FPToFixed // ===========

// Convert N-bit precision floating point 'op' to M-bit fixed point with // FBITS fractional bits, controlled by UNSIGNED and ROUNDING.

func FPToFixed{M, N}(op : bits(N), fbits : integer, unsigned : boolean, fpcr : FPCR_Type, rounding : FPRounding) => bits(M) begin assert N IN {16,32,64}; assert M IN {8,16,32,64}; assert fbits >= 0; assert rounding != FPRounding_ODD;

// When alternative floating-point support is TRUE, do not generate
// Input Denormal floating-point exceptions.
let altfp = IsFeatureImplemented(FEAT_AFP) && () && fpcr.AH ==
'1';
let fpexc = !altfp;

// Unpack using fpcr to determine if subnormals are flushed-to-zero.
var (fptype,sign,value) : (FPType, bit, real) = FPUnpack{N}(op, fpcr, fpexc);

// If NaN, set cumulative flag or take exception.
if fptype == FPType_SNaN || fptype == FPType_QNaN then
    FPProcessException(FPExc_InvalidOp, fpcr);
end;

// Scale by fractional bits and produce integer rounded towards
minus-infinity.
value = value * 2.0^fbits;
var int_result : integer = RoundDown(value);
let error : real         = value - Real(int_result);

// Determine whether supplied rounding mode requires an increment.
var round_up : boolean;
case rounding of
    when FPRounding_TIEEVEN =>
        round_up = (error > 0.5 || (error == 0.5 && int_result[0] == '1'));
    when FPRounding_POSINF =>
        round_up = (error != 0.0);
    when FPRounding_NEGINF =>
        round_up = FALSE;
    when FPRounding_ZERO =>
        round_up = (error != 0.0 && int_result < 0);
    when FPRounding_TIEAWAY =>
        round_up = (error > 0.5 || (error == 0.5 && int_result >= 0));
end;

if round_up then int_result = int_result + 1; end;

// Generate saturated result and exceptions.
let (result, overflow) : (bits(M), boolean) = SatQ{M}(int_result, unsigned);
if overflow then
    FPProcessException(FPExc_InvalidOp, fpcr);
elsif error != 0.0 then
    FPProcessException(FPExc_Inexact, fpcr);
end;

return result;

end;


## Library pseudocode for shared/functions/float/fptofixedjs/FPToFixedJS

// FPToFixedJS // =============

// Converts a double precision floating point input value // to a signed integer, with rounding to zero.

func FPToFixedJS(op : bits(64), fpcr : FPCR_Type) => (bits(32), bit) begin

// If FALSE, never generate Input Denormal floating-point
exceptions.
let fpexc_idenorm : boolean = !(IsFeatureImplemented(FEAT_AFP) &&
                                () && fpcr.AH == '1');

// Unpack using fpcr to determine if subnormals are flushed-to-zero.
let (fptype,sign,value) = FPUnpack{64}(op, fpcr, fpexc_idenorm);

var z : bit = '1';
// If NaN, set cumulative flag or take exception.
if fptype == FPType_SNaN || fptype == FPType_QNaN then
    FPProcessException(FPExc_InvalidOp, fpcr);
    z = '0';
end;

var int_result = RoundDown(value);
let error = value - Real(int_result);

// Determine whether supplied rounding mode requires an increment.

let round_it_up = (error != 0.0 && int_result < 0);
if round_it_up then int_result = int_result + 1; end;

var result : integer;
if int_result < 0 then
    result = int_result - 2^32*RoundUp(Real(int_result)/Real(2^32));
else
    result = int_result - 2^32*RoundDown(Real(int_result)/Real(2^32));
end;

// Generate exceptions.
if int_result < -(2^31) || int_result > (2^31)-1 then
    FPProcessException(FPExc_InvalidOp, fpcr);
    z = '0';
elsif error != 0.0 then
    FPProcessException(FPExc_Inexact, fpcr);
    z = '0';
elsif sign == '1' && value == 0.0 then
    z = '0';
elsif sign == '0' && value == 0.0 && !IsZero(op[51:0]) then
    z = '0';
end;

if fptype == FPType_Infinity then result = 0; end;

return (result[31:0], z);

end;


## Library pseudocode for shared/functions/float/fptwo/FPTwo

// FPTwo // =======

func FPTwo{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) = '1'::Zeros{E-1}; let frac : bits(F) = Zeros{}; let result : bits((F + E) + 1) = sign :: exp :: frac; return result; end;


## Library pseudocode for shared/functions/float/fptype/FPType

// FPType // ======

type FPType of enumeration {FPType_Zero, FPType_Denormal, FPType_Nonzero, FPType_Infinity, FPType_QNaN, FPType_SNaN};


## Library pseudocode for shared/functions/float/fpunpack/FPUnpack

// FPUnpack // ==========

func FPUnpack{N}(fpval : bits(N), fpcr_in : FPCR_Type) => (FPType, bit, real) begin var fpcr : FPCR_Type = fpcr_in; fpcr.AHP = '0'; let fpexc : boolean = TRUE; // Generate floating-point exceptions

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