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 #include "softfloat.h"
44 
45 float128_t
softfloat_subMagsF128(uint_fast64_t uiA64,uint_fast64_t uiA0,uint_fast64_t uiB64,uint_fast64_t uiB0,bool signZ)46  softfloat_subMagsF128(
47      uint_fast64_t uiA64,
48      uint_fast64_t uiA0,
49      uint_fast64_t uiB64,
50      uint_fast64_t uiB0,
51      bool signZ
52  )
53 {
54     int_fast32_t expA;
55     struct uint128 sigA;
56     int_fast32_t expB;
57     struct uint128 sigB, sigZ;
58     int_fast32_t expDiff, expZ;
59     struct uint128 uiZ;
60     union ui128_f128 uZ;
61 
62     expA = expF128UI64( uiA64 );
63     sigA.v64 = fracF128UI64( uiA64 );
64     sigA.v0  = uiA0;
65     expB = expF128UI64( uiB64 );
66     sigB.v64 = fracF128UI64( uiB64 );
67     sigB.v0  = uiB0;
68     sigA = softfloat_shortShiftLeft128( sigA.v64, sigA.v0, 4 );
69     sigB = softfloat_shortShiftLeft128( sigB.v64, sigB.v0, 4 );
70     expDiff = expA - expB;
71     if ( 0 < expDiff ) goto expABigger;
72     if ( expDiff < 0 ) goto expBBigger;
73     if ( expA == 0x7FFF ) {
74         if ( sigA.v64 | sigA.v0 | sigB.v64 | sigB.v0 ) goto propagateNaN;
75         softfloat_raiseFlags( softfloat_flag_invalid );
76         uiZ.v64 = defaultNaNF128UI64;
77         uiZ.v0  = defaultNaNF128UI0;
78         goto uiZ;
79     }
80     expZ = expA;
81     if ( ! expZ ) expZ = 1;
82     if ( sigB.v64 < sigA.v64 ) goto aBigger;
83     if ( sigA.v64 < sigB.v64 ) goto bBigger;
84     if ( sigB.v0 < sigA.v0 ) goto aBigger;
85     if ( sigA.v0 < sigB.v0 ) goto bBigger;
86     uiZ.v64 =
87         packToF128UI64(
88             (softfloat_roundingMode == softfloat_round_min), 0, 0 );
89     uiZ.v0 = 0;
90     goto uiZ;
91  expBBigger:
92     if ( expB == 0x7FFF ) {
93         if ( sigB.v64 | sigB.v0 ) goto propagateNaN;
94         uiZ.v64 = packToF128UI64( signZ ^ 1, 0x7FFF, 0 );
95         uiZ.v0  = 0;
96         goto uiZ;
97     }
98     if ( expA ) {
99         sigA.v64 |= UINT64_C( 0x0010000000000000 );
100     } else {
101         ++expDiff;
102         if ( ! expDiff ) goto newlyAlignedBBigger;
103     }
104     sigA = softfloat_shiftRightJam128( sigA.v64, sigA.v0, -expDiff );
105  newlyAlignedBBigger:
106     expZ = expB;
107     sigB.v64 |= UINT64_C( 0x0010000000000000 );
108  bBigger:
109     signZ ^= 1;
110     sigZ = softfloat_sub128( sigB.v64, sigB.v0, sigA.v64, sigA.v0 );
111     goto normRoundPack;
112  expABigger:
113     if ( expA == 0x7FFF ) {
114         if ( sigA.v64 | sigA.v0 ) goto propagateNaN;
115         uiZ.v64 = uiA64;
116         uiZ.v0  = uiA0;
117         goto uiZ;
118     }
119     if ( expB ) {
120         sigB.v64 |= UINT64_C( 0x0010000000000000 );
121     } else {
122         --expDiff;
123         if ( ! expDiff ) goto newlyAlignedABigger;
124     }
125     sigB = softfloat_shiftRightJam128( sigB.v64, sigB.v0, expDiff );
126  newlyAlignedABigger:
127     expZ = expA;
128     sigA.v64 |= UINT64_C( 0x0010000000000000 );
129  aBigger:
130     sigZ = softfloat_sub128( sigA.v64, sigA.v0, sigB.v64, sigB.v0 );
131  normRoundPack:
132     return softfloat_normRoundPackToF128( signZ, expZ - 5, sigZ.v64, sigZ.v0 );
133  propagateNaN:
134     uiZ = softfloat_propagateNaNF128UI( uiA64, uiA0, uiB64, uiB0 );
135  uiZ:
136     uZ.ui = uiZ;
137     return uZ.f;
138 
139 }
140 
141