Shared pseudocode from the offline Arm documentation snapshot. Part 27 of 36.
output_fault.statuscode = Fault_SyncExternalOnWalk; end;end;if IsFeatureImplemented(FEAT_RAS) then output_fault.merrorstate = memretstatus.merrorstate;end;if (output_fault) then PendSErrorInterrupt(output_fault); output_fault.statuscode = Fault_None;end;output_fault.paddress = memaddrdesc.paddress;return output_fault;
end;
## Library pseudocode for shared/functions/externalaborts/HandleExternalWriteAbort
// HandleExternalWriteAbort // ========================== // Wrapper function for HandleExternalAbort function in case of an External // Abort on memory write.
func HandleExternalWriteAbort(memstatus : PhysMemRetStatus, memaddrdesc : AddressDescriptor, size : integer, accdesc : AccessDescriptor) begin let iswrite : boolean = TRUE; HandleExternalAbort(memstatus, iswrite, memaddrdesc, size, accdesc); end;
## Library pseudocode for shared/functions/externalaborts/IsDelegatedSErrorPending
// IsDelegatedSErrorPending // ========================== // Return TRUE if a delegated SError interrupt is pending.
func IsDelegatedSErrorPending() => boolean begin return SCR_EL3().DSE == '1'; end;
## Library pseudocode for shared/functions/externalaborts/IsExternalAbortTakenSynchronously
// IsExternalAbortTakenSynchronously // =================================== // Return an implementation specific value: // TRUE if the fault returned for the access can be taken synchronously, // FALSE otherwise. // // This might vary between accesses, for example depending on the error type // or memory type being accessed. // External aborts on data accesses and translation table walks on data accesses // can be either synchronous or asynchronous. // // When FEAT_DoubleFault is not implemented, External aborts on instruction // fetches and translation table walks on instruction fetches can be either // synchronous or asynchronous. // When FEAT_DoubleFault is implemented, all External abort exceptions on // instruction fetches and translation table walks on instruction fetches // must be synchronous.
impdef func IsExternalAbortTakenSynchronously(memstatus : PhysMemRetStatus, iswrite : boolean, desc : AddressDescriptor, size : integer, accdesc : AccessDescriptor) => boolean begin return FALSE; end;
## Library pseudocode for shared/functions/externalaborts/IsPhysicalSErrorPending
// IsPhysicalSErrorPending // ========================= // Returns TRUE if a physical SError interrupt is pending.
readonly impdef func IsPhysicalSErrorPending() => boolean begin return FALSE; end;
## Library pseudocode for shared/functions/externalaborts/IsSErrorEdgeTriggered
// IsSErrorEdgeTriggered // ======================= // Returns TRUE if the physical SError interrupt is edge-triggered // and FALSE otherwise.
func IsSErrorEdgeTriggered() => boolean begin if IsFeatureImplemented(FEAT_DoubleFault) then return TRUE; else return ImpDefBool("Edge-triggered SError"); end; end;
## Library pseudocode for shared/functions/externalaborts/IsSynchronizablePhysicalSErrorPending
// IsSynchronizablePhysicalSErrorPending // ======================================= // Returns TRUE if a synchronizable physical SError interrupt is pending.
impdef func IsSynchronizablePhysicalSErrorPending() => boolean begin return FALSE; end;
## Library pseudocode for shared/functions/externalaborts/IsVirtualSErrorPending
// IsVirtualSErrorPending // ======================== // Return TRUE if a virtual SError interrupt is pending.
readonly func IsVirtualSErrorPending() => boolean begin if ELUsingAArch32(EL2) then return HCR().VA == '1'; else return HCR_EL2().VSE == '1'; end; end;
## Library pseudocode for shared/functions/externalaborts/PEErrorState
// PEErrorState // ============== // Returns the error state of the PE on taking an error exception: // The PE error state reported to software through the exception syndrome also // depends on how the exception is taken, and so might differ from the value // returned from this function.
func PEErrorState(fault : FaultRecord) => ErrorState begin assert (); if (() || (() && ()) || ReportErrorAsUC()) then return ErrorState_UC; end;
if () || ReportErrorAsUEU() then return ErrorState_UEU; end;if ActionRequired() || ReportErrorAsUER() then return ErrorState_UER; end;
return ErrorState_UEO;
end;
## Library pseudocode for shared/functions/externalaborts/PendSErrorInterrupt
// PendSErrorInterrupt // ===================== // Pend the SError Interrupt.
impdef func PendSErrorInterrupt(fault : FaultRecord) begin return; end;
## Library pseudocode for shared/functions/externalaborts/ReportErrorAsIMPDEF
// ReportErrorAsIMPDEF // ===================== // Return an implementation specific value: // returns TRUE if Error is IMPDEF, FALSE otherwise.
impdef func ReportErrorAsIMPDEF() => boolean begin return FALSE; end;
## Library pseudocode for shared/functions/externalaborts/ReportErrorAsUC
// ReportErrorAsUC // ================= // Return an implementation specific value: // returns TRUE if Error is Uncontainable, FALSE otherwise.
impdef func ReportErrorAsUC() => boolean begin return FALSE; end;
## Library pseudocode for shared/functions/externalaborts/ReportErrorAsUER
// ReportErrorAsUER // ================== // Return an implementation specific value: // returns TRUE if Error is Recoverable, FALSE otherwise.
impdef func ReportErrorAsUER() => boolean begin return FALSE; end;
## Library pseudocode for shared/functions/externalaborts/ReportErrorAsUEU
// ReportErrorAsUEU // ================== // Return an implementation specific value: // returns TRUE if Error is Unrecoverable, FALSE otherwise.
impdef func ReportErrorAsUEU() => boolean begin return FALSE; end;
## Library pseudocode for shared/functions/externalaborts/ReportErrorAsUncategorized
// ReportErrorAsUncategorized // ============================ // Return an implementation specific value: // returns TRUE if Error is uncategorized, FALSE otherwise.
impdef func ReportErrorAsUncategorized() => boolean begin return FALSE; end;
## Library pseudocode for shared/functions/externalaborts/StateIsRecoverable
// StateIsRecoverable // ==================== // Return an implementation specific value: // returns TRUE is PE State is unrecoverable else FALSE.
impdef func StateIsRecoverable() => boolean begin return TRUE; end;
## Library pseudocode for shared/functions/float/bfloat/BFAdd
// BFAdd // ======= // Non-widening BFloat16 addition used by SVE2 instructions.
func BFAdd{N}(op1 : bits(N), op2 : bits(N), fpcr : FPCR_Type) => bits(N) begin let fpexc : boolean = TRUE; return BFAdd{N}(op1, op2, fpcr, fpexc); end;
// BFAdd // ======= // Non-widening BFloat16 addition following computational behaviors // corresponding to instructions that read and write BFloat16 values. // Calculates op1 + op2. // The 'fpcr' argument supplies the FPCR control bits.
func BFAdd{N}(op1 : bits(N), op2 : bits(N), fpcr : FPCR_Type, fpexc : boolean) => bits(N) begin assert N == 16; let rounding : FPRounding = FPRoundingMode(fpcr); var done : boolean; var result : bits(2*N);
let op1_s : bits(2*N) = op1 :: Zeros{N}; let op2_s : bits(2*N) = op2 :: Zeros{N}; let (type1,sign1,value1) = FPUnpack{2*N}(op1_s, fpcr, fpexc); let (type2,sign2,value2) = FPUnpack{2*N}(op2_s, fpcr, fpexc);(done,result) = FPProcessNaNs{2*N}(type1, type2, op1_s, op2_s, fpcr, fpexc);
if !done then let inf1 = (type1 == FPType_Infinity); let inf2 = (type2 == FPType_Infinity); let zero1 = (type1 == FPType_Zero); let zero2 = (type2 == FPType_Zero);
if inf1 && inf2 && sign1 == NOT(sign2) then result = FPDefaultNaN{2*N}(fpcr); if fpexc then FPProcessException(FPExc_InvalidOp, fpcr); end;elsif (inf1 && sign1 == '0') || (inf2 && sign2 == '0') then result = FPInfinity{2*N}('0');elsif (inf1 && sign1 == '1') || (inf2 && sign2 == '1') then result = FPInfinity{2*N}('1');elsif zero1 && zero2 && sign1 == sign2 then result = FPZero{2*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{2*N}(result_sign); else result = FPRoundBF{2*N}(result_value, fpcr, rounding, fpexc); end;end;if fpexc then FPProcessDenorms(type1, type2, 2*N, fpcr); end;end;
return result[2*N-1:N];
end;
## Library pseudocode for shared/functions/float/bfloat/BFAdd\_ZA
// BFAdd_ZA // ========== // Non-widening BFloat16 addition used by SME2 ZA-targeting instructions.
func BFAdd_ZA(op1 : bits(16), op2 : bits(16), fpcr_in : FPCR_Type) => bits(16) begin let fpexc : boolean = FALSE; var fpcr : FPCR_Type = fpcr_in; fpcr.DN = '1'; // Generate default NaN values return BFAdd{16}(op1, op2, fpcr, fpexc); end;
## Library pseudocode for shared/functions/float/bfloat/BFClampScale
// BFClampScale // ==============
func BFClampScale(op : bits(16), scale_in : integer) => integer begin let E : integer = 8; let F : integer = 7; let exp : integer = UInt(op[14:7]); let emax : integer = (1 << E) - 1; let min_scale : integer = -(F + 1); let max_scale : integer = emax + (F + 1);
let scale : integer = Max(min_scale - exp, Min(scale_in, max_scale - exp));return scale;
end;
## Library pseudocode for shared/functions/float/bfloat/BFDotAdd
// BFDotAdd // ========== // BFloat16 2-way dot-product and add to single-precision // result = addend + op1_aop2_a + op1_bop2_b
func BFDotAdd(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);
var result : bits(32); if !IsFeatureImplemented(FEAT_EBF16) || fpcr.EBF == '0' then // Standard BFloat16 behaviors prod = FPAdd_BF16(BFMulH(op1_a, op2_a, fpcr), BFMulH(op1_b, op2_b, fpcr), fpcr); result = FPAdd_BF16(addend, prod, fpcr); else // Extended BFloat16 behaviors let isbfloat16 : boolean = TRUE; 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); result = FPAdd{32}(addend, prod, fpcr, fpexc); end;
return result;
end;
## Library pseudocode for shared/functions/float/bfloat/BFInfinity
// BFInfinity // ============
func BFInfinity{N}(sign : bit) => bits(N) begin assert N == 16; let E : integer{} = 8; let F : integer{} = N - (E + 1); return sign :: Ones{E} :: Zeros{F}; end;
## Library pseudocode for shared/functions/float/bfloat/BFMatMulAdd
// BFMatMulAdd // ============= // BFloat16 matrix multiply and add to same precision matrix // result[2, 2] = addend[2, 2] + (op1[2, 2] * op2[2, 2])
func BFMatMulAdd(addend : bits(64), op1 : bits(64), op2 : bits(64), fpcr : FPCR_Type) => bits(64) begin
var result : bits(64);for i = 0 to 1 do for j = 0 to 1 do let sum : bits(16) = addend[(2i + j):16]; let prod0 : bits(16) = BFMul(op1[(2i + 0):16], op2[(2j + 0):16], fpcr); let prod1 : bits(16) = BFMul(op1[(2i + 1):16], op2[(2j + 1):16], fpcr); result[(2i + j):16] = BFAdd{16}(sum, BFAdd{16}(prod0, prod1, fpcr), fpcr); end; end;
return result;
end;
## Library pseudocode for shared/functions/float/bfloat/BFMatMulAddH
// BFMatMulAddH // ============== // BFloat16 matrix multiply and add to single-precision matrix // result[2, 2] = addend[2, 2] + (op1[2, 4] * op2[4, 2])
func BFMatMulAddH(addend : bits(128), op1 : bits(128), op2 : bits(128), fpcr : FPCR_Type) => bits(128) begin
var result : bits(128); var sum : bits(32);for i = 0 to 1 do for j = 0 to 1 do sum = addend[(2i + j):32]; for k = 0 to 1 do let elt1_a : bits(16) = op1[(4i + 2k + 0):16]; let elt1_b : bits(16) = op1[(4i + 2k + 1):16]; let elt2_a : bits(16) = op2[(4j + 2k + 0):16]; let elt2_b : bits(16) = op2[(4j + 2k + 1):16]; sum = BFDotAdd(sum, elt1_a, elt1_b, elt2_a, elt2_b, fpcr); end; result[(2i + j):32] = sum; end; end;
return result;
end;
## Library pseudocode for shared/functions/float/bfloat/BFMax
// BFMax // ======= // BFloat16 maximum.
func BFMax{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 BFMax{N}(op1, op2, fpcr, altfp, fpexc); end;
// BFMax // ======= // BFloat16 maximum.
func BFMax{N}(op1 : bits(N), op2 : bits(N), fpcr : FPCR_Type, altfp : boolean) => bits(N) begin let fpexc : boolean = TRUE; return BFMax{N}(op1, op2, fpcr, altfp, fpexc); end;
// BFMax // ======= // BFloat16 maximum following computational behaviors // corresponding to instructions that read and write BFloat16 values. // Compare op1 and op2 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 BFMax{N}(op1 : bits(N), op2 : bits(N), fpcr_in : FPCR_Type, altfp : boolean, fpexc : boolean) => bits(N) begin assert N == 16; var fpcr : FPCR_Type = fpcr_in; let rounding : FPRounding = FPRoundingMode(fpcr); var done : boolean; var result : bits(2*N);
let op1_s : bits(2*N) = op1 :: Zeros{N}; let op2_s : bits(2*N) = op2 :: Zeros{N}; let (type1,sign1,value1) = FPUnpack{2*N}(op1_s, fpcr, fpexc); let (type2,sign2,value2) = FPUnpack{2*N}(op2_s, fpcr, fpexc);if altfp && type1 == FPType_Zero && type2 == FPType_Zero && sign1 != sign2 then // Alternate handling of zeros with differing sign return BFZero{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 BFZero{N}(sign2) else op2); end;
(done,result) = FPProcessNaNs{2N}(type1, type2, op1_s, op2_s, 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{2N}(sign); elsif fptype == FPType_Zero then sign = sign1 AND sign2; // Use most positive sign result = FPZero{2N}(sign); else if altfp then // Denormal output is not flushed to zero fpcr.FZ = '0'; end; result = FPRoundBF{2N}(value, fpcr, rounding, fpexc); end;
if fpexc then FPProcessDenorms(type1, type2, 2*N, fpcr); end;end;
return result[2*N-1:N];
end;
## Library pseudocode for shared/functions/float/bfloat/BFMaxNum
// BFMaxNum // ==========
func BFMaxNum{N}(op1 : bits(N), op2 : bits(N), fpcr : FPCR_Type) => bits(N) begin let fpexc : boolean = TRUE; return BFMaxNum{N}(op1, op2, fpcr, fpexc); end;
// BFMaxNum // ========== // BFloat16 maximum number following computational behaviors corresponding // to instructions that read and write BFloat16 values. // Compare op1 and op2 and return the larger number operand after rounding. // The 'fpcr' argument supplies the FPCR control bits.
func BFMaxNum{N}(op1_in : bits(N), op2_in : bits(N), fpcr : FPCR_Type, fpexc : boolean) => bits(N) begin assert N == 16; let isbfloat16 : boolean = TRUE; var op1 : bits(N) = op1_in; var op2 : bits(N) = op2_in; let altfp : boolean = IsFeatureImplemented(FEAT_AFP) && () && fpcr.AH == '1'; var result : bits(N);
let (type1,-,-) = FPUnpackBase{N}(op1, fpcr, fpexc, isbfloat16); let (type2,-,-) = FPUnpackBase{N}(op2, fpcr, fpexc, isbfloat16);let type1_nan : boolean = type1 IN {FPType_QNaN, FPType_SNaN}; let type2_nan : boolean = type2 IN {FPType_QNaN, FPType_SNaN};
if !(altfp && type1_nan && type2_nan) then // Treat a single quiet-NaN as -Infinity. if type1 == FPType_QNaN && type2 != FPType_QNaN then op1 = BFInfinity{N}('1'); elsif type1 != FPType_QNaN && type2 == FPType_QNaN then op2 = BFInfinity{N}('1'); end; end;
let altfmaxfmin : boolean = FALSE; // Do not use alternate NaN handling result = BFMax{N}(op1, op2, fpcr, altfmaxfmin, fpexc);
return result;
end;
## Library pseudocode for shared/functions/float/bfloat/BFMin
// BFMin // ======= // BFloat16 minimum.
func BFMin{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 BFMin{N}(op1, op2, fpcr, altfp, fpexc); end;
// BFMin // ======= // BFloat16 minimum.
func BFMin{N}(op1 : bits(N), op2 : bits(N), fpcr : FPCR_Type, altfp : boolean) => bits(N) begin let fpexc : boolean = TRUE; return BFMin{N}(op1, op2, fpcr, altfp, fpexc); end;
// BFMin // ======= // BFloat16 minimum following computational behaviors // corresponding to instructions that read and write BFloat16 values. // Compare op1 and op2 and return the smaller value after rounding. // The 'fpcr' argument supplies the FPCR control bits and 'altfp' determines // if the function should use alternative floating-point behavior.
func BFMin{N}(op1 : bits(N), op2 : bits(N), fpcr_in : FPCR_Type, altfp : boolean, fpexc : boolean) => bits(N) begin assert N == 16; var fpcr : FPCR_Type = fpcr_in; let rounding : FPRounding = FPRoundingMode(fpcr); var done : boolean; var result : bits(2*N);
let op1_s : bits(2*N) = op1 :: Zeros{N}; let op2_s : bits(2*N) = op2 :: Zeros{N}; let (type1,sign1,value1) = FPUnpack{2*N}(op1_s, fpcr, fpexc); let (type2,sign2,value2) = FPUnpack{2*N}(op2_s, fpcr, fpexc);if altfp && type1 == FPType_Zero && type2 == FPType_Zero && sign1 != sign2 then // Alternate handling of zeros with differing sign return BFZero{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 BFZero{N}(sign2) else op2); end;
(done,result) = FPProcessNaNs{2N}(type1, type2, op1_s, op2_s, 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{2N}(sign); elsif fptype == FPType_Zero then sign = sign1 OR sign2; // Use most negative sign result = FPZero{2N}(sign); else if altfp then // Denormal output is not flushed to zero fpcr.FZ = '0'; end; result = FPRoundBF{2N}(value, fpcr, rounding, fpexc); end;
if fpexc then FPProcessDenorms(type1, type2, 2*N, fpcr); end;end;
return result[2*N-1:N];
end;
## Library pseudocode for shared/functions/float/bfloat/BFMinNum
// BFMinNum // ==========
func BFMinNum{N}(op1 : bits(N), op2 : bits(N), fpcr : FPCR_Type) => bits(N) begin let fpexc : boolean = TRUE; return BFMinNum{N}(op1, op2, fpcr, fpexc); end;
// BFMinNum // ========== // BFloat16 minimum number following computational behaviors corresponding // to instructions that read and write BFloat16 values. // Compare op1 and op2 and return the smaller number operand after rounding. // The 'fpcr' argument supplies the FPCR control bits.
func BFMinNum{N}(op1_in : bits(N), op2_in : bits(N), fpcr : FPCR_Type, fpexc : boolean) => bits(N) begin assert N == 16; let isbfloat16 : boolean = TRUE; var op1 : bits(N) = op1_in; var op2 : bits(N) = op2_in; let altfp : boolean = IsFeatureImplemented(FEAT_AFP) && () && fpcr.AH == '1'; var result : bits(N);
let (type1,-,-) = FPUnpackBase{N}(op1, fpcr, fpexc, isbfloat16); let (type2,-,-) = FPUnpackBase{N}(op2, fpcr, fpexc, isbfloat16);let type1_nan : boolean = type1 IN {FPType_QNaN, FPType_SNaN}; let type2_nan : boolean = type2 IN {FPType_QNaN, FPType_SNaN};
if !(altfp && type1_nan && type2_nan) then // Treat a single quiet-NaN as +Infinity. if type1 == FPType_QNaN && type2 != FPType_QNaN then op1 = BFInfinity{N}('0'); elsif type1 != FPType_QNaN && type2 == FPType_QNaN then op2 = BFInfinity{N}('0'); end; end;
let altfmaxfmin : boolean = FALSE; // Do not use alternate NaN handling result = BFMin{N}(op1, op2, fpcr, altfmaxfmin, fpexc);
return result;
end;
## Library pseudocode for shared/functions/float/bfloat/BFMul
// BFMul // ======= // Non-widening BFloat16 multiply used by SVE2 instructions.
func BFMul(op1 : bits(16), op2 : bits(16), fpcr : FPCR_Type) => bits(16) begin let fpexc : boolean = TRUE; return BFMul(op1, op2, fpcr, fpexc); end;
// BFMul // ======= // Non-widening BFloat16 multiply following computational behaviors // corresponding to instructions that read and write BFloat16 values. // Calculates op1 * op2. // The 'fpcr' argument supplies the FPCR control bits.
func BFMul(op1 : bits(16), op2 : bits(16), fpcr : FPCR_Type, fpexc : boolean) => bits(16) begin let rounding : FPRounding = FPRoundingMode(fpcr); var done : boolean; var result : bits(32);
let op1_s : bits(32) = op1 :: Zeros{16}; let op2_s : bits(32) = op2 :: Zeros{16}; let (type1,sign1,value1) = FPUnpack{32}(op1_s, fpcr, fpexc); let (type2,sign2,value2) = FPUnpack{32}(op2_s, fpcr, fpexc);(done,result) = FPProcessNaNs{32}(type1, type2, op1_s, op2_s, fpcr, fpexc);
if !done then let inf1 = (type1 == FPType_Infinity); let inf2 = (type2 == FPType_Infinity); let zero1 = (type1 == FPType_Zero); let zero2 = (type2 == FPType_Zero);
if (inf1 && zero2) || (zero1 && inf2) then result = FPDefaultNaN{32}(fpcr); if fpexc then FPProcessException(FPExc_InvalidOp, fpcr); end;elsif inf1 || inf2 then result = FPInfinity{32}(sign1 XOR sign2);elsif zero1 || zero2 then result = FPZero{32}(sign1 XOR sign2);else result = FPRoundBF{32}(value1*value2, fpcr, rounding, fpexc);end;if fpexc then FPProcessDenorms(type1, type2, 32, fpcr); end;end;
return result[31:16];
end;
## Library pseudocode for shared/functions/float/bfloat/BFMulAdd
// BFMulAdd // ========== // Non-widening BFloat16 fused multiply-add used by SVE2 instructions.
func BFMulAdd(addend : bits(16), op1 : bits(16), op2 : bits(16), fpcr : FPCR_Type) => bits(16) begin let fpexc : boolean = TRUE; return BFMulAdd(addend, op1, op2, fpcr, fpexc); end;
// BFMulAdd // ========== // Non-widening BFloat16 fused multiply-add following computational behaviors // corresponding to instructions that read and write BFloat16 values. // Calculates addend + op1*op2 with a single rounding. // The 'fpcr' argument supplies the FPCR control bits.
func BFMulAdd(addend : bits(16), op1 : bits(16), op2 : bits(16), fpcr : FPCR_Type, fpexc : boolean) => bits(16) begin let rounding : FPRounding = FPRoundingMode(fpcr); var done : boolean; var result : bits(32);
let addend_s : bits(32) = addend :: Zeros{16}; let op1_s : bits(32) = op1 :: Zeros{16}; let op2_s : bits(32) = op2 :: Zeros{16}; let (typeA,signA,valueA) = FPUnpack{32}(addend_s, fpcr, fpexc); let (type1,sign1,value1) = FPUnpack{32}(op1_s, fpcr, fpexc); let (type2,sign2,value2) = FPUnpack{32}(op2_s, fpcr, fpexc);let inf1 = (type1 == FPType_Infinity); let inf2 = (type2 == FPType_Infinity); let zero1 = (type1 == FPType_Zero); let zero2 = (type2 == FPType_Zero);
(done,result) = FPProcessNaNs3{32}(typeA, type1, type2, addend_s, op1_s, op2_s, 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 : 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{32}(result_sign); else result = FPRoundBF{32}(result_value, fpcr, rounding, fpexc); end;end;if !invalidop && fpexc then FPProcessDenorms3(typeA, type1, type2, 32, fpcr);end;end;
return result[31:16];
end;
## Library pseudocode for shared/functions/float/bfloat/BFMulAddH
// BFMulAddH // =========== // Used by BFMLALB, BFMLALT, BFMLSLB and BFMLSLT instructions.
func BFMulAddH(addend : bits(32), op1 : bits(16), op2 : bits(16), fpcr_in : FPCR_Type) => bits(32) begin let value1 : bits(32) = op1 :: Zeros{16}; let value2 : bits(32) = op2 :: Zeros{16}; var fpcr : FPCR_Type = fpcr_in; let altfp : boolean = IsFeatureImplemented(FEAT_AFP) && fpcr.AH == '1'; // When using alternative floating-point behavior, do not generate floating-point exceptions let fpexc : boolean = !altfp; if altfp then fpcr.[FIZ,FZ] = '11'; end; // Flush denormal input and // output to zero if altfp then fpcr.RMode = '00'; end; // Use RNE rounding mode return FPMulAdd{32}(addend, value1, value2, fpcr, fpexc); end;
## Library pseudocode for shared/functions/float/bfloat/BFMulAddH\_ZA
// BFMulAddH_ZA // ============== // Used by SME2 ZA-targeting BFMLAL and BFMLSL instructions.
func BFMulAddH_ZA(addend : bits(32), op1 : bits(16), op2 : bits(16), fpcr : FPCR_Type) => bits(32) begin let value1 : bits(32) = op1 :: Zeros{16}; let value2 : bits(32) = op2 :: Zeros{16}; return FPMulAdd_ZA{32}(addend, value1, value2, fpcr); end;
## Library pseudocode for shared/functions/float/bfloat/BFMulAdd\_ZA
// BFMulAdd_ZA // ============= // Non-widening BFloat16 fused multiply-add used by SME2 ZA-targeting instructions.
func BFMulAdd_ZA(addend : bits(16), op1 : bits(16), op2 : bits(16), fpcr_in : FPCR_Type) => bits(16) begin let fpexc : boolean = FALSE; var fpcr : FPCR_Type = fpcr_in; fpcr.DN = '1'; // Generate default NaN values return BFMulAdd(addend, op1, op2, fpcr, fpexc); end;
## Library pseudocode for shared/functions/float/bfloat/BFMulH
// BFMulH // ======== // BFloat16 widening multiply to single-precision following BFloat16 // computation behaviors.
func BFMulH(op1 : bits(16), op2 : bits(16), fpcr : FPCR_Type) => bits(32) begin var result : bits(32);
let (type1,sign1,value1) = BFUnpack{16}(op1); let (type2,sign2,value2) = BFUnpack{16}(op2); if type1 == FPType_QNaN || type2 == FPType_QNaN then result = FPDefaultNaN{32}(fpcr); else let inf1 = (type1 == FPType_Infinity); let inf2 = (type2 == FPType_Infinity); let zero1 = (type1 == FPType_Zero); let zero2 = (type2 == FPType_Zero); if (inf1 && zero2) || (zero1 && inf2) then result = FPDefaultNaN{32}(fpcr); elsif inf1 || inf2 then result = FPInfinity{32}(sign1 XOR sign2); elsif zero1 || zero2 then result = FPZero{32}(sign1 XOR sign2); else result = BFRound(value1*value2); end; end;
return result;
end;
## Library pseudocode for shared/functions/float/bfloat/BFNeg
// BFNeg // =======
func BFNeg(op : bits(16)) => bits(16) begin let honor_altfp : boolean = TRUE; // Honor alternate handling return BFNeg(op, honor_altfp); end;
// BFNeg // =======
func BFNeg(op : bits(16), honor_altfp : boolean) => bits(16) begin if honor_altfp && () && IsFeatureImplemented(FEAT_AFP) then if FPCR().AH == '1' then let fpexc : boolean = FALSE; let isbfloat16 : boolean = TRUE; let (fptype, -, -) = FPUnpackBase{16}(op, FPCR(), fpexc, isbfloat16); 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[15]) :: op[14:0]; end;
## Library pseudocode for shared/functions/float/bfloat/BFRound
// BFRound // ========= // Converts a real number OP into a single-precision value using the // Round to Odd rounding mode and following BFloat16 computation behaviors.
func BFRound(op : real) => bits(32) begin assert op != 0.0; var result : bits(32);
// Format parameters - minimum exponent, numbers of exponent and fraction bits. let minimum_exp : integer = -126; let E : integer{} = 8; let F : integer{} = 23;// 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); // Fixed Flush-to-zero. if exponent < minimum_exp then return FPZero{32}(sign); end;
// 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). let 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); let error : real = mantissa * 2.0^F - Real(int_mant);
// Round to Odd if error != 0.0 && int_mant[0] == '0' then int_mant = int_mant + 1; end;
// Deal with overflow and generate result. if biased_exp >= 2^E - 1 then result = FPInfinity{32}(sign); // Overflows generate appropriately-signed Infinity else result = sign :: biased_exp[E-1:0] :: int_mant[F-1:0]; end;
return result;
end;
## Library pseudocode for shared/functions/float/bfloat/BFScale
// BFScale // ========= // Scales BFloat16 operand by 2.0 to the power of the signed integer value.
func BFScale(op : bits(16), scale : integer, fpcr : FPCR_Type) => bits(16) begin var result : bits(32);
let op_s : bits(32) = op :: Zeros{16}; let (fptype,sign,value) = FPUnpack{32}(op_s, fpcr);if fptype == FPType_SNaN || fptype == FPType_QNaN then result = FPProcessNaN{32}(fptype, op_s, fpcr); elsif fptype == FPType_Zero then result = FPZero{32}(sign); elsif fptype == FPType_Infinity then result = FPInfinity{32}(sign); else let rounding : FPRounding = FPRoundingMode(fpcr); let fpexc : boolean = TRUE; let clamped_scale : integer = BFClampScale(op, scale); result = FPRoundBF{32}(value * (2.0^clamped_scale), fpcr, rounding, fpexc); if fpexc then FPProcessDenorm(fptype, 32, fpcr); end; end;
return result[31:16];
end;
## Library pseudocode for shared/functions/float/bfloat/BFSub
// BFSub // ======= // Non-widening BFloat16 subtraction used by SVE2 instructions.
func BFSub(op1 : bits(16), op2 : bits(16), fpcr : FPCR_Type) => bits(16) begin let fpexc : boolean = TRUE; return BFSub(op1, op2, fpcr, fpexc); end;
// BFSub // ======= // Non-widening BFloat16 subtraction following computational behaviors // corresponding to instructions that read and write BFloat16 values. // Calculates op1 - op2. // The 'fpcr' argument supplies the FPCR control bits.
func BFSub(op1 : bits(16), op2 : bits(16), fpcr : FPCR_Type, fpexc : boolean) => bits(16) begin let rounding : FPRounding = FPRoundingMode(fpcr); var done : boolean; var result : bits(32);
let op1_s : bits(32) = op1 :: Zeros{16}; let op2_s : bits(32) = op2 :: Zeros{16}; let (type1,sign1,value1) = FPUnpack{32}(op1_s, fpcr, fpexc); let (type2,sign2,value2) = FPUnpack{32}(op2_s, fpcr, fpexc);(done,result) = FPProcessNaNs{32}(type1, type2, op1_s, op2_s, fpcr, fpexc);
if !done then let inf1 = (type1 == FPType_Infinity); let inf2 = (type2 == FPType_Infinity); let zero1 = (type1 == FPType_Zero); let zero2 = (type2 == FPType_Zero);
if inf1 && inf2 && sign1 == sign2 then result = FPDefaultNaN{32}(fpcr); if fpexc then FPProcessException(FPExc_InvalidOp, fpcr); end;elsif (inf1 && sign1 == '0') || (inf2 && sign2 == '1') then result = FPInfinity{32}('0');elsif (inf1 && sign1 == '1') || (inf2 && sign2 == '0') then result = FPInfinity{32}('1');elsif zero1 && zero2 && sign1 == NOT(sign2) then result = FPZero{32}(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{32}(result_sign); else result = FPRoundBF{32}(result_value, fpcr, rounding, fpexc); end;end;if fpexc then FPProcessDenorms(type1, type2, 32, fpcr); end;end;
return result[31:16];
end;
## Library pseudocode for shared/functions/float/bfloat/BFSub\_ZA
// BFSub_ZA // ========== // Non-widening BFloat16 subtraction used by SME2 ZA-targeting instructions.
func BFSub_ZA(op1 : bits(16), op2 : bits(16), fpcr_in : FPCR_Type) => bits(16) begin let fpexc : boolean = FALSE; var fpcr : FPCR_Type = fpcr_in; fpcr.DN = '1'; // Generate default NaN values return BFSub(op1, op2, fpcr, fpexc); end;
## Library pseudocode for shared/functions/float/bfloat/BFUnpack
// BFUnpack // ========== // Unpacks a BFloat16 or single-precision value into its type, // sign bit and real number that it represents. // The real number result has the correct sign for numbers and infinities, // is very large in magnitude for infinities, and is 0.0 for NaNs. // (These values are chosen to simplify the description of // comparisons and conversions.)
func BFUnpack{N}(fpval : bits(N)) => (FPType, bit, real) begin assert N IN {16,32};
var sign : bit; var exp : bits(8); var frac : bits(23); if N == 16 then sign = fpval[15]; exp = fpval[14:7]; frac = fpval[6:0] :: Zeros{16}; else // N == 32 sign = fpval[31]; exp = fpval[30:23]; frac = fpval[22:0]; end;var fptype : FPType; var value : real; if IsZero(exp) then fptype = FPType_Zero; value = 0.0; // Fixed Flush to Zero elsif IsOnes(exp) then if IsZero(frac) then fptype = FPType_Infinity; value = 2.0^1000000; else // no SNaN for BF16 arithmetic fptype = FPType_QNaN; value = 0.0; end; else fptype = FPType_Nonzero; value = 2.0^(UInt(exp)-127) * (1.0 + Real(UInt(frac)) * 2.0^-23); end;
if sign == '1' then value = -value; end;
return (fptype, sign, value);
end;
## Library pseudocode for shared/functions/float/bfloat/BFZero
// BFZero // ========
func BFZero{N}(sign : bit) => bits(N) begin assert N == 16; let E : integer{} = 8; let F : integer{} = N - (E + 1); return sign :: Zeros{E} :: Zeros{F}; end;
## Library pseudocode for shared/functions/float/bfloat/FPAdd\_BF16
// FPAdd_BF16 // ============ // Single-precision add following BFloat16 computation behaviors.
func FPAdd_BF16(op1 : bits(32), op2 : bits(32), fpcr : FPCR_Type) => bits(32) begin var result : bits(32);
let (type1,sign1,value1) = BFUnpack{32}(op1); let (type2,sign2,value2) = BFUnpack{32}(op2); if type1 == FPType_QNaN || type2 == FPType_QNaN then result = FPDefaultNaN{32}(fpcr); else let inf1 = (type1 == FPType_Infinity); let inf2 = (type2 == FPType_Infinity); let zero1 = (type1 == FPType_Zero); let zero2 = (type2 == FPType_Zero); if inf1 && inf2 && sign1 == NOT(sign2) then result = FPDefaultNaN{32}(fpcr); elsif (inf1 && sign1 == '0') || (inf2 && sign2 == '0') then result = FPInfinity{32}('0'); elsif (inf1 && sign1 == '1') || (inf2 && sign2 == '1') then result = FPInfinity{32}('1'); elsif zero1 && zero2 && sign1 == sign2 then result = FPZero{32}(sign1); else let result_value : real = value1 + value2; if result_value == 0.0 then result = FPZero{32}('0'); // Positive sign when Round to Odd else result = BFRound(result_value); end; end; end;
return result;
end;
## Library pseudocode for shared/functions/float/bfloat/FPConvertBF
// FPConvertBF // ============= // Converts a single-precision OP to BFloat16 value using the // Round to Nearest Even rounding mode when executed from AArch64 state and // FPCR.AH == '1', otherwise rounding is controlled by FPCR/FPSCR.
func FPConvertBF(op : bits(32), fpcr_in : FPCR_Type, rounding_in : FPRounding) => bits(16) begin let halfsize : integer{} = 16; var fpcr : FPCR_Type = fpcr_in; var rounding : FPRounding = rounding_in; var result : bits(32); // BF16 value in top 16 bits 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 if altfp then rounding = FPRounding_TIEEVEN; end; // Use RNE rounding mode
// Unpack floating-point operand, with always flush-to-zero if fpcr.AH == '1'. let (fptype,sign,value) = FPUnpack{32}(op, fpcr, fpexc);if fptype == FPType_SNaN || fptype == FPType_QNaN then if fpcr.DN == '1' then result = FPDefaultNaN{32}(fpcr); else result = FPConvertNaN{32, 32}(op); end; if fptype == FPType_SNaN then if fpexc then FPProcessException(FPExc_InvalidOp, fpcr); end; end; elsif fptype == FPType_Infinity then result = FPInfinity{32}(sign); elsif fptype == FPType_Zero then result = FPZero{32}(sign); else result = FPRoundBF{32}(value, fpcr, rounding, fpexc); end;
// Returns correctly rounded BF16 value from top 16 bits return result[(2*halfsize)-1:halfsize];
end;
// FPConvertBF // ============= // Converts a single-precision operand to BFloat16 value.
func FPConvertBF(op : bits(32), fpcr : FPCR_Type) => bits(16) begin return FPConvertBF(op, fpcr, FPRoundingMode(fpcr)); end;
## Library pseudocode for shared/functions/float/bfloat/FPRoundBF
// FPRoundBF // =========== // Converts a real number OP into a BFloat16 value using the supplied // rounding mode RMODE. The 'fpexc' argument controls the generation of // floating-point exceptions.
func FPRoundBF{N}(op : real, fpcr : FPCR_Type, rounding : FPRounding, fpexc : boolean) => bits(N) begin assert N == 32; let isbfloat16 : boolean = TRUE; return FPRoundBase{N}(op, fpcr, rounding, isbfloat16, fpexc); end;
## Library pseudocode for shared/functions/float/fixedtofp/FixedToFP
// FixedToFP // ===========
// Convert M-bit fixed point 'op' with FBITS fractional bits to // N-bit precision floating point, controlled by UNSIGNED and ROUNDING.
func FixedToFP{N, M}(op : bits(M), fbits : integer, unsigned : boolean, fpcr : FPCR_Type, rounding : FPRounding) => bits(N) begin assert N IN {16,32,64}; assert M IN {8,16,32,64}; var result : bits(N); assert fbits >= 0; assert rounding != FPRounding_ODD;
// Correct signed-ness let int_operand : integer{} = if unsigned then UInt(op) else SInt(op);// Scale by fractional bits and generate a real value let real_operand : real = Real(int_operand) / 2.0^fbits;
if real_operand == 0.0 then result = FPZero{N}('0'); else result = FPRound{N}(real_operand, fpcr, rounding); end;
return result;
end;
## Library pseudocode for shared/functions/float/fp8float/BFConvertFP8
// BFConvertFP8 // ============== // Converts a BFloat16 OP to FP8 value.
func BFConvertFP8(op_in : bits(16), fpcr : FPCR_Type, fpmr : FPMR_Type) => bits(8) begin let op : bits(32) = op_in :: Zeros{16}; return FPConvertFP8{8, 32}(op, fpcr, fpmr); end;
## Library pseudocode for shared/functions/float/fp8float/FP8Bits
// FP8Bits // ========= // Returns the minimum exponent, numbers of exponent and fraction bits.
func FP8Bits(fp8type : FP8Type) => FPBitsType begin var minimum_exp : integer; var F : integer{2..3}; if fp8type == FP8Type_OFP8_E4M3 then minimum_exp = -6; F = 3; else // fp8type == FP8Type_OFP8_E5M2 minimum_exp = -14; F = 2; end;
return (F, minimum_exp);
end;
## Library pseudocode for shared/functions/float/fp8float/FP8ConvertBF
// FP8ConvertBF // ============== // Converts an FP8 operand to BFloat16 value.
func FP8ConvertBF(op : bits(8), issrc2 : boolean, fpcr : FPCR_Type, fpmr : FPMR_Type) => bits(16) begin let isbfloat16 : boolean = TRUE; let result : bits(32) = FP8ConvertFP{}(op, issrc2, fpcr, fpmr, isbfloat16); return result[16+:16]; end;
## Library pseudocode for shared/functions/float/fp8float/FP8ConvertFP
// FP8ConvertFP // ============== // Converts an FP8 operand to half-precision value.
func FP8ConvertFP(op : bits(8), issrc2 : boolean, fpcr : FPCR_Type, fpmr : FPMR_Type) => bits(16) begin let isbfloat16 : boolean = FALSE; return FP8ConvertFP{16}(op, issrc2, fpcr, fpmr, isbfloat16); end;
// FP8ConvertFP // ============== // Converts an FP8 operand to half-precision or BFloat16 value. // The downscaling factor in FPMR.LSCALE or FPMR.LSCALE2 is applied to // the value before rounding.
func FP8ConvertFP{M}(op : bits(8), issrc2 : boolean, fpcr_in : FPCR_Type, fpmr : FPMR_Type, isbfloat16 : boolean) => bits(M) begin assert M IN {16,32}; var result : bits(M);
let fpexc : boolean = TRUE; var fpcr : FPCR_Type = fpcr_in; // Do not flush denormal inputs and outputs to zero. // Do not support alternative half-precision format. fpcr.[FIZ,FZ,FZ16,AHP] = '0000'; let rounding = FPRounding_TIEEVEN; let fp8type : FP8Type = (if issrc2 then FP8DecodeType(fpmr.F8S2) else FP8DecodeType(fpmr.F8S1));let (fptype,sign,value) = FP8Unpack{8}(op, fp8type);
if fptype == FPType_SNaN || fptype == FPType_QNaN then result = FPDefaultNaN{M}(fpcr); if fptype == FPType_SNaN then FPProcessException(FPExc_InvalidOp, fpcr); end; elsif fptype == FPType_Infinity then result = FPInfinity{M}(sign); elsif fptype == FPType_Zero then result = FPZero{M}(sign); else var dscale : integer; if issrc2 then dscale = (if M == 16 then UInt(fpmr.LSCALE2[3:0]) else UInt(fpmr.LSCALE2[:6])); else dscale = (if M == 16 then UInt(fpmr.LSCALE[3:0]) else UInt(fpmr.LSCALE[:6])); end; let result_value : real = value * (2.0^-dscale); result = FPRoundBase{M}(result_value, fpcr, rounding, isbfloat16, fpexc); end;
return result;
end;
## Library pseudocode for shared/functions/float/fp8float/FP8DecodeType
// FP8DecodeType // =============== // Decode the FP8 format encoded in F8S1, F8S2 or F8D field in FPMR
func FP8DecodeType(f8format : bits(3)) => FP8Type begin case f8format of when '000' => return FP8Type_OFP8_E5M2; when '001' => return FP8Type_OFP8_E4M3; otherwise => return FP8Type_UNSUPPORTED; end; end;
## Library pseudocode for shared/functions/float/fp8float/FP8DefaultNaN
// FP8DefaultNaN // ===============
func FP8DefaultNaN{N}(fp8type : FP8Type, fpcr : FPCR_Type) => bits(N) begin assert N == 8; assert fp8type IN {FP8Type_OFP8_E5M2, FP8Type_OFP8_E4M3}; let sign : bit = if IsFeatureImplemented(FEAT_AFP) then fpcr.AH else '0'; 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}; when FP8Type_OFP8_E5M2 => exp = Ones{E}; frac = '1'::Zeros{F-1}; end;
return sign :: exp :: frac;
end;
## Library pseudocode for shared/functions/float/fp8float/FP8DotAddFP
// FP8DotAddFP // =============
func FP8DotAddFP{M, N}(addend : bits(M), op1 : bits(N), op2 : bits(N), fpcr : FPCR_Type, fpmr : FPMR_Type) => bits(M) begin let E : integer{} = (N DIV 8) as integer{1, 2, 4, 8}; return FP8DotAddFP{M, N}(addend, op1, op2, E, fpcr, fpmr); end;
// FP8DotAddFP // ============== // Calculates result of "E"-way 8-bit floating-point dot-product with scaling // and addition to half-precision or single-precision value without // intermediate rounding. // c = round(c + 2^-S*(a1b1+..+aEbE)) // The 8-bit floating-point format for op1 is determined by FPMR.F8S1 // and the one for op2 by FPMR.F8S2. The scaling factor in FPMR.LSCALE // is applied to the sum-of-products before adding to the addend and rounding.
func FP8DotAddFP{M, N}(addend : bits(M), op1 : bits(N), op2 : bits(N), E : integer{1, 2, 4, 8}, fpcr_in : FPCR_Type, fpmr : FPMR_Type) => bits(M) begin assert M IN {16,32}; assert N IN {2*M, M, M DIV 2, M DIV 4}; var fpcr : FPCR_Type = fpcr_in; var result : bits(M);
fpcr.[FIZ,FZ,FZ16] = '000'; // Do not flush denormal inputs and outputs to zero fpcr.DN = '1'; let rounding = FPRounding_TIEEVEN;let fp8type1 : FP8Type = FP8DecodeType(fpmr.F8S1); let fp8type2 : FP8Type = FP8DecodeType(fpmr.F8S2);
var type1 : array[[E]] of FPType; var type2 : array[[E]] of FPType; var sign1 : array[[E]] of bit; var sign2 : array[[E]] of bit; var value1 : array[[E]] of real; var value2 : array[[E]] of real; var inf1 : array[[E]] of boolean; var inf2 : array[[E]] of boolean; var zero1 : array[[E]] of boolean; var zero2 : array[[E]] of boolean;
let fpexc : boolean = FALSE; let (typeA,signA,valueA) = FPUnpack{M}(addend, fpcr, fpexc); let infA = (typeA == FPType_Infinity); let zeroA = (typeA == FPType_Zero); var any_nan : boolean = typeA IN {FPType_SNaN, FPType_QNaN}; for i = 0 to E-1 do (type1
-
, sign1
-
, value1
-
) = FP8Unpack{N DIV E}(op1[i*:(N DIV E)], fp8type1); (type2
-
, sign2
-
, value2
-
) = FP8Unpack{N DIV E}(op2[i*:(N DIV E)], fp8type2); inf1
-
= (type1
-
== FPType_Infinity); zero1
-
= (type1
-
== FPType_Zero); inf2
-
= (type2
-
== FPType_Infinity); zero2
-
= (type2
-
== FPType_Zero); any_nan = (any_nan || type1
-
IN {FPType_SNaN, FPType_QNaN} || type2
-
IN {FPType_SNaN, FPType_QNaN}); end;
if any_nan then result = FPDefaultNaN{M}(fpcr); else // Determine sign and type products will have if it does not cause an Invalid // Operation. var signP : array [[E]] of bit; var infP : array [[E]] of boolean; var zeroP : array [[E]] of boolean; for i = 0 to E-1 do signP
-
= sign1
-
XOR sign2
-
; infP
-
= inf1
-
|| inf2
-
; zeroP
-
= zero1
-
|| zero2
-
; end;
// Detect non-numeric results of dot product and accumulate var posInfR : boolean = (infA && signA == '0'); var negInfR : boolean = (infA && signA == '1'); var zeroR : boolean = zeroA; var invalidop : boolean = FALSE; for i = 0 to E-1 do // Result is infinity if any input is infinity posInfR = posInfR || (infP -
&& signP
-
== '0'); negInfR = negInfR || (infP
-
&& signP
-
== '1'); // Result is zero if the addend and the products are zeros of the same sign zeroR = zeroR && zeroP
-
&& (signA == signP
-
); // Non SNaN-generated Invalid Operation cases are multiplies of zero // by infinity and additions of opposite-signed infinities. invalidop = (invalidop || (inf1
-
&& zero2
-
) || (zero1
-
&& inf2
-
) || (infA && infP
-
&& (signA != signP
-
))); for j = i+1 to E-1 do invalidop = invalidop || (infP
-
&& infP[[j]] && (signP
-
!= signP[[j]])); end; end;
if invalidop then result = FPDefaultNaN{M}(fpcr);// Other cases involving infinities produce an infinity of the same sign. elsif posInfR then result = FPInfinity{M}('0'); elsif negInfR then result = FPInfinity{M}('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 zeroR then result = FPZero{M}(signA);
// Otherwise calculate numerical value and round it. else // Apply scaling to sum-of-product let dscale : integer = if M == 32 then UInt(fpmr.LSCALE) else UInt(fpmr.LSCALE[3:0]);
var dp_value : real = value1[[0]] * value2[[0]]; for i = 1 to E-1 do dp_value = dp_value + value1 -
- value2
-
; end;
let result_value : real = valueA + dp_value * (2.0^-dscale); if result_value == 0.0 then // Sign of exact zero result is '0' for RNE rounding mode result = FPZero{M}('0'); else let satoflo : boolean = (fpmr.OSM == '1'); result = FPRound_FP8{M}(result_value, fpcr, rounding, satoflo); end; end;end;
return result; end;
## Library pseudocode for shared/functions/float/fp8float/FP8Infinity
// FP8Infinity // =============
func FP8Infinity{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}; when FP8Type_OFP8_E5M2 => exp = Ones{E}; frac = Zeros{F}; end;
return sign :: exp :: frac;
end;
## Library pseudocode for shared/functions/float/fp8float/FP8MatMulAddFP
// FP8MatMulAddFP // ================ // 8-bit floating-point matrix multiply with scaling and add to half-precision // or single-precision matrix. // result[2, 2] = addend[2, 2] + (op1[2, E] * op2[E, 2])