1 //===-- floatdisf.c - Implement __floatdisf -------------------------------===//
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 __floatdisf for the compiler_rt library.
11 //===----------------------------------------------------------------------===//
13 // Returns: convert a to a float, rounding toward even.
15 // Assumption: float is a IEEE 32 bit floating point type
16 // di_int is a 64 bit integral type
18 // seee eeee emmm mmmm mmmm mmmm mmmm mmmm
22 COMPILER_RT_ABI
float __floatdisf(di_int a
) {
25 const unsigned N
= sizeof(di_int
) * CHAR_BIT
;
26 const di_int s
= a
>> (N
- 1);
28 int sd
= N
- __builtin_clzll(a
); // number of significant digits
29 si_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
= ((du_int
)a
>> (sd
- (FLT_MANT_DIG
+ 2))) |
46 ((a
& ((du_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
& ((du_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
= ((su_int
)s
& 0x80000000) | // sign
64 ((e
+ 127) << 23) | // exponent
65 ((su_int
)a
& 0x007FFFFF); // mantissa
69 #if defined(__ARM_EABI__)
70 #if defined(COMPILER_RT_ARMHF_TARGET)
71 AEABI_RTABI
float __aeabi_l2f(di_int a
) { return __floatdisf(a
); }
73 COMPILER_RT_ALIAS(__floatdisf
, __aeabi_l2f
)
77 #if defined(__MINGW32__) && defined(__arm__)
78 COMPILER_RT_ALIAS(__floatdisf
, __i64tos
)