1 1.1 joerg //===-- lib/floatsidf.c - integer -> double-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 integer to double-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 DOUBLE_PRECISION 17 1.1 joerg #include "fp_lib.h" 18 1.1 joerg 19 1.1 joerg #include "int_lib.h" 20 1.1 joerg 21 1.2 rin COMPILER_RT_ABI fp_t 22 1.2 rin __floatsidf(int a) { 23 1.1 joerg 24 1.1 joerg const int aWidth = sizeof a * CHAR_BIT; 25 1.1 joerg 26 1.1 joerg // Handle zero as a special case to protect clz 27 1.1 joerg if (a == 0) 28 1.1 joerg return fromRep(0); 29 1.1 joerg 30 1.1 joerg // All other cases begin by extracting the sign and absolute value of a 31 1.1 joerg rep_t sign = 0; 32 1.1 joerg if (a < 0) { 33 1.1 joerg sign = signBit; 34 1.1 joerg a = -a; 35 1.1 joerg } 36 1.1 joerg 37 1.1 joerg // Exponent of (fp_t)a is the width of abs(a). 38 1.1 joerg const int exponent = (aWidth - 1) - __builtin_clz(a); 39 1.1 joerg rep_t result; 40 1.1 joerg 41 1.1 joerg // Shift a into the significand field and clear the implicit bit. Extra 42 1.1 joerg // cast to unsigned int is necessary to get the correct behavior for 43 1.1 joerg // the input INT_MIN. 44 1.1 joerg const int shift = significandBits - exponent; 45 1.1 joerg result = (rep_t)(unsigned int)a << shift ^ implicitBit; 46 1.1 joerg 47 1.1 joerg // Insert the exponent 48 1.1 joerg result += (rep_t)(exponent + exponentBias) << significandBits; 49 1.1 joerg // Insert the sign bit and return 50 1.1 joerg return fromRep(result | sign); 51 1.1 joerg } 52 1.2 rin 53 1.2 rin #if defined(__ARM_EABI__) 54 1.3 rin #if defined(COMPILER_RT_ARMHF_TARGET) 55 1.2 rin AEABI_RTABI fp_t __aeabi_i2d(int a) { 56 1.2 rin return __floatsidf(a); 57 1.2 rin } 58 1.3 rin #else 59 1.3 rin AEABI_RTABI fp_t __aeabi_i2d(int a) COMPILER_RT_ALIAS(__floatsidf); 60 1.3 rin #endif 61 1.2 rin #endif 62