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