1214152Sed//===-- lib/fixsfsi.c - Single-precision -> integer 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 single-precision to integer conversion for the 11214152Sed// compiler-rt library. No range checking is performed; the behavior of this 12214152Sed// conversion is undefined for out of range values in the C standard. 13214152Sed// 14214152Sed//===----------------------------------------------------------------------===// 15214152Sed 16214152Sed#define SINGLE_PRECISION 17214152Sed#include "fp_lib.h" 18214152Sed 19239138SandrewARM_EABI_FNALIAS(f2iz, fixsfsi) 20222656Sed 21222656SedCOMPILER_RT_ABI int 22222656Sed__fixsfsi(fp_t a) { 23214152Sed // Break a into sign, exponent, significand 24214152Sed const rep_t aRep = toRep(a); 25214152Sed const rep_t aAbs = aRep & absMask; 26214152Sed const int sign = aRep & signBit ? -1 : 1; 27214152Sed const int exponent = (aAbs >> significandBits) - exponentBias; 28214152Sed const rep_t significand = (aAbs & significandMask) | implicitBit; 29214152Sed 30214152Sed // If 0 < exponent < significandBits, right shift to get the result. 31214152Sed if ((unsigned int)exponent < significandBits) { 32214152Sed return sign * (significand >> (significandBits - exponent)); 33214152Sed } 34214152Sed 35214152Sed // If exponent is negative, the result is zero. 36214152Sed else if (exponent < 0) { 37214152Sed return 0; 38214152Sed } 39214152Sed 40214152Sed // If significandBits < exponent, left shift to get the result. This shift 41214152Sed // may end up being larger than the type width, which incurs undefined 42214152Sed // behavior, but the conversion itself is undefined in that case, so 43214152Sed // whatever the compiler decides to do is fine. 44214152Sed else { 45214152Sed return sign * (significand << (exponent - significandBits)); 46214152Sed } 47214152Sed} 48