xref: /optee_os/lib/libutils/isoc/arch/arm/softfloat/source/s_mulAddF32.c (revision 9403c583381528e7fb391e3769644cc9653cfbb6)
1 
2 /*============================================================================
3 
4 This C source file is part of the SoftFloat IEEE Floating-Point Arithmetic
5 Package, Release 3a, by John R. Hauser.
6 
7 Copyright 2011, 2012, 2013, 2014 The Regents of the University of California.
8 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 float32_t
45  softfloat_mulAddF32(
46      uint_fast32_t uiA, uint_fast32_t uiB, uint_fast32_t uiC, uint_fast8_t op )
47 {
48     bool signA;
49     int_fast16_t expA;
50     uint_fast32_t sigA;
51     bool signB;
52     int_fast16_t expB;
53     uint_fast32_t sigB;
54     bool signC;
55     int_fast16_t expC;
56     uint_fast32_t sigC;
57     bool signProd;
58     uint_fast32_t magBits, uiZ;
59     struct exp16_sig32 normExpSig;
60     int_fast16_t expProd;
61     uint_fast64_t sigProd;
62     bool signZ;
63     int_fast16_t expZ;
64     uint_fast32_t sigZ;
65     int_fast16_t expDiff;
66     uint_fast64_t sig64Z, sig64C;
67     int_fast8_t shiftCount;
68     union ui32_f32 uZ;
69 
70     signA = signF32UI( uiA );
71     expA  = expF32UI( uiA );
72     sigA  = fracF32UI( uiA );
73     signB = signF32UI( uiB );
74     expB  = expF32UI( uiB );
75     sigB  = fracF32UI( uiB );
76     signC = signF32UI( uiC ) ^ (op == softfloat_mulAdd_subC);
77     expC  = expF32UI( uiC );
78     sigC  = fracF32UI( uiC );
79     signProd = signA ^ signB ^ (op == softfloat_mulAdd_subProd);
80     if ( expA == 0xFF ) {
81         if ( sigA || ((expB == 0xFF) && sigB) ) goto propagateNaN_ABC;
82         magBits = expB | sigB;
83         goto infProdArg;
84     }
85     if ( expB == 0xFF ) {
86         if ( sigB ) goto propagateNaN_ABC;
87         magBits = expA | sigA;
88         goto infProdArg;
89     }
90     if ( expC == 0xFF ) {
91         if ( sigC ) {
92             uiZ = 0;
93             goto propagateNaN_ZC;
94         }
95         uiZ = uiC;
96         goto uiZ;
97     }
98     if ( ! expA ) {
99         if ( ! sigA ) goto zeroProd;
100         normExpSig = softfloat_normSubnormalF32Sig( sigA );
101         expA = normExpSig.exp;
102         sigA = normExpSig.sig;
103     }
104     if ( ! expB ) {
105         if ( ! sigB ) goto zeroProd;
106         normExpSig = softfloat_normSubnormalF32Sig( sigB );
107         expB = normExpSig.exp;
108         sigB = normExpSig.sig;
109     }
110     expProd = expA + expB - 0x7E;
111     sigA = (sigA | 0x00800000)<<7;
112     sigB = (sigB | 0x00800000)<<7;
113     sigProd = (uint_fast64_t) sigA * sigB;
114     if ( sigProd < UINT64_C( 0x2000000000000000 ) ) {
115         --expProd;
116         sigProd <<= 1;
117     }
118     signZ = signProd;
119     if ( ! expC ) {
120         if ( ! sigC ) {
121             expZ = expProd - 1;
122             sigZ = softfloat_shortShiftRightJam64( sigProd, 31 );
123             goto roundPack;
124         }
125         normExpSig = softfloat_normSubnormalF32Sig( sigC );
126         expC = normExpSig.exp;
127         sigC = normExpSig.sig;
128     }
129     sigC = (sigC | 0x00800000)<<6;
130     expDiff = expProd - expC;
131     if ( signProd == signC ) {
132         if ( expDiff <= 0 ) {
133             expZ = expC;
134             sigZ = sigC + softfloat_shiftRightJam64( sigProd, 32 - expDiff );
135         } else {
136             expZ = expProd;
137             sig64Z =
138                 sigProd
139                     + softfloat_shiftRightJam64(
140                           (uint_fast64_t) sigC<<32, expDiff );
141             sigZ = softfloat_shortShiftRightJam64( sig64Z, 32 );
142         }
143         if ( sigZ < 0x40000000 ) {
144             --expZ;
145             sigZ <<= 1;
146         }
147     } else {
148         sig64C = (uint_fast64_t) sigC<<32;
149         if ( expDiff < 0 ) {
150             signZ = signC;
151             expZ = expC;
152             sig64Z = sig64C - softfloat_shiftRightJam64( sigProd, -expDiff );
153         } else if ( ! expDiff ) {
154             expZ = expProd;
155             sig64Z = sigProd - sig64C;
156             if ( ! sig64Z ) goto completeCancellation;
157             if ( sig64Z & UINT64_C( 0x8000000000000000 ) ) {
158                 signZ ^= 1;
159                 sig64Z = -sig64Z;
160             }
161         } else {
162             expZ = expProd;
163             sig64Z = sigProd - softfloat_shiftRightJam64( sig64C, expDiff );
164         }
165         shiftCount = softfloat_countLeadingZeros64( sig64Z ) - 1;
166         expZ -= shiftCount;
167         shiftCount -= 32;
168         if ( shiftCount < 0 ) {
169             sigZ = softfloat_shortShiftRightJam64( sig64Z, -shiftCount );
170         } else {
171             sigZ = (uint_fast32_t) sig64Z<<shiftCount;
172         }
173     }
174  roundPack:
175     return softfloat_roundPackToF32( signZ, expZ, sigZ );
176  propagateNaN_ABC:
177     uiZ = softfloat_propagateNaNF32UI( uiA, uiB );
178     goto propagateNaN_ZC;
179  infProdArg:
180     if ( magBits ) {
181         uiZ = packToF32UI( signProd, 0xFF, 0 );
182         if ( expC != 0xFF ) goto uiZ;
183         if ( sigC ) goto propagateNaN_ZC;
184         if ( signProd == signC ) goto uiZ;
185     }
186  invalid:
187     softfloat_raiseFlags( softfloat_flag_invalid );
188     uiZ = defaultNaNF32UI;
189  propagateNaN_ZC:
190     uiZ = softfloat_propagateNaNF32UI( uiZ, uiC );
191     goto uiZ;
192  zeroProd:
193     uiZ = uiC;
194     if ( ! (expC | sigC) && (signProd != signC) ) {
195  completeCancellation:
196         uiZ =
197             packToF32UI( softfloat_roundingMode == softfloat_round_min, 0, 0 );
198     }
199  uiZ:
200     uZ.ui = uiZ;
201     return uZ.f;
202 
203 }
204 
205