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 
f64_mul(float64_t a,float64_t b)45 float64_t f64_mul( float64_t a, float64_t b )
46 {
47     union ui64_f64 uA;
48     uint_fast64_t uiA;
49     bool signA;
50     int_fast16_t expA;
51     uint_fast64_t sigA;
52     union ui64_f64 uB;
53     uint_fast64_t uiB;
54     bool signB;
55     int_fast16_t expB;
56     uint_fast64_t sigB;
57     bool signZ;
58     uint_fast64_t magBits;
59     struct exp16_sig64 normExpSig;
60     int_fast16_t expZ;
61 #ifdef SOFTFLOAT_FAST_INT64
62     struct uint128 sig128Z;
63 #else
64     uint32_t sig128Z[4];
65 #endif
66     uint_fast64_t sigZ, uiZ;
67     union ui64_f64 uZ;
68 
69     /*------------------------------------------------------------------------
70     *------------------------------------------------------------------------*/
71     uA.f = a;
72     uiA = uA.ui;
73     signA = signF64UI( uiA );
74     expA  = expF64UI( uiA );
75     sigA  = fracF64UI( uiA );
76     uB.f = b;
77     uiB = uB.ui;
78     signB = signF64UI( uiB );
79     expB  = expF64UI( uiB );
80     sigB  = fracF64UI( uiB );
81     signZ = signA ^ signB;
82     /*------------------------------------------------------------------------
83     *------------------------------------------------------------------------*/
84     if ( expA == 0x7FF ) {
85         if ( sigA || ((expB == 0x7FF) && sigB) ) goto propagateNaN;
86         magBits = expB | sigB;
87         goto infArg;
88     }
89     if ( expB == 0x7FF ) {
90         if ( sigB ) goto propagateNaN;
91         magBits = expA | sigA;
92         goto infArg;
93     }
94     /*------------------------------------------------------------------------
95     *------------------------------------------------------------------------*/
96     if ( ! expA ) {
97         if ( ! sigA ) goto zero;
98         normExpSig = softfloat_normSubnormalF64Sig( sigA );
99         expA = normExpSig.exp;
100         sigA = normExpSig.sig;
101     }
102     if ( ! expB ) {
103         if ( ! sigB ) goto zero;
104         normExpSig = softfloat_normSubnormalF64Sig( sigB );
105         expB = normExpSig.exp;
106         sigB = normExpSig.sig;
107     }
108     /*------------------------------------------------------------------------
109     *------------------------------------------------------------------------*/
110     expZ = expA + expB - 0x3FF;
111     sigA = (sigA | UINT64_C( 0x0010000000000000 ))<<10;
112     sigB = (sigB | UINT64_C( 0x0010000000000000 ))<<11;
113 #ifdef SOFTFLOAT_FAST_INT64
114     sig128Z = softfloat_mul64To128( sigA, sigB );
115     sigZ = sig128Z.v64 | (sig128Z.v0 != 0);
116 #else
117     softfloat_mul64To128M( sigA, sigB, sig128Z );
118     sigZ =
119         (uint64_t) sig128Z[indexWord( 4, 3 )]<<32 | sig128Z[indexWord( 4, 2 )];
120     if ( sig128Z[indexWord( 4, 1 )] || sig128Z[indexWord( 4, 0 )] ) sigZ |= 1;
121 #endif
122     if ( sigZ < UINT64_C( 0x4000000000000000 ) ) {
123         --expZ;
124         sigZ <<= 1;
125     }
126     return softfloat_roundPackToF64( signZ, expZ, sigZ );
127     /*------------------------------------------------------------------------
128     *------------------------------------------------------------------------*/
129  propagateNaN:
130     uiZ = softfloat_propagateNaNF64UI( uiA, uiB );
131     goto uiZ;
132     /*------------------------------------------------------------------------
133     *------------------------------------------------------------------------*/
134  infArg:
135     if ( ! magBits ) {
136         softfloat_raiseFlags( softfloat_flag_invalid );
137         uiZ = defaultNaNF64UI;
138     } else {
139         uiZ = packToF64UI( signZ, 0x7FF, 0 );
140     }
141     goto uiZ;
142     /*------------------------------------------------------------------------
143     *------------------------------------------------------------------------*/
144  zero:
145     uiZ = packToF64UI( signZ, 0, 0 );
146  uiZ:
147     uZ.ui = uiZ;
148     return uZ.f;
149 
150 }
151 
152