1 //===-- lib/fp_lib.h - Floating-point utilities -------------------*- C -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a configuration header for soft-float routines in compiler-rt.
11 // This file does not provide any part of the compiler-rt interface, but defines
12 // many useful constants and utility routines that are used in the
13 // implementation of the soft-float routines in compiler-rt.
15 // Assumes that float, double and long double correspond to the IEEE-754
16 // binary32, binary64 and binary 128 types, respectively, and that integer
17 // endianness matches floating point endianness on the target platform.
19 //===----------------------------------------------------------------------===//
30 // x86_64 FreeBSD prior v9.3 define fixed-width types incorrectly in
32 #if defined(__FreeBSD__) && defined(__i386__)
33 # include <sys/param.h>
34 # if __FreeBSD_version < 903000 // v9.3
35 # define uint64_t unsigned long long
36 # define int64_t long long
38 # define UINT64_C(c) (c ## ULL)
42 #if defined SINGLE_PRECISION
44 typedef uint32_t rep_t;
45 typedef int32_t srep_t;
47 #define REP_C UINT32_C
48 #define significandBits 23
50 static __inline int rep_clz(rep_t a) {
51 return __builtin_clz(a);
54 // 32x32 --> 64 bit multiply
55 static __inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {
56 const uint64_t product = (uint64_t)a*b;
60 COMPILER_RT_ABI fp_t __addsf3(fp_t a, fp_t b);
62 #elif defined DOUBLE_PRECISION
64 typedef uint64_t rep_t;
65 typedef int64_t srep_t;
67 #define REP_C UINT64_C
68 #define significandBits 52
70 static __inline int rep_clz(rep_t a) {
72 return __builtin_clzl(a);
74 if (a & REP_C(0xffffffff00000000))
75 return __builtin_clz(a >> 32);
77 return 32 + __builtin_clz(a & REP_C(0xffffffff));
81 #define loWord(a) (a & 0xffffffffU)
82 #define hiWord(a) (a >> 32)
84 // 64x64 -> 128 wide multiply for platforms that don't have such an operation;
85 // many 64-bit platforms have this operation, but they tend to have hardware
86 // floating-point, so we don't bother with a special case for them here.
87 static __inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {
88 // Each of the component 32x32 -> 64 products
89 const uint64_t plolo = loWord(a) * loWord(b);
90 const uint64_t plohi = loWord(a) * hiWord(b);
91 const uint64_t philo = hiWord(a) * loWord(b);
92 const uint64_t phihi = hiWord(a) * hiWord(b);
93 // Sum terms that contribute to lo in a way that allows us to get the carry
94 const uint64_t r0 = loWord(plolo);
95 const uint64_t r1 = hiWord(plolo) + loWord(plohi) + loWord(philo);
96 *lo = r0 + (r1 << 32);
97 // Sum terms contributing to hi with the carry from lo
98 *hi = hiWord(plohi) + hiWord(philo) + hiWord(r1) + phihi;
103 COMPILER_RT_ABI fp_t __adddf3(fp_t a, fp_t b);
105 #elif defined QUAD_PRECISION
106 #if __LDBL_MANT_DIG__ == 113
107 #define CRT_LDBL_128BIT
108 typedef __uint128_t rep_t;
109 typedef __int128_t srep_t;
110 typedef long double fp_t;
111 #define REP_C (__uint128_t)
112 // Note: Since there is no explicit way to tell compiler the constant is a
113 // 128-bit integer, we let the constant be casted to 128-bit integer
114 #define significandBits 112
116 static __inline int rep_clz(rep_t a) {
121 struct { uint64_t high, low; } s;
123 struct { uint64_t low, high; } s;
138 return __builtin_clzll(word) + add;
141 #define Word_LoMask UINT64_C(0x00000000ffffffff)
142 #define Word_HiMask UINT64_C(0xffffffff00000000)
143 #define Word_FullMask UINT64_C(0xffffffffffffffff)
144 #define Word_1(a) (uint64_t)((a >> 96) & Word_LoMask)
145 #define Word_2(a) (uint64_t)((a >> 64) & Word_LoMask)
146 #define Word_3(a) (uint64_t)((a >> 32) & Word_LoMask)
147 #define Word_4(a) (uint64_t)(a & Word_LoMask)
149 // 128x128 -> 256 wide multiply for platforms that don't have such an operation;
150 // many 64-bit platforms have this operation, but they tend to have hardware
151 // floating-point, so we don't bother with a special case for them here.
152 static __inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {
154 const uint64_t product11 = Word_1(a) * Word_1(b);
155 const uint64_t product12 = Word_1(a) * Word_2(b);
156 const uint64_t product13 = Word_1(a) * Word_3(b);
157 const uint64_t product14 = Word_1(a) * Word_4(b);
158 const uint64_t product21 = Word_2(a) * Word_1(b);
159 const uint64_t product22 = Word_2(a) * Word_2(b);
160 const uint64_t product23 = Word_2(a) * Word_3(b);
161 const uint64_t product24 = Word_2(a) * Word_4(b);
162 const uint64_t product31 = Word_3(a) * Word_1(b);
163 const uint64_t product32 = Word_3(a) * Word_2(b);
164 const uint64_t product33 = Word_3(a) * Word_3(b);
165 const uint64_t product34 = Word_3(a) * Word_4(b);
166 const uint64_t product41 = Word_4(a) * Word_1(b);
167 const uint64_t product42 = Word_4(a) * Word_2(b);
168 const uint64_t product43 = Word_4(a) * Word_3(b);
169 const uint64_t product44 = Word_4(a) * Word_4(b);
171 const __uint128_t sum0 = (__uint128_t)product44;
172 const __uint128_t sum1 = (__uint128_t)product34 +
173 (__uint128_t)product43;
174 const __uint128_t sum2 = (__uint128_t)product24 +
175 (__uint128_t)product33 +
176 (__uint128_t)product42;
177 const __uint128_t sum3 = (__uint128_t)product14 +
178 (__uint128_t)product23 +
179 (__uint128_t)product32 +
180 (__uint128_t)product41;
181 const __uint128_t sum4 = (__uint128_t)product13 +
182 (__uint128_t)product22 +
183 (__uint128_t)product31;
184 const __uint128_t sum5 = (__uint128_t)product12 +
185 (__uint128_t)product21;
186 const __uint128_t sum6 = (__uint128_t)product11;
188 const __uint128_t r0 = (sum0 & Word_FullMask) +
189 ((sum1 & Word_LoMask) << 32);
190 const __uint128_t r1 = (sum0 >> 64) +
191 ((sum1 >> 32) & Word_FullMask) +
192 (sum2 & Word_FullMask) +
193 ((sum3 << 32) & Word_HiMask);
195 *lo = r0 + (r1 << 64);
211 #endif // __LDBL_MANT_DIG__ == 113
213 #error SINGLE_PRECISION, DOUBLE_PRECISION or QUAD_PRECISION must be defined.
216 #if defined(SINGLE_PRECISION) || defined(DOUBLE_PRECISION) || defined(CRT_LDBL_128BIT)
217 #define typeWidth (sizeof(rep_t)*CHAR_BIT)
218 #define exponentBits (typeWidth - significandBits - 1)
219 #define maxExponent ((1 << exponentBits) - 1)
220 #define exponentBias (maxExponent >> 1)
222 #define implicitBit (REP_C(1) << significandBits)
223 #define significandMask (implicitBit - 1U)
224 #define signBit (REP_C(1) << (significandBits + exponentBits))
225 #define absMask (signBit - 1U)
226 #define exponentMask (absMask ^ significandMask)
227 #define oneRep ((rep_t)exponentBias << significandBits)
228 #define infRep exponentMask
229 #define quietBit (implicitBit >> 1)
230 #define qnanRep (exponentMask | quietBit)
232 static __inline rep_t toRep(fp_t x) {
233 const union { fp_t f; rep_t i; } rep = {.f = x};
237 static __inline fp_t fromRep(rep_t x) {
238 const union { fp_t f; rep_t i; } rep = {.i = x};
242 static __inline int normalize(rep_t *significand) {
243 const int shift = rep_clz(*significand) - rep_clz(implicitBit);
244 *significand <<= shift;
248 static __inline void wideLeftShift(rep_t *hi, rep_t *lo, int count) {
249 *hi = *hi << count | *lo >> (typeWidth - count);
253 static __inline void wideRightShiftWithSticky(rep_t *hi, rep_t *lo, unsigned int count) {
254 if (count < typeWidth) {
255 const bool sticky = *lo << (typeWidth - count);
256 *lo = *hi << (typeWidth - count) | *lo >> count | sticky;
259 else if (count < 2*typeWidth) {
260 const bool sticky = *hi << (2*typeWidth - count) | *lo;
261 *lo = *hi >> (count - typeWidth) | sticky;
264 const bool sticky = *hi | *lo;
270 // Implements logb methods (logb, logbf, logbl) for IEEE-754. This avoids
271 // pulling in a libm dependency from compiler-rt, but is not meant to replace
272 // it (i.e. code calling logb() should get the one from libm, not this), hence
273 // the __compiler_rt prefix.
274 static __inline fp_t __compiler_rt_logbX(fp_t x) {
275 rep_t rep = toRep(x);
276 int exp = (rep & exponentMask) >> significandBits;
279 // 1) +/- inf returns +inf; NaN returns NaN
280 // 2) 0.0 returns -inf
281 if (exp == maxExponent) {
282 if (((rep & signBit) == 0) || (x != x)) {
283 return x; // NaN or +inf: return x
285 return -x; // -inf: return -x
287 } else if (x == 0.0) {
289 return fromRep(infRep | signBit);
294 return exp - exponentBias; // Unbias exponent
296 // Subnormal number; normalize and repeat
298 const int shift = 1 - normalize(&rep);
299 exp = (rep & exponentMask) >> significandBits;
300 return exp - exponentBias - shift; // Unbias exponent
305 #if defined(SINGLE_PRECISION)
306 static __inline fp_t __compiler_rt_logbf(fp_t x) {
307 return __compiler_rt_logbX(x);
309 #elif defined(DOUBLE_PRECISION)
310 static __inline fp_t __compiler_rt_logb(fp_t x) {
311 return __compiler_rt_logbX(x);
313 #elif defined(QUAD_PRECISION)
314 #if defined(CRT_LDBL_128BIT)
315 static __inline fp_t __compiler_rt_logbl(fp_t x) {
316 return __compiler_rt_logbX(x);
319 // The generic implementation only works for ieee754 floating point. For other
320 // floating point types, continue to rely on the libm implementation for now.
321 static __inline long double __compiler_rt_logbl(long double x) {
327 #endif // FP_LIB_HEADER