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, 2017 The Regents of the
8 University of 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"
44 float16_t
softfloat_subMagsF16( uint_fast16_t uiA
, uint_fast16_t uiB
)
54 int_fast8_t shiftDist
, expZ
;
55 uint_fast16_t sigZ
, sigX
, sigY
;
57 int_fast8_t roundingMode
;
60 /*------------------------------------------------------------------------
61 *------------------------------------------------------------------------*/
62 expA
= expF16UI( uiA
);
63 sigA
= fracF16UI( uiA
);
64 expB
= expF16UI( uiB
);
65 sigB
= fracF16UI( uiB
);
66 /*------------------------------------------------------------------------
67 *------------------------------------------------------------------------*/
68 expDiff
= expA
- expB
;
70 /*--------------------------------------------------------------------
71 *--------------------------------------------------------------------*/
73 if ( sigA
| sigB
) goto propagateNaN
;
74 softfloat_raiseFlags( softfloat_flag_invalid
);
75 uiZ
= defaultNaNF16UI
;
78 sigDiff
= sigA
- sigB
;
82 (softfloat_roundingMode
== softfloat_round_min
), 0, 0 );
86 signZ
= signF16UI( uiA
);
91 shiftDist
= softfloat_countLeadingZeros16( sigDiff
) - 5;
92 expZ
= expA
- shiftDist
;
97 sigZ
= sigDiff
<<shiftDist
;
100 /*--------------------------------------------------------------------
101 *--------------------------------------------------------------------*/
102 signZ
= signF16UI( uiA
);
104 /*----------------------------------------------------------------
105 *----------------------------------------------------------------*/
107 if ( expB
== 0x1F ) {
108 if ( sigB
) goto propagateNaN
;
109 uiZ
= packToF16UI( signZ
, 0x1F, 0 );
112 if ( expDiff
<= -13 ) {
113 uiZ
= packToF16UI( signZ
, expB
, sigB
);
114 if ( expA
| sigA
) goto subEpsilon
;
118 sigX
= sigB
| 0x0400;
119 sigY
= sigA
+ (expA
? 0x0400 : sigA
);
122 /*----------------------------------------------------------------
123 *----------------------------------------------------------------*/
125 if ( expA
== 0x1F ) {
126 if ( sigA
) goto propagateNaN
;
129 if ( 13 <= expDiff
) {
130 if ( expB
| sigB
) goto subEpsilon
;
134 sigX
= sigA
| 0x0400;
135 sigY
= sigB
+ (expB
? 0x0400 : sigB
);
137 sig32Z
= ((uint_fast32_t) sigX
<<expDiff
) - sigY
;
138 shiftDist
= softfloat_countLeadingZeros32( sig32Z
) - 1;
139 sig32Z
<<= shiftDist
;
142 if ( sig32Z
& 0xFFFF ) {
145 if ( ! (sigZ
& 0xF) && ((unsigned int) expZ
< 0x1E) ) {
150 return softfloat_roundPackToF16( signZ
, expZ
, sigZ
);
152 /*------------------------------------------------------------------------
153 *------------------------------------------------------------------------*/
155 uiZ
= softfloat_propagateNaNF16UI( uiA
, uiB
);
157 /*------------------------------------------------------------------------
158 *------------------------------------------------------------------------*/
160 roundingMode
= softfloat_roundingMode
;
161 if ( roundingMode
!= softfloat_round_near_even
) {
163 (roundingMode
== softfloat_round_minMag
)
165 == (signF16UI( uiZ
) ? softfloat_round_max
166 : softfloat_round_min
))
170 #ifdef SOFTFLOAT_ROUND_ODD
171 else if ( roundingMode
== softfloat_round_odd
) {
176 softfloat_exceptionFlags
|= softfloat_flag_inexact
;
178 /*------------------------------------------------------------------------
179 *------------------------------------------------------------------------*/
181 uiZ
= packToF16UI( signZ
, expZ
, sigZ
);