2 /*============================================================================
4 This C source file is part of the SoftFloat IEEE Floating-Point Arithmetic
5 Package, Release 3d, by John R. Hauser.
7 Copyright 2011, 2012, 2013, 2014, 2015, 2016 The Regents of the University of
8 California. All rights reserved.
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
13 1. Redistributions of source code must retain the above copyright notice,
14 this list of conditions, and the following disclaimer.
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.
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.
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.
35 =============================================================================*/
40 #include "internals.h"
41 #include "specialize.h"
42 #include "softfloat.h"
46 uint_fast16_t uiA
, uint_fast16_t uiB
, uint_fast16_t uiC
, uint_fast8_t op
)
58 uint_fast16_t magBits
, uiZ
;
59 struct exp8_sig16 normExpSig
;
61 uint_fast32_t sigProd
;
66 uint_fast32_t sig32Z
, sig32C
;
67 int_fast8_t shiftDist
;
70 /*------------------------------------------------------------------------
71 *------------------------------------------------------------------------*/
72 signA
= signF16UI( uiA
);
73 expA
= expF16UI( uiA
);
74 sigA
= fracF16UI( uiA
);
75 signB
= signF16UI( uiB
);
76 expB
= expF16UI( uiB
);
77 sigB
= fracF16UI( uiB
);
78 signC
= signF16UI( uiC
) ^ (op
== softfloat_mulAdd_subC
);
79 expC
= expF16UI( uiC
);
80 sigC
= fracF16UI( uiC
);
81 signProd
= signA
^ signB
^ (op
== softfloat_mulAdd_subProd
);
82 /*------------------------------------------------------------------------
83 *------------------------------------------------------------------------*/
85 if ( sigA
|| ((expB
== 0x1F) && sigB
) ) goto propagateNaN_ABC
;
86 magBits
= expB
| sigB
;
90 if ( sigB
) goto propagateNaN_ABC
;
91 magBits
= expA
| sigA
;
102 /*------------------------------------------------------------------------
103 *------------------------------------------------------------------------*/
105 if ( ! sigA
) goto zeroProd
;
106 normExpSig
= softfloat_normSubnormalF16Sig( sigA
);
107 expA
= normExpSig
.exp
;
108 sigA
= normExpSig
.sig
;
111 if ( ! sigB
) goto zeroProd
;
112 normExpSig
= softfloat_normSubnormalF16Sig( sigB
);
113 expB
= normExpSig
.exp
;
114 sigB
= normExpSig
.sig
;
116 /*------------------------------------------------------------------------
117 *------------------------------------------------------------------------*/
118 expProd
= expA
+ expB
- 0xE;
119 sigA
= (sigA
| 0x0400)<<4;
120 sigB
= (sigB
| 0x0400)<<4;
121 sigProd
= (uint_fast32_t) sigA
* sigB
;
122 if ( sigProd
< 0x20000000 ) {
130 sigZ
= sigProd
>>15 | ((sigProd
& 0x7FFF) != 0);
133 normExpSig
= softfloat_normSubnormalF16Sig( sigC
);
134 expC
= normExpSig
.exp
;
135 sigC
= normExpSig
.sig
;
137 sigC
= (sigC
| 0x0400)<<3;
138 /*------------------------------------------------------------------------
139 *------------------------------------------------------------------------*/
140 expDiff
= expProd
- expC
;
141 if ( signProd
== signC
) {
142 /*--------------------------------------------------------------------
143 *--------------------------------------------------------------------*/
144 if ( expDiff
<= 0 ) {
146 sigZ
= sigC
+ softfloat_shiftRightJam32( sigProd
, 16 - expDiff
);
151 + softfloat_shiftRightJam32(
152 (uint_fast32_t) sigC
<<16, expDiff
);
153 sigZ
= sig32Z
>>16 | ((sig32Z
& 0xFFFF) != 0 );
155 if ( sigZ
< 0x4000 ) {
160 /*--------------------------------------------------------------------
161 *--------------------------------------------------------------------*/
162 sig32C
= (uint_fast32_t) sigC
<<16;
166 sig32Z
= sig32C
- softfloat_shiftRightJam32( sigProd
, -expDiff
);
167 } else if ( ! expDiff
) {
169 sig32Z
= sigProd
- sig32C
;
170 if ( ! sig32Z
) goto completeCancellation
;
171 if ( sig32Z
& 0x80000000 ) {
177 sig32Z
= sigProd
- softfloat_shiftRightJam32( sig32C
, expDiff
);
179 shiftDist
= softfloat_countLeadingZeros32( sig32Z
) - 1;
182 if ( shiftDist
< 0 ) {
185 | ((uint32_t) (sig32Z
<<(shiftDist
& 31)) != 0);
187 sigZ
= (uint_fast16_t) sig32Z
<<shiftDist
;
191 return softfloat_roundPackToF16( signZ
, expZ
, sigZ
);
192 /*------------------------------------------------------------------------
193 *------------------------------------------------------------------------*/
195 uiZ
= softfloat_propagateNaNF16UI( uiA
, uiB
);
196 goto propagateNaN_ZC
;
197 /*------------------------------------------------------------------------
198 *------------------------------------------------------------------------*/
201 uiZ
= packToF16UI( signProd
, 0x1F, 0 );
202 if ( expC
!= 0x1F ) goto uiZ
;
203 if ( sigC
) goto propagateNaN_ZC
;
204 if ( signProd
== signC
) goto uiZ
;
206 softfloat_raiseFlags( softfloat_flag_invalid
);
207 uiZ
= defaultNaNF16UI
;
209 uiZ
= softfloat_propagateNaNF16UI( uiZ
, uiC
);
211 /*------------------------------------------------------------------------
212 *------------------------------------------------------------------------*/
215 if ( ! (expC
| sigC
) && (signProd
!= signC
) ) {
216 completeCancellation
:
219 (softfloat_roundingMode
== softfloat_round_min
), 0, 0 );