blob: a0e19ab6a8f68811bdceb8c2d1ce991efa6e21ad [file] [log] [blame]
Stephen Canon5c6d2ec2010-07-01 17:58:24 +00001//===-- lib/fp_lib.h - Floating-point utilities -------------------*- 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 is a configuration header for soft-float routines in compiler-rt.
Stephen Canon5c6d2ec2010-07-01 17:58:24 +000011// 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.
14//
Joerg Sonnenbergerbac1be22014-03-28 10:29:31 +000015// 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.
Stephen Canon5c6d2ec2010-07-01 17:58:24 +000018//
19//===----------------------------------------------------------------------===//
Stephen Canone5086322010-07-01 15:52:42 +000020
21#ifndef FP_LIB_HEADER
22#define FP_LIB_HEADER
23
24#include <stdint.h>
25#include <stdbool.h>
26#include <limits.h>
Daniel Dunbar0ae9d252011-11-15 18:34:44 +000027#include "int_lib.h"
Jordan Rupprechtb58a8aa2018-09-24 20:39:19 +000028#include "int_math.h"
Stephen Canone5086322010-07-01 15:52:42 +000029
Viktor Kutuzovd878d672014-07-08 08:52:57 +000030// x86_64 FreeBSD prior v9.3 define fixed-width types incorrectly in
31// 32-bit mode.
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
37# undef UINT64_C
38# define UINT64_C(c) (c ## ULL)
39# endif
40#endif
41
Stephen Canone5086322010-07-01 15:52:42 +000042#if defined SINGLE_PRECISION
Stephen Canone5086322010-07-01 15:52:42 +000043
44typedef uint32_t rep_t;
45typedef int32_t srep_t;
46typedef float fp_t;
47#define REP_C UINT32_C
48#define significandBits 23
49
Saleem Abdulrasool85a0d712015-10-10 21:21:28 +000050static __inline int rep_clz(rep_t a) {
Stephen Canone5086322010-07-01 15:52:42 +000051 return __builtin_clz(a);
52}
53
Stephen Canon12a7d092010-07-04 16:53:39 +000054// 32x32 --> 64 bit multiply
Saleem Abdulrasool85a0d712015-10-10 21:21:28 +000055static __inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {
Stephen Canon12a7d092010-07-04 16:53:39 +000056 const uint64_t product = (uint64_t)a*b;
57 *hi = product >> 32;
58 *lo = product;
59}
Joerg Sonnenberger36f321b2014-04-01 18:39:58 +000060COMPILER_RT_ABI fp_t __addsf3(fp_t a, fp_t b);
Stephen Canon12a7d092010-07-04 16:53:39 +000061
Stephen Canone5086322010-07-01 15:52:42 +000062#elif defined DOUBLE_PRECISION
Stephen Canone5086322010-07-01 15:52:42 +000063
64typedef uint64_t rep_t;
65typedef int64_t srep_t;
66typedef double fp_t;
67#define REP_C UINT64_C
68#define significandBits 52
69
Saleem Abdulrasool85a0d712015-10-10 21:21:28 +000070static __inline int rep_clz(rep_t a) {
Stephen Canone5086322010-07-01 15:52:42 +000071#if defined __LP64__
72 return __builtin_clzl(a);
73#else
74 if (a & REP_C(0xffffffff00000000))
Stephen Canonc41370b2010-07-26 18:17:00 +000075 return __builtin_clz(a >> 32);
Joerg Sonnenbergerbac1be22014-03-28 10:29:31 +000076 else
Stephen Canonc41370b2010-07-26 18:17:00 +000077 return 32 + __builtin_clz(a & REP_C(0xffffffff));
Stephen Canone5086322010-07-01 15:52:42 +000078#endif
79}
80
Stephen Canon12a7d092010-07-04 16:53:39 +000081#define loWord(a) (a & 0xffffffffU)
82#define hiWord(a) (a >> 32)
83
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.
Saleem Abdulrasool85a0d712015-10-10 21:21:28 +000087static __inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {
Stephen Canon12a7d092010-07-04 16:53:39 +000088 // 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;
99}
Joerg Sonnenberger2fd71ac2014-02-26 20:38:24 +0000100#undef loWord
101#undef hiWord
Stephen Canon12a7d092010-07-04 16:53:39 +0000102
Joerg Sonnenberger36f321b2014-04-01 18:39:58 +0000103COMPILER_RT_ABI fp_t __adddf3(fp_t a, fp_t b);
104
Joerg Sonnenbergerbac1be22014-03-28 10:29:31 +0000105#elif defined QUAD_PRECISION
106#if __LDBL_MANT_DIG__ == 113
107#define CRT_LDBL_128BIT
108typedef __uint128_t rep_t;
109typedef __int128_t srep_t;
110typedef 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
115
Saleem Abdulrasool85a0d712015-10-10 21:21:28 +0000116static __inline int rep_clz(rep_t a) {
Joerg Sonnenbergerbac1be22014-03-28 10:29:31 +0000117 const union
118 {
119 __uint128_t ll;
120#if _YUGA_BIG_ENDIAN
121 struct { uint64_t high, low; } s;
Stephen Canone5086322010-07-01 15:52:42 +0000122#else
Joerg Sonnenbergerbac1be22014-03-28 10:29:31 +0000123 struct { uint64_t low, high; } s;
124#endif
125 } uu = { .ll = a };
126
127 uint64_t word;
128 uint64_t add;
129
130 if (uu.s.high){
131 word = uu.s.high;
132 add = 0;
133 }
134 else{
135 word = uu.s.low;
136 add = 64;
137 }
138 return __builtin_clzll(word) + add;
139}
140
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)
148
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.
Saleem Abdulrasool85a0d712015-10-10 21:21:28 +0000152static __inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {
Joerg Sonnenbergerbac1be22014-03-28 10:29:31 +0000153
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);
170
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;
187
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);
194
195 *lo = r0 + (r1 << 64);
196 *hi = (r1 >> 64) +
197 (sum1 >> 96) +
198 (sum2 >> 64) +
199 (sum3 >> 32) +
200 sum4 +
201 (sum5 << 32) +
202 (sum6 << 64);
203}
204#undef Word_1
205#undef Word_2
206#undef Word_3
207#undef Word_4
208#undef Word_HiMask
209#undef Word_LoMask
210#undef Word_FullMask
211#endif // __LDBL_MANT_DIG__ == 113
212#else
213#error SINGLE_PRECISION, DOUBLE_PRECISION or QUAD_PRECISION must be defined.
Stephen Canone5086322010-07-01 15:52:42 +0000214#endif
215
Joerg Sonnenbergerbac1be22014-03-28 10:29:31 +0000216#if defined(SINGLE_PRECISION) || defined(DOUBLE_PRECISION) || defined(CRT_LDBL_128BIT)
Stephen Canone5086322010-07-01 15:52:42 +0000217#define typeWidth (sizeof(rep_t)*CHAR_BIT)
218#define exponentBits (typeWidth - significandBits - 1)
219#define maxExponent ((1 << exponentBits) - 1)
220#define exponentBias (maxExponent >> 1)
221
Stephen Canone5086322010-07-01 15:52:42 +0000222#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)
231
Saleem Abdulrasool85a0d712015-10-10 21:21:28 +0000232static __inline rep_t toRep(fp_t x) {
Stephen Canone5086322010-07-01 15:52:42 +0000233 const union { fp_t f; rep_t i; } rep = {.f = x};
234 return rep.i;
235}
236
Saleem Abdulrasool85a0d712015-10-10 21:21:28 +0000237static __inline fp_t fromRep(rep_t x) {
Stephen Canone5086322010-07-01 15:52:42 +0000238 const union { fp_t f; rep_t i; } rep = {.i = x};
239 return rep.f;
240}
241
Saleem Abdulrasool85a0d712015-10-10 21:21:28 +0000242static __inline int normalize(rep_t *significand) {
Stephen Canone5086322010-07-01 15:52:42 +0000243 const int shift = rep_clz(*significand) - rep_clz(implicitBit);
244 *significand <<= shift;
245 return 1 - shift;
246}
247
Saleem Abdulrasool85a0d712015-10-10 21:21:28 +0000248static __inline void wideLeftShift(rep_t *hi, rep_t *lo, int count) {
Stephen Canone5086322010-07-01 15:52:42 +0000249 *hi = *hi << count | *lo >> (typeWidth - count);
250 *lo = *lo << count;
251}
252
Saleem Abdulrasool85a0d712015-10-10 21:21:28 +0000253static __inline void wideRightShiftWithSticky(rep_t *hi, rep_t *lo, unsigned int count) {
Stephen Canone5086322010-07-01 15:52:42 +0000254 if (count < typeWidth) {
255 const bool sticky = *lo << (typeWidth - count);
256 *lo = *hi << (typeWidth - count) | *lo >> count | sticky;
257 *hi = *hi >> count;
258 }
259 else if (count < 2*typeWidth) {
260 const bool sticky = *hi << (2*typeWidth - count) | *lo;
261 *lo = *hi >> (count - typeWidth) | sticky;
262 *hi = 0;
263 } else {
264 const bool sticky = *hi | *lo;
265 *lo = sticky;
266 *hi = 0;
267 }
268}
Jordan Rupprechtb58a8aa2018-09-24 20:39:19 +0000269
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.
274static __inline fp_t __compiler_rt_logbX(fp_t x) {
275 rep_t rep = toRep(x);
276 int exp = (rep & exponentMask) >> significandBits;
277
278 // Abnormal cases:
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
284 } else {
285 return -x; // -inf: return -x
286 }
287 } else if (x == 0.0) {
288 // 0.0: return -inf
289 return fromRep(infRep | signBit);
290 }
291
292 if (exp != 0) {
293 // Normal number
294 return exp - exponentBias; // Unbias exponent
295 } else {
296 // Subnormal number; normalize and repeat
297 rep &= absMask;
298 const int shift = 1 - normalize(&rep);
299 exp = (rep & exponentMask) >> significandBits;
300 return exp - exponentBias - shift; // Unbias exponent
301 }
302}
303#endif
304
305#if defined(SINGLE_PRECISION)
306static __inline fp_t __compiler_rt_logbf(fp_t x) {
307 return __compiler_rt_logbX(x);
308}
309#elif defined(DOUBLE_PRECISION)
310static __inline fp_t __compiler_rt_logb(fp_t x) {
311 return __compiler_rt_logbX(x);
312}
313#elif defined(QUAD_PRECISION)
314 #if defined(CRT_LDBL_128BIT)
315static __inline fp_t __compiler_rt_logbl(fp_t x) {
316 return __compiler_rt_logbX(x);
317}
318 #else
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.
321static __inline long double __compiler_rt_logbl(long double x) {
322 return crt_logbl(x);
323}
324 #endif
Joerg Sonnenbergerbac1be22014-03-28 10:29:31 +0000325#endif
Joerg Sonnenbergerbfbb8bb2014-03-01 15:30:50 +0000326
Stephen Canone5086322010-07-01 15:52:42 +0000327#endif // FP_LIB_HEADER