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
40// The source type may use a usual IEEE-754 interchange format or Intel 80-bit
41// format. In particular, for the source type srcSigFracBits may be not equal to
42// srcSigBits. The destination type is assumed to be one of IEEE-754 standard
43// types.
44static __inline dst_t __extendXfYf2__(src_t a) {
45 // Various constants whose values follow from the type parameters.
46 // Any reasonable optimizer will fold and propagate all of these.
47 const int srcInfExp = (1 << srcExpBits) - 1;
48 const int srcExpBias = srcInfExp >> 1;
49
50 const int dstInfExp = (1 << dstExpBits) - 1;
51 const int dstExpBias = dstInfExp >> 1;
52
53 // Break a into a sign and representation of the absolute value.
54 const src_rep_t aRep = srcToRep(x: a);
55 const src_rep_t srcSign = extract_sign_from_src(x: aRep);
56 const src_rep_t srcExp = extract_exp_from_src(x: aRep);
57 const src_rep_t srcSigFrac = extract_sig_frac_from_src(x: aRep);
58
59 dst_rep_t dstSign = srcSign;
60 dst_rep_t dstExp;
61 dst_rep_t dstSigFrac;
62
63 if (srcExp >= 1 && srcExp < (src_rep_t)srcInfExp) {
64 // a is a normal number.
65 dstExp = (dst_rep_t)srcExp + (dst_rep_t)(dstExpBias - srcExpBias);
66 dstSigFrac = (dst_rep_t)srcSigFrac << (dstSigFracBits - srcSigFracBits);
67 }
68
69 else if (srcExp == srcInfExp) {
70 // a is NaN or infinity.
71 dstExp = dstInfExp;
72 dstSigFrac = (dst_rep_t)srcSigFrac << (dstSigFracBits - srcSigFracBits);
73 }
74
75 else if (srcSigFrac) {
76 // a is denormal.
77 if (srcExpBits == dstExpBits) {
78 // The exponent fields are identical and this is a denormal number, so all
79 // the non-significand bits are zero. In particular, this branch is always
80 // taken when we extend a denormal F80 to F128.
81 dstExp = 0;
82 dstSigFrac = ((dst_rep_t)srcSigFrac) << (dstSigFracBits - srcSigFracBits);
83 } else {
84#ifndef src_rep_t_clz
85 // If src_rep_t_clz is not defined this branch must be unreachable.
86 __builtin_unreachable();
87#else
88 // Renormalize the significand and clear the leading bit.
89 // For F80 -> F128 this codepath is unused.
90 const int scale = clz_in_sig_frac(sigFrac: srcSigFrac) + 1;
91 dstExp = dstExpBias - srcExpBias - scale + 1;
92 dstSigFrac = (dst_rep_t)srcSigFrac
93 << (dstSigFracBits - srcSigFracBits + scale);
94 const dst_rep_t dstMinNormal = DST_REP_C(1) << (dstBits - 1 - dstExpBits);
95 dstSigFrac ^= dstMinNormal;
96#endif
97 }
98 }
99
100 else {
101 // a is zero.
102 dstExp = 0;
103 dstSigFrac = 0;
104 }
105
106 const dst_rep_t result = construct_dst_rep(sign: dstSign, exp: dstExp, sigFrac: dstSigFrac);
107 return dstFromRep(x: result);
108}
109

source code of compiler-rt/lib/builtins/fp_extend_impl.inc