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, 2015, 2016 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 | extern const uint16_t softfloat_approxRecip_1k0s[];
|
---|
45 | extern const uint16_t softfloat_approxRecip_1k1s[];
|
---|
46 |
|
---|
47 | float16_t f16_div( float16_t a, float16_t b SOFTFLOAT_STATE_DECL_COMMA )
|
---|
48 | {
|
---|
49 | union ui16_f16 uA;
|
---|
50 | uint_fast16_t uiA;
|
---|
51 | bool signA;
|
---|
52 | int_fast8_t expA;
|
---|
53 | uint_fast16_t sigA;
|
---|
54 | union ui16_f16 uB;
|
---|
55 | uint_fast16_t uiB;
|
---|
56 | bool signB;
|
---|
57 | int_fast8_t expB;
|
---|
58 | uint_fast16_t sigB;
|
---|
59 | bool signZ;
|
---|
60 | struct exp8_sig16 normExpSig;
|
---|
61 | int_fast8_t expZ;
|
---|
62 | #ifdef SOFTFLOAT_FAST_DIV32TO16
|
---|
63 | uint_fast32_t sig32A;
|
---|
64 | uint_fast16_t sigZ;
|
---|
65 | #else
|
---|
66 | int index;
|
---|
67 | uint16_t r0;
|
---|
68 | uint_fast16_t sigZ, rem;
|
---|
69 | #endif
|
---|
70 | uint_fast16_t uiZ;
|
---|
71 | union ui16_f16 uZ;
|
---|
72 |
|
---|
73 | /*------------------------------------------------------------------------
|
---|
74 | *------------------------------------------------------------------------*/
|
---|
75 | uA.f = a;
|
---|
76 | uiA = uA.ui;
|
---|
77 | signA = signF16UI( uiA );
|
---|
78 | expA = expF16UI( uiA );
|
---|
79 | sigA = fracF16UI( uiA );
|
---|
80 | uB.f = b;
|
---|
81 | uiB = uB.ui;
|
---|
82 | signB = signF16UI( uiB );
|
---|
83 | expB = expF16UI( uiB );
|
---|
84 | sigB = fracF16UI( uiB );
|
---|
85 | signZ = signA ^ signB;
|
---|
86 | /*------------------------------------------------------------------------
|
---|
87 | *------------------------------------------------------------------------*/
|
---|
88 | if ( expA == 0x1F ) {
|
---|
89 | if ( sigA ) goto propagateNaN;
|
---|
90 | if ( expB == 0x1F ) {
|
---|
91 | if ( sigB ) goto propagateNaN;
|
---|
92 | goto invalid;
|
---|
93 | }
|
---|
94 | goto infinity;
|
---|
95 | }
|
---|
96 | if ( expB == 0x1F ) {
|
---|
97 | if ( sigB ) goto propagateNaN;
|
---|
98 | goto zero;
|
---|
99 | }
|
---|
100 | /*------------------------------------------------------------------------
|
---|
101 | *------------------------------------------------------------------------*/
|
---|
102 | if ( ! expB ) {
|
---|
103 | if ( ! sigB ) {
|
---|
104 | if ( ! (expA | sigA) ) goto invalid;
|
---|
105 | softfloat_raiseFlags( softfloat_flag_infinite SOFTFLOAT_STATE_ARG_COMMA );
|
---|
106 | goto infinity;
|
---|
107 | }
|
---|
108 | normExpSig = softfloat_normSubnormalF16Sig( sigB );
|
---|
109 | expB = normExpSig.exp;
|
---|
110 | sigB = normExpSig.sig;
|
---|
111 | }
|
---|
112 | if ( ! expA ) {
|
---|
113 | if ( ! sigA ) goto zero;
|
---|
114 | normExpSig = softfloat_normSubnormalF16Sig( sigA );
|
---|
115 | expA = normExpSig.exp;
|
---|
116 | sigA = normExpSig.sig;
|
---|
117 | }
|
---|
118 | /*------------------------------------------------------------------------
|
---|
119 | *------------------------------------------------------------------------*/
|
---|
120 | expZ = expA - expB + 0xE;
|
---|
121 | sigA |= 0x0400;
|
---|
122 | sigB |= 0x0400;
|
---|
123 | #ifdef SOFTFLOAT_FAST_DIV32TO16
|
---|
124 | if ( sigA < sigB ) {
|
---|
125 | --expZ;
|
---|
126 | sig32A = (uint_fast32_t) sigA<<15;
|
---|
127 | } else {
|
---|
128 | sig32A = (uint_fast32_t) sigA<<14;
|
---|
129 | }
|
---|
130 | sigZ = sig32A / sigB;
|
---|
131 | if ( ! (sigZ & 7) ) sigZ |= ((uint_fast32_t) sigB * sigZ != sig32A);
|
---|
132 | #else
|
---|
133 | if ( sigA < sigB ) {
|
---|
134 | --expZ;
|
---|
135 | sigA <<= 5;
|
---|
136 | } else {
|
---|
137 | sigA <<= 4;
|
---|
138 | }
|
---|
139 | index = sigB>>6 & 0xF;
|
---|
140 | r0 = softfloat_approxRecip_1k0s[index]
|
---|
141 | - (((uint_fast32_t) softfloat_approxRecip_1k1s[index]
|
---|
142 | * (sigB & 0x3F))
|
---|
143 | >>10);
|
---|
144 | sigZ = ((uint_fast32_t) sigA * r0)>>16;
|
---|
145 | rem = (sigA<<10) - sigZ * sigB;
|
---|
146 | sigZ += (rem * (uint_fast32_t) r0)>>26;
|
---|
147 | /*------------------------------------------------------------------------
|
---|
148 | *------------------------------------------------------------------------*/
|
---|
149 | ++sigZ;
|
---|
150 | if ( ! (sigZ & 7) ) {
|
---|
151 | sigZ &= ~1;
|
---|
152 | rem = (sigA<<10) - sigZ * sigB;
|
---|
153 | if ( rem & 0x8000 ) {
|
---|
154 | sigZ -= 2;
|
---|
155 | } else {
|
---|
156 | if ( rem ) sigZ |= 1;
|
---|
157 | }
|
---|
158 | }
|
---|
159 | #endif
|
---|
160 | return softfloat_roundPackToF16( signZ, expZ, sigZ SOFTFLOAT_STATE_ARG_COMMA );
|
---|
161 | /*------------------------------------------------------------------------
|
---|
162 | *------------------------------------------------------------------------*/
|
---|
163 | propagateNaN:
|
---|
164 | uiZ = softfloat_propagateNaNF16UI( uiA, uiB SOFTFLOAT_STATE_ARG_COMMA );
|
---|
165 | goto uiZ;
|
---|
166 | /*------------------------------------------------------------------------
|
---|
167 | *------------------------------------------------------------------------*/
|
---|
168 | invalid:
|
---|
169 | softfloat_raiseFlags( softfloat_flag_invalid SOFTFLOAT_STATE_ARG_COMMA );
|
---|
170 | uiZ = defaultNaNF16UI;
|
---|
171 | goto uiZ;
|
---|
172 | /*------------------------------------------------------------------------
|
---|
173 | *------------------------------------------------------------------------*/
|
---|
174 | infinity:
|
---|
175 | uiZ = packToF16UI( signZ, 0x1F, 0 );
|
---|
176 | goto uiZ;
|
---|
177 | /*------------------------------------------------------------------------
|
---|
178 | *------------------------------------------------------------------------*/
|
---|
179 | zero:
|
---|
180 | uiZ = packToF16UI( signZ, 0, 0 );
|
---|
181 | uiZ:
|
---|
182 | uZ.ui = uiZ;
|
---|
183 | return uZ.f;
|
---|
184 |
|
---|
185 | }
|
---|
186 |
|
---|