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, 2015 The Regents of the University of
9 California.  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_roundPackMToExtF80M(bool sign,int32_t exp,uint32_t * extSigPtr,uint_fast8_t roundingPrecision,struct extFloat80M * zSPtr)45  softfloat_roundPackMToExtF80M(
46      bool sign,
47      int32_t exp,
48      uint32_t *extSigPtr,
49      uint_fast8_t roundingPrecision,
50      struct extFloat80M *zSPtr
51  )
52 {
53     uint_fast8_t roundingMode;
54     bool roundNearEven;
55     uint64_t sig, roundIncrement, roundMask, roundBits;
56     bool isTiny;
57     uint32_t sigExtra;
58     bool doIncrement;
59 
60     /*------------------------------------------------------------------------
61     *------------------------------------------------------------------------*/
62     roundingMode = softfloat_roundingMode;
63     roundNearEven = (roundingMode == softfloat_round_near_even);
64     sig =
65         (uint64_t) extSigPtr[indexWord( 3, 2 )]<<32
66             | extSigPtr[indexWord( 3, 1 )];
67     if ( roundingPrecision == 80 ) goto precision80;
68     if ( roundingPrecision == 64 ) {
69         roundIncrement = UINT64_C( 0x0000000000000400 );
70         roundMask = UINT64_C( 0x00000000000007FF );
71     } else if ( roundingPrecision == 32 ) {
72         roundIncrement = UINT64_C( 0x0000008000000000 );
73         roundMask = UINT64_C( 0x000000FFFFFFFFFF );
74     } else {
75         goto precision80;
76     }
77     /*------------------------------------------------------------------------
78     *------------------------------------------------------------------------*/
79     if ( extSigPtr[indexWordLo( 3 )] ) sig |= 1;
80     if ( ! roundNearEven && (roundingMode != softfloat_round_near_maxMag) ) {
81         roundIncrement =
82             (roundingMode
83                  == (sign ? softfloat_round_min : softfloat_round_max))
84                 ? roundMask
85                 : 0;
86     }
87     roundBits = sig & roundMask;
88     /*------------------------------------------------------------------------
89     *------------------------------------------------------------------------*/
90     if ( 0x7FFD <= (uint32_t) (exp - 1) ) {
91         if ( exp <= 0 ) {
92             isTiny =
93                    (softfloat_detectTininess
94                         == softfloat_tininess_beforeRounding)
95                 || (exp < 0)
96                 || (sig <= (uint64_t) (sig + roundIncrement));
97             sig = softfloat_shiftRightJam64( sig, 1 - exp );
98             roundBits = sig & roundMask;
99             if ( roundBits ) {
100                 if ( isTiny ) softfloat_raiseFlags( softfloat_flag_underflow );
101                 softfloat_exceptionFlags |= softfloat_flag_inexact;
102             }
103             sig += roundIncrement;
104             exp = ((sig & UINT64_C( 0x8000000000000000 )) != 0);
105             roundIncrement = roundMask + 1;
106             if ( roundNearEven && (roundBits<<1 == roundIncrement) ) {
107                 roundMask |= roundIncrement;
108             }
109             sig &= ~roundMask;
110             goto packReturn;
111         }
112         if (
113                (0x7FFE < exp)
114             || ((exp == 0x7FFE) && ((uint64_t) (sig + roundIncrement) < sig))
115         ) {
116             goto overflow;
117         }
118     }
119     /*------------------------------------------------------------------------
120     *------------------------------------------------------------------------*/
121     if ( roundBits ) softfloat_exceptionFlags |= softfloat_flag_inexact;
122     sig += roundIncrement;
123     if ( sig < roundIncrement ) {
124         ++exp;
125         sig = UINT64_C( 0x8000000000000000 );
126     }
127     roundIncrement = roundMask + 1;
128     if ( roundNearEven && (roundBits<<1 == roundIncrement) ) {
129         roundMask |= roundIncrement;
130     }
131     sig &= ~roundMask;
132     goto packReturn;
133     /*------------------------------------------------------------------------
134     *------------------------------------------------------------------------*/
135  precision80:
136     sigExtra = extSigPtr[indexWordLo( 3 )];
137     doIncrement = (0x80000000 <= sigExtra);
138     if ( ! roundNearEven && (roundingMode != softfloat_round_near_maxMag) ) {
139         doIncrement =
140             (roundingMode
141                  == (sign ? softfloat_round_min : softfloat_round_max))
142                 && sigExtra;
143     }
144     /*------------------------------------------------------------------------
145     *------------------------------------------------------------------------*/
146     if ( 0x7FFD <= (uint32_t) (exp - 1) ) {
147         /*--------------------------------------------------------------------
148         *--------------------------------------------------------------------*/
149         if ( exp <= 0 ) {
150             isTiny =
151                    (softfloat_detectTininess
152                         == softfloat_tininess_beforeRounding)
153                 || (exp < 0)
154                 || ! doIncrement
155                 || (sig < UINT64_C( 0xFFFFFFFFFFFFFFFF ));
156             softfloat_shiftRightJam96M( extSigPtr, 1 - exp, extSigPtr );
157             sigExtra = extSigPtr[indexWordLo( 3 )];
158             if ( sigExtra ) {
159                 if ( isTiny ) softfloat_raiseFlags( softfloat_flag_underflow );
160                 softfloat_exceptionFlags |= softfloat_flag_inexact;
161             }
162             doIncrement = (0x80000000 <= sigExtra);
163             if (
164                    ! roundNearEven
165                 && (roundingMode != softfloat_round_near_maxMag)
166             ) {
167                 doIncrement =
168                     (roundingMode
169                          == (sign ? softfloat_round_min : softfloat_round_max))
170                         && sigExtra;
171             }
172             exp = 0;
173             sig =
174                 (uint64_t) extSigPtr[indexWord( 3, 2 )]<<32
175                     | extSigPtr[indexWord( 3, 1 )];
176             if ( doIncrement ) {
177                 ++sig;
178                 sig &= ~(uint64_t) (! (sigExtra & 0x7FFFFFFF) & roundNearEven);
179                 exp = ((sig & UINT64_C( 0x8000000000000000 )) != 0);
180             }
181             goto packReturn;
182         }
183         /*--------------------------------------------------------------------
184         *--------------------------------------------------------------------*/
185         if (
186                (0x7FFE < exp)
187             || ((exp == 0x7FFE) && (sig == UINT64_C( 0xFFFFFFFFFFFFFFFF ))
188                     && doIncrement)
189         ) {
190             roundMask = 0;
191  overflow:
192             softfloat_raiseFlags(
193                 softfloat_flag_overflow | softfloat_flag_inexact );
194             if (
195                    roundNearEven
196                 || (roundingMode == softfloat_round_near_maxMag)
197                 || (roundingMode
198                         == (sign ? softfloat_round_min : softfloat_round_max))
199             ) {
200                 exp = 0x7FFF;
201                 sig = UINT64_C( 0x8000000000000000 );
202             } else {
203                 exp = 0x7FFE;
204                 sig = ~roundMask;
205             }
206             goto packReturn;
207         }
208     }
209     /*------------------------------------------------------------------------
210     *------------------------------------------------------------------------*/
211     if ( sigExtra ) softfloat_exceptionFlags |= softfloat_flag_inexact;
212     if ( doIncrement ) {
213         ++sig;
214         if ( ! sig ) {
215             ++exp;
216             sig = UINT64_C( 0x8000000000000000 );
217         } else {
218             sig &= ~(uint64_t) (! (sigExtra & 0x7FFFFFFF) & roundNearEven);
219         }
220     }
221     /*------------------------------------------------------------------------
222     *------------------------------------------------------------------------*/
223  packReturn:
224     zSPtr->signExp = packToExtF80UI64( sign, exp );
225     zSPtr->signif = sig;
226 
227 }
228 
229