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