Sergey Dmitrouk | 5708a23 | 2015-08-18 13:43:37 +0000 | [diff] [blame] | 1 | //===-- lib/floatditf.c - integer -> quad-precision conversion ----*- C -*-===// |
| 2 | // |
Chandler Carruth | 7a739a0 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Sergey Dmitrouk | 5708a23 | 2015-08-18 13:43:37 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements di_int to quad-precision conversion for the |
| 10 | // compiler-rt library in the IEEE-754 default round-to-nearest, ties-to-even |
| 11 | // mode. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #define QUAD_PRECISION |
| 16 | #include "fp_lib.h" |
| 17 | |
| 18 | #if defined(CRT_HAS_128BIT) && defined(CRT_LDBL_128BIT) |
| 19 | COMPILER_RT_ABI fp_t __floatditf(di_int a) { |
| 20 | |
| 21 | const int aWidth = sizeof a * CHAR_BIT; |
| 22 | |
| 23 | // Handle zero as a special case to protect clz |
| 24 | if (a == 0) |
| 25 | return fromRep(0); |
| 26 | |
| 27 | // All other cases begin by extracting the sign and absolute value of a |
| 28 | rep_t sign = 0; |
| 29 | du_int aAbs = (du_int)a; |
| 30 | if (a < 0) { |
| 31 | sign = signBit; |
| 32 | aAbs = ~(du_int)a + 1U; |
| 33 | } |
| 34 | |
| 35 | // Exponent of (fp_t)a is the width of abs(a). |
| 36 | const int exponent = (aWidth - 1) - __builtin_clzll(aAbs); |
| 37 | rep_t result; |
| 38 | |
| 39 | // Shift a into the significand field, rounding if it is a right-shift |
| 40 | const int shift = significandBits - exponent; |
| 41 | result = (rep_t)aAbs << shift ^ implicitBit; |
| 42 | |
| 43 | // Insert the exponent |
| 44 | result += (rep_t)(exponent + exponentBias) << significandBits; |
| 45 | // Insert the sign bit and return |
| 46 | return fromRep(result | sign); |
| 47 | } |
| 48 | |
| 49 | #endif |