1 //===-- floatuntisf.c - Implement __floatuntisf ---------------------------===//
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 __floatuntisf for the compiler_rt library.
11 //===----------------------------------------------------------------------===//
17 // Returns: convert a to a float, rounding toward even.
19 // Assumption: float is a IEEE 32 bit floating point type
20 // tu_int is a 128 bit integral type
22 // seee eeee emmm mmmm mmmm mmmm mmmm mmmm
24 COMPILER_RT_ABI
float __floatuntisf(tu_int a
) {
27 const unsigned N
= sizeof(tu_int
) * CHAR_BIT
;
28 int sd
= N
- __clzti2(a
); // number of significant digits
29 int e
= sd
- 1; // exponent
30 if (sd
> FLT_MANT_DIG
) {
31 // start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
32 // finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
33 // 12345678901234567890123456
35 // P = bit FLT_MANT_DIG-1 bits to the right of 1
36 // Q = bit FLT_MANT_DIG bits to the right of 1
37 // R = "or" of all bits to the right of Q
39 case FLT_MANT_DIG
+ 1:
42 case FLT_MANT_DIG
+ 2:
45 a
= (a
>> (sd
- (FLT_MANT_DIG
+ 2))) |
46 ((a
& ((tu_int
)(-1) >> ((N
+ FLT_MANT_DIG
+ 2) - sd
))) != 0);
49 a
|= (a
& 4) != 0; // Or P into R
50 ++a
; // round - this step may add a significant bit
51 a
>>= 2; // dump Q and R
52 // a is now rounded to FLT_MANT_DIG or FLT_MANT_DIG+1 bits
53 if (a
& ((tu_int
)1 << FLT_MANT_DIG
)) {
57 // a is now rounded to FLT_MANT_DIG bits
59 a
<<= (FLT_MANT_DIG
- sd
);
60 // a is now rounded to FLT_MANT_DIG bits
63 fb
.u
= ((e
+ 127) << 23) | // exponent
64 ((su_int
)a
& 0x007FFFFF); // mantissa
68 #endif // CRT_HAS_128BIT