13d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar//===-- lib/floatunditf.c - uint -> quad-precision conversion -----*- C -*-===//
23d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar//
33d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar//                     The LLVM Compiler Infrastructure
43d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar//
53d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar// This file is dual licensed under the MIT and the University of Illinois Open
63d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar// Source Licenses. See LICENSE.TXT for details.
73d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar//
83d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar//===----------------------------------------------------------------------===//
93d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar//
103d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar// This file implements du_int to quad-precision conversion for the
113d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar// compiler-rt library in the IEEE-754 default round-to-nearest, ties-to-even
123d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar// mode.
133d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar//
143d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar//===----------------------------------------------------------------------===//
153d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar
163d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar#define QUAD_PRECISION
173d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar#include "fp_lib.h"
183d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar
193d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar#if defined(CRT_HAS_128BIT) && defined(CRT_LDBL_128BIT)
203d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga NainarCOMPILER_RT_ABI fp_t __floatunditf(du_int a) {
213d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar
223d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar    const int aWidth = sizeof a * CHAR_BIT;
233d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar
243d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar    // Handle zero as a special case to protect clz
253d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar    if (a == 0) return fromRep(0);
263d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar
273d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar    // Exponent of (fp_t)a is the width of abs(a).
283d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar    const int exponent = (aWidth - 1) - __builtin_clzll(a);
293d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar    rep_t result;
303d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar
313d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar    // Shift a into the significand field and clear the implicit bit.
323d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar    const int shift = significandBits - exponent;
333d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar    result = (rep_t)a << shift ^ implicitBit;
343d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar
353d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar    // Insert the exponent
363d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar    result += (rep_t)(exponent + exponentBias) << significandBits;
373d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar    return fromRep(result);
383d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar}
393d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar
403d763c0d3700e73b3aead8e65e04ec28efc56138Pirama Arumuga Nainar#endif
41