blob: 86841a75dc661e25f8a6dd3438c8e71ff2d6bf82 [file] [log] [blame]
Edward O'Callaghan55836322009-08-07 20:30:09 +00001/*===-- floatundisf.c - Implement __floatundisf ---------------------------===
2 *
3 * The LLVM Compiler Infrastructure
4 *
Howard Hinnant5b791f62010-11-16 22:13:33 +00005 * This file is dual licensed under the MIT and the University of Illinois Open
6 * Source Licenses. See LICENSE.TXT for details.
Edward O'Callaghan55836322009-08-07 20:30:09 +00007 *
8 * ===----------------------------------------------------------------------===
9 *
10 * This file implements __floatundisf for the compiler_rt library.
11 *
12 *===----------------------------------------------------------------------===
13 */
Daniel Dunbarfd089992009-06-26 16:47:03 +000014
Edward O'Callaghan55836322009-08-07 20:30:09 +000015/* Returns: convert a to a float, rounding toward even. */
Daniel Dunbarfd089992009-06-26 16:47:03 +000016
Edward O'Callaghan55836322009-08-07 20:30:09 +000017/* Assumption: float is a IEEE 32 bit floating point type
18 * du_int is a 64 bit integral type
19 */
Daniel Dunbarfd089992009-06-26 16:47:03 +000020
Edward O'Callaghan55836322009-08-07 20:30:09 +000021/* seee eeee emmm mmmm mmmm mmmm mmmm mmmm */
Daniel Dunbarfd089992009-06-26 16:47:03 +000022
Anton Korobeynikov75e3c192011-04-19 17:51:24 +000023#include "int_lib.h"
24
Anton Korobeynikove63da932011-04-19 17:52:09 +000025COMPILER_RT_ABI float
Daniel Dunbarfd089992009-06-26 16:47:03 +000026__floatundisf(du_int a)
27{
28 if (a == 0)
29 return 0.0F;
30 const unsigned N = sizeof(du_int) * CHAR_BIT;
Edward O'Callaghan55836322009-08-07 20:30:09 +000031 int sd = N - __builtin_clzll(a); /* number of significant digits */
32 int e = sd - 1; /* 8 exponent */
Daniel Dunbarfd089992009-06-26 16:47:03 +000033 if (sd > FLT_MANT_DIG)
34 {
Edward O'Callaghan55836322009-08-07 20:30:09 +000035 /* start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
36 * finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
37 * 12345678901234567890123456
38 * 1 = msb 1 bit
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
42 */
Daniel Dunbarfd089992009-06-26 16:47:03 +000043 switch (sd)
44 {
45 case FLT_MANT_DIG + 1:
46 a <<= 1;
47 break;
48 case FLT_MANT_DIG + 2:
49 break;
50 default:
51 a = (a >> (sd - (FLT_MANT_DIG+2))) |
52 ((a & ((du_int)(-1) >> ((N + FLT_MANT_DIG+2) - sd))) != 0);
53 };
Edward O'Callaghan55836322009-08-07 20:30:09 +000054 /* finish: */
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 */
Daniel Dunbarfd089992009-06-26 16:47:03 +000059 if (a & ((du_int)1 << FLT_MANT_DIG))
60 {
61 a >>= 1;
62 ++e;
63 }
Edward O'Callaghan55836322009-08-07 20:30:09 +000064 /* a is now rounded to FLT_MANT_DIG bits */
Daniel Dunbarfd089992009-06-26 16:47:03 +000065 }
66 else
67 {
68 a <<= (FLT_MANT_DIG - sd);
Edward O'Callaghan55836322009-08-07 20:30:09 +000069 /* a is now rounded to FLT_MANT_DIG bits */
Daniel Dunbarfd089992009-06-26 16:47:03 +000070 }
71 float_bits fb;
Edward O'Callaghan55836322009-08-07 20:30:09 +000072 fb.u = ((e + 127) << 23) | /* exponent */
73 ((su_int)a & 0x007FFFFF); /* mantissa */
Daniel Dunbarfd089992009-06-26 16:47:03 +000074 return fb.f;
75}
Saleem Abdulrasool36ac5dd2017-05-16 16:41:37 +000076
77#if defined(__ARM_EABI__)
78AEABI_RTABI float __aeabi_ul2f(du_int a) {
79 return __floatundisf(a);
80}
81#endif
82