1*7c3d14c8STreehugger Robot //===-- lib/floatsidf.c - integer -> double-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 double-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 DOUBLE_PRECISION 17*7c3d14c8STreehugger Robot #include "fp_lib.h" 18*7c3d14c8STreehugger Robot 19*7c3d14c8STreehugger Robot #include "int_lib.h" 20*7c3d14c8STreehugger Robot ARM_EABI_FNALIAS(i2d,floatsidf)21*7c3d14c8STreehugger RobotARM_EABI_FNALIAS(i2d, floatsidf) 22*7c3d14c8STreehugger Robot 23*7c3d14c8STreehugger Robot COMPILER_RT_ABI fp_t 24*7c3d14c8STreehugger Robot __floatsidf(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 and clear the implicit bit. Extra 44*7c3d14c8STreehugger Robot // cast to unsigned int is necessary to get the correct behavior for 45*7c3d14c8STreehugger Robot // the input INT_MIN. 46*7c3d14c8STreehugger Robot const int shift = significandBits - exponent; 47*7c3d14c8STreehugger Robot result = (rep_t)(unsigned int)a << shift ^ implicitBit; 48*7c3d14c8STreehugger Robot 49*7c3d14c8STreehugger Robot // Insert the exponent 50*7c3d14c8STreehugger Robot result += (rep_t)(exponent + exponentBias) << significandBits; 51*7c3d14c8STreehugger Robot // Insert the sign bit and return 52*7c3d14c8STreehugger Robot return fromRep(result | sign); 53*7c3d14c8STreehugger Robot } 54