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"
44 extern const uint16_t softfloat_approxRecip_1k0s
[];
45 extern const uint16_t softfloat_approxRecip_1k1s
[];
47 float16_t
f16_div( float16_t a
, float16_t b
)
60 struct exp8_sig16 normExpSig
;
62 #ifdef SOFTFLOAT_FAST_DIV32TO16
68 uint_fast16_t sigZ
, rem
;
73 /*------------------------------------------------------------------------
74 *------------------------------------------------------------------------*/
77 signA
= signF16UI( uiA
);
78 expA
= expF16UI( uiA
);
79 sigA
= fracF16UI( uiA
);
82 signB
= signF16UI( uiB
);
83 expB
= expF16UI( uiB
);
84 sigB
= fracF16UI( uiB
);
85 signZ
= signA
^ signB
;
86 /*------------------------------------------------------------------------
87 *------------------------------------------------------------------------*/
89 if ( sigA
) goto propagateNaN
;
91 if ( sigB
) goto propagateNaN
;
97 if ( sigB
) goto propagateNaN
;
100 /*------------------------------------------------------------------------
101 *------------------------------------------------------------------------*/
104 if ( ! (expA
| sigA
) ) goto invalid
;
105 softfloat_raiseFlags( softfloat_flag_infinite
);
108 normExpSig
= softfloat_normSubnormalF16Sig( sigB
);
109 expB
= normExpSig
.exp
;
110 sigB
= normExpSig
.sig
;
113 if ( ! sigA
) goto zero
;
114 normExpSig
= softfloat_normSubnormalF16Sig( sigA
);
115 expA
= normExpSig
.exp
;
116 sigA
= normExpSig
.sig
;
118 /*------------------------------------------------------------------------
119 *------------------------------------------------------------------------*/
120 expZ
= expA
- expB
+ 0xE;
123 #ifdef SOFTFLOAT_FAST_DIV32TO16
126 sig32A
= (uint_fast32_t) sigA
<<15;
128 sig32A
= (uint_fast32_t) sigA
<<14;
130 sigZ
= sig32A
/ sigB
;
131 if ( ! (sigZ
& 7) ) sigZ
|= ((uint_fast32_t) sigB
* sigZ
!= sig32A
);
139 index
= sigB
>>6 & 0xF;
140 r0
= softfloat_approxRecip_1k0s
[index
]
141 - (((uint_fast32_t) softfloat_approxRecip_1k1s
[index
]
144 sigZ
= ((uint_fast32_t) sigA
* r0
)>>16;
145 rem
= (sigA
<<10) - sigZ
* sigB
;
146 sigZ
+= (rem
* (uint_fast32_t) r0
)>>26;
147 /*------------------------------------------------------------------------
148 *------------------------------------------------------------------------*/
150 if ( ! (sigZ
& 7) ) {
152 rem
= (sigA
<<10) - sigZ
* sigB
;
153 if ( rem
& 0x8000 ) {
156 if ( rem
) sigZ
|= 1;
160 return softfloat_roundPackToF16( signZ
, expZ
, sigZ
);
161 /*------------------------------------------------------------------------
162 *------------------------------------------------------------------------*/
164 uiZ
= softfloat_propagateNaNF16UI( uiA
, uiB
);
166 /*------------------------------------------------------------------------
167 *------------------------------------------------------------------------*/
169 softfloat_raiseFlags( softfloat_flag_invalid
);
170 uiZ
= defaultNaNF16UI
;
172 /*------------------------------------------------------------------------
173 *------------------------------------------------------------------------*/
175 uiZ
= packToF16UI( signZ
, 0x1F, 0 );
177 /*------------------------------------------------------------------------
178 *------------------------------------------------------------------------*/
180 uiZ
= packToF16UI( signZ
, 0, 0 );