Joerg Sonnenberger | ed35a3e | 2014-09-16 20:34:41 +0000 | [diff] [blame] | 1 | //===-- lib/floatsitf.c - integer -> quad-precision 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 integer to quad-precision conversion for the |
| 11 | // compiler-rt library in the IEEE-754 default round-to-nearest, ties-to-even |
| 12 | // mode. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #define QUAD_PRECISION |
| 17 | #include "fp_lib.h" |
| 18 | |
| 19 | #if defined(CRT_HAS_128BIT) && defined(CRT_LDBL_128BIT) |
| 20 | COMPILER_RT_ABI fp_t __floatsitf(int a) { |
| 21 | |
| 22 | const int aWidth = sizeof a * CHAR_BIT; |
| 23 | |
| 24 | // Handle zero as a special case to protect clz |
| 25 | if (a == 0) |
| 26 | return fromRep(0); |
| 27 | |
| 28 | // All other cases begin by extracting the sign and absolute value of a |
| 29 | rep_t sign = 0; |
| 30 | unsigned aAbs = (unsigned)a; |
| 31 | if (a < 0) { |
| 32 | sign = signBit; |
Sergey Dmitrouk | a2ce083 | 2015-07-31 13:32:09 +0000 | [diff] [blame] | 33 | aAbs = ~(unsigned)a + 1U; |
Joerg Sonnenberger | ed35a3e | 2014-09-16 20:34:41 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | // Exponent of (fp_t)a is the width of abs(a). |
Sergey Dmitrouk | a2ce083 | 2015-07-31 13:32:09 +0000 | [diff] [blame] | 37 | const int exponent = (aWidth - 1) - __builtin_clz(aAbs); |
Joerg Sonnenberger | ed35a3e | 2014-09-16 20:34:41 +0000 | [diff] [blame] | 38 | rep_t result; |
| 39 | |
Sergey Dmitrouk | a2ce083 | 2015-07-31 13:32:09 +0000 | [diff] [blame] | 40 | // Shift a into the significand field and clear the implicit bit. |
Joerg Sonnenberger | ed35a3e | 2014-09-16 20:34:41 +0000 | [diff] [blame] | 41 | const int shift = significandBits - exponent; |
| 42 | result = (rep_t)aAbs << shift ^ implicitBit; |
| 43 | |
| 44 | // Insert the exponent |
| 45 | result += (rep_t)(exponent + exponentBias) << significandBits; |
| 46 | // Insert the sign bit and return |
| 47 | return fromRep(result | sign); |
| 48 | } |
| 49 | |
| 50 | #endif |