1 1.1 joerg //===-- lib/floatsisf.c - integer -> single-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 single-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 SINGLE_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 __floatsisf(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, rounding if it is a right-shift 42 1.1 joerg if (exponent <= significandBits) { 43 1.1 joerg const int shift = significandBits - exponent; 44 1.1 joerg result = (rep_t)a << shift ^ implicitBit; 45 1.1 joerg } else { 46 1.1 joerg const int shift = exponent - significandBits; 47 1.1 joerg result = (rep_t)a >> shift ^ implicitBit; 48 1.1 joerg rep_t round = (rep_t)a << (typeWidth - shift); 49 1.1 joerg if (round > signBit) result++; 50 1.1 joerg if (round == signBit) result += result & 1; 51 1.1 joerg } 52 1.1 joerg 53 1.1 joerg // Insert the exponent 54 1.1 joerg result += (rep_t)(exponent + exponentBias) << significandBits; 55 1.1 joerg // Insert the sign bit and return 56 1.1 joerg return fromRep(result | sign); 57 1.1 joerg } 58 1.2 rin 59 1.2 rin #if defined(__ARM_EABI__) 60 1.3 rin #if defined(COMPILER_RT_ARMHF_TARGET) 61 1.2 rin AEABI_RTABI fp_t __aeabi_i2f(int a) { 62 1.2 rin return __floatsisf(a); 63 1.2 rin } 64 1.3 rin #else 65 1.3 rin AEABI_RTABI fp_t __aeabi_i2f(int a) COMPILER_RT_ALIAS(__floatsisf); 66 1.3 rin #endif 67 1.2 rin #endif 68