1 // SPDX-License-Identifier: BSD-3-Clause
2
3 /*============================================================================
4
5 This C source file is part of the SoftFloat IEEE Floating-Point Arithmetic
6 Package, Release 3a, by John R. Hauser.
7
8 Copyright 2011, 2012, 2013, 2014 The Regents of the University of California.
9 All rights reserved.
10
11 Redistribution and use in source and binary forms, with or without
12 modification, are permitted provided that the following conditions are met:
13
14 1. Redistributions of source code must retain the above copyright notice,
15 this list of conditions, and the following disclaimer.
16
17 2. Redistributions in binary form must reproduce the above copyright notice,
18 this list of conditions, and the following disclaimer in the documentation
19 and/or other materials provided with the distribution.
20
21 3. Neither the name of the University nor the names of its contributors may
22 be used to endorse or promote products derived from this software without
23 specific prior written permission.
24
25 THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
26 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
28 DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
29 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
32 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
36 =============================================================================*/
37
38 #include <stdbool.h>
39 #include <stdint.h>
40 #include "platform.h"
41 #include "internals.h"
42 #include "specialize.h"
43 #include "softfloat.h"
44
extF80_mul(extFloat80_t a,extFloat80_t b)45 extFloat80_t extF80_mul( extFloat80_t a, extFloat80_t b )
46 {
47 union { struct extFloat80M s; extFloat80_t f; } uA;
48 uint_fast16_t uiA64;
49 uint_fast64_t uiA0;
50 bool signA;
51 int_fast32_t expA;
52 uint_fast64_t sigA;
53 union { struct extFloat80M s; extFloat80_t f; } uB;
54 uint_fast16_t uiB64;
55 uint_fast64_t uiB0;
56 bool signB;
57 int_fast32_t expB;
58 uint_fast64_t sigB;
59 bool signZ;
60 uint_fast64_t magBits;
61 struct exp32_sig64 normExpSig;
62 int_fast32_t expZ;
63 struct uint128 sig128Z, uiZ;
64 uint_fast16_t uiZ64;
65 uint_fast64_t uiZ0;
66 union { struct extFloat80M s; extFloat80_t f; } uZ;
67
68 /*------------------------------------------------------------------------
69 *------------------------------------------------------------------------*/
70 uA.f = a;
71 uiA64 = uA.s.signExp;
72 uiA0 = uA.s.signif;
73 signA = signExtF80UI64( uiA64 );
74 expA = expExtF80UI64( uiA64 );
75 sigA = uiA0;
76 uB.f = b;
77 uiB64 = uB.s.signExp;
78 uiB0 = uB.s.signif;
79 signB = signExtF80UI64( uiB64 );
80 expB = expExtF80UI64( uiB64 );
81 sigB = uiB0;
82 signZ = signA ^ signB;
83 /*------------------------------------------------------------------------
84 *------------------------------------------------------------------------*/
85 if ( expA == 0x7FFF ) {
86 if (
87 (sigA & UINT64_C( 0x7FFFFFFFFFFFFFFF ))
88 || ((expB == 0x7FFF) && (sigB & UINT64_C( 0x7FFFFFFFFFFFFFFF )))
89 ) {
90 goto propagateNaN;
91 }
92 magBits = expB | sigB;
93 goto infArg;
94 }
95 if ( expB == 0x7FFF ) {
96 if ( sigB & UINT64_C( 0x7FFFFFFFFFFFFFFF ) ) goto propagateNaN;
97 magBits = expA | sigA;
98 goto infArg;
99 }
100 /*------------------------------------------------------------------------
101 *------------------------------------------------------------------------*/
102 if ( ! expA ) expA = 1;
103 if ( ! (sigA & UINT64_C( 0x8000000000000000 )) ) {
104 if ( ! sigA ) goto zero;
105 normExpSig = softfloat_normSubnormalExtF80Sig( sigA );
106 expA += normExpSig.exp;
107 sigA = normExpSig.sig;
108 }
109 if ( ! expB ) expB = 1;
110 if ( ! (sigB & UINT64_C( 0x8000000000000000 )) ) {
111 if ( ! sigB ) goto zero;
112 normExpSig = softfloat_normSubnormalExtF80Sig( sigB );
113 expB += normExpSig.exp;
114 sigB = normExpSig.sig;
115 }
116 /*------------------------------------------------------------------------
117 *------------------------------------------------------------------------*/
118 expZ = expA + expB - 0x3FFE;
119 sig128Z = softfloat_mul64To128( sigA, sigB );
120 if ( sig128Z.v64 < UINT64_C( 0x8000000000000000 ) ) {
121 --expZ;
122 sig128Z =
123 softfloat_add128(
124 sig128Z.v64, sig128Z.v0, sig128Z.v64, sig128Z.v0 );
125 }
126 return
127 softfloat_roundPackToExtF80(
128 signZ, expZ, sig128Z.v64, sig128Z.v0, extF80_roundingPrecision );
129 /*------------------------------------------------------------------------
130 *------------------------------------------------------------------------*/
131 propagateNaN:
132 uiZ = softfloat_propagateNaNExtF80UI( uiA64, uiA0, uiB64, uiB0 );
133 uiZ64 = uiZ.v64;
134 uiZ0 = uiZ.v0;
135 goto uiZ;
136 /*------------------------------------------------------------------------
137 *------------------------------------------------------------------------*/
138 infArg:
139 if ( ! magBits ) {
140 softfloat_raiseFlags( softfloat_flag_invalid );
141 uiZ64 = defaultNaNExtF80UI64;
142 uiZ0 = defaultNaNExtF80UI0;
143 } else {
144 uiZ64 = packToExtF80UI64( signZ, 0x7FFF );
145 uiZ0 = UINT64_C( 0x8000000000000000 );
146 }
147 goto uiZ;
148 /*------------------------------------------------------------------------
149 *------------------------------------------------------------------------*/
150 zero:
151 uiZ64 = packToExtF80UI64( signZ, 0 );
152 uiZ0 = 0;
153 uiZ:
154 uZ.s.signExp = uiZ64;
155 uZ.s.signif = uiZ0;
156 return uZ.f;
157
158 }
159
160