1*0b57cec5SDimitry Andric //===-- lib/floatsidf.c - integer -> double-precision conversion --*- C -*-===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric // 9*0b57cec5SDimitry Andric // This file implements integer to double-precision conversion for the 10*0b57cec5SDimitry Andric // compiler-rt library in the IEEE-754 default round-to-nearest, ties-to-even 11*0b57cec5SDimitry Andric // mode. 12*0b57cec5SDimitry Andric // 13*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 14*0b57cec5SDimitry Andric 15*0b57cec5SDimitry Andric #define DOUBLE_PRECISION 16*0b57cec5SDimitry Andric #include "fp_lib.h" 17*0b57cec5SDimitry Andric 18*0b57cec5SDimitry Andric #include "int_lib.h" 19*0b57cec5SDimitry Andric 20*0b57cec5SDimitry Andric COMPILER_RT_ABI fp_t __floatsidf(int a) { 21*0b57cec5SDimitry Andric 22*0b57cec5SDimitry Andric const int aWidth = sizeof a * CHAR_BIT; 23*0b57cec5SDimitry Andric 24*0b57cec5SDimitry Andric // Handle zero as a special case to protect clz 25*0b57cec5SDimitry Andric if (a == 0) 26*0b57cec5SDimitry Andric return fromRep(0); 27*0b57cec5SDimitry Andric 28*0b57cec5SDimitry Andric // All other cases begin by extracting the sign and absolute value of a 29*0b57cec5SDimitry Andric rep_t sign = 0; 30*0b57cec5SDimitry Andric if (a < 0) { 31*0b57cec5SDimitry Andric sign = signBit; 32*0b57cec5SDimitry Andric a = -a; 33*0b57cec5SDimitry Andric } 34*0b57cec5SDimitry Andric 35*0b57cec5SDimitry Andric // Exponent of (fp_t)a is the width of abs(a). 36*0b57cec5SDimitry Andric const int exponent = (aWidth - 1) - __builtin_clz(a); 37*0b57cec5SDimitry Andric rep_t result; 38*0b57cec5SDimitry Andric 39*0b57cec5SDimitry Andric // Shift a into the significand field and clear the implicit bit. Extra 40*0b57cec5SDimitry Andric // cast to unsigned int is necessary to get the correct behavior for 41*0b57cec5SDimitry Andric // the input INT_MIN. 42*0b57cec5SDimitry Andric const int shift = significandBits - exponent; 43*0b57cec5SDimitry Andric result = (rep_t)(unsigned int)a << shift ^ implicitBit; 44*0b57cec5SDimitry Andric 45*0b57cec5SDimitry Andric // Insert the exponent 46*0b57cec5SDimitry Andric result += (rep_t)(exponent + exponentBias) << significandBits; 47*0b57cec5SDimitry Andric // Insert the sign bit and return 48*0b57cec5SDimitry Andric return fromRep(result | sign); 49*0b57cec5SDimitry Andric } 50*0b57cec5SDimitry Andric 51*0b57cec5SDimitry Andric #if defined(__ARM_EABI__) 52*0b57cec5SDimitry Andric #if defined(COMPILER_RT_ARMHF_TARGET) 53*0b57cec5SDimitry Andric AEABI_RTABI fp_t __aeabi_i2d(int a) { return __floatsidf(a); } 54*0b57cec5SDimitry Andric #else 55*0b57cec5SDimitry Andric COMPILER_RT_ALIAS(__floatsidf, __aeabi_i2d) 56*0b57cec5SDimitry Andric #endif 57*0b57cec5SDimitry Andric #endif 58