blob: 9466de7808089a88512d9f75a57bb0cec5ccd873 [file] [log] [blame]
Stephen Canon74eaf1f2010-07-01 17:58:241//===-- lib/extendsfdf2.c - single -> double conversion -----------*- C -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Howard Hinnant5b791f62010-11-16 22:13:335// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Stephen Canon74eaf1f2010-07-01 17:58:247//
8//===----------------------------------------------------------------------===//
9//
Stephen Canonb1fdde12010-07-01 15:52:4210// This file implements a fairly generic conversion from a narrower to a wider
Stephen Canon74eaf1f2010-07-01 17:58:2411// IEEE-754 floating-point type. The constants and types defined following the
12// includes below parameterize the conversion.
Stephen Canonb1fdde12010-07-01 15:52:4213//
14// This routine can be trivially adapted to support conversions from
15// half-precision or to quad-precision. It does not support types that don't
16// use the usual IEEE-754 interchange formats; specifically, some work would be
17// needed to adapt it to (for example) the Intel 80-bit format or PowerPC
18// double-double format.
19//
20// Note please, however, that this implementation is only intended to support
21// *widening* operations; if you need to convert to a *narrower* floating-point
22// type (e.g. double -> float), then this routine will not do what you want it
23// to.
24//
25// It also requires that integer types at least as large as both formats
26// are available on the target platform; this may pose a problem when trying
27// to add support for quad on some 32-bit systems, for example. You also may
28// run into trouble finding an appropriate CLZ function for wide source types;
29// you will likely need to roll your own on some platforms.
30//
31// Finally, the following assumptions are made:
32//
33// 1. floating-point types and integer types have the same endianness on the
34// target platform
35//
36// 2. quiet NaNs, if supported, are indicated by the leading bit of the
37// significand field being set
Stephen Canon74eaf1f2010-07-01 17:58:2438//
39//===----------------------------------------------------------------------===//
Stephen Canonb1fdde12010-07-01 15:52:4240
Daniel Dunbarb6f75f72011-11-15 18:34:4441#include "int_lib.h"
Anton Korobeynikov75e3c192011-04-19 17:51:2442
Stephen Canonb1fdde12010-07-01 15:52:4243typedef float src_t;
44typedef uint32_t src_rep_t;
45#define SRC_REP_C UINT32_C
46static const int srcSigBits = 23;
47#define src_rep_t_clz __builtin_clz
48
49typedef double dst_t;
50typedef uint64_t dst_rep_t;
51#define DST_REP_C UINT64_C
52static const int dstSigBits = 52;
53
54// End of specialization parameters. Two helper routines for conversion to and
55// from the representation of floating-point data as integer values follow.
56
57static inline src_rep_t srcToRep(src_t x) {
58 const union { src_t f; src_rep_t i; } rep = {.f = x};
59 return rep.i;
60}
61
62static inline dst_t dstFromRep(dst_rep_t x) {
63 const union { dst_t f; dst_rep_t i; } rep = {.i = x};
64 return rep.f;
65}
66
67// End helper routines. Conversion implementation follows.
68
Anton Korobeynikov75e3c192011-04-19 17:51:2469ARM_EABI_FNALIAS(f2d, extendsfdf2);
70
Stephen Canon74eaf1f2010-07-01 17:58:2471dst_t __extendsfdf2(src_t a) {
Stephen Canonb1fdde12010-07-01 15:52:4272
73 // Various constants whose values follow from the type parameters.
74 // Any reasonable optimizer will fold and propagate all of these.
75 const int srcBits = sizeof(src_t)*CHAR_BIT;
76 const int srcExpBits = srcBits - srcSigBits - 1;
77 const int srcInfExp = (1 << srcExpBits) - 1;
78 const int srcExpBias = srcInfExp >> 1;
Stephen Canon74eaf1f2010-07-01 17:58:2479
Stephen Canonb1fdde12010-07-01 15:52:4280 const src_rep_t srcMinNormal = SRC_REP_C(1) << srcSigBits;
81 const src_rep_t srcInfinity = (src_rep_t)srcInfExp << srcSigBits;
82 const src_rep_t srcSignMask = SRC_REP_C(1) << (srcSigBits + srcExpBits);
83 const src_rep_t srcAbsMask = srcSignMask - 1;
84 const src_rep_t srcQNaN = SRC_REP_C(1) << (srcSigBits - 1);
85 const src_rep_t srcNaNCode = srcQNaN - 1;
Stephen Canon74eaf1f2010-07-01 17:58:2486
Stephen Canonb1fdde12010-07-01 15:52:4287 const int dstBits = sizeof(dst_t)*CHAR_BIT;
88 const int dstExpBits = dstBits - dstSigBits - 1;
89 const int dstInfExp = (1 << dstExpBits) - 1;
90 const int dstExpBias = dstInfExp >> 1;
Stephen Canon74eaf1f2010-07-01 17:58:2491
Stephen Canonb1fdde12010-07-01 15:52:4292 const dst_rep_t dstMinNormal = DST_REP_C(1) << dstSigBits;
93
94 // Break a into a sign and representation of the absolute value
Stephen Canon74eaf1f2010-07-01 17:58:2495 const src_rep_t aRep = srcToRep(a);
96 const src_rep_t aAbs = aRep & srcAbsMask;
97 const src_rep_t sign = aRep & srcSignMask;
Stephen Canonb1fdde12010-07-01 15:52:4298 dst_rep_t absResult;
99
100 if (aAbs - srcMinNormal < srcInfinity - srcMinNormal) {
101 // a is a normal number.
102 // Extend to the destination type by shifting the significand and
103 // exponent into the proper position and rebiasing the exponent.
104 absResult = (dst_rep_t)aAbs << (dstSigBits - srcSigBits);
105 absResult += (dst_rep_t)(dstExpBias - srcExpBias) << dstSigBits;
106 }
107
108 else if (aAbs >= srcInfinity) {
109 // a is NaN or infinity.
110 // Conjure the result by beginning with infinity, then setting the qNaN
Stephen Canon74eaf1f2010-07-01 17:58:24111 // bit (if needed) and right-aligning the rest of the trailing NaN
112 // payload field.
Stephen Canonb1fdde12010-07-01 15:52:42113 absResult = (dst_rep_t)dstInfExp << dstSigBits;
114 absResult |= (dst_rep_t)(aAbs & srcQNaN) << (dstSigBits - srcSigBits);
Stephen Canon74eaf1f2010-07-01 17:58:24115 absResult |= aAbs & srcNaNCode;
Stephen Canonb1fdde12010-07-01 15:52:42116 }
117
118 else if (aAbs) {
119 // a is denormal.
120 // renormalize the significand and clear the leading bit, then insert
121 // the correct adjusted exponent in the destination type.
122 const int scale = src_rep_t_clz(aAbs) - src_rep_t_clz(srcMinNormal);
123 absResult = (dst_rep_t)aAbs << (dstSigBits - srcSigBits + scale);
124 absResult ^= dstMinNormal;
125 const int resultExponent = dstExpBias - srcExpBias - scale + 1;
126 absResult |= (dst_rep_t)resultExponent << dstSigBits;
127 }
128
129 else {
130 // a is zero.
131 absResult = 0;
132 }
133
134 // Apply the signbit to (dst_t)abs(a).
Stephen Canon74eaf1f2010-07-01 17:58:24135 const dst_rep_t result = absResult | (dst_rep_t)sign << (dstBits - srcBits);
Stephen Canonb1fdde12010-07-01 15:52:42136 return dstFromRep(result);
137}