1 //===-- floattidf.c - Implement __floattidf -------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements __floattidf for the compiler_rt library.
11 //===----------------------------------------------------------------------===//
17 // Returns: convert a to a double, rounding toward even.
19 // Assumption: double is a IEEE 64 bit floating point type
20 // ti_int is a 128 bit integral type
22 // seee eeee eeee mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm
25 COMPILER_RT_ABI
double __floattidf(ti_int a
) {
28 const unsigned N
= sizeof(ti_int
) * CHAR_BIT
;
29 const ti_int s
= a
>> (N
- 1);
31 int sd
= N
- __clzti2(a
); // number of significant digits
32 int e
= sd
- 1; // exponent
33 if (sd
> DBL_MANT_DIG
) {
34 // start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
35 // finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
36 // 12345678901234567890123456
38 // P = bit DBL_MANT_DIG-1 bits to the right of 1
39 // Q = bit DBL_MANT_DIG bits to the right of 1
40 // R = "or" of all bits to the right of Q
42 case DBL_MANT_DIG
+ 1:
45 case DBL_MANT_DIG
+ 2:
48 a
= ((tu_int
)a
>> (sd
- (DBL_MANT_DIG
+ 2))) |
49 ((a
& ((tu_int
)(-1) >> ((N
+ DBL_MANT_DIG
+ 2) - sd
))) != 0);
52 a
|= (a
& 4) != 0; // Or P into R
53 ++a
; // round - this step may add a significant bit
54 a
>>= 2; // dump Q and R
55 // a is now rounded to DBL_MANT_DIG or DBL_MANT_DIG+1 bits
56 if (a
& ((tu_int
)1 << DBL_MANT_DIG
)) {
60 // a is now rounded to DBL_MANT_DIG bits
62 a
<<= (DBL_MANT_DIG
- sd
);
63 // a is now rounded to DBL_MANT_DIG bits
66 fb
.u
.s
.high
= ((su_int
)s
& 0x80000000) | // sign
67 ((e
+ 1023) << 20) | // exponent
68 ((su_int
)(a
>> 32) & 0x000FFFFF); // mantissa-high
69 fb
.u
.s
.low
= (su_int
)a
; // mantissa-low
73 #endif // CRT_HAS_128BIT