blob: c0b628dffcdd90b56db067d0390c9609406a8dff [file] [log] [blame]
Stephen Canon5c6d2ec2010-07-01 17:58:24 +00001//===-- lib/extendsfdf2.c - single -> double conversion -----------*- C -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Howard Hinnant9ad441f2010-11-16 22:13:33 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Stephen Canon5c6d2ec2010-07-01 17:58:24 +00007//
8//===----------------------------------------------------------------------===//
9//
Stephen Canone5086322010-07-01 15:52:42 +000010// This file implements a fairly generic conversion from a narrower to a wider
Stephen Canon5c6d2ec2010-07-01 17:58:24 +000011// IEEE-754 floating-point type. The constants and types defined following the
12// includes below parameterize the conversion.
Stephen Canone5086322010-07-01 15:52:42 +000013//
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 Canon5c6d2ec2010-07-01 17:58:24 +000038//
39//===----------------------------------------------------------------------===//
Stephen Canone5086322010-07-01 15:52:42 +000040
Stephen Canon5c6d2ec2010-07-01 17:58:24 +000041#include <stdint.h>
42#include <limits.h>
Stephen Canone5086322010-07-01 15:52:42 +000043
Anton Korobeynikov1c5f89b2011-04-19 17:52:09 +000044#include "abi.h"
Anton Korobeynikov37b97d12011-04-19 17:51:24 +000045
Stephen Canone5086322010-07-01 15:52:42 +000046typedef float src_t;
47typedef uint32_t src_rep_t;
48#define SRC_REP_C UINT32_C
49static const int srcSigBits = 23;
50#define src_rep_t_clz __builtin_clz
51
52typedef double dst_t;
53typedef uint64_t dst_rep_t;
54#define DST_REP_C UINT64_C
55static const int dstSigBits = 52;
56
57// End of specialization parameters. Two helper routines for conversion to and
58// from the representation of floating-point data as integer values follow.
59
60static inline src_rep_t srcToRep(src_t x) {
61 const union { src_t f; src_rep_t i; } rep = {.f = x};
62 return rep.i;
63}
64
65static inline dst_t dstFromRep(dst_rep_t x) {
66 const union { dst_t f; dst_rep_t i; } rep = {.i = x};
67 return rep.f;
68}
69
70// End helper routines. Conversion implementation follows.
71
Anton Korobeynikov37b97d12011-04-19 17:51:24 +000072ARM_EABI_FNALIAS(f2d, extendsfdf2);
73
Stephen Canon5c6d2ec2010-07-01 17:58:24 +000074dst_t __extendsfdf2(src_t a) {
Stephen Canone5086322010-07-01 15:52:42 +000075
76 // Various constants whose values follow from the type parameters.
77 // Any reasonable optimizer will fold and propagate all of these.
78 const int srcBits = sizeof(src_t)*CHAR_BIT;
79 const int srcExpBits = srcBits - srcSigBits - 1;
80 const int srcInfExp = (1 << srcExpBits) - 1;
81 const int srcExpBias = srcInfExp >> 1;
Stephen Canon5c6d2ec2010-07-01 17:58:24 +000082
Stephen Canone5086322010-07-01 15:52:42 +000083 const src_rep_t srcMinNormal = SRC_REP_C(1) << srcSigBits;
84 const src_rep_t srcInfinity = (src_rep_t)srcInfExp << srcSigBits;
85 const src_rep_t srcSignMask = SRC_REP_C(1) << (srcSigBits + srcExpBits);
86 const src_rep_t srcAbsMask = srcSignMask - 1;
87 const src_rep_t srcQNaN = SRC_REP_C(1) << (srcSigBits - 1);
88 const src_rep_t srcNaNCode = srcQNaN - 1;
Stephen Canon5c6d2ec2010-07-01 17:58:24 +000089
Stephen Canone5086322010-07-01 15:52:42 +000090 const int dstBits = sizeof(dst_t)*CHAR_BIT;
91 const int dstExpBits = dstBits - dstSigBits - 1;
92 const int dstInfExp = (1 << dstExpBits) - 1;
93 const int dstExpBias = dstInfExp >> 1;
Stephen Canon5c6d2ec2010-07-01 17:58:24 +000094
Stephen Canone5086322010-07-01 15:52:42 +000095 const dst_rep_t dstMinNormal = DST_REP_C(1) << dstSigBits;
96
97 // Break a into a sign and representation of the absolute value
Stephen Canon5c6d2ec2010-07-01 17:58:24 +000098 const src_rep_t aRep = srcToRep(a);
99 const src_rep_t aAbs = aRep & srcAbsMask;
100 const src_rep_t sign = aRep & srcSignMask;
Stephen Canone5086322010-07-01 15:52:42 +0000101 dst_rep_t absResult;
102
103 if (aAbs - srcMinNormal < srcInfinity - srcMinNormal) {
104 // a is a normal number.
105 // Extend to the destination type by shifting the significand and
106 // exponent into the proper position and rebiasing the exponent.
107 absResult = (dst_rep_t)aAbs << (dstSigBits - srcSigBits);
108 absResult += (dst_rep_t)(dstExpBias - srcExpBias) << dstSigBits;
109 }
110
111 else if (aAbs >= srcInfinity) {
112 // a is NaN or infinity.
113 // Conjure the result by beginning with infinity, then setting the qNaN
Stephen Canon5c6d2ec2010-07-01 17:58:24 +0000114 // bit (if needed) and right-aligning the rest of the trailing NaN
115 // payload field.
Stephen Canone5086322010-07-01 15:52:42 +0000116 absResult = (dst_rep_t)dstInfExp << dstSigBits;
117 absResult |= (dst_rep_t)(aAbs & srcQNaN) << (dstSigBits - srcSigBits);
Stephen Canon5c6d2ec2010-07-01 17:58:24 +0000118 absResult |= aAbs & srcNaNCode;
Stephen Canone5086322010-07-01 15:52:42 +0000119 }
120
121 else if (aAbs) {
122 // a is denormal.
123 // renormalize the significand and clear the leading bit, then insert
124 // the correct adjusted exponent in the destination type.
125 const int scale = src_rep_t_clz(aAbs) - src_rep_t_clz(srcMinNormal);
126 absResult = (dst_rep_t)aAbs << (dstSigBits - srcSigBits + scale);
127 absResult ^= dstMinNormal;
128 const int resultExponent = dstExpBias - srcExpBias - scale + 1;
129 absResult |= (dst_rep_t)resultExponent << dstSigBits;
130 }
131
132 else {
133 // a is zero.
134 absResult = 0;
135 }
136
137 // Apply the signbit to (dst_t)abs(a).
Stephen Canon5c6d2ec2010-07-01 17:58:24 +0000138 const dst_rep_t result = absResult | (dst_rep_t)sign << (dstBits - srcBits);
Stephen Canone5086322010-07-01 15:52:42 +0000139 return dstFromRep(result);
140}