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 float64_t
softfloat_subMagsF64(uint_fast64_t uiA,uint_fast64_t uiB,bool signZ)46  softfloat_subMagsF64( uint_fast64_t uiA, uint_fast64_t uiB, bool signZ )
47 {
48     int_fast16_t expA;
49     uint_fast64_t sigA;
50     int_fast16_t expB;
51     uint_fast64_t sigB;
52     int_fast16_t expDiff;
53     uint_fast64_t uiZ;
54     int_fast16_t expZ;
55     uint_fast64_t sigZ;
56     union ui64_f64 uZ;
57 
58     expA = expF64UI( uiA );
59     sigA = fracF64UI( uiA );
60     expB = expF64UI( uiB );
61     sigB = fracF64UI( uiB );
62     expDiff = expA - expB;
63     sigA <<= 10;
64     sigB <<= 10;
65     if ( 0 < expDiff ) goto expABigger;
66     if ( expDiff < 0 ) goto expBBigger;
67     if ( expA == 0x7FF ) {
68         if ( sigA | sigB ) goto propagateNaN;
69         softfloat_raiseFlags( softfloat_flag_invalid );
70         uiZ = defaultNaNF64UI;
71         goto uiZ;
72     }
73     if ( ! expA ) {
74         expA = 1;
75         expB = 1;
76     }
77     if ( sigB < sigA ) goto aBigger;
78     if ( sigA < sigB ) goto bBigger;
79     uiZ = packToF64UI( softfloat_roundingMode == softfloat_round_min, 0, 0 );
80     goto uiZ;
81  expBBigger:
82     if ( expB == 0x7FF ) {
83         if ( sigB ) goto propagateNaN;
84         uiZ = packToF64UI( signZ ^ 1, 0x7FF, 0 );
85         goto uiZ;
86     }
87     sigA += expA ? UINT64_C( 0x4000000000000000 ) : sigA;
88     sigA = softfloat_shiftRightJam64( sigA, -expDiff );
89     sigB |= UINT64_C( 0x4000000000000000 );
90  bBigger:
91     signZ ^= 1;
92     expZ = expB;
93     sigZ = sigB - sigA;
94     goto normRoundPack;
95  expABigger:
96     if ( expA == 0x7FF ) {
97         if ( sigA ) goto propagateNaN;
98         uiZ = uiA;
99         goto uiZ;
100     }
101     sigB += expB ? UINT64_C( 0x4000000000000000 ) : sigB;
102     sigB = softfloat_shiftRightJam64( sigB, expDiff );
103     sigA |= UINT64_C( 0x4000000000000000 );
104  aBigger:
105     expZ = expA;
106     sigZ = sigA - sigB;
107  normRoundPack:
108     return softfloat_normRoundPackToF64( signZ, expZ - 1, sigZ );
109  propagateNaN:
110     uiZ = softfloat_propagateNaNF64UI( uiA, uiB );
111  uiZ:
112     uZ.ui = uiZ;
113     return uZ.f;
114 
115 }
116 
117