Home | History | Annotate | Line # | Download | only in builtins
      1  1.1  joerg //===-- lib/floatditf.c - integer -> quad-precision conversion ----*- C -*-===//
      2  1.1  joerg //
      3  1.1  joerg //                     The LLVM Compiler Infrastructure
      4  1.1  joerg //
      5  1.1  joerg // This file is dual licensed under the MIT and the University of Illinois Open
      6  1.1  joerg // Source Licenses. See LICENSE.TXT for details.
      7  1.1  joerg //
      8  1.1  joerg //===----------------------------------------------------------------------===//
      9  1.1  joerg //
     10  1.1  joerg // This file implements di_int to quad-precision conversion for the
     11  1.1  joerg // compiler-rt library in the IEEE-754 default round-to-nearest, ties-to-even
     12  1.1  joerg // mode.
     13  1.1  joerg //
     14  1.1  joerg //===----------------------------------------------------------------------===//
     15  1.1  joerg 
     16  1.1  joerg #define QUAD_PRECISION
     17  1.1  joerg #include "fp_lib.h"
     18  1.1  joerg 
     19  1.1  joerg #if defined(CRT_HAS_128BIT) && defined(CRT_LDBL_128BIT)
     20  1.1  joerg COMPILER_RT_ABI fp_t __floatditf(di_int a) {
     21  1.1  joerg 
     22  1.1  joerg     const int aWidth = sizeof a * CHAR_BIT;
     23  1.1  joerg 
     24  1.1  joerg     // Handle zero as a special case to protect clz
     25  1.1  joerg     if (a == 0)
     26  1.1  joerg         return fromRep(0);
     27  1.1  joerg 
     28  1.1  joerg     // All other cases begin by extracting the sign and absolute value of a
     29  1.1  joerg     rep_t sign = 0;
     30  1.1  joerg     du_int aAbs = (du_int)a;
     31  1.1  joerg     if (a < 0) {
     32  1.1  joerg         sign = signBit;
     33  1.1  joerg         aAbs = ~(du_int)a + 1U;
     34  1.1  joerg     }
     35  1.1  joerg 
     36  1.1  joerg     // Exponent of (fp_t)a is the width of abs(a).
     37  1.1  joerg     const int exponent = (aWidth - 1) - __builtin_clzll(aAbs);
     38  1.1  joerg     rep_t result;
     39  1.1  joerg 
     40  1.1  joerg     // Shift a into the significand field, rounding if it is a right-shift
     41  1.1  joerg     const int shift = significandBits - exponent;
     42  1.1  joerg     result = (rep_t)aAbs << shift ^ implicitBit;
     43  1.1  joerg 
     44  1.1  joerg     // Insert the exponent
     45  1.1  joerg     result += (rep_t)(exponent + exponentBias) << significandBits;
     46  1.1  joerg     // Insert the sign bit and return
     47  1.1  joerg     return fromRep(result | sign);
     48  1.1  joerg }
     49  1.1  joerg 
     50  1.1  joerg #endif
     51