1
2/*============================================================================
3
4This C source file is part of the SoftFloat IEEE Floating-Point Arithmetic
5Package, Release 3e, by John R. Hauser.
6
7Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the
8University of California.  All rights reserved.
9
10Redistribution and use in source and binary forms, with or without
11modification, 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
24THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
25EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
27DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
28DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31ON 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
33SOFTWARE, 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
44float16_t softfloat_subMagsF16( uint_fast16_t uiA, uint_fast16_t uiB )
45{
46    int_fast8_t expA;
47    uint_fast16_t sigA;
48    int_fast8_t expB;
49    uint_fast16_t sigB;
50    int_fast8_t expDiff;
51    uint_fast16_t uiZ;
52    int_fast16_t sigDiff;
53    bool signZ;
54    int_fast8_t shiftDist, expZ;
55    uint_fast16_t sigZ, sigX, sigY;
56    uint_fast32_t sig32Z;
57    int_fast8_t roundingMode;
58    union ui16_f16 uZ;
59
60    /*------------------------------------------------------------------------
61    *------------------------------------------------------------------------*/
62    expA = expF16UI( uiA );
63    sigA = fracF16UI( uiA );
64    expB = expF16UI( uiB );
65    sigB = fracF16UI( uiB );
66    /*------------------------------------------------------------------------
67    *------------------------------------------------------------------------*/
68    expDiff = expA - expB;
69    if ( ! expDiff ) {
70        /*--------------------------------------------------------------------
71        *--------------------------------------------------------------------*/
72        if ( expA == 0x1F ) {
73            if ( sigA | sigB ) goto propagateNaN;
74            softfloat_raiseFlags( softfloat_flag_invalid );
75            uiZ = defaultNaNF16UI;
76            goto uiZ;
77        }
78        sigDiff = sigA - sigB;
79        if ( ! sigDiff ) {
80            uiZ =
81                packToF16UI(
82                    (softfloat_roundingMode == softfloat_round_min), 0, 0 );
83            goto uiZ;
84        }
85        if ( expA ) --expA;
86        signZ = signF16UI( uiA );
87        if ( sigDiff < 0 ) {
88            signZ = ! signZ;
89            sigDiff = -sigDiff;
90        }
91        shiftDist = softfloat_countLeadingZeros16( sigDiff ) - 5;
92        expZ = expA - shiftDist;
93        if ( expZ < 0 ) {
94            shiftDist = expA;
95            expZ = 0;
96        }
97        sigZ = sigDiff<<shiftDist;
98        goto pack;
99    } else {
100        /*--------------------------------------------------------------------
101        *--------------------------------------------------------------------*/
102        signZ = signF16UI( uiA );
103        if ( expDiff < 0 ) {
104            /*----------------------------------------------------------------
105            *----------------------------------------------------------------*/
106            signZ = ! signZ;
107            if ( expB == 0x1F ) {
108                if ( sigB ) goto propagateNaN;
109                uiZ = packToF16UI( signZ, 0x1F, 0 );
110                goto uiZ;
111            }
112            if ( expDiff <= -13 ) {
113                uiZ = packToF16UI( signZ, expB, sigB );
114                if ( expA | sigA ) goto subEpsilon;
115                goto uiZ;
116            }
117            expZ = expA + 19;
118            sigX = sigB | 0x0400;
119            sigY = sigA + (expA ? 0x0400 : sigA);
120            expDiff = -expDiff;
121        } else {
122            /*----------------------------------------------------------------
123            *----------------------------------------------------------------*/
124            uiZ = uiA;
125            if ( expA == 0x1F ) {
126                if ( sigA ) goto propagateNaN;
127                goto uiZ;
128            }
129            if ( 13 <= expDiff ) {
130                if ( expB | sigB ) goto subEpsilon;
131                goto uiZ;
132            }
133            expZ = expB + 19;
134            sigX = sigA | 0x0400;
135            sigY = sigB + (expB ? 0x0400 : sigB);
136        }
137        sig32Z = ((uint_fast32_t) sigX<<expDiff) - sigY;
138        shiftDist = softfloat_countLeadingZeros32( sig32Z ) - 1;
139        sig32Z <<= shiftDist;
140        expZ -= shiftDist;
141        sigZ = sig32Z>>16;
142        if ( sig32Z & 0xFFFF ) {
143            sigZ |= 1;
144        } else {
145            if ( ! (sigZ & 0xF) && ((unsigned int) expZ < 0x1E) ) {
146                sigZ >>= 4;
147                goto pack;
148            }
149        }
150        return softfloat_roundPackToF16( signZ, expZ, sigZ );
151    }
152    /*------------------------------------------------------------------------
153    *------------------------------------------------------------------------*/
154 propagateNaN:
155    uiZ = softfloat_propagateNaNF16UI( uiA, uiB );
156    goto uiZ;
157    /*------------------------------------------------------------------------
158    *------------------------------------------------------------------------*/
159 subEpsilon:
160    roundingMode = softfloat_roundingMode;
161    if ( roundingMode != softfloat_round_near_even ) {
162        if (
163            (roundingMode == softfloat_round_minMag)
164                || (roundingMode
165                        == (signF16UI( uiZ ) ? softfloat_round_max
166                                : softfloat_round_min))
167        ) {
168            --uiZ;
169        }
170#ifdef SOFTFLOAT_ROUND_ODD
171        else if ( roundingMode == softfloat_round_odd ) {
172            uiZ = (uiZ - 1) | 1;
173        }
174#endif
175    }
176    softfloat_raiseFlags( softfloat_flag_inexact );
177    goto uiZ;
178    /*------------------------------------------------------------------------
179    *------------------------------------------------------------------------*/
180 pack:
181    uiZ = packToF16UI( signZ, expZ, sigZ );
182 uiZ:
183    uZ.ui = uiZ;
184    return uZ.f;
185
186}
187
188