void softfloat_mulAddF128M( const uint32_t *aWPtr, const uint32_t *bWPtr, const uint32_t *cWPtr, uint32_t *zWPtr, uint_fast8_t op ) { uint32_t uiA96; int32_t expA; uint32_t uiB96; int32_t expB; uint32_t uiC96; bool signC; int32_t expC; bool signProd, prodIsInfinite; uint32_t *ptr, uiZ96, sigA[4]; uint_fast8_t shiftCount; uint32_t sigX[5]; int32_t expProd; uint32_t sigProd[8], wordSig; bool doSub; uint_fast8_t (*addCarryMRoutinePtr)( uint_fast8_t, const uint32_t *, const uint32_t *, uint_fast8_t, uint32_t * ); int32_t expDiff; bool signZ; int32_t expZ; uint32_t *extSigPtr; uint_fast8_t carry; void (*roundPackRoutinePtr)( bool, int32_t, uint32_t *, uint32_t * ); /*------------------------------------------------------------------------ *------------------------------------------------------------------------*/ uiA96 = aWPtr[indexWordHi( 4 )]; expA = expF128UI96( uiA96 ); uiB96 = bWPtr[indexWordHi( 4 )]; expB = expF128UI96( uiB96 ); uiC96 = cWPtr[indexWordHi( 4 )]; signC = signF128UI96( uiC96 ) ^ (op == softfloat_mulAdd_subC); expC = expF128UI96( uiC96 ); signProd = signF128UI96( uiA96 ) ^ signF128UI96( uiB96 ) ^ (op == softfloat_mulAdd_subProd); /*------------------------------------------------------------------------ *------------------------------------------------------------------------*/ prodIsInfinite = false; if ( (expA == 0x7FFF) || (expB == 0x7FFF) ) { if ( softfloat_tryPropagateNaNF128M( aWPtr, bWPtr, zWPtr ) ) { goto propagateNaN_ZC; } ptr = (uint32_t *) aWPtr; if ( ! (uint32_t) (uiA96<<1) ) goto possibleInvalidProd; if ( ! (uint32_t) (uiB96<<1) ) { ptr = (uint32_t *) bWPtr; possibleInvalidProd: if ( ! (ptr[indexWord( 4, 2 )] | ptr[indexWord( 4, 1 )] | ptr[indexWord( 4, 0 )]) ) { goto invalid; } } prodIsInfinite = true; } if ( expC == 0x7FFF ) { if ( fracF128UI96( uiC96 ) || (cWPtr[indexWord( 4, 2 )] | cWPtr[indexWord( 4, 1 )] | cWPtr[indexWord( 4, 0 )]) ) { zWPtr[indexWordHi( 4 )] = 0; goto propagateNaN_ZC; } if ( prodIsInfinite && (signProd != signC) ) goto invalid; goto copyC; } if ( prodIsInfinite ) { uiZ96 = packToF128UI96( signProd, 0x7FFF, 0 ); goto uiZ; } /*------------------------------------------------------------------------ *------------------------------------------------------------------------*/ if ( expA ) { sigA[indexWordHi( 4 )] = fracF128UI96( uiA96 ) | 0x00010000; sigA[indexWord( 4, 2 )] = aWPtr[indexWord( 4, 2 )]; sigA[indexWord( 4, 1 )] = aWPtr[indexWord( 4, 1 )]; sigA[indexWord( 4, 0 )] = aWPtr[indexWord( 4, 0 )]; } else { expA = softfloat_shiftNormSigF128M( aWPtr, 0, sigA ); if ( expA == -128 ) goto zeroProd; } if ( expB ) { sigX[indexWordHi( 4 )] = fracF128UI96( uiB96 ) | 0x00010000; sigX[indexWord( 4, 2 )] = bWPtr[indexWord( 4, 2 )]; sigX[indexWord( 4, 1 )] = bWPtr[indexWord( 4, 1 )]; sigX[indexWord( 4, 0 )] = bWPtr[indexWord( 4, 0 )]; } else { expB = softfloat_shiftNormSigF128M( bWPtr, 0, sigX ); if ( expB == -128 ) goto zeroProd; } /*------------------------------------------------------------------------ *------------------------------------------------------------------------*/ expProd = expA + expB - 0x3FF0; softfloat_mul128MTo256M( sigA, sigX, sigProd ); /*------------------------------------------------------------------------ *------------------------------------------------------------------------*/ wordSig = fracF128UI96( uiC96 ); if ( expC ) { --expC; wordSig |= 0x00010000; } sigX[indexWordHi( 5 )] = wordSig; sigX[indexWord( 5, 3 )] = cWPtr[indexWord( 4, 2 )]; sigX[indexWord( 5, 2 )] = cWPtr[indexWord( 4, 1 )]; sigX[indexWord( 5, 1 )] = cWPtr[indexWord( 4, 0 )]; /*------------------------------------------------------------------------ *------------------------------------------------------------------------*/ doSub = (signProd != signC); addCarryMRoutinePtr = doSub ? softfloat_addComplCarryM : softfloat_addCarryM; expDiff = expProd - expC; if ( expDiff <= 0 ) { /*-------------------------------------------------------------------- *--------------------------------------------------------------------*/ signZ = signC; expZ = expC; if ( sigProd[indexWord( 8, 2 )] || (sigProd[indexWord( 8, 1 )] | sigProd[indexWord( 8, 0 )]) ) { sigProd[indexWord( 8, 3 )] |= 1; } extSigPtr = &sigProd[indexMultiwordHi( 8, 5 )]; if ( expDiff ) { softfloat_shiftRightJam160M( extSigPtr, -expDiff, extSigPtr ); } carry = 0; if ( doSub ) { wordSig = extSigPtr[indexWordLo( 5 )]; extSigPtr[indexWordLo( 5 )] = -wordSig; carry = ! wordSig; } (*addCarryMRoutinePtr)( 4, &sigX[indexMultiwordHi( 5, 4 )], extSigPtr + indexMultiwordHi( 5, 4 ), carry, extSigPtr + indexMultiwordHi( 5, 4 ) ); wordSig = extSigPtr[indexWordHi( 5 )]; if ( ! expZ ) { if ( wordSig & 0x80000000 ) { signZ = ! signZ; softfloat_negX160M( extSigPtr ); wordSig = extSigPtr[indexWordHi( 5 )]; } goto checkCancellation; } if ( wordSig < 0x00010000 ) { --expZ; softfloat_add160M( extSigPtr, extSigPtr, extSigPtr ); goto roundPack; } goto extSigReady_noCancellation; } else { /*-------------------------------------------------------------------- *--------------------------------------------------------------------*/ signZ = signProd; expZ = expProd; sigX[indexWordLo( 5 )] = 0; expDiff -= 128; if ( 0 <= expDiff ) { /*---------------------------------------------------------------- *----------------------------------------------------------------*/ if ( expDiff ) softfloat_shiftRightJam160M( sigX, expDiff, sigX ); wordSig = sigX[indexWordLo( 5 )]; carry = 0; if ( doSub ) { carry = ! wordSig; wordSig = -wordSig; } carry = (*addCarryMRoutinePtr)( 4, &sigProd[indexMultiwordLo( 8, 4 )], &sigX[indexMultiwordHi( 5, 4 )], carry, &sigProd[indexMultiwordLo( 8, 4 )] ); sigProd[indexWord( 8, 2 )] |= wordSig; ptr = &sigProd[indexWord( 8, 4 )]; } else { /*---------------------------------------------------------------- *----------------------------------------------------------------*/ shiftCount = expDiff & 31; if ( shiftCount ) { softfloat_shortShiftRight160M( sigX, shiftCount, sigX ); } expDiff >>= 5; extSigPtr = &sigProd[indexMultiwordLo( 8, 5 )] - wordIncr + expDiff * -wordIncr; carry = (*addCarryMRoutinePtr)( 5, extSigPtr, sigX, doSub, extSigPtr ); if ( expDiff == -4 ) { /*------------------------------------------------------------ *------------------------------------------------------------*/ wordSig = sigProd[indexWordHi( 8 )]; if ( wordSig & 0x80000000 ) { signZ = ! signZ; softfloat_negX256M( sigProd ); wordSig = sigProd[indexWordHi( 8 )]; } /*------------------------------------------------------------ *------------------------------------------------------------*/ if ( wordSig ) goto expProdBigger_noWordShift; wordSig = sigProd[indexWord( 8, 6 )]; if ( 0x00040000 <= wordSig ) goto expProdBigger_noWordShift; expZ -= 32; extSigPtr = &sigProd[indexMultiwordHi( 8, 5 )] - wordIncr; for (;;) { if ( wordSig ) break; wordSig = extSigPtr[indexWord( 5, 3 )]; if ( 0x00040000 <= wordSig ) break; expZ -= 32; extSigPtr -= wordIncr; if ( extSigPtr == &sigProd[indexMultiwordLo( 8, 5 )] ) { goto checkCancellation; } } /*------------------------------------------------------------ *------------------------------------------------------------*/ ptr = extSigPtr + indexWordLo( 5 ); do { ptr -= wordIncr; if ( *ptr ) { extSigPtr[indexWordLo( 5 )] |= 1; break; } } while ( ptr != &sigProd[indexWordLo( 8 )] ); wordSig = extSigPtr[indexWordHi( 5 )]; goto extSigReady; } ptr = extSigPtr + indexWordHi( 5 ) + wordIncr; } /*-------------------------------------------------------------------- *--------------------------------------------------------------------*/ if ( carry != doSub ) { if ( doSub ) { do { wordSig = *ptr; *ptr = wordSig - 1; ptr += wordIncr; } while ( ! wordSig ); } else { do { wordSig = *ptr + 1; *ptr = wordSig; ptr += wordIncr; } while ( ! wordSig ); } } /*-------------------------------------------------------------------- *--------------------------------------------------------------------*/ expProdBigger_noWordShift: if ( sigProd[indexWord( 8, 2 )] || (sigProd[indexWord( 8, 1 )] | sigProd[indexWord( 8, 0 )]) ) { sigProd[indexWord( 8, 3 )] |= 1; } extSigPtr = &sigProd[indexMultiwordHi( 8, 5 )]; wordSig = extSigPtr[indexWordHi( 5 )]; } extSigReady: roundPackRoutinePtr = softfloat_normRoundPackMToF128M; if ( wordSig < 0x00010000 ) goto doRoundPack; extSigReady_noCancellation: if ( 0x00020000 <= wordSig ) { ++expZ; softfloat_shortShiftRightJam160M( extSigPtr, 1, extSigPtr ); } roundPack: roundPackRoutinePtr = softfloat_roundPackMToF128M; doRoundPack: (*roundPackRoutinePtr)( signZ, expZ, extSigPtr, zWPtr ); return; /*------------------------------------------------------------------------ *------------------------------------------------------------------------*/ invalid: softfloat_invalidF128M( zWPtr ); propagateNaN_ZC: softfloat_propagateNaNF128M( zWPtr, cWPtr, zWPtr ); return; /*------------------------------------------------------------------------ *------------------------------------------------------------------------*/ zeroProd: if ( ! (uint32_t) (uiC96<<1) && (signProd != signC) && ! cWPtr[indexWord( 4, 2 )] && ! (cWPtr[indexWord( 4, 1 )] | cWPtr[indexWord( 4, 0 )]) ) { goto completeCancellation; } copyC: zWPtr[indexWordHi( 4 )] = uiC96; zWPtr[indexWord( 4, 2 )] = cWPtr[indexWord( 4, 2 )]; zWPtr[indexWord( 4, 1 )] = cWPtr[indexWord( 4, 1 )]; zWPtr[indexWord( 4, 0 )] = cWPtr[indexWord( 4, 0 )]; return; /*------------------------------------------------------------------------ *------------------------------------------------------------------------*/ checkCancellation: if ( wordSig || (extSigPtr[indexWord( 5, 3 )] | extSigPtr[indexWord( 5, 2 )]) || (extSigPtr[indexWord( 5, 1 )] | extSigPtr[indexWord( 5, 0 )]) ) { goto extSigReady; } completeCancellation: uiZ96 = packToF128UI96( (softfloat_roundingMode == softfloat_round_min), 0, 0 ); uiZ: zWPtr[indexWordHi( 4 )] = uiZ96; zWPtr[indexWord( 4, 2 )] = 0; zWPtr[indexWord( 4, 1 )] = 0; zWPtr[indexWord( 4, 0 )] = 0; }
void f128M_mul( const float128_t *aPtr, const float128_t *bPtr, float128_t *zPtr ) { const uint32_t *aWPtr, *bWPtr; uint32_t *zWPtr; uint32_t uiA96; int32_t expA; uint32_t uiB96; int32_t expB; bool signZ; const uint32_t *ptr; uint32_t uiZ96, sigA[4]; uint_fast8_t shiftDist; uint32_t sigB[4]; int32_t expZ; uint32_t sigProd[8], *extSigZPtr; /*------------------------------------------------------------------------ *------------------------------------------------------------------------*/ aWPtr = (const uint32_t *) aPtr; bWPtr = (const uint32_t *) bPtr; zWPtr = (uint32_t *) zPtr; /*------------------------------------------------------------------------ *------------------------------------------------------------------------*/ uiA96 = aWPtr[indexWordHi( 4 )]; expA = expF128UI96( uiA96 ); uiB96 = bWPtr[indexWordHi( 4 )]; expB = expF128UI96( uiB96 ); signZ = signF128UI96( uiA96 ) ^ signF128UI96( uiB96 ); /*------------------------------------------------------------------------ *------------------------------------------------------------------------*/ if ( (expA == 0x7FFF) || (expB == 0x7FFF) ) { if ( softfloat_tryPropagateNaNF128M( aWPtr, bWPtr, zWPtr ) ) return; ptr = aWPtr; if ( ! expA ) goto possiblyInvalid; if ( ! expB ) { ptr = bWPtr; possiblyInvalid: if ( ! fracF128UI96( ptr[indexWordHi( 4 )] ) && ! (ptr[indexWord( 4, 2 )] | ptr[indexWord( 4, 1 )] | ptr[indexWord( 4, 0 )]) ) { softfloat_invalidF128M( zWPtr ); return; } } uiZ96 = packToF128UI96( signZ, 0x7FFF, 0 ); goto uiZ96; } /*------------------------------------------------------------------------ *------------------------------------------------------------------------*/ if ( expA ) { sigA[indexWordHi( 4 )] = fracF128UI96( uiA96 ) | 0x00010000; sigA[indexWord( 4, 2 )] = aWPtr[indexWord( 4, 2 )]; sigA[indexWord( 4, 1 )] = aWPtr[indexWord( 4, 1 )]; sigA[indexWord( 4, 0 )] = aWPtr[indexWord( 4, 0 )]; } else { expA = softfloat_shiftNormSigF128M( aWPtr, 0, sigA ); if ( expA == -128 ) goto zero; } if ( expB ) { sigB[indexWordHi( 4 )] = fracF128UI96( uiB96 ) | 0x00010000; sigB[indexWord( 4, 2 )] = bWPtr[indexWord( 4, 2 )]; sigB[indexWord( 4, 1 )] = bWPtr[indexWord( 4, 1 )]; sigB[indexWord( 4, 0 )] = bWPtr[indexWord( 4, 0 )]; } else { expB = softfloat_shiftNormSigF128M( bWPtr, 0, sigB ); if ( expB == -128 ) goto zero; } /*------------------------------------------------------------------------ *------------------------------------------------------------------------*/ expZ = expA + expB - 0x4000; softfloat_mul128MTo256M( sigA, sigB, sigProd ); if ( sigProd[indexWord( 8, 2 )] || (sigProd[indexWord( 8, 1 )] | sigProd[indexWord( 8, 0 )]) ) { sigProd[indexWord( 8, 3 )] |= 1; } extSigZPtr = &sigProd[indexMultiwordHi( 8, 5 )]; shiftDist = 16; if ( extSigZPtr[indexWordHi( 5 )] & 2 ) { ++expZ; shiftDist = 15; } softfloat_shortShiftLeft160M( extSigZPtr, shiftDist, extSigZPtr ); softfloat_roundPackMToF128M( signZ, expZ, extSigZPtr, zWPtr ); return; /*------------------------------------------------------------------------ *------------------------------------------------------------------------*/ zero: uiZ96 = packToF128UI96( signZ, 0, 0 ); uiZ96: zWPtr[indexWordHi( 4 )] = uiZ96; zWPtr[indexWord( 4, 2 )] = 0; zWPtr[indexWord( 4, 1 )] = 0; zWPtr[indexWord( 4, 0 )] = 0; }
void softfloat_addF128M( const uint32_t *aWPtr, const uint32_t *bWPtr, uint32_t *zWPtr, bool negateB ) { uint32_t uiA96; int32_t expA; uint32_t uiB96; int32_t expB; uint32_t uiZ96; bool signZ, signB; const uint32_t *tempPtr; uint32_t sig96A, sig96B; int32_t expDiff; uint_fast8_t (*addCarryMRoutinePtr)( uint_fast8_t, const uint32_t *, const uint32_t *, uint_fast8_t, uint32_t * ); uint32_t extSigZ[5], wordSigZ; uint_fast8_t carry; void (*roundPackRoutinePtr)( bool, int32_t, uint32_t *, uint32_t * ); /*------------------------------------------------------------------------ *------------------------------------------------------------------------*/ uiA96 = aWPtr[indexWordHi( 4 )]; expA = expF128UI96( uiA96 ); uiB96 = bWPtr[indexWordHi( 4 )]; expB = expF128UI96( uiB96 ); /*------------------------------------------------------------------------ *------------------------------------------------------------------------*/ if ( (expA == 0x7FFF) || (expB == 0x7FFF) ) { if ( softfloat_tryPropagateNaNF128M( aWPtr, bWPtr, zWPtr ) ) return; uiZ96 = uiA96; if ( expB == 0x7FFF ) { uiZ96 = uiB96 ^ packToF128UI96( negateB, 0, 0 ); if ( (expA == 0x7FFF) && (uiZ96 != uiA96) ) { softfloat_invalidF128M( zWPtr ); return; } } zWPtr[indexWordHi( 4 )] = uiZ96; zWPtr[indexWord( 4, 2 )] = 0; zWPtr[indexWord( 4, 1 )] = 0; zWPtr[indexWord( 4, 0 )] = 0; return; } /*------------------------------------------------------------------------ *------------------------------------------------------------------------*/ signZ = signF128UI96( uiA96 ); signB = signF128UI96( uiB96 ) ^ negateB; negateB = (signZ != signB); if ( (uint32_t) (uiA96<<1) < (uint32_t) (uiB96<<1) ) { signZ = signB; expA = expB; expB = expF128UI96( uiA96 ); tempPtr = aWPtr; aWPtr = bWPtr; bWPtr = tempPtr; uiA96 = uiB96; uiB96 = bWPtr[indexWordHi( 4 )]; } sig96A = fracF128UI96( uiA96 ); sig96B = fracF128UI96( uiB96 ); if ( expA ) { --expA; sig96A |= 0x00010000; if ( expB ) { --expB; sig96B |= 0x00010000; } } /*------------------------------------------------------------------------ *------------------------------------------------------------------------*/ addCarryMRoutinePtr = negateB ? softfloat_addComplCarryM : softfloat_addCarryM; expDiff = expA - expB; if ( expDiff ) { /*-------------------------------------------------------------------- *--------------------------------------------------------------------*/ extSigZ[indexWordHi( 5 )] = sig96B; extSigZ[indexWord( 5, 3 )] = bWPtr[indexWord( 4, 2 )]; extSigZ[indexWord( 5, 2 )] = bWPtr[indexWord( 4, 1 )]; extSigZ[indexWord( 5, 1 )] = bWPtr[indexWord( 4, 0 )]; extSigZ[indexWord( 5, 0 )] = 0; softfloat_shiftRightJam160M( extSigZ, expDiff, extSigZ ); sig96B = extSigZ[indexWordHi( 5 )]; carry = 0; if ( negateB ) { sig96B = ~sig96B; wordSigZ = extSigZ[indexWordLo( 5 )]; extSigZ[indexWordLo( 5 )] = -wordSigZ; carry = ! wordSigZ; } carry = (*addCarryMRoutinePtr)( 3, &aWPtr[indexMultiwordLo( 4, 3 )], &extSigZ[indexMultiword( 5, 3, 1 )], carry, &extSigZ[indexMultiword( 5, 3, 1 )] ); wordSigZ = sig96A + sig96B + carry; } else { /*-------------------------------------------------------------------- *--------------------------------------------------------------------*/ extSigZ[indexWordLo( 5 )] = 0; carry = (*addCarryMRoutinePtr)( 3, &aWPtr[indexMultiwordLo( 4, 3 )], &bWPtr[indexMultiwordLo( 4, 3 )], negateB, &extSigZ[indexMultiword( 5, 3, 1 )] ); if ( negateB ) { wordSigZ = sig96A + ~sig96B + carry; if ( wordSigZ & 0x80000000 ) { signZ = ! signZ; carry = softfloat_addComplCarry96M( &bWPtr[indexMultiwordLo( 4, 3 )], &aWPtr[indexMultiwordLo( 4, 3 )], 1, &extSigZ[indexMultiword( 5, 3, 1 )] ); wordSigZ = sig96B + ~sig96A + carry; } else { if ( ! wordSigZ && ! extSigZ[indexWord( 5, 3 )] && ! ( extSigZ[indexWord( 5, 2 )] | extSigZ[indexWord( 5, 1 )] | extSigZ[indexWord( 5, 0 )] ) ) { signZ = (softfloat_roundingMode == softfloat_round_min); zWPtr[indexWordHi( 4 )] = packToF128UI96( signZ, 0, 0 ); zWPtr[indexWord( 4, 2 )] = 0; zWPtr[indexWord( 4, 1 )] = 0; zWPtr[indexWord( 4, 0 )] = 0; return; } } } else { wordSigZ = sig96A + sig96B + carry; } } extSigZ[indexWordHi( 5 )] = wordSigZ; /*------------------------------------------------------------------------ *------------------------------------------------------------------------*/ roundPackRoutinePtr = softfloat_normRoundPackMToF128M; if ( 0x00010000 <= wordSigZ ) { if ( 0x00020000 <= wordSigZ ) { ++expA; softfloat_shortShiftRightJam160M( extSigZ, 1, extSigZ ); } roundPackRoutinePtr = softfloat_roundPackMToF128M; } (*roundPackRoutinePtr)( signZ, expA, extSigZ, zWPtr ); }