]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - contrib/compiler-rt/lib/fixsfsi.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / contrib / compiler-rt / lib / fixsfsi.c
1 //===-- lib/fixsfsi.c - Single-precision -> integer conversion ----*- C -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
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 #include "abi.h"
16
17 #define SINGLE_PRECISION
18 #include "fp_lib.h"
19
20 ARM_EABI_FNALIAS(f2iz, fixsfsi);
21
22 COMPILER_RT_ABI int
23 __fixsfsi(fp_t a) {
24     // Break a into sign, exponent, significand
25     const rep_t aRep = toRep(a);
26     const rep_t aAbs = aRep & absMask;
27     const int sign = aRep & signBit ? -1 : 1;
28     const int exponent = (aAbs >> significandBits) - exponentBias;
29     const rep_t significand = (aAbs & significandMask) | implicitBit;
30     
31     // If 0 < exponent < significandBits, right shift to get the result.
32     if ((unsigned int)exponent < significandBits) {
33         return sign * (significand >> (significandBits - exponent));
34     }
35     
36     // If exponent is negative, the result is zero.
37     else if (exponent < 0) {
38         return 0;
39     }
40     
41     // If significandBits < exponent, left shift to get the result.  This shift
42     // may end up being larger than the type width, which incurs undefined
43     // behavior, but the conversion itself is undefined in that case, so
44     // whatever the compiler decides to do is fine.
45     else {
46         return sign * (significand << (exponent - significandBits));
47     }
48 }