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 
f64_to_i64(float64_t a,uint_fast8_t roundingMode,bool exact)44 int_fast64_t f64_to_i64( float64_t a, uint_fast8_t roundingMode, bool exact )
45 {
46     union ui64_f64 uA;
47     uint_fast64_t uiA;
48     bool sign;
49     int_fast16_t exp;
50     uint_fast64_t sig;
51     int_fast16_t shiftCount;
52 #ifdef SOFTFLOAT_FAST_INT64
53     struct uint64_extra sigExtra;
54 #else
55     uint32_t extSig[3];
56 #endif
57 
58     uA.f = a;
59     uiA = uA.ui;
60     sign = signF64UI( uiA );
61     exp  = expF64UI( uiA );
62     sig  = fracF64UI( uiA );
63     if ( exp ) sig |= UINT64_C( 0x0010000000000000 );
64     shiftCount = 0x433 - exp;
65 #ifdef SOFTFLOAT_FAST_INT64
66     if ( shiftCount <= 0 ) {
67         if ( shiftCount < -11 ) {
68             softfloat_raiseFlags( softfloat_flag_invalid );
69             return
70                 ! sign
71                     || ((exp == 0x7FF)
72                             && (sig != UINT64_C( 0x0010000000000000 )))
73                     ? INT64_C( 0x7FFFFFFFFFFFFFFF )
74                     : -INT64_C( 0x7FFFFFFFFFFFFFFF ) - 1;
75         }
76         sigExtra.v = sig<<-shiftCount;
77         sigExtra.extra = 0;
78     } else {
79         sigExtra = softfloat_shiftRightJam64Extra( sig, 0, shiftCount );
80     }
81     return
82         softfloat_roundPackToI64(
83             sign, sigExtra.v, sigExtra.extra, roundingMode, exact );
84 #else
85     extSig[indexWord( 3, 0 )] = 0;
86     if ( shiftCount <= 0 ) {
87         if ( shiftCount < -11 ) {
88             softfloat_raiseFlags( softfloat_flag_invalid );
89             return
90                 ! sign
91                     || ((exp == 0x7FF)
92                             && (sig != UINT64_C( 0x0010000000000000 )))
93                     ? INT64_C( 0x7FFFFFFFFFFFFFFF )
94                     : -INT64_C( 0x7FFFFFFFFFFFFFFF ) - 1;
95         }
96         sig <<= -shiftCount;
97         extSig[indexWord( 3, 2 )] = sig>>32;
98         extSig[indexWord( 3, 1 )] = sig;
99     } else {
100         extSig[indexWord( 3, 2 )] = sig>>32;
101         extSig[indexWord( 3, 1 )] = sig;
102         softfloat_shiftRightJam96M( extSig, shiftCount, extSig );
103     }
104     return softfloat_roundPackMToI64( sign, extSig, roundingMode, exact );
105 #endif
106 
107 }
108 
109