1//=-lib/fp_extend_impl.inc - low precision -> high precision conversion -*-- -// 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 a fairly generic conversion from a narrower to a wider 11// IEEE-754 floating-point type. The constants and types defined following the 12// includes below parameterize the conversion. 13// 14// It does not support types that don't use the usual IEEE-754 interchange 15// formats; specifically, some work would be needed to adapt it to 16// (for example) the Intel 80-bit format or PowerPC double-double format. 17// 18// Note please, however, that this implementation is only intended to support 19// *widening* operations; if you need to convert to a *narrower* floating-point 20// type (e.g. double -> float), then this routine will not do what you want it 21// to. 22// 23// It also requires that integer types at least as large as both formats 24// are available on the target platform; this may pose a problem when trying 25// to add support for quad on some 32-bit systems, for example. You also may 26// run into trouble finding an appropriate CLZ function for wide source types; 27// you will likely need to roll your own on some platforms. 28// 29// Finally, the following assumptions are made: 30// 31// 1. floating-point types and integer types have the same endianness on the 32// target platform 33// 34// 2. quiet NaNs, if supported, are indicated by the leading bit of the 35// significand field being set 36// 37//===----------------------------------------------------------------------===// 38 39#include "fp_extend.h" 40 41static inline dst_t __extendXfYf2__(src_t a) { 42 // Various constants whose values follow from the type parameters. 43 // Any reasonable optimizer will fold and propagate all of these. 44 const int srcBits = sizeof(src_t)*CHAR_BIT; 45 const int srcExpBits = srcBits - srcSigBits - 1; 46 const int srcInfExp = (1 << srcExpBits) - 1; 47 const int srcExpBias = srcInfExp >> 1; 48 49 const src_rep_t srcMinNormal = SRC_REP_C(1) << srcSigBits; 50 const src_rep_t srcInfinity = (src_rep_t)srcInfExp << srcSigBits; 51 const src_rep_t srcSignMask = SRC_REP_C(1) << (srcSigBits + srcExpBits); 52 const src_rep_t srcAbsMask = srcSignMask - 1; 53 const src_rep_t srcQNaN = SRC_REP_C(1) << (srcSigBits - 1); 54 const src_rep_t srcNaNCode = srcQNaN - 1; 55 56 const int dstBits = sizeof(dst_t)*CHAR_BIT; 57 const int dstExpBits = dstBits - dstSigBits - 1; 58 const int dstInfExp = (1 << dstExpBits) - 1; 59 const int dstExpBias = dstInfExp >> 1; 60 61 const dst_rep_t dstMinNormal = DST_REP_C(1) << dstSigBits; 62 63 // Break a into a sign and representation of the absolute value 64 const src_rep_t aRep = srcToRep(a); 65 const src_rep_t aAbs = aRep & srcAbsMask; 66 const src_rep_t sign = aRep & srcSignMask; 67 dst_rep_t absResult; 68 69 // If sizeof(src_rep_t) < sizeof(int), the subtraction result is promoted 70 // to (signed) int. To avoid that, explicitly cast to src_rep_t. 71 if ((src_rep_t)(aAbs - srcMinNormal) < srcInfinity - srcMinNormal) { 72 // a is a normal number. 73 // Extend to the destination type by shifting the significand and 74 // exponent into the proper position and rebiasing the exponent. 75 absResult = (dst_rep_t)aAbs << (dstSigBits - srcSigBits); 76 absResult += (dst_rep_t)(dstExpBias - srcExpBias) << dstSigBits; 77 } 78 79 else if (aAbs >= srcInfinity) { 80 // a is NaN or infinity. 81 // Conjure the result by beginning with infinity, then setting the qNaN 82 // bit (if needed) and right-aligning the rest of the trailing NaN 83 // payload field. 84 absResult = (dst_rep_t)dstInfExp << dstSigBits; 85 absResult |= (dst_rep_t)(aAbs & srcQNaN) << (dstSigBits - srcSigBits); 86 absResult |= (dst_rep_t)(aAbs & srcNaNCode) << (dstSigBits - srcSigBits); 87 } 88 89 else if (aAbs) { 90 // a is denormal. 91 // renormalize the significand and clear the leading bit, then insert 92 // the correct adjusted exponent in the destination type. 93 const int scale = src_rep_t_clz(aAbs) - src_rep_t_clz(srcMinNormal); 94 absResult = (dst_rep_t)aAbs << (dstSigBits - srcSigBits + scale); 95 absResult ^= dstMinNormal; 96 const int resultExponent = dstExpBias - srcExpBias - scale + 1; 97 absResult |= (dst_rep_t)resultExponent << dstSigBits; 98 } 99 100 else { 101 // a is zero. 102 absResult = 0; 103 } 104 105 // Apply the signbit to (dst_t)abs(a). 106 const dst_rep_t result = absResult | (dst_rep_t)sign << (dstBits - srcBits); 107 return dstFromRep(result); 108} 109