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