Merge pull request #156 from p12nGH/noncontiguous_harts
[riscv-isa-sim.git] / softfloat / s_subMagsF128.c
1
2 /*============================================================================
3
4 This C source file is part of the SoftFloat IEEE Floating-Point Arithmetic
5 Package, Release 3d, by John R. Hauser.
6
7 Copyright 2011, 2012, 2013, 2014, 2015 The Regents of the University of
8 California. 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 float128_t
45 softfloat_subMagsF128(
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, sigZ;
57 int_fast32_t expDiff, expZ;
58 struct uint128 uiZ;
59 union ui128_f128 uZ;
60
61 expA = expF128UI64( uiA64 );
62 sigA.v64 = fracF128UI64( uiA64 );
63 sigA.v0 = uiA0;
64 expB = expF128UI64( uiB64 );
65 sigB.v64 = fracF128UI64( uiB64 );
66 sigB.v0 = uiB0;
67 sigA = softfloat_shortShiftLeft128( sigA.v64, sigA.v0, 4 );
68 sigB = softfloat_shortShiftLeft128( sigB.v64, sigB.v0, 4 );
69 expDiff = expA - expB;
70 if ( 0 < expDiff ) goto expABigger;
71 if ( expDiff < 0 ) goto expBBigger;
72 if ( expA == 0x7FFF ) {
73 if ( sigA.v64 | sigA.v0 | sigB.v64 | sigB.v0 ) goto propagateNaN;
74 softfloat_raiseFlags( softfloat_flag_invalid );
75 uiZ.v64 = defaultNaNF128UI64;
76 uiZ.v0 = defaultNaNF128UI0;
77 goto uiZ;
78 }
79 expZ = expA;
80 if ( ! expZ ) expZ = 1;
81 if ( sigB.v64 < sigA.v64 ) goto aBigger;
82 if ( sigA.v64 < sigB.v64 ) goto bBigger;
83 if ( sigB.v0 < sigA.v0 ) goto aBigger;
84 if ( sigA.v0 < sigB.v0 ) goto bBigger;
85 uiZ.v64 =
86 packToF128UI64(
87 (softfloat_roundingMode == softfloat_round_min), 0, 0 );
88 uiZ.v0 = 0;
89 goto uiZ;
90 expBBigger:
91 if ( expB == 0x7FFF ) {
92 if ( sigB.v64 | sigB.v0 ) goto propagateNaN;
93 uiZ.v64 = packToF128UI64( signZ ^ 1, 0x7FFF, 0 );
94 uiZ.v0 = 0;
95 goto uiZ;
96 }
97 if ( expA ) {
98 sigA.v64 |= UINT64_C( 0x0010000000000000 );
99 } else {
100 ++expDiff;
101 if ( ! expDiff ) goto newlyAlignedBBigger;
102 }
103 sigA = softfloat_shiftRightJam128( sigA.v64, sigA.v0, -expDiff );
104 newlyAlignedBBigger:
105 expZ = expB;
106 sigB.v64 |= UINT64_C( 0x0010000000000000 );
107 bBigger:
108 signZ = ! signZ;
109 sigZ = softfloat_sub128( sigB.v64, sigB.v0, sigA.v64, sigA.v0 );
110 goto normRoundPack;
111 expABigger:
112 if ( expA == 0x7FFF ) {
113 if ( sigA.v64 | sigA.v0 ) goto propagateNaN;
114 uiZ.v64 = uiA64;
115 uiZ.v0 = uiA0;
116 goto uiZ;
117 }
118 if ( expB ) {
119 sigB.v64 |= UINT64_C( 0x0010000000000000 );
120 } else {
121 --expDiff;
122 if ( ! expDiff ) goto newlyAlignedABigger;
123 }
124 sigB = softfloat_shiftRightJam128( sigB.v64, sigB.v0, expDiff );
125 newlyAlignedABigger:
126 expZ = expA;
127 sigA.v64 |= UINT64_C( 0x0010000000000000 );
128 aBigger:
129 sigZ = softfloat_sub128( sigA.v64, sigA.v0, sigB.v64, sigB.v0 );
130 normRoundPack:
131 return softfloat_normRoundPackToF128( signZ, expZ - 5, sigZ.v64, sigZ.v0 );
132 propagateNaN:
133 uiZ = softfloat_propagateNaNF128UI( uiA64, uiA0, uiB64, uiB0 );
134 uiZ:
135 uZ.ui = uiZ;
136 return uZ.f;
137
138 }
139