fp_extend_impl.inc revision 5d71de26cedae3dafc17449fe0182045c0bd20e8
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 (aAbs - srcMinNormal < srcInfinity - srcMinNormal) {
70        // a is a normal number.
71        // Extend to the destination type by shifting the significand and
72        // exponent into the proper position and rebiasing the exponent.
73        absResult = (dst_rep_t)aAbs << (dstSigBits - srcSigBits);
74        absResult += (dst_rep_t)(dstExpBias - srcExpBias) << dstSigBits;
75    }
76
77    else if (aAbs >= srcInfinity) {
78        // a is NaN or infinity.
79        // Conjure the result by beginning with infinity, then setting the qNaN
80        // bit (if needed) and right-aligning the rest of the trailing NaN
81        // payload field.
82        absResult = (dst_rep_t)dstInfExp << dstSigBits;
83        absResult |= (dst_rep_t)(aAbs & srcQNaN) << (dstSigBits - srcSigBits);
84        absResult |= (dst_rep_t)(aAbs & srcNaNCode) << (dstSigBits - srcSigBits);
85    }
86
87    else if (aAbs) {
88        // a is denormal.
89        // renormalize the significand and clear the leading bit, then insert
90        // the correct adjusted exponent in the destination type.
91        const int scale = src_rep_t_clz(aAbs) - src_rep_t_clz(srcMinNormal);
92        absResult = (dst_rep_t)aAbs << (dstSigBits - srcSigBits + scale);
93        absResult ^= dstMinNormal;
94        const int resultExponent = dstExpBias - srcExpBias - scale + 1;
95        absResult |= (dst_rep_t)resultExponent << dstSigBits;
96    }
97
98    else {
99        // a is zero.
100        absResult = 0;
101    }
102
103    // Apply the signbit to (dst_t)abs(a).
104    const dst_rep_t result = absResult | (dst_rep_t)sign << (dstBits - srcBits);
105    return dstFromRep(result);
106}
107