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
|
---|
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 | SOFTFLOAT_STATE_DECL_COMMA
|
---|
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 SOFTFLOAT_STATE_ARG_COMMA );
|
---|
148 | propagateNaN:
|
---|
149 | uiZ = softfloat_propagateNaNF128UI( uiA64, uiA0, uiB64, uiB0 SOFTFLOAT_STATE_ARG_COMMA );
|
---|
150 | uiZ:
|
---|
151 | uZ.ui = uiZ;
|
---|
152 | return uZ.f;
|
---|
153 |
|
---|
154 | }
|
---|
155 |
|
---|