Home | History | Annotate | Line # | Download | only in builtins
      1 //===-- lib/floatsisf.c - integer -> single-precision conversion --*- C -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file implements integer to single-precision conversion for the
     11 // compiler-rt library in the IEEE-754 default round-to-nearest, ties-to-even
     12 // mode.
     13 //
     14 //===----------------------------------------------------------------------===//
     15 
     16 #define SINGLE_PRECISION
     17 #include "fp_lib.h"
     18 
     19 #include "int_lib.h"
     20 
     21 COMPILER_RT_ABI fp_t
     22 __floatsisf(int a) {
     23 
     24     const int aWidth = sizeof a * CHAR_BIT;
     25 
     26     // Handle zero as a special case to protect clz
     27     if (a == 0)
     28         return fromRep(0);
     29 
     30     // All other cases begin by extracting the sign and absolute value of a
     31     rep_t sign = 0;
     32     if (a < 0) {
     33         sign = signBit;
     34         a = -a;
     35     }
     36 
     37     // Exponent of (fp_t)a is the width of abs(a).
     38     const int exponent = (aWidth - 1) - __builtin_clz(a);
     39     rep_t result;
     40 
     41     // Shift a into the significand field, rounding if it is a right-shift
     42     if (exponent <= significandBits) {
     43         const int shift = significandBits - exponent;
     44         result = (rep_t)a << shift ^ implicitBit;
     45     } else {
     46         const int shift = exponent - significandBits;
     47         result = (rep_t)a >> shift ^ implicitBit;
     48         rep_t round = (rep_t)a << (typeWidth - shift);
     49         if (round > signBit) result++;
     50         if (round == signBit) result += result & 1;
     51     }
     52 
     53     // Insert the exponent
     54     result += (rep_t)(exponent + exponentBias) << significandBits;
     55     // Insert the sign bit and return
     56     return fromRep(result | sign);
     57 }
     58 
     59 #if defined(__ARM_EABI__)
     60 #if defined(COMPILER_RT_ARMHF_TARGET)
     61 AEABI_RTABI fp_t __aeabi_i2f(int a) {
     62   return __floatsisf(a);
     63 }
     64 #else
     65 AEABI_RTABI fp_t __aeabi_i2f(int a) COMPILER_RT_ALIAS(__floatsisf);
     66 #endif
     67 #endif
     68