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
f128_mul(float128_t a,float128_t b)45 float128_t f128_mul( float128_t a, float128_t b )
46 {
47 union ui128_f128 uA;
48 uint_fast64_t uiA64, uiA0;
49 bool signA;
50 int_fast32_t expA;
51 struct uint128 sigA;
52 union ui128_f128 uB;
53 uint_fast64_t uiB64, uiB0;
54 bool signB;
55 int_fast32_t expB;
56 struct uint128 sigB;
57 bool signZ;
58 uint_fast64_t magBits;
59 struct exp32_sig128 normExpSig;
60 int_fast32_t expZ;
61 uint64_t sig256Z[4];
62 uint_fast64_t sigZExtra;
63 struct uint128 sigZ;
64 struct uint128_extra sig128Extra;
65 struct uint128 uiZ;
66 union ui128_f128 uZ;
67
68 /*------------------------------------------------------------------------
69 *------------------------------------------------------------------------*/
70 uA.f = a;
71 uiA64 = uA.ui.v64;
72 uiA0 = uA.ui.v0;
73 signA = signF128UI64( uiA64 );
74 expA = expF128UI64( uiA64 );
75 sigA.v64 = fracF128UI64( uiA64 );
76 sigA.v0 = uiA0;
77 uB.f = b;
78 uiB64 = uB.ui.v64;
79 uiB0 = uB.ui.v0;
80 signB = signF128UI64( uiB64 );
81 expB = expF128UI64( uiB64 );
82 sigB.v64 = fracF128UI64( uiB64 );
83 sigB.v0 = uiB0;
84 signZ = signA ^ signB;
85 /*------------------------------------------------------------------------
86 *------------------------------------------------------------------------*/
87 if ( expA == 0x7FFF ) {
88 if (
89 (sigA.v64 | sigA.v0) || ((expB == 0x7FFF) && (sigB.v64 | sigB.v0))
90 ) {
91 goto propagateNaN;
92 }
93 magBits = expB | sigB.v64 | sigB.v0;
94 goto infArg;
95 }
96 if ( expB == 0x7FFF ) {
97 if ( sigB.v64 | sigB.v0 ) goto propagateNaN;
98 magBits = expA | sigA.v64 | sigA.v0;
99 goto infArg;
100 }
101 /*------------------------------------------------------------------------
102 *------------------------------------------------------------------------*/
103 if ( ! expA ) {
104 if ( ! (sigA.v64 | sigA.v0) ) goto zero;
105 normExpSig = softfloat_normSubnormalF128Sig( sigA.v64, sigA.v0 );
106 expA = normExpSig.exp;
107 sigA = normExpSig.sig;
108 }
109 if ( ! expB ) {
110 if ( ! (sigB.v64 | sigB.v0) ) goto zero;
111 normExpSig = softfloat_normSubnormalF128Sig( sigB.v64, sigB.v0 );
112 expB = normExpSig.exp;
113 sigB = normExpSig.sig;
114 }
115 /*------------------------------------------------------------------------
116 *------------------------------------------------------------------------*/
117 expZ = expA + expB - 0x4000;
118 sigA.v64 |= UINT64_C( 0x0001000000000000 );
119 sigB = softfloat_shortShiftLeft128( sigB.v64, sigB.v0, 16 );
120 softfloat_mul128To256M( sigA.v64, sigA.v0, sigB.v64, sigB.v0, sig256Z );
121 sigZExtra = sig256Z[indexWord( 4, 1 )] | (sig256Z[indexWord( 4, 0 )] != 0);
122 sigZ =
123 softfloat_add128(
124 sig256Z[indexWord( 4, 3 )], sig256Z[indexWord( 4, 2 )],
125 sigA.v64, sigA.v0
126 );
127 if ( UINT64_C( 0x0002000000000000 ) <= sigZ.v64 ) {
128 ++expZ;
129 sig128Extra =
130 softfloat_shortShiftRightJam128Extra(
131 sigZ.v64, sigZ.v0, sigZExtra, 1 );
132 sigZ = sig128Extra.v;
133 sigZExtra = sig128Extra.extra;
134 }
135 return
136 softfloat_roundPackToF128( signZ, expZ, sigZ.v64, sigZ.v0, sigZExtra );
137 /*------------------------------------------------------------------------
138 *------------------------------------------------------------------------*/
139 propagateNaN:
140 uiZ = softfloat_propagateNaNF128UI( uiA64, uiA0, uiB64, uiB0 );
141 goto uiZ;
142 /*------------------------------------------------------------------------
143 *------------------------------------------------------------------------*/
144 infArg:
145 if ( ! magBits ) {
146 softfloat_raiseFlags( softfloat_flag_invalid );
147 uiZ.v64 = defaultNaNF128UI64;
148 uiZ.v0 = defaultNaNF128UI0;
149 goto uiZ;
150 }
151 uiZ.v64 = packToF128UI64( signZ, 0x7FFF, 0 );
152 goto uiZ0;
153 /*------------------------------------------------------------------------
154 *------------------------------------------------------------------------*/
155 zero:
156 uiZ.v64 = packToF128UI64( signZ, 0, 0 );
157 uiZ0:
158 uiZ.v0 = 0;
159 uiZ:
160 uZ.ui = uiZ;
161 return uZ.f;
162
163 }
164
165