1 // SPDX-License-Identifier: BSD-3-Clause
2 
3 /*============================================================================
4 
5 This C source file is part of the SoftFloat IEEE Floating-Point Arithmetic
6 Package, Release 3a, by John R. Hauser.
7 
8 Copyright 2011, 2012, 2013, 2014 The Regents of the University of California.
9 All rights reserved.
10 
11 Redistribution and use in source and binary forms, with or without
12 modification, are permitted provided that the following conditions are met:
13 
14  1. Redistributions of source code must retain the above copyright notice,
15     this list of conditions, and the following disclaimer.
16 
17  2. Redistributions in binary form must reproduce the above copyright notice,
18     this list of conditions, and the following disclaimer in the documentation
19     and/or other materials provided with the distribution.
20 
21  3. Neither the name of the University nor the names of its contributors may
22     be used to endorse or promote products derived from this software without
23     specific prior written permission.
24 
25 THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
26 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
28 DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
29 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
32 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 
36 =============================================================================*/
37 
38 #include <stdbool.h>
39 #include <stdint.h>
40 #include "platform.h"
41 #include "internals.h"
42 #include "specialize.h"
43 
44 float128_t
softfloat_addMagsF128(uint_fast64_t uiA64,uint_fast64_t uiA0,uint_fast64_t uiB64,uint_fast64_t uiB0,bool signZ)45  softfloat_addMagsF128(
46      uint_fast64_t uiA64,
47      uint_fast64_t uiA0,
48      uint_fast64_t uiB64,
49      uint_fast64_t uiB0,
50      bool signZ
51  )
52 {
53     int_fast32_t expA;
54     struct uint128 sigA;
55     int_fast32_t expB;
56     struct uint128 sigB;
57     int_fast32_t expDiff;
58     struct uint128 uiZ, sigZ;
59     int_fast32_t expZ;
60     uint_fast64_t sigZExtra;
61     struct uint128_extra sig128Extra;
62     union ui128_f128 uZ;
63 
64     expA = expF128UI64( uiA64 );
65     sigA.v64 = fracF128UI64( uiA64 );
66     sigA.v0  = uiA0;
67     expB = expF128UI64( uiB64 );
68     sigB.v64 = fracF128UI64( uiB64 );
69     sigB.v0  = uiB0;
70     expDiff = expA - expB;
71     if ( ! expDiff ) {
72         if ( expA == 0x7FFF ) {
73             if ( sigA.v64 | sigA.v0 | sigB.v64 | sigB.v0 ) goto propagateNaN;
74             uiZ.v64 = uiA64;
75             uiZ.v0  = uiA0;
76             goto uiZ;
77         }
78         sigZ = softfloat_add128( sigA.v64, sigA.v0, sigB.v64, sigB.v0 );
79         if ( ! expA ) {
80             uiZ.v64 = packToF128UI64( signZ, 0, sigZ.v64 );
81             uiZ.v0  = sigZ.v0;
82             goto uiZ;
83         }
84         expZ = expA;
85         sigZ.v64 |= UINT64_C( 0x0002000000000000 );
86         sigZExtra = 0;
87         goto shiftRight1;
88     }
89     if ( expDiff < 0 ) {
90         if ( expB == 0x7FFF ) {
91             if ( sigB.v64 | sigB.v0 ) goto propagateNaN;
92             uiZ.v64 = packToF128UI64( signZ, 0x7FFF, 0 );
93             uiZ.v0  = 0;
94             goto uiZ;
95         }
96         expZ = expB;
97         if ( expA ) {
98             sigA.v64 |= UINT64_C( 0x0001000000000000 );
99         } else {
100             ++expDiff;
101             sigZExtra = 0;
102             if ( ! expDiff ) goto newlyAligned;
103         }
104         sig128Extra =
105             softfloat_shiftRightJam128Extra( sigA.v64, sigA.v0, 0, -expDiff );
106         sigA = sig128Extra.v;
107         sigZExtra = sig128Extra.extra;
108     } else {
109         if ( expA == 0x7FFF ) {
110             if ( sigA.v64 | sigA.v0 ) goto propagateNaN;
111             uiZ.v64 = uiA64;
112             uiZ.v0  = uiA0;
113             goto uiZ;
114         }
115         expZ = expA;
116         if ( expB ) {
117             sigB.v64 |= UINT64_C( 0x0001000000000000 );
118         } else {
119             --expDiff;
120             sigZExtra = 0;
121             if ( ! expDiff ) goto newlyAligned;
122         }
123         sig128Extra =
124             softfloat_shiftRightJam128Extra( sigB.v64, sigB.v0, 0, expDiff );
125         sigB = sig128Extra.v;
126         sigZExtra = sig128Extra.extra;
127     }
128  newlyAligned:
129     sigZ =
130         softfloat_add128(
131             sigA.v64 | UINT64_C( 0x0001000000000000 ),
132             sigA.v0,
133             sigB.v64,
134             sigB.v0
135         );
136     --expZ;
137     if ( sigZ.v64 < UINT64_C( 0x0002000000000000 ) ) goto roundAndPack;
138     ++expZ;
139  shiftRight1:
140     sig128Extra =
141         softfloat_shortShiftRightJam128Extra(
142             sigZ.v64, sigZ.v0, sigZExtra, 1 );
143     sigZ = sig128Extra.v;
144     sigZExtra = sig128Extra.extra;
145  roundAndPack:
146     return
147         softfloat_roundPackToF128( signZ, expZ, sigZ.v64, sigZ.v0, sigZExtra );
148  propagateNaN:
149     uiZ = softfloat_propagateNaNF128UI( uiA64, uiA0, uiB64, uiB0 );
150  uiZ:
151     uZ.ui = uiZ;
152     return uZ.f;
153 
154 }
155 
156