blob: 79b4b82e3a03c6f9940c89e3f6b0a42008fc8a36 [file] [log] [blame]
Edward O'Callaghan55836322009-08-07 20:30:09 +00001/*===-- floatundisf.c - Implement __floatundisf ---------------------------===
2 *
3 * The LLVM Compiler Infrastructure
4 *
5 * This file is distributed under the University of Illinois Open Source
6 * License. See LICENSE.TXT for details.
7 *
8 * ===----------------------------------------------------------------------===
9 *
10 * This file implements __floatundisf for the compiler_rt library.
11 *
12 *===----------------------------------------------------------------------===
13 */
Daniel Dunbarfd089992009-06-26 16:47:03 +000014
15#include "int_lib.h"
16#include <float.h>
17
Edward O'Callaghan55836322009-08-07 20:30:09 +000018/* Returns: convert a to a float, rounding toward even. */
Daniel Dunbarfd089992009-06-26 16:47:03 +000019
Edward O'Callaghan55836322009-08-07 20:30:09 +000020/* Assumption: float is a IEEE 32 bit floating point type
21 * du_int is a 64 bit integral type
22 */
Daniel Dunbarfd089992009-06-26 16:47:03 +000023
Edward O'Callaghan55836322009-08-07 20:30:09 +000024/* seee eeee emmm mmmm mmmm mmmm mmmm mmmm */
Daniel Dunbarfd089992009-06-26 16:47:03 +000025
26float
27__floatundisf(du_int a)
28{
29 if (a == 0)
30 return 0.0F;
31 const unsigned N = sizeof(du_int) * CHAR_BIT;
Edward O'Callaghan55836322009-08-07 20:30:09 +000032 int sd = N - __builtin_clzll(a); /* number of significant digits */
33 int e = sd - 1; /* 8 exponent */
Daniel Dunbarfd089992009-06-26 16:47:03 +000034 if (sd > FLT_MANT_DIG)
35 {
Edward O'Callaghan55836322009-08-07 20:30:09 +000036 /* start: 0000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQxxxxxxxxxxxxxxxxxx
37 * finish: 000000000000000000000000000000000000001xxxxxxxxxxxxxxxxxxxxxxPQR
38 * 12345678901234567890123456
39 * 1 = msb 1 bit
40 * P = bit FLT_MANT_DIG-1 bits to the right of 1
41 * Q = bit FLT_MANT_DIG bits to the right of 1
42 * R = "or" of all bits to the right of Q
43 */
Daniel Dunbarfd089992009-06-26 16:47:03 +000044 switch (sd)
45 {
46 case FLT_MANT_DIG + 1:
47 a <<= 1;
48 break;
49 case FLT_MANT_DIG + 2:
50 break;
51 default:
52 a = (a >> (sd - (FLT_MANT_DIG+2))) |
53 ((a & ((du_int)(-1) >> ((N + FLT_MANT_DIG+2) - sd))) != 0);
54 };
Edward O'Callaghan55836322009-08-07 20:30:09 +000055 /* finish: */
56 a |= (a & 4) != 0; /* Or P into R */
57 ++a; /* round - this step may add a significant bit */
58 a >>= 2; /* dump Q and R */
59 /* a is now rounded to FLT_MANT_DIG or FLT_MANT_DIG+1 bits */
Daniel Dunbarfd089992009-06-26 16:47:03 +000060 if (a & ((du_int)1 << FLT_MANT_DIG))
61 {
62 a >>= 1;
63 ++e;
64 }
Edward O'Callaghan55836322009-08-07 20:30:09 +000065 /* a is now rounded to FLT_MANT_DIG bits */
Daniel Dunbarfd089992009-06-26 16:47:03 +000066 }
67 else
68 {
69 a <<= (FLT_MANT_DIG - sd);
Edward O'Callaghan55836322009-08-07 20:30:09 +000070 /* a is now rounded to FLT_MANT_DIG bits */
Daniel Dunbarfd089992009-06-26 16:47:03 +000071 }
72 float_bits fb;
Edward O'Callaghan55836322009-08-07 20:30:09 +000073 fb.u = ((e + 127) << 23) | /* exponent */
74 ((su_int)a & 0x007FFFFF); /* mantissa */
Daniel Dunbarfd089992009-06-26 16:47:03 +000075 return fb.f;
76}