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 
45 #ifdef SOFTFLOAT_FAST_INT64
46 
47 void
f128M_mul(const float128_t * aPtr,const float128_t * bPtr,float128_t * zPtr)48  f128M_mul( const float128_t *aPtr, const float128_t *bPtr, float128_t *zPtr )
49 {
50 
51     *zPtr = f128_mul( *aPtr, *bPtr );
52 
53 }
54 
55 #else
56 
57 void
f128M_mul(const float128_t * aPtr,const float128_t * bPtr,float128_t * zPtr)58  f128M_mul( const float128_t *aPtr, const float128_t *bPtr, float128_t *zPtr )
59 {
60     const uint32_t *aWPtr, *bWPtr;
61     uint32_t *zWPtr;
62     uint32_t uiA96;
63     int32_t expA;
64     uint32_t uiB96;
65     int32_t expB;
66     bool signZ;
67     const uint32_t *ptr;
68     uint32_t uiZ96, sigA[4];
69     uint_fast8_t shiftCount;
70     uint32_t sigB[4];
71     int32_t expZ;
72     uint32_t sigProd[8], *extSigZPtr;
73 
74     /*------------------------------------------------------------------------
75     *------------------------------------------------------------------------*/
76     aWPtr = (const uint32_t *) aPtr;
77     bWPtr = (const uint32_t *) bPtr;
78     zWPtr = (uint32_t *) zPtr;
79     /*------------------------------------------------------------------------
80     *------------------------------------------------------------------------*/
81     uiA96 = aWPtr[indexWordHi( 4 )];
82     expA = expF128UI96( uiA96 );
83     uiB96 = bWPtr[indexWordHi( 4 )];
84     expB = expF128UI96( uiB96 );
85     signZ = signF128UI96( uiA96 ) ^ signF128UI96( uiB96 );
86     /*------------------------------------------------------------------------
87     *------------------------------------------------------------------------*/
88     if ( (expA == 0x7FFF) || (expB == 0x7FFF) ) {
89         if ( softfloat_tryPropagateNaNF128M( aWPtr, bWPtr, zWPtr ) ) return;
90         ptr = aWPtr;
91         if ( ! expA ) goto possiblyInvalid;
92         if ( ! expB ) {
93             ptr = bWPtr;
94      possiblyInvalid:
95             if (
96                 ! fracF128UI96( ptr[indexWordHi( 4 )] )
97                     && ! (ptr[indexWord( 4, 2 )] | ptr[indexWord( 4, 1 )]
98                               | ptr[indexWord( 4, 0 )])
99             ) {
100                 softfloat_invalidF128M( zWPtr );
101                 return;
102             }
103         }
104         uiZ96 = packToF128UI96( signZ, 0x7FFF, 0 );
105         goto uiZ96;
106     }
107     /*------------------------------------------------------------------------
108     *------------------------------------------------------------------------*/
109     if ( expA ) {
110         sigA[indexWordHi( 4 )] = fracF128UI96( uiA96 ) | 0x00010000;
111         sigA[indexWord( 4, 2 )] = aWPtr[indexWord( 4, 2 )];
112         sigA[indexWord( 4, 1 )] = aWPtr[indexWord( 4, 1 )];
113         sigA[indexWord( 4, 0 )] = aWPtr[indexWord( 4, 0 )];
114     } else {
115         expA = softfloat_shiftNormSigF128M( aWPtr, 0, sigA );
116         if ( expA == -128 ) goto zero;
117     }
118     if ( expB ) {
119         sigB[indexWordHi( 4 )] = fracF128UI96( uiB96 ) | 0x00010000;
120         sigB[indexWord( 4, 2 )] = bWPtr[indexWord( 4, 2 )];
121         sigB[indexWord( 4, 1 )] = bWPtr[indexWord( 4, 1 )];
122         sigB[indexWord( 4, 0 )] = bWPtr[indexWord( 4, 0 )];
123     } else {
124         expB = softfloat_shiftNormSigF128M( bWPtr, 0, sigB );
125         if ( expB == -128 ) goto zero;
126     }
127     /*------------------------------------------------------------------------
128     *------------------------------------------------------------------------*/
129     expZ = expA + expB - 0x4000;
130     softfloat_mul128MTo256M( sigA, sigB, sigProd );
131     if (
132         sigProd[indexWord( 8, 2 )]
133             || (sigProd[indexWord( 8, 1 )] | sigProd[indexWord( 8, 0 )])
134     ) {
135         sigProd[indexWord( 8, 3 )] |= 1;
136     }
137     extSigZPtr = &sigProd[indexMultiwordHi( 8, 5 )];
138     shiftCount = 16;
139     if ( extSigZPtr[indexWordHi( 5 )] & 2 ) {
140         ++expZ;
141         shiftCount = 15;
142     }
143     softfloat_shortShiftLeft160M( extSigZPtr, shiftCount, extSigZPtr );
144     softfloat_roundPackMToF128M( signZ, expZ, extSigZPtr, zWPtr );
145     return;
146     /*------------------------------------------------------------------------
147     *------------------------------------------------------------------------*/
148  zero:
149     uiZ96 = packToF128UI96( signZ, 0, 0 );
150  uiZ96:
151     zWPtr[indexWordHi( 4 )] = uiZ96;
152     zWPtr[indexWord( 4, 2 )] = 0;
153     zWPtr[indexWord( 4, 1 )] = 0;
154     zWPtr[indexWord( 4, 0 )] = 0;
155 
156 }
157 
158 #endif
159 
160