Stephen Canon | 0ef6213 | 2010-07-02 22:10:58 +0000 | [diff] [blame] | 1 | //===-- lib/fixsfsi.c - Single-precision -> integer conversion ----*- C -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Howard Hinnant | 9ad441f | 2010-11-16 22:13:33 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Stephen Canon | 0ef6213 | 2010-07-02 22:10:58 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements single-precision to integer conversion for the |
| 11 | // compiler-rt library. No range checking is performed; the behavior of this |
| 12 | // conversion is undefined for out of range values in the C standard. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #define SINGLE_PRECISION |
| 17 | #include "fp_lib.h" |
| 18 | |
Chandler Carruth | 0193b74 | 2012-06-22 21:09:15 +0000 | [diff] [blame] | 19 | ARM_EABI_FNALIAS(f2iz, fixsfsi) |
Anton Korobeynikov | 37b97d1 | 2011-04-19 17:51:24 +0000 | [diff] [blame] | 20 | |
Anton Korobeynikov | 1c5f89b | 2011-04-19 17:52:09 +0000 | [diff] [blame] | 21 | COMPILER_RT_ABI int |
| 22 | __fixsfsi(fp_t a) { |
Stephen Canon | 0ef6213 | 2010-07-02 22:10:58 +0000 | [diff] [blame] | 23 | // Break a into sign, exponent, significand |
| 24 | const rep_t aRep = toRep(a); |
| 25 | const rep_t aAbs = aRep & absMask; |
| 26 | const int sign = aRep & signBit ? -1 : 1; |
| 27 | const int exponent = (aAbs >> significandBits) - exponentBias; |
| 28 | const rep_t significand = (aAbs & significandMask) | implicitBit; |
| 29 | |
| 30 | // If 0 < exponent < significandBits, right shift to get the result. |
| 31 | if ((unsigned int)exponent < significandBits) { |
| 32 | return sign * (significand >> (significandBits - exponent)); |
| 33 | } |
| 34 | |
| 35 | // If exponent is negative, the result is zero. |
| 36 | else if (exponent < 0) { |
| 37 | return 0; |
| 38 | } |
| 39 | |
| 40 | // If significandBits < exponent, left shift to get the result. This shift |
| 41 | // may end up being larger than the type width, which incurs undefined |
| 42 | // behavior, but the conversion itself is undefined in that case, so |
| 43 | // whatever the compiler decides to do is fine. |
| 44 | else { |
| 45 | return sign * (significand << (exponent - significandBits)); |
| 46 | } |
| 47 | } |