1214152Sed//===-- lib/floatsidf.c - integer -> double-precision conversion --*- C -*-===// 2214152Sed// 3214152Sed// The LLVM Compiler Infrastructure 4214152Sed// 5222656Sed// This file is dual licensed under the MIT and the University of Illinois Open 6222656Sed// Source Licenses. See LICENSE.TXT for details. 7214152Sed// 8214152Sed//===----------------------------------------------------------------------===// 9214152Sed// 10214152Sed// This file implements integer to double-precision conversion for the 11214152Sed// compiler-rt library in the IEEE-754 default round-to-nearest, ties-to-even 12214152Sed// mode. 13214152Sed// 14214152Sed//===----------------------------------------------------------------------===// 15214152Sed 16214152Sed#define DOUBLE_PRECISION 17214152Sed#include "fp_lib.h" 18214152Sed 19222656Sed#include "int_lib.h" 20222656Sed 21239138SandrewARM_EABI_FNALIAS(i2d, floatsidf) 22222656Sed 23214152Sedfp_t __floatsidf(int a) { 24214152Sed 25214152Sed const int aWidth = sizeof a * CHAR_BIT; 26214152Sed 27214152Sed // Handle zero as a special case to protect clz 28214152Sed if (a == 0) 29214152Sed return fromRep(0); 30214152Sed 31214152Sed // All other cases begin by extracting the sign and absolute value of a 32214152Sed rep_t sign = 0; 33214152Sed if (a < 0) { 34214152Sed sign = signBit; 35214152Sed a = -a; 36214152Sed } 37214152Sed 38214152Sed // Exponent of (fp_t)a is the width of abs(a). 39214152Sed const int exponent = (aWidth - 1) - __builtin_clz(a); 40214152Sed rep_t result; 41214152Sed 42214152Sed // Shift a into the significand field and clear the implicit bit. Extra 43214152Sed // cast to unsigned int is necessary to get the correct behavior for 44214152Sed // the input INT_MIN. 45214152Sed const int shift = significandBits - exponent; 46214152Sed result = (rep_t)(unsigned int)a << shift ^ implicitBit; 47214152Sed 48214152Sed // Insert the exponent 49214152Sed result += (rep_t)(exponent + exponentBias) << significandBits; 50214152Sed // Insert the sign bit and return 51214152Sed return fromRep(result | sign); 52214152Sed} 53