blob: 6313c87af28d731a46a5575be9ad9301fa6e6c52 [file] [log] [blame]
Stephen Canon5c6d2ec2010-07-01 17:58:24 +00001//===-- lib/truncdfsf2.c - double -> single conversion ------------*- C -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements a fairly generic conversion from a wider to a narrower
11// IEEE-754 floating-point type in the default (round to nearest, ties to even)
12// rounding mode. The constants and types defined following the includes below
13// parameterize the conversion.
14//
15// This routine can be trivially adapted to support conversions to
16// half-precision or from quad-precision. It does not support types that don't
17// use the usual IEEE-754 interchange formats; specifically, some work would be
18// needed to adapt it to (for example) the Intel 80-bit format or PowerPC
19// double-double format.
20//
21// Note please, however, that this implementation is only intended to support
22// *narrowing* operations; if you need to convert to a *wider* floating-point
23// type (e.g. float -> double), then this routine will not do what you want it
24// to.
25//
26// It also requires that integer types at least as large as both formats
27// are available on the target platform; this may pose a problem when trying
28// to add support for quad on some 32-bit systems, for example.
29//
30// Finally, the following assumptions are made:
31//
32// 1. floating-point types and integer types have the same endianness on the
33// target platform
34//
35// 2. quiet NaNs, if supported, are indicated by the leading bit of the
36// significand field being set
37//
38//===----------------------------------------------------------------------===//
39
40#include <stdint.h>
41#include <limits.h>
42#include <stdbool.h>
43
44typedef double src_t;
45typedef uint64_t src_rep_t;
46#define SRC_REP_C UINT64_C
47static const int srcSigBits = 52;
48
49typedef float dst_t;
50typedef uint32_t dst_rep_t;
51#define DST_REP_C UINT32_C
52static const int dstSigBits = 23;
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
69dst_t __truncdfsf2(src_t a) {
70
71 // Various constants whose values follow from the type parameters.
72 // Any reasonable optimizer will fold and propagate all of these.
73 const int srcBits = sizeof(src_t)*CHAR_BIT;
74 const int srcExpBits = srcBits - srcSigBits - 1;
75 const int srcInfExp = (1 << srcExpBits) - 1;
76 const int srcExpBias = srcInfExp >> 1;
77
78 const src_rep_t srcMinNormal = SRC_REP_C(1) << srcSigBits;
79 const src_rep_t srcSignificandMask = srcMinNormal - 1;
80 const src_rep_t srcInfinity = (src_rep_t)srcInfExp << srcSigBits;
81 const src_rep_t srcSignMask = SRC_REP_C(1) << (srcSigBits + srcExpBits);
82 const src_rep_t srcAbsMask = srcSignMask - 1;
83 const src_rep_t srcQNaN = SRC_REP_C(1) << (srcSigBits - 1);
84 const src_rep_t srcNaNCode = srcQNaN - 1;
85 const src_rep_t roundMask = (SRC_REP_C(1) << (srcSigBits - dstSigBits)) - 1;
86 const src_rep_t halfway = SRC_REP_C(1) << (srcSigBits - dstSigBits - 1);
87
88 const int dstBits = sizeof(dst_t)*CHAR_BIT;
89 const int dstExpBits = dstBits - dstSigBits - 1;
90 const int dstInfExp = (1 << dstExpBits) - 1;
91 const int dstExpBias = dstInfExp >> 1;
92
93 const int underflowExponent = srcExpBias + 1 - dstExpBias;
94 const int overflowExponent = srcExpBias + dstInfExp - dstExpBias;
95 const src_rep_t underflow = (src_rep_t)underflowExponent << srcSigBits;
96 const src_rep_t overflow = (src_rep_t)overflowExponent << srcSigBits;
97
98 const dst_rep_t dstQNaN = DST_REP_C(1) << (dstSigBits - 1);
99 const dst_rep_t dstNaNCode = dstQNaN - 1;
100
101 // Break a into a sign and representation of the absolute value
102 const src_rep_t aRep = srcToRep(a);
103 const src_rep_t aAbs = aRep & srcAbsMask;
104 const src_rep_t sign = aRep & srcSignMask;
105 dst_rep_t absResult;
106
107 if (aAbs - underflow < aAbs - overflow) {
108 // The exponent of a is within the range of normal numbers in the
109 // destination format. We can convert by simply right-shifting with
110 // rounding and adjusting the exponent.
111 absResult = aAbs >> (srcSigBits - dstSigBits);
112 absResult -= (dst_rep_t)(srcExpBias - dstExpBias) << dstSigBits;
113
114 const src_rep_t roundBits = aAbs & roundMask;
115
116 // Round to nearest
117 if (roundBits > halfway)
118 absResult++;
119
120 // Ties to even
121 else if (roundBits == halfway)
122 absResult += absResult & 1;
123 }
124
125 else if (aAbs > srcInfinity) {
126 // a is NaN.
127 // Conjure the result by beginning with infinity, setting the qNaN
128 // bit and inserting the (truncated) trailing NaN field.
129 absResult = (dst_rep_t)dstInfExp << dstSigBits;
130 absResult |= dstQNaN;
131 absResult |= aAbs & dstNaNCode;
132 }
133
134 else if (aAbs > overflow) {
135 // a overflows to infinity.
136 absResult = (dst_rep_t)dstInfExp << dstSigBits;
137 }
138
139 else {
140 // a underflows on conversion to the destination type or is an exact
141 // zero. The result may be a denormal or zero. Extract the exponent
142 // to get the shift amount for the denormalization.
143 const int aExp = aAbs >> srcSigBits;
144 const int shift = srcExpBias - dstExpBias - aExp + 1;
145
146 const src_rep_t significand = aRep & srcSignificandMask | srcMinNormal;
147
148 // Right shift by the denormalization amount with sticky.
149 if (shift > srcSigBits) {
150 absResult = 0;
151 } else {
152 const bool sticky = significand << (srcBits - shift);
153 src_rep_t denormalizedSignificand = significand >> shift | sticky;
154 absResult = denormalizedSignificand >> (srcSigBits - dstSigBits);
155 const src_rep_t roundBits = denormalizedSignificand & roundMask;
156 // Round to nearest
157 if (roundBits > halfway)
158 absResult++;
159 // Ties to even
160 else if (roundBits == halfway)
161 absResult += absResult & 1;
162 }
163 }
164
165 // Apply the signbit to (dst_t)abs(a).
166 const dst_rep_t result = absResult | sign >> (srcBits - dstBits);
167 return dstFromRep(result);
168
169}