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