xref: /optee_os/lib/libutils/isoc/arch/arm/softfloat/source/extF80M_roundToInt.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 "specialize.h"
42 #include "softfloat.h"
43 
44 #ifdef SOFTFLOAT_FAST_INT64
45 
46 void
47  extF80M_roundToInt(
48      const extFloat80_t *aPtr,
49      uint_fast8_t roundingMode,
50      bool exact,
51      extFloat80_t *zPtr
52  )
53 {
54 
55     *zPtr = extF80_roundToInt( *aPtr, roundingMode, exact );
56 
57 }
58 
59 #else
60 
61 void
62  extF80M_roundToInt(
63      const extFloat80_t *aPtr,
64      uint_fast8_t roundingMode,
65      bool exact,
66      extFloat80_t *zPtr
67  )
68 {
69     const struct extFloat80M *aSPtr;
70     struct extFloat80M *zSPtr;
71     uint_fast16_t uiA64, signUI64;
72     int32_t exp;
73     uint64_t sigA;
74     uint_fast16_t uiZ64;
75     uint64_t sigZ, lastBitMask, roundBitsMask;
76 
77     /*------------------------------------------------------------------------
78     *------------------------------------------------------------------------*/
79     aSPtr = (const struct extFloat80M *) aPtr;
80     zSPtr = (struct extFloat80M *) zPtr;
81     /*------------------------------------------------------------------------
82     *------------------------------------------------------------------------*/
83     uiA64 = aSPtr->signExp;
84     signUI64 = uiA64 & packToExtF80UI64( 1, 0 );
85     exp = expExtF80UI64( uiA64 );
86     sigA = aSPtr->signif;
87     /*------------------------------------------------------------------------
88     *------------------------------------------------------------------------*/
89     if ( ! (sigA & UINT64_C( 0x8000000000000000 )) && (exp != 0x7FFF) ) {
90         if ( ! sigA ) {
91             uiZ64 = signUI64;
92             sigZ = 0;
93             goto uiZ;
94         }
95         exp += softfloat_normExtF80SigM( &sigA );
96     }
97     /*------------------------------------------------------------------------
98     *------------------------------------------------------------------------*/
99     if ( exp <= 0x3FFE ) {
100         if ( exact ) softfloat_exceptionFlags |= softfloat_flag_inexact;
101         switch ( roundingMode ) {
102          case softfloat_round_near_even:
103             if ( ! (sigA & UINT64_C( 0x7FFFFFFFFFFFFFFF )) ) break;
104          case softfloat_round_near_maxMag:
105             if ( exp == 0x3FFE ) goto mag1;
106             break;
107          case softfloat_round_min:
108             if ( signUI64 ) goto mag1;
109             break;
110          case softfloat_round_max:
111             if ( ! signUI64 ) goto mag1;
112             break;
113         }
114         uiZ64 = signUI64;
115         sigZ = 0;
116         goto uiZ;
117      mag1:
118         uiZ64 = signUI64 | 0x3FFF;
119         sigZ = UINT64_C( 0x8000000000000000 );
120         goto uiZ;
121     }
122     /*------------------------------------------------------------------------
123     *------------------------------------------------------------------------*/
124     if ( 0x403E <= exp ) {
125         if ( exp == 0x7FFF ) {
126             if ( sigA & UINT64_C( 0x7FFFFFFFFFFFFFFF ) ) {
127                 softfloat_propagateNaNExtF80M( aSPtr, 0, zSPtr );
128                 return;
129             }
130             sigZ = UINT64_C( 0x8000000000000000 );
131         } else {
132             sigZ = sigA;
133         }
134         uiZ64 = signUI64 | exp;
135         goto uiZ;
136     }
137     /*------------------------------------------------------------------------
138     *------------------------------------------------------------------------*/
139     uiZ64 = signUI64 | exp;
140     lastBitMask = (uint64_t) 1<<(0x403E - exp);
141     roundBitsMask = lastBitMask - 1;
142     sigZ = sigA;
143     if ( roundingMode == softfloat_round_near_maxMag ) {
144         sigZ += lastBitMask>>1;
145     } else if ( roundingMode == softfloat_round_near_even ) {
146         sigZ += lastBitMask>>1;
147         if ( ! (sigZ & roundBitsMask) ) sigZ &= ~lastBitMask;
148     } else if ( roundingMode != softfloat_round_minMag ) {
149         if ( (signUI64 != 0) ^ (roundingMode == softfloat_round_max) ) {
150             sigZ += roundBitsMask;
151         }
152     }
153     sigZ &= ~roundBitsMask;
154     if ( ! sigZ ) {
155         ++uiZ64;
156         sigZ = UINT64_C( 0x8000000000000000 );
157     }
158     if ( exact && (sigZ != sigA) ) {
159         softfloat_exceptionFlags |= softfloat_flag_inexact;
160     }
161  uiZ:
162     zSPtr->signExp = uiZ64;
163     zSPtr->signif = sigZ;
164     return;
165 
166 }
167 
168 #endif
169 
170