blob: ff79377198f46d5184b74e8ca3d7e836ddd73444 [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//
5// This file is distributed under the University of Illinois Open Source
6// License. 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
16#define SINGLE_PRECISION
17#include "fp_lib.h"
18
19int __fixsfsi(fp_t a) {
20
21 // Break a into sign, exponent, significand
22 const rep_t aRep = toRep(a);
23 const rep_t aAbs = aRep & absMask;
24 const int sign = aRep & signBit ? -1 : 1;
25 const int exponent = (aAbs >> significandBits) - exponentBias;
26 const rep_t significand = (aAbs & significandMask) | implicitBit;
27
28 // If 0 < exponent < significandBits, right shift to get the result.
29 if ((unsigned int)exponent < significandBits) {
30 return sign * (significand >> (significandBits - exponent));
31 }
32
33 // If exponent is negative, the result is zero.
34 else if (exponent < 0) {
35 return 0;
36 }
37
38 // If significandBits < exponent, left shift to get the result. This shift
39 // may end up being larger than the type width, which incurs undefined
40 // behavior, but the conversion itself is undefined in that case, so
41 // whatever the compiler decides to do is fine.
42 else {
43 return sign * (significand << (exponent - significandBits));
44 }
45}