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, 2017 The Regents of the University of
8California.  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 "softfloat.h"
42
43void
44 softfloat_roundPackMToF128M(
45     bool sign, int32_t exp, uint32_t *extSigPtr, uint32_t *zWPtr )
46{
47    uint_fast8_t roundingMode;
48    bool roundNearEven;
49    uint32_t sigExtra;
50    bool doIncrement, isTiny;
51    static const uint32_t maxSig[4] =
52        INIT_UINTM4( 0x0001FFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF );
53    uint32_t ui, uj;
54
55    /*------------------------------------------------------------------------
56    *------------------------------------------------------------------------*/
57    roundingMode = softfloat_roundingMode;
58    roundNearEven = (roundingMode == softfloat_round_near_even);
59    sigExtra = extSigPtr[indexWordLo( 5 )];
60    doIncrement = (0x80000000 <= sigExtra);
61    if ( ! roundNearEven && (roundingMode != softfloat_round_near_maxMag) ) {
62        doIncrement =
63            (roundingMode
64                 == (sign ? softfloat_round_min : softfloat_round_max))
65                && sigExtra;
66    }
67    /*------------------------------------------------------------------------
68    *------------------------------------------------------------------------*/
69    if ( 0x7FFD <= (uint32_t) exp ) {
70        if ( exp < 0 ) {
71            /*----------------------------------------------------------------
72            *----------------------------------------------------------------*/
73            isTiny =
74                   (softfloat_detectTininess
75                        == softfloat_tininess_beforeRounding)
76                || (exp < -1)
77                || ! doIncrement
78                || (softfloat_compare128M(
79                        extSigPtr + indexMultiwordHi( 5, 4 ), maxSig )
80                        < 0);
81            softfloat_shiftRightJam160M( extSigPtr, -exp, extSigPtr );
82            exp = 0;
83            sigExtra = extSigPtr[indexWordLo( 5 )];
84            if ( isTiny && sigExtra ) {
85                softfloat_raiseFlags( softfloat_flag_underflow );
86            }
87            doIncrement = (0x80000000 <= sigExtra);
88            if (
89                   ! roundNearEven
90                && (roundingMode != softfloat_round_near_maxMag)
91            ) {
92                doIncrement =
93                    (roundingMode
94                         == (sign ? softfloat_round_min : softfloat_round_max))
95                        && sigExtra;
96            }
97        } else if (
98               (0x7FFD < exp)
99            || ((exp == 0x7FFD) && doIncrement
100                    && (softfloat_compare128M(
101                            extSigPtr + indexMultiwordHi( 5, 4 ), maxSig )
102                            == 0))
103        ) {
104            /*----------------------------------------------------------------
105            *----------------------------------------------------------------*/
106            softfloat_raiseFlags(
107                softfloat_flag_overflow | softfloat_flag_inexact );
108            if (
109                   roundNearEven
110                || (roundingMode == softfloat_round_near_maxMag)
111                || (roundingMode
112                        == (sign ? softfloat_round_min : softfloat_round_max))
113            ) {
114                ui = packToF128UI96( sign, 0x7FFF, 0 );
115                uj = 0;
116            } else {
117                ui = packToF128UI96( sign, 0x7FFE, 0x0000FFFF );
118                uj = 0xFFFFFFFF;
119            }
120            zWPtr[indexWordHi( 4 )] = ui;
121            zWPtr[indexWord( 4, 2 )] = uj;
122            zWPtr[indexWord( 4, 1 )] = uj;
123            zWPtr[indexWord( 4, 0 )] = uj;
124            return;
125        }
126    }
127    /*------------------------------------------------------------------------
128    *------------------------------------------------------------------------*/
129    uj = extSigPtr[indexWord( 5, 1 )];
130    if ( sigExtra ) {
131        softfloat_raiseFlags( softfloat_flag_inexact );
132#ifdef SOFTFLOAT_ROUND_ODD
133        if ( roundingMode == softfloat_round_odd ) {
134            uj |= 1;
135            goto noIncrementPackReturn;
136        }
137#endif
138    }
139    if ( doIncrement ) {
140        ++uj;
141        if ( uj ) {
142            if ( ! (sigExtra & 0x7FFFFFFF) && roundNearEven ) uj &= ~1;
143            zWPtr[indexWord( 4, 2 )] = extSigPtr[indexWord( 5, 3 )];
144            zWPtr[indexWord( 4, 1 )] = extSigPtr[indexWord( 5, 2 )];
145            zWPtr[indexWord( 4, 0 )] = uj;
146            ui = extSigPtr[indexWordHi( 5 )];
147        } else {
148            zWPtr[indexWord( 4, 0 )] = uj;
149            ui = extSigPtr[indexWord( 5, 2 )] + 1;
150            zWPtr[indexWord( 4, 1 )] = ui;
151            uj = extSigPtr[indexWord( 5, 3 )];
152            if ( ui ) {
153                zWPtr[indexWord( 4, 2 )] = uj;
154                ui = extSigPtr[indexWordHi( 5 )];
155            } else {
156                ++uj;
157                zWPtr[indexWord( 4, 2 )] = uj;
158                ui = extSigPtr[indexWordHi( 5 )];
159                if ( ! uj ) ++ui;
160            }
161        }
162    } else {
163 noIncrementPackReturn:
164        zWPtr[indexWord( 4, 0 )] = uj;
165        ui = extSigPtr[indexWord( 5, 2 )];
166        zWPtr[indexWord( 4, 1 )] = ui;
167        uj |= ui;
168        ui = extSigPtr[indexWord( 5, 3 )];
169        zWPtr[indexWord( 4, 2 )] = ui;
170        uj |= ui;
171        ui = extSigPtr[indexWordHi( 5 )];
172        uj |= ui;
173        if ( ! uj ) exp = 0;
174    }
175    zWPtr[indexWordHi( 4 )] = packToF128UI96( sign, exp, ui );
176
177}
178
179