1 //===-- lib/floatuntitf.c - uint128 -> quad-precision conversion --*- C -*-===//
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 tu_int to quad-precision conversion for the
10 // compiler-rt library in the IEEE-754 default round-to-nearest, ties-to-even
13 //===----------------------------------------------------------------------===//
15 #define QUAD_PRECISION
19 // Returns: convert a tu_int to a fp_t, rounding toward even.
21 // Assumption: fp_t is a IEEE 128 bit floating point type
22 // tu_int is a 128 bit integral type
24 // seee eeee eeee eeee mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm
25 // mmmm | mmmm mmmm mmmm mmmm mmmm mmmm mmmm mmmm | mmmm mmmm mmmm mmmm mmmm
28 #if defined(CRT_HAS_128BIT) && defined(CRT_LDBL_128BIT)
29 COMPILER_RT_ABI fp_t
__floatuntitf(tu_int a
) {
32 const unsigned N
= sizeof(tu_int
) * CHAR_BIT
;
33 int sd
= N
- __clzti2(a
); // number of significant digits
34 int e
= sd
- 1; // exponent
35 if (sd
> LDBL_MANT_DIG
) {
36 // start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
37 // finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
38 // 12345678901234567890123456
40 // P = bit LDBL_MANT_DIG-1 bits to the right of 1
41 // Q = bit LDBL_MANT_DIG bits to the right of 1
42 // R = "or" of all bits to the right of Q
44 case LDBL_MANT_DIG
+ 1:
47 case LDBL_MANT_DIG
+ 2:
50 a
= (a
>> (sd
- (LDBL_MANT_DIG
+ 2))) |
51 ((a
& ((tu_int
)(-1) >> ((N
+ LDBL_MANT_DIG
+ 2) - sd
))) != 0);
54 a
|= (a
& 4) != 0; // Or P into R
55 ++a
; // round - this step may add a significant bit
56 a
>>= 2; // dump Q and R
57 // a is now rounded to LDBL_MANT_DIG or LDBL_MANT_DIG+1 bits
58 if (a
& ((tu_int
)1 << LDBL_MANT_DIG
)) {
62 // a is now rounded to LDBL_MANT_DIG bits
64 a
<<= (LDBL_MANT_DIG
- sd
);
65 // a is now rounded to LDBL_MANT_DIG bits
69 fb
.u
.high
.all
= (du_int
)(e
+ 16383) << 48 // exponent
70 | ((a
>> 64) & 0x0000ffffffffffffLL
); // significand
71 fb
.u
.low
.all
= (du_int
)(a
);