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 float128_t
softfloat_roundPackToF128(bool sign,int_fast32_t exp,uint_fast64_t sig64,uint_fast64_t sig0,uint_fast64_t sigExtra)45  softfloat_roundPackToF128(
46      bool sign,
47      int_fast32_t exp,
48      uint_fast64_t sig64,
49      uint_fast64_t sig0,
50      uint_fast64_t sigExtra
51  )
52 {
53     uint_fast8_t roundingMode;
54     bool roundNearEven, doIncrement, isTiny;
55     struct uint128_extra sig128Extra;
56     uint_fast64_t uiZ64, uiZ0;
57     struct uint128 sig128;
58     union ui128_f128 uZ;
59 
60     roundingMode = softfloat_roundingMode;
61     roundNearEven = (roundingMode == softfloat_round_near_even);
62     doIncrement = (UINT64_C( 0x8000000000000000 ) <= sigExtra);
63     if ( ! roundNearEven && (roundingMode != softfloat_round_near_maxMag) ) {
64         doIncrement =
65             (roundingMode
66                  == (sign ? softfloat_round_min : softfloat_round_max))
67                 && sigExtra;
68     }
69     if ( 0x7FFD <= (uint32_t) exp ) {
70         if ( exp < 0 ) {
71             isTiny =
72                    (softfloat_detectTininess
73                         == softfloat_tininess_beforeRounding)
74                 || (exp < -1)
75                 || ! doIncrement
76                 || softfloat_lt128(
77                        sig64,
78                        sig0,
79                        UINT64_C( 0x0001FFFFFFFFFFFF ),
80                        UINT64_C( 0xFFFFFFFFFFFFFFFF )
81                    );
82             sig128Extra =
83                 softfloat_shiftRightJam128Extra( sig64, sig0, sigExtra, -exp );
84             sig64 = sig128Extra.v.v64;
85             sig0  = sig128Extra.v.v0;
86             sigExtra = sig128Extra.extra;
87             exp = 0;
88             if ( isTiny && sigExtra ) {
89                 softfloat_raiseFlags( softfloat_flag_underflow );
90             }
91             doIncrement = (UINT64_C( 0x8000000000000000 ) <= sigExtra);
92             if (
93                    ! roundNearEven
94                 && (roundingMode != softfloat_round_near_maxMag)
95             ) {
96                 doIncrement =
97                     (roundingMode
98                          == (sign ? softfloat_round_min : softfloat_round_max))
99                         && sigExtra;
100             }
101         } else if (
102                (0x7FFD < exp)
103             || ((exp == 0x7FFD)
104                     && softfloat_eq128(
105                            sig64,
106                            sig0,
107                            UINT64_C( 0x0001FFFFFFFFFFFF ),
108                            UINT64_C( 0xFFFFFFFFFFFFFFFF )
109                        )
110                     && doIncrement)
111         ) {
112             softfloat_raiseFlags(
113                 softfloat_flag_overflow | softfloat_flag_inexact );
114             if (
115                    roundNearEven
116                 || (roundingMode == softfloat_round_near_maxMag)
117                 || (roundingMode
118                         == (sign ? softfloat_round_min : softfloat_round_max))
119             ) {
120                 uiZ64 = packToF128UI64( sign, 0x7FFF, 0 );
121                 uiZ0  = 0;
122             } else {
123                 uiZ64 =
124                     packToF128UI64(
125                         sign, 0x7FFE, UINT64_C( 0x0000FFFFFFFFFFFF ) );
126                 uiZ0 = UINT64_C( 0xFFFFFFFFFFFFFFFF );
127             }
128             goto uiZ;
129         }
130     }
131     if ( sigExtra ) softfloat_exceptionFlags |= softfloat_flag_inexact;
132     if ( doIncrement ) {
133         sig128 = softfloat_add128( sig64, sig0, 0, 1 );
134         sig64 = sig128.v64;
135         sig0 =
136             sig128.v0
137                 & ~(uint64_t)
138                        (! (sigExtra & UINT64_C( 0x7FFFFFFFFFFFFFFF ))
139                             & roundNearEven);
140     } else {
141         if ( ! (sig64 | sig0) ) exp = 0;
142     }
143     uiZ64 = packToF128UI64( sign, exp, sig64 );
144     uiZ0  = sig0;
145  uiZ:
146     uZ.ui.v64 = uiZ64;
147     uZ.ui.v0  = uiZ0;
148     return uZ.f;
149 
150 }
151 
152