mirror of
https://github.com/urbit/shrub.git
synced 2024-12-25 04:52:06 +03:00
209 lines
8.0 KiB
C
209 lines
8.0 KiB
C
|
|
/*============================================================================
|
|
|
|
This C source file is part of the SoftFloat IEEE Floating-Point Arithmetic
|
|
Package, Release 3, by John R. Hauser.
|
|
|
|
Copyright 2011, 2012, 2013, 2014 The Regents of the University of California
|
|
(Regents). All Rights Reserved. Redistribution and use in source and binary
|
|
forms, with or without modification, are permitted provided that the following
|
|
conditions are met:
|
|
|
|
Redistributions of source code must retain the above copyright notice,
|
|
this list of conditions, and the following two paragraphs of disclaimer.
|
|
Redistributions in binary form must reproduce the above copyright notice,
|
|
this list of conditions, and the following two paragraphs of disclaimer in the
|
|
documentation and/or other materials provided with the distribution. Neither
|
|
the name of the Regents nor the names of its contributors may be used to
|
|
endorse or promote products derived from this software without specific prior
|
|
written permission.
|
|
|
|
IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
|
|
SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING
|
|
OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF REGENTS HAS
|
|
BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
|
|
HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
|
|
MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
|
|
|
|
=============================================================================*/
|
|
|
|
#include <stdbool.h>
|
|
#include <stdint.h>
|
|
#include "platform.h"
|
|
#include "internals.h"
|
|
#include "specialize.h"
|
|
#include "softfloat.h"
|
|
|
|
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 );
|
|
|
|
}
|
|
|