Home | History | Annotate | Download | only in lib
      1 /*===-- 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  */
     14 
     15 #include "int_lib.h"
     16 #include <float.h>
     17 
     18 /* Returns: convert a to a float, rounding toward even. */
     19 
     20 /* Assumption: float is a IEEE 32 bit floating point type
     21  *            du_int is a 64 bit integral type
     22  */
     23 
     24 /* seee eeee emmm mmmm mmmm mmmm mmmm mmmm */
     25 
     26 float
     27 __floatundisf(du_int a)
     28 {
     29     if (a == 0)
     30         return 0.0F;
     31     const unsigned N = sizeof(du_int) * CHAR_BIT;
     32     int sd = N - __builtin_clzll(a);  /* number of significant digits */
     33     int e = sd - 1;             /* 8 exponent */
     34     if (sd > FLT_MANT_DIG)
     35     {
     36         /*  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          */
     44         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         };
     55         /* 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 */
     60         if (a & ((du_int)1 << FLT_MANT_DIG))
     61         {
     62             a >>= 1;
     63             ++e;
     64         }
     65         /* a is now rounded to FLT_MANT_DIG bits */
     66     }
     67     else
     68     {
     69         a <<= (FLT_MANT_DIG - sd);
     70         /* a is now rounded to FLT_MANT_DIG bits */
     71     }
     72     float_bits fb;
     73     fb.u = ((e + 127) << 23)       |  /* exponent */
     74            ((su_int)a & 0x007FFFFF);  /* mantissa */
     75     return fb.f;
     76 }
     77