xref: /optee_os/lib/libutils/isoc/arch/arm/softfloat/source/s_roundPackMToF128M.c (revision 9403c583381528e7fb391e3769644cc9653cfbb6)
1 
2 /*============================================================================
3 
4 This C source file is part of the SoftFloat IEEE Floating-Point Arithmetic
5 Package, Release 3a, by John R. Hauser.
6 
7 Copyright 2011, 2012, 2013, 2014 The Regents of the University of California.
8 All rights reserved.
9 
10 Redistribution and use in source and binary forms, with or without
11 modification, 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 
24 THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY
25 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE
27 DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
28 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 ON 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
33 SOFTWARE, 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 
43 void
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     roundingMode = softfloat_roundingMode;
56     roundNearEven = (roundingMode == softfloat_round_near_even);
57     sigExtra = extSigPtr[indexWordLo( 5 )];
58     doIncrement = (0x80000000 <= sigExtra);
59     if ( ! roundNearEven && (roundingMode != softfloat_round_near_maxMag) ) {
60         doIncrement =
61             (roundingMode
62                  == (sign ? softfloat_round_min : softfloat_round_max))
63                 && sigExtra;
64     }
65     if ( 0x7FFD <= (uint32_t) exp ) {
66         if ( exp < 0 ) {
67             isTiny =
68                    (softfloat_detectTininess
69                         == softfloat_tininess_beforeRounding)
70                 || (exp < -1)
71                 || ! doIncrement
72                 || (softfloat_compare128M(
73                         extSigPtr + indexMultiwordHi( 5, 4 ), maxSig )
74                         < 0);
75             softfloat_shiftRightJam160M( extSigPtr, -exp, extSigPtr );
76             exp = 0;
77             sigExtra = extSigPtr[indexWordLo( 5 )];
78             if ( isTiny && sigExtra ) {
79                 softfloat_raiseFlags( softfloat_flag_underflow );
80             }
81             doIncrement = (0x80000000 <= sigExtra);
82             if (
83                    ! roundNearEven
84                 && (roundingMode != softfloat_round_near_maxMag)
85             ) {
86                 doIncrement =
87                     (roundingMode
88                          == (sign ? softfloat_round_min : softfloat_round_max))
89                         && sigExtra;
90             }
91         } else if (
92                (0x7FFD < exp)
93             || ((exp == 0x7FFD) && doIncrement
94                     && (softfloat_compare128M(
95                             extSigPtr + indexMultiwordHi( 5, 4 ), maxSig )
96                             == 0))
97         ) {
98             softfloat_raiseFlags(
99                 softfloat_flag_overflow | softfloat_flag_inexact );
100             if (
101                    roundNearEven
102                 || (roundingMode == softfloat_round_near_maxMag)
103                 || (roundingMode
104                         == (sign ? softfloat_round_min : softfloat_round_max))
105             ) {
106                 ui = packToF128UI96( sign, 0x7FFF, 0 );
107                 uj = 0;
108             } else {
109                 ui = packToF128UI96( sign, 0x7FFE, 0x0000FFFF );
110                 uj = 0xFFFFFFFF;
111             }
112             zWPtr[indexWordHi( 4 )] = ui;
113             zWPtr[indexWord( 4, 2 )] = uj;
114             zWPtr[indexWord( 4, 1 )] = uj;
115             zWPtr[indexWord( 4, 0 )] = uj;
116             return;
117         }
118     }
119     if ( sigExtra ) softfloat_exceptionFlags |= softfloat_flag_inexact;
120     uj = extSigPtr[indexWord( 5, 1 )];
121     if ( doIncrement ) {
122         ++uj;
123         if ( uj ) {
124             if ( ! (sigExtra & 0x7FFFFFFF) && roundNearEven ) uj &= ~1;
125             zWPtr[indexWord( 4, 2 )] = extSigPtr[indexWord( 5, 3 )];
126             zWPtr[indexWord( 4, 1 )] = extSigPtr[indexWord( 5, 2 )];
127             zWPtr[indexWord( 4, 0 )] = uj;
128             ui = extSigPtr[indexWordHi( 5 )];
129         } else {
130             zWPtr[indexWord( 4, 0 )] = uj;
131             ui = extSigPtr[indexWord( 5, 2 )] + 1;
132             zWPtr[indexWord( 4, 1 )] = ui;
133             uj = extSigPtr[indexWord( 5, 3 )];
134             if ( ui ) {
135                 zWPtr[indexWord( 4, 2 )] = uj;
136                 ui = extSigPtr[indexWordHi( 5 )];
137             } else {
138                 ++uj;
139                 zWPtr[indexWord( 4, 2 )] = uj;
140                 ui = extSigPtr[indexWordHi( 5 )];
141                 if ( ! uj ) ++ui;
142             }
143         }
144     } else {
145         zWPtr[indexWord( 4, 0 )] = uj;
146         ui = extSigPtr[indexWord( 5, 2 )];
147         zWPtr[indexWord( 4, 1 )] = ui;
148         uj |= ui;
149         ui = extSigPtr[indexWord( 5, 3 )];
150         zWPtr[indexWord( 4, 2 )] = ui;
151         uj |= ui;
152         ui = extSigPtr[indexWordHi( 5 )];
153         uj |= ui;
154         if ( ! uj ) exp = 0;
155     }
156     zWPtr[indexWordHi( 4 )] = packToF128UI96( sign, exp, ui );
157 
158 }
159 
160