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