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 
43 float128_t
softfloat_addMagsF128(uint_fast64_t uiA64,uint_fast64_t uiA0,uint_fast64_t uiB64,uint_fast64_t uiB0,bool signZ)44  softfloat_addMagsF128(
45      uint_fast64_t uiA64,
46      uint_fast64_t uiA0,
47      uint_fast64_t uiB64,
48      uint_fast64_t uiB0,
49      bool signZ
50  )
51 {
52     int_fast32_t expA;
53     struct uint128 sigA;
54     int_fast32_t expB;
55     struct uint128 sigB;
56     int_fast32_t expDiff;
57     struct uint128 uiZ, sigZ;
58     int_fast32_t expZ;
59     uint_fast64_t sigZExtra;
60     struct uint128_extra sig128Extra;
61     union ui128_f128 uZ;
62 
63     expA = expF128UI64( uiA64 );
64     sigA.v64 = fracF128UI64( uiA64 );
65     sigA.v0  = uiA0;
66     expB = expF128UI64( uiB64 );
67     sigB.v64 = fracF128UI64( uiB64 );
68     sigB.v0  = uiB0;
69     expDiff = expA - expB;
70     if ( ! expDiff ) {
71         if ( expA == 0x7FFF ) {
72             if ( sigA.v64 | sigA.v0 | sigB.v64 | sigB.v0 ) goto propagateNaN;
73             uiZ.v64 = uiA64;
74             uiZ.v0  = uiA0;
75             goto uiZ;
76         }
77         sigZ = softfloat_add128( sigA.v64, sigA.v0, sigB.v64, sigB.v0 );
78         if ( ! expA ) {
79             uiZ.v64 = packToF128UI64( signZ, 0, sigZ.v64 );
80             uiZ.v0  = sigZ.v0;
81             goto uiZ;
82         }
83         expZ = expA;
84         sigZ.v64 |= UINT64_C( 0x0002000000000000 );
85         sigZExtra = 0;
86         goto shiftRight1;
87     }
88     if ( expDiff < 0 ) {
89         if ( expB == 0x7FFF ) {
90             if ( sigB.v64 | sigB.v0 ) goto propagateNaN;
91             uiZ.v64 = packToF128UI64( signZ, 0x7FFF, 0 );
92             uiZ.v0  = 0;
93             goto uiZ;
94         }
95         expZ = expB;
96         if ( expA ) {
97             sigA.v64 |= UINT64_C( 0x0001000000000000 );
98         } else {
99             ++expDiff;
100             sigZExtra = 0;
101             if ( ! expDiff ) goto newlyAligned;
102         }
103         sig128Extra =
104             softfloat_shiftRightJam128Extra( sigA.v64, sigA.v0, 0, -expDiff );
105         sigA = sig128Extra.v;
106         sigZExtra = sig128Extra.extra;
107     } else {
108         if ( expA == 0x7FFF ) {
109             if ( sigA.v64 | sigA.v0 ) goto propagateNaN;
110             uiZ.v64 = uiA64;
111             uiZ.v0  = uiA0;
112             goto uiZ;
113         }
114         expZ = expA;
115         if ( expB ) {
116             sigB.v64 |= UINT64_C( 0x0001000000000000 );
117         } else {
118             --expDiff;
119             sigZExtra = 0;
120             if ( ! expDiff ) goto newlyAligned;
121         }
122         sig128Extra =
123             softfloat_shiftRightJam128Extra( sigB.v64, sigB.v0, 0, expDiff );
124         sigB = sig128Extra.v;
125         sigZExtra = sig128Extra.extra;
126     }
127  newlyAligned:
128     sigZ =
129         softfloat_add128(
130             sigA.v64 | UINT64_C( 0x0001000000000000 ),
131             sigA.v0,
132             sigB.v64,
133             sigB.v0
134         );
135     --expZ;
136     if ( sigZ.v64 < UINT64_C( 0x0002000000000000 ) ) goto roundAndPack;
137     ++expZ;
138  shiftRight1:
139     sig128Extra =
140         softfloat_shortShiftRightJam128Extra(
141             sigZ.v64, sigZ.v0, sigZExtra, 1 );
142     sigZ = sig128Extra.v;
143     sigZExtra = sig128Extra.extra;
144  roundAndPack:
145     return
146         softfloat_roundPackToF128( signZ, expZ, sigZ.v64, sigZ.v0, sigZExtra );
147  propagateNaN:
148     uiZ = softfloat_propagateNaNF128UI( uiA64, uiA0, uiB64, uiB0 );
149  uiZ:
150     uZ.ui = uiZ;
151     return uZ.f;
152 
153 }
154 
155