1 /*===-- floatundisf.c - Implement __floatundisf ---------------------------===
3 * The LLVM Compiler Infrastructure
5 * This file is dual licensed under the MIT and the University of Illinois Open
6 * Source Licenses. See LICENSE.TXT for details.
8 * ===----------------------------------------------------------------------===
10 * This file implements __floatundisf for the compiler_rt library.
12 *===----------------------------------------------------------------------===
15 /* Returns: convert a to a float, rounding toward even. */
17 /* Assumption: float is a IEEE 32 bit floating point type
18 * du_int is a 64 bit integral type
21 /* seee eeee emmm mmmm mmmm mmmm mmmm mmmm */
26 __floatundisf(du_int a)
30 const unsigned N = sizeof(du_int) * CHAR_BIT;
31 int sd = N - __builtin_clzll(a); /* number of significant digits */
32 int e = sd - 1; /* 8 exponent */
33 if (sd > FLT_MANT_DIG)
35 /* start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
36 * finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
37 * 12345678901234567890123456
39 * P = bit FLT_MANT_DIG-1 bits to the right of 1
40 * Q = bit FLT_MANT_DIG bits to the right of 1
41 * R = "or" of all bits to the right of Q
45 case FLT_MANT_DIG + 1:
48 case FLT_MANT_DIG + 2:
51 a = (a >> (sd - (FLT_MANT_DIG+2))) |
52 ((a & ((du_int)(-1) >> ((N + FLT_MANT_DIG+2) - sd))) != 0);
55 a |= (a & 4) != 0; /* Or P into R */
56 ++a; /* round - this step may add a significant bit */
57 a >>= 2; /* dump Q and R */
58 /* a is now rounded to FLT_MANT_DIG or FLT_MANT_DIG+1 bits */
59 if (a & ((du_int)1 << FLT_MANT_DIG))
64 /* a is now rounded to FLT_MANT_DIG bits */
68 a <<= (FLT_MANT_DIG - sd);
69 /* a is now rounded to FLT_MANT_DIG bits */
72 fb.u = ((e + 127) << 23) | /* exponent */
73 ((su_int)a & 0x007FFFFF); /* mantissa */
77 #if defined(__ARM_EABI__)
78 #if defined(COMPILER_RT_ARMHF_TARGET)
79 AEABI_RTABI float __aeabi_ul2f(du_int a) {
80 return __floatundisf(a);
83 AEABI_RTABI float __aeabi_ul2f(du_int a) COMPILER_RT_ALIAS(__floatundisf);