1 
2 /*============================================================================
3 
4 This C source file is part of the SoftFloat IEEE Floating-Point Arithmetic
5 Package, Release 3e, by John R. Hauser.
6 
7 Copyright 2011, 2012, 2013, 2014 The Regents of the University of California.
8 All rights reserved.
9 
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
12 
13  1. Redistributions of source code must retain the above copyright notice,
14     this list of conditions, and the following disclaimer.
15 
16  2. Redistributions in binary form must reproduce the above copyright notice,
17     this list of conditions, and the following disclaimer in the documentation
18     and/or other materials provided with the distribution.
19 
20  3. Neither the name of the University nor the names of its contributors may
21     be used to endorse or promote products derived from this software without
22     specific prior written permission.
23 
24 THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
25 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
27 DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
28 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 
35 =============================================================================*/
36 
37 #include <stdbool.h>
38 #include <stdint.h>
39 #include "platform.h"
40 #include "internals.h"
41 #include "specialize.h"
42 #include "softfloat.h"
43 
44 void
softfloat_addF128M(const uint32_t * aWPtr,const uint32_t * bWPtr,uint32_t * zWPtr,bool negateB)45  softfloat_addF128M(
46      const uint32_t *aWPtr,
47      const uint32_t *bWPtr,
48      uint32_t *zWPtr,
49      bool negateB
50  )
51 {
52     uint32_t uiA96;
53     int32_t expA;
54     uint32_t uiB96;
55     int32_t expB;
56     uint32_t uiZ96;
57     bool signZ, signB;
58     const uint32_t *tempPtr;
59     uint32_t sig96A, sig96B;
60     int32_t expDiff;
61     uint_fast8_t
62      (*addCarryMRoutinePtr)(
63          uint_fast8_t,
64          const uint32_t *,
65          const uint32_t *,
66          uint_fast8_t,
67          uint32_t *
68      );
69     uint32_t extSigZ[5], wordSigZ;
70     uint_fast8_t carry;
71     void (*roundPackRoutinePtr)( bool, int32_t, uint32_t *, uint32_t * );
72 
73     /*------------------------------------------------------------------------
74     *------------------------------------------------------------------------*/
75     uiA96 = aWPtr[indexWordHi( 4 )];
76     expA = expF128UI96( uiA96 );
77     uiB96 = bWPtr[indexWordHi( 4 )];
78     expB = expF128UI96( uiB96 );
79     /*------------------------------------------------------------------------
80     *------------------------------------------------------------------------*/
81     if ( (expA == 0x7FFF) || (expB == 0x7FFF) ) {
82         if ( softfloat_tryPropagateNaNF128M( aWPtr, bWPtr, zWPtr ) ) return;
83         uiZ96 = uiA96;
84         if ( expB == 0x7FFF ) {
85             uiZ96 = uiB96 ^ packToF128UI96( negateB, 0, 0 );
86             if ( (expA == 0x7FFF) && (uiZ96 != uiA96) ) {
87                 softfloat_invalidF128M( zWPtr );
88                 return;
89             }
90         }
91         zWPtr[indexWordHi( 4 )] = uiZ96;
92         zWPtr[indexWord( 4, 2 )] = 0;
93         zWPtr[indexWord( 4, 1 )] = 0;
94         zWPtr[indexWord( 4, 0 )] = 0;
95         return;
96     }
97     /*------------------------------------------------------------------------
98     *------------------------------------------------------------------------*/
99     signZ = signF128UI96( uiA96 );
100     signB = signF128UI96( uiB96 ) ^ negateB;
101     negateB = (signZ != signB);
102     if ( (uint32_t) (uiA96<<1) < (uint32_t) (uiB96<<1) ) {
103         signZ = signB;
104         expA = expB;
105         expB = expF128UI96( uiA96 );
106         tempPtr = aWPtr;
107         aWPtr = bWPtr;
108         bWPtr = tempPtr;
109         uiA96 = uiB96;
110         uiB96 = bWPtr[indexWordHi( 4 )];
111     }
112     sig96A = fracF128UI96( uiA96 );
113     sig96B = fracF128UI96( uiB96 );
114     if ( expA ) {
115         --expA;
116         sig96A |= 0x00010000;
117         if ( expB ) {
118             --expB;
119             sig96B |= 0x00010000;
120         }
121     }
122     /*------------------------------------------------------------------------
123     *------------------------------------------------------------------------*/
124     addCarryMRoutinePtr =
125         negateB ? softfloat_addComplCarryM : softfloat_addCarryM;
126     expDiff = expA - expB;
127     if ( expDiff ) {
128         /*--------------------------------------------------------------------
129         *--------------------------------------------------------------------*/
130         extSigZ[indexWordHi( 5 )] = sig96B;
131         extSigZ[indexWord( 5, 3 )] = bWPtr[indexWord( 4, 2 )];
132         extSigZ[indexWord( 5, 2 )] = bWPtr[indexWord( 4, 1 )];
133         extSigZ[indexWord( 5, 1 )] = bWPtr[indexWord( 4, 0 )];
134         extSigZ[indexWord( 5, 0 )] = 0;
135         softfloat_shiftRightJam160M( extSigZ, expDiff, extSigZ );
136         sig96B = extSigZ[indexWordHi( 5 )];
137         carry = 0;
138         if ( negateB ) {
139             sig96B = ~sig96B;
140             wordSigZ = extSigZ[indexWordLo( 5 )];
141             extSigZ[indexWordLo( 5 )] = -wordSigZ;
142             carry = ! wordSigZ;
143         }
144         carry =
145             (*addCarryMRoutinePtr)(
146                 3,
147                 &aWPtr[indexMultiwordLo( 4, 3 )],
148                 &extSigZ[indexMultiword( 5, 3, 1 )],
149                 carry,
150                 &extSigZ[indexMultiword( 5, 3, 1 )]
151             );
152         wordSigZ = sig96A + sig96B + carry;
153     } else {
154         /*--------------------------------------------------------------------
155         *--------------------------------------------------------------------*/
156         extSigZ[indexWordLo( 5 )] = 0;
157         carry =
158             (*addCarryMRoutinePtr)(
159                 3,
160                 &aWPtr[indexMultiwordLo( 4, 3 )],
161                 &bWPtr[indexMultiwordLo( 4, 3 )],
162                 negateB,
163                 &extSigZ[indexMultiword( 5, 3, 1 )]
164             );
165         if ( negateB ) {
166             wordSigZ = sig96A + ~sig96B + carry;
167             if ( wordSigZ & 0x80000000 ) {
168                 signZ = ! signZ;
169                 carry =
170                     softfloat_addComplCarry96M(
171                         &bWPtr[indexMultiwordLo( 4, 3 )],
172                         &aWPtr[indexMultiwordLo( 4, 3 )],
173                         1,
174                         &extSigZ[indexMultiword( 5, 3, 1 )]
175                     );
176                 wordSigZ = sig96B + ~sig96A + carry;
177             } else {
178                 if (
179                     ! wordSigZ && ! extSigZ[indexWord( 5, 3 )]
180                         && ! (  extSigZ[indexWord( 5, 2 )]
181                               | extSigZ[indexWord( 5, 1 )]
182                               | extSigZ[indexWord( 5, 0 )]
183                              )
184                 ) {
185                     signZ = (softfloat_roundingMode == softfloat_round_min);
186                     zWPtr[indexWordHi( 4 )] = packToF128UI96( signZ, 0, 0 );
187                     zWPtr[indexWord( 4, 2 )] = 0;
188                     zWPtr[indexWord( 4, 1 )] = 0;
189                     zWPtr[indexWord( 4, 0 )] = 0;
190                     return;
191                 }
192             }
193         } else {
194             wordSigZ = sig96A + sig96B + carry;
195         }
196     }
197     extSigZ[indexWordHi( 5 )] = wordSigZ;
198     /*------------------------------------------------------------------------
199     *------------------------------------------------------------------------*/
200     roundPackRoutinePtr = softfloat_normRoundPackMToF128M;
201     if ( 0x00010000 <= wordSigZ ) {
202         if ( 0x00020000 <= wordSigZ ) {
203             ++expA;
204             softfloat_shortShiftRightJam160M( extSigZ, 1, extSigZ );
205         }
206         roundPackRoutinePtr = softfloat_roundPackMToF128M;
207     }
208     (*roundPackRoutinePtr)( signZ, expA, extSigZ, zWPtr );
209 
210 }
211 
212