floatsisf.c revision 222656
1214152Sed//===-- lib/floatsisf.c - integer -> single-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 single-precision conversion for the 11214152Sed// compiler-rt library in the IEEE-754 default round-to-nearest, ties-to-even 12214152Sed// mode. 13214152Sed// 14214152Sed//===----------------------------------------------------------------------===// 15222656Sed#include "abi.h" 16214152Sed 17214152Sed#define SINGLE_PRECISION 18214152Sed#include "fp_lib.h" 19214152Sed 20222656Sed#include "int_lib.h" 21222656Sed 22222656SedARM_EABI_FNALIAS(i2f, floatsisf); 23222656Sed 24214152Sedfp_t __floatsisf(int a) { 25214152Sed 26214152Sed const int aWidth = sizeof a * CHAR_BIT; 27214152Sed 28214152Sed // Handle zero as a special case to protect clz 29214152Sed if (a == 0) 30214152Sed return fromRep(0); 31214152Sed 32214152Sed // All other cases begin by extracting the sign and absolute value of a 33214152Sed rep_t sign = 0; 34214152Sed if (a < 0) { 35214152Sed sign = signBit; 36214152Sed a = -a; 37214152Sed } 38214152Sed 39214152Sed // Exponent of (fp_t)a is the width of abs(a). 40214152Sed const int exponent = (aWidth - 1) - __builtin_clz(a); 41214152Sed rep_t result; 42214152Sed 43214152Sed // Shift a into the significand field, rounding if it is a right-shift 44214152Sed if (exponent <= significandBits) { 45214152Sed const int shift = significandBits - exponent; 46214152Sed result = (rep_t)a << shift ^ implicitBit; 47214152Sed } else { 48214152Sed const int shift = exponent - significandBits; 49214152Sed result = (rep_t)a >> shift ^ implicitBit; 50214152Sed rep_t round = (rep_t)a << (typeWidth - shift); 51214152Sed if (round > signBit) result++; 52214152Sed if (round == signBit) result += result & 1; 53214152Sed } 54214152Sed 55214152Sed // Insert the exponent 56214152Sed result += (rep_t)(exponent + exponentBias) << significandBits; 57214152Sed // Insert the sign bit and return 58214152Sed return fromRep(result | sign); 59214152Sed} 60