blob: 4cdc47f7b4fa13cb8817a439f57a1484a03fdf6f [file] [log] [blame]
Stephen Canon0ef62132010-07-02 22:10:58 +00001//===-- lib/fixsfsi.c - Single-precision -> integer conversion ----*- C -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Howard Hinnant9ad441f2010-11-16 22:13:33 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Stephen Canon0ef62132010-07-02 22:10:58 +00007//
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
Anton Korobeynikov37b97d12011-04-19 17:51:24 +000019#include "int_lib.h"
20
21ARM_EABI_FNALIAS(f2iz, fixsfsi);
22
Stephen Canon0ef62132010-07-02 22:10:58 +000023int __fixsfsi(fp_t a) {
24
25 // Break a into sign, exponent, significand
26 const rep_t aRep = toRep(a);
27 const rep_t aAbs = aRep & absMask;
28 const int sign = aRep & signBit ? -1 : 1;
29 const int exponent = (aAbs >> significandBits) - exponentBias;
30 const rep_t significand = (aAbs & significandMask) | implicitBit;
31
32 // If 0 < exponent < significandBits, right shift to get the result.
33 if ((unsigned int)exponent < significandBits) {
34 return sign * (significand >> (significandBits - exponent));
35 }
36
37 // If exponent is negative, the result is zero.
38 else if (exponent < 0) {
39 return 0;
40 }
41
42 // If significandBits < exponent, left shift to get the result. This shift
43 // may end up being larger than the type width, which incurs undefined
44 // behavior, but the conversion itself is undefined in that case, so
45 // whatever the compiler decides to do is fine.
46 else {
47 return sign * (significand << (exponent - significandBits));
48 }
49}