1*7c3d14c8STreehugger Robot //===-- lib/fp_lib.h - Floating-point utilities -------------------*- C -*-===//
2*7c3d14c8STreehugger Robot //
3*7c3d14c8STreehugger Robot // The LLVM Compiler Infrastructure
4*7c3d14c8STreehugger Robot //
5*7c3d14c8STreehugger Robot // This file is dual licensed under the MIT and the University of Illinois Open
6*7c3d14c8STreehugger Robot // Source Licenses. See LICENSE.TXT for details.
7*7c3d14c8STreehugger Robot //
8*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
9*7c3d14c8STreehugger Robot //
10*7c3d14c8STreehugger Robot // This file is a configuration header for soft-float routines in compiler-rt.
11*7c3d14c8STreehugger Robot // This file does not provide any part of the compiler-rt interface, but defines
12*7c3d14c8STreehugger Robot // many useful constants and utility routines that are used in the
13*7c3d14c8STreehugger Robot // implementation of the soft-float routines in compiler-rt.
14*7c3d14c8STreehugger Robot //
15*7c3d14c8STreehugger Robot // Assumes that float, double and long double correspond to the IEEE-754
16*7c3d14c8STreehugger Robot // binary32, binary64 and binary 128 types, respectively, and that integer
17*7c3d14c8STreehugger Robot // endianness matches floating point endianness on the target platform.
18*7c3d14c8STreehugger Robot //
19*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
20*7c3d14c8STreehugger Robot
21*7c3d14c8STreehugger Robot #ifndef FP_LIB_HEADER
22*7c3d14c8STreehugger Robot #define FP_LIB_HEADER
23*7c3d14c8STreehugger Robot
24*7c3d14c8STreehugger Robot #include <stdint.h>
25*7c3d14c8STreehugger Robot #include <stdbool.h>
26*7c3d14c8STreehugger Robot #include <limits.h>
27*7c3d14c8STreehugger Robot #include "int_lib.h"
28*7c3d14c8STreehugger Robot
29*7c3d14c8STreehugger Robot // x86_64 FreeBSD prior v9.3 define fixed-width types incorrectly in
30*7c3d14c8STreehugger Robot // 32-bit mode.
31*7c3d14c8STreehugger Robot #if defined(__FreeBSD__) && defined(__i386__)
32*7c3d14c8STreehugger Robot # include <sys/param.h>
33*7c3d14c8STreehugger Robot # if __FreeBSD_version < 903000 // v9.3
34*7c3d14c8STreehugger Robot # define uint64_t unsigned long long
35*7c3d14c8STreehugger Robot # define int64_t long long
36*7c3d14c8STreehugger Robot # undef UINT64_C
37*7c3d14c8STreehugger Robot # define UINT64_C(c) (c ## ULL)
38*7c3d14c8STreehugger Robot # endif
39*7c3d14c8STreehugger Robot #endif
40*7c3d14c8STreehugger Robot
41*7c3d14c8STreehugger Robot #if defined SINGLE_PRECISION
42*7c3d14c8STreehugger Robot
43*7c3d14c8STreehugger Robot typedef uint32_t rep_t;
44*7c3d14c8STreehugger Robot typedef int32_t srep_t;
45*7c3d14c8STreehugger Robot typedef float fp_t;
46*7c3d14c8STreehugger Robot #define REP_C UINT32_C
47*7c3d14c8STreehugger Robot #define significandBits 23
48*7c3d14c8STreehugger Robot
rep_clz(rep_t a)49*7c3d14c8STreehugger Robot static __inline int rep_clz(rep_t a) {
50*7c3d14c8STreehugger Robot return __builtin_clz(a);
51*7c3d14c8STreehugger Robot }
52*7c3d14c8STreehugger Robot
53*7c3d14c8STreehugger Robot // 32x32 --> 64 bit multiply
wideMultiply(rep_t a,rep_t b,rep_t * hi,rep_t * lo)54*7c3d14c8STreehugger Robot static __inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {
55*7c3d14c8STreehugger Robot const uint64_t product = (uint64_t)a*b;
56*7c3d14c8STreehugger Robot *hi = product >> 32;
57*7c3d14c8STreehugger Robot *lo = product;
58*7c3d14c8STreehugger Robot }
59*7c3d14c8STreehugger Robot COMPILER_RT_ABI fp_t __addsf3(fp_t a, fp_t b);
60*7c3d14c8STreehugger Robot
61*7c3d14c8STreehugger Robot #elif defined DOUBLE_PRECISION
62*7c3d14c8STreehugger Robot
63*7c3d14c8STreehugger Robot typedef uint64_t rep_t;
64*7c3d14c8STreehugger Robot typedef int64_t srep_t;
65*7c3d14c8STreehugger Robot typedef double fp_t;
66*7c3d14c8STreehugger Robot #define REP_C UINT64_C
67*7c3d14c8STreehugger Robot #define significandBits 52
68*7c3d14c8STreehugger Robot
rep_clz(rep_t a)69*7c3d14c8STreehugger Robot static __inline int rep_clz(rep_t a) {
70*7c3d14c8STreehugger Robot #if defined __LP64__
71*7c3d14c8STreehugger Robot return __builtin_clzl(a);
72*7c3d14c8STreehugger Robot #else
73*7c3d14c8STreehugger Robot if (a & REP_C(0xffffffff00000000))
74*7c3d14c8STreehugger Robot return __builtin_clz(a >> 32);
75*7c3d14c8STreehugger Robot else
76*7c3d14c8STreehugger Robot return 32 + __builtin_clz(a & REP_C(0xffffffff));
77*7c3d14c8STreehugger Robot #endif
78*7c3d14c8STreehugger Robot }
79*7c3d14c8STreehugger Robot
80*7c3d14c8STreehugger Robot #define loWord(a) (a & 0xffffffffU)
81*7c3d14c8STreehugger Robot #define hiWord(a) (a >> 32)
82*7c3d14c8STreehugger Robot
83*7c3d14c8STreehugger Robot // 64x64 -> 128 wide multiply for platforms that don't have such an operation;
84*7c3d14c8STreehugger Robot // many 64-bit platforms have this operation, but they tend to have hardware
85*7c3d14c8STreehugger Robot // floating-point, so we don't bother with a special case for them here.
wideMultiply(rep_t a,rep_t b,rep_t * hi,rep_t * lo)86*7c3d14c8STreehugger Robot static __inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {
87*7c3d14c8STreehugger Robot // Each of the component 32x32 -> 64 products
88*7c3d14c8STreehugger Robot const uint64_t plolo = loWord(a) * loWord(b);
89*7c3d14c8STreehugger Robot const uint64_t plohi = loWord(a) * hiWord(b);
90*7c3d14c8STreehugger Robot const uint64_t philo = hiWord(a) * loWord(b);
91*7c3d14c8STreehugger Robot const uint64_t phihi = hiWord(a) * hiWord(b);
92*7c3d14c8STreehugger Robot // Sum terms that contribute to lo in a way that allows us to get the carry
93*7c3d14c8STreehugger Robot const uint64_t r0 = loWord(plolo);
94*7c3d14c8STreehugger Robot const uint64_t r1 = hiWord(plolo) + loWord(plohi) + loWord(philo);
95*7c3d14c8STreehugger Robot *lo = r0 + (r1 << 32);
96*7c3d14c8STreehugger Robot // Sum terms contributing to hi with the carry from lo
97*7c3d14c8STreehugger Robot *hi = hiWord(plohi) + hiWord(philo) + hiWord(r1) + phihi;
98*7c3d14c8STreehugger Robot }
99*7c3d14c8STreehugger Robot #undef loWord
100*7c3d14c8STreehugger Robot #undef hiWord
101*7c3d14c8STreehugger Robot
102*7c3d14c8STreehugger Robot COMPILER_RT_ABI fp_t __adddf3(fp_t a, fp_t b);
103*7c3d14c8STreehugger Robot
104*7c3d14c8STreehugger Robot #elif defined QUAD_PRECISION
105*7c3d14c8STreehugger Robot #if __LDBL_MANT_DIG__ == 113
106*7c3d14c8STreehugger Robot #define CRT_LDBL_128BIT
107*7c3d14c8STreehugger Robot typedef __uint128_t rep_t;
108*7c3d14c8STreehugger Robot typedef __int128_t srep_t;
109*7c3d14c8STreehugger Robot typedef long double fp_t;
110*7c3d14c8STreehugger Robot #define REP_C (__uint128_t)
111*7c3d14c8STreehugger Robot // Note: Since there is no explicit way to tell compiler the constant is a
112*7c3d14c8STreehugger Robot // 128-bit integer, we let the constant be casted to 128-bit integer
113*7c3d14c8STreehugger Robot #define significandBits 112
114*7c3d14c8STreehugger Robot
rep_clz(rep_t a)115*7c3d14c8STreehugger Robot static __inline int rep_clz(rep_t a) {
116*7c3d14c8STreehugger Robot const union
117*7c3d14c8STreehugger Robot {
118*7c3d14c8STreehugger Robot __uint128_t ll;
119*7c3d14c8STreehugger Robot #if _YUGA_BIG_ENDIAN
120*7c3d14c8STreehugger Robot struct { uint64_t high, low; } s;
121*7c3d14c8STreehugger Robot #else
122*7c3d14c8STreehugger Robot struct { uint64_t low, high; } s;
123*7c3d14c8STreehugger Robot #endif
124*7c3d14c8STreehugger Robot } uu = { .ll = a };
125*7c3d14c8STreehugger Robot
126*7c3d14c8STreehugger Robot uint64_t word;
127*7c3d14c8STreehugger Robot uint64_t add;
128*7c3d14c8STreehugger Robot
129*7c3d14c8STreehugger Robot if (uu.s.high){
130*7c3d14c8STreehugger Robot word = uu.s.high;
131*7c3d14c8STreehugger Robot add = 0;
132*7c3d14c8STreehugger Robot }
133*7c3d14c8STreehugger Robot else{
134*7c3d14c8STreehugger Robot word = uu.s.low;
135*7c3d14c8STreehugger Robot add = 64;
136*7c3d14c8STreehugger Robot }
137*7c3d14c8STreehugger Robot return __builtin_clzll(word) + add;
138*7c3d14c8STreehugger Robot }
139*7c3d14c8STreehugger Robot
140*7c3d14c8STreehugger Robot #define Word_LoMask UINT64_C(0x00000000ffffffff)
141*7c3d14c8STreehugger Robot #define Word_HiMask UINT64_C(0xffffffff00000000)
142*7c3d14c8STreehugger Robot #define Word_FullMask UINT64_C(0xffffffffffffffff)
143*7c3d14c8STreehugger Robot #define Word_1(a) (uint64_t)((a >> 96) & Word_LoMask)
144*7c3d14c8STreehugger Robot #define Word_2(a) (uint64_t)((a >> 64) & Word_LoMask)
145*7c3d14c8STreehugger Robot #define Word_3(a) (uint64_t)((a >> 32) & Word_LoMask)
146*7c3d14c8STreehugger Robot #define Word_4(a) (uint64_t)(a & Word_LoMask)
147*7c3d14c8STreehugger Robot
148*7c3d14c8STreehugger Robot // 128x128 -> 256 wide multiply for platforms that don't have such an operation;
149*7c3d14c8STreehugger Robot // many 64-bit platforms have this operation, but they tend to have hardware
150*7c3d14c8STreehugger Robot // floating-point, so we don't bother with a special case for them here.
wideMultiply(rep_t a,rep_t b,rep_t * hi,rep_t * lo)151*7c3d14c8STreehugger Robot static __inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {
152*7c3d14c8STreehugger Robot
153*7c3d14c8STreehugger Robot const uint64_t product11 = Word_1(a) * Word_1(b);
154*7c3d14c8STreehugger Robot const uint64_t product12 = Word_1(a) * Word_2(b);
155*7c3d14c8STreehugger Robot const uint64_t product13 = Word_1(a) * Word_3(b);
156*7c3d14c8STreehugger Robot const uint64_t product14 = Word_1(a) * Word_4(b);
157*7c3d14c8STreehugger Robot const uint64_t product21 = Word_2(a) * Word_1(b);
158*7c3d14c8STreehugger Robot const uint64_t product22 = Word_2(a) * Word_2(b);
159*7c3d14c8STreehugger Robot const uint64_t product23 = Word_2(a) * Word_3(b);
160*7c3d14c8STreehugger Robot const uint64_t product24 = Word_2(a) * Word_4(b);
161*7c3d14c8STreehugger Robot const uint64_t product31 = Word_3(a) * Word_1(b);
162*7c3d14c8STreehugger Robot const uint64_t product32 = Word_3(a) * Word_2(b);
163*7c3d14c8STreehugger Robot const uint64_t product33 = Word_3(a) * Word_3(b);
164*7c3d14c8STreehugger Robot const uint64_t product34 = Word_3(a) * Word_4(b);
165*7c3d14c8STreehugger Robot const uint64_t product41 = Word_4(a) * Word_1(b);
166*7c3d14c8STreehugger Robot const uint64_t product42 = Word_4(a) * Word_2(b);
167*7c3d14c8STreehugger Robot const uint64_t product43 = Word_4(a) * Word_3(b);
168*7c3d14c8STreehugger Robot const uint64_t product44 = Word_4(a) * Word_4(b);
169*7c3d14c8STreehugger Robot
170*7c3d14c8STreehugger Robot const __uint128_t sum0 = (__uint128_t)product44;
171*7c3d14c8STreehugger Robot const __uint128_t sum1 = (__uint128_t)product34 +
172*7c3d14c8STreehugger Robot (__uint128_t)product43;
173*7c3d14c8STreehugger Robot const __uint128_t sum2 = (__uint128_t)product24 +
174*7c3d14c8STreehugger Robot (__uint128_t)product33 +
175*7c3d14c8STreehugger Robot (__uint128_t)product42;
176*7c3d14c8STreehugger Robot const __uint128_t sum3 = (__uint128_t)product14 +
177*7c3d14c8STreehugger Robot (__uint128_t)product23 +
178*7c3d14c8STreehugger Robot (__uint128_t)product32 +
179*7c3d14c8STreehugger Robot (__uint128_t)product41;
180*7c3d14c8STreehugger Robot const __uint128_t sum4 = (__uint128_t)product13 +
181*7c3d14c8STreehugger Robot (__uint128_t)product22 +
182*7c3d14c8STreehugger Robot (__uint128_t)product31;
183*7c3d14c8STreehugger Robot const __uint128_t sum5 = (__uint128_t)product12 +
184*7c3d14c8STreehugger Robot (__uint128_t)product21;
185*7c3d14c8STreehugger Robot const __uint128_t sum6 = (__uint128_t)product11;
186*7c3d14c8STreehugger Robot
187*7c3d14c8STreehugger Robot const __uint128_t r0 = (sum0 & Word_FullMask) +
188*7c3d14c8STreehugger Robot ((sum1 & Word_LoMask) << 32);
189*7c3d14c8STreehugger Robot const __uint128_t r1 = (sum0 >> 64) +
190*7c3d14c8STreehugger Robot ((sum1 >> 32) & Word_FullMask) +
191*7c3d14c8STreehugger Robot (sum2 & Word_FullMask) +
192*7c3d14c8STreehugger Robot ((sum3 << 32) & Word_HiMask);
193*7c3d14c8STreehugger Robot
194*7c3d14c8STreehugger Robot *lo = r0 + (r1 << 64);
195*7c3d14c8STreehugger Robot *hi = (r1 >> 64) +
196*7c3d14c8STreehugger Robot (sum1 >> 96) +
197*7c3d14c8STreehugger Robot (sum2 >> 64) +
198*7c3d14c8STreehugger Robot (sum3 >> 32) +
199*7c3d14c8STreehugger Robot sum4 +
200*7c3d14c8STreehugger Robot (sum5 << 32) +
201*7c3d14c8STreehugger Robot (sum6 << 64);
202*7c3d14c8STreehugger Robot }
203*7c3d14c8STreehugger Robot #undef Word_1
204*7c3d14c8STreehugger Robot #undef Word_2
205*7c3d14c8STreehugger Robot #undef Word_3
206*7c3d14c8STreehugger Robot #undef Word_4
207*7c3d14c8STreehugger Robot #undef Word_HiMask
208*7c3d14c8STreehugger Robot #undef Word_LoMask
209*7c3d14c8STreehugger Robot #undef Word_FullMask
210*7c3d14c8STreehugger Robot #endif // __LDBL_MANT_DIG__ == 113
211*7c3d14c8STreehugger Robot #else
212*7c3d14c8STreehugger Robot #error SINGLE_PRECISION, DOUBLE_PRECISION or QUAD_PRECISION must be defined.
213*7c3d14c8STreehugger Robot #endif
214*7c3d14c8STreehugger Robot
215*7c3d14c8STreehugger Robot #if defined(SINGLE_PRECISION) || defined(DOUBLE_PRECISION) || defined(CRT_LDBL_128BIT)
216*7c3d14c8STreehugger Robot #define typeWidth (sizeof(rep_t)*CHAR_BIT)
217*7c3d14c8STreehugger Robot #define exponentBits (typeWidth - significandBits - 1)
218*7c3d14c8STreehugger Robot #define maxExponent ((1 << exponentBits) - 1)
219*7c3d14c8STreehugger Robot #define exponentBias (maxExponent >> 1)
220*7c3d14c8STreehugger Robot
221*7c3d14c8STreehugger Robot #define implicitBit (REP_C(1) << significandBits)
222*7c3d14c8STreehugger Robot #define significandMask (implicitBit - 1U)
223*7c3d14c8STreehugger Robot #define signBit (REP_C(1) << (significandBits + exponentBits))
224*7c3d14c8STreehugger Robot #define absMask (signBit - 1U)
225*7c3d14c8STreehugger Robot #define exponentMask (absMask ^ significandMask)
226*7c3d14c8STreehugger Robot #define oneRep ((rep_t)exponentBias << significandBits)
227*7c3d14c8STreehugger Robot #define infRep exponentMask
228*7c3d14c8STreehugger Robot #define quietBit (implicitBit >> 1)
229*7c3d14c8STreehugger Robot #define qnanRep (exponentMask | quietBit)
230*7c3d14c8STreehugger Robot
toRep(fp_t x)231*7c3d14c8STreehugger Robot static __inline rep_t toRep(fp_t x) {
232*7c3d14c8STreehugger Robot const union { fp_t f; rep_t i; } rep = {.f = x};
233*7c3d14c8STreehugger Robot return rep.i;
234*7c3d14c8STreehugger Robot }
235*7c3d14c8STreehugger Robot
fromRep(rep_t x)236*7c3d14c8STreehugger Robot static __inline fp_t fromRep(rep_t x) {
237*7c3d14c8STreehugger Robot const union { fp_t f; rep_t i; } rep = {.i = x};
238*7c3d14c8STreehugger Robot return rep.f;
239*7c3d14c8STreehugger Robot }
240*7c3d14c8STreehugger Robot
normalize(rep_t * significand)241*7c3d14c8STreehugger Robot static __inline int normalize(rep_t *significand) {
242*7c3d14c8STreehugger Robot const int shift = rep_clz(*significand) - rep_clz(implicitBit);
243*7c3d14c8STreehugger Robot *significand <<= shift;
244*7c3d14c8STreehugger Robot return 1 - shift;
245*7c3d14c8STreehugger Robot }
246*7c3d14c8STreehugger Robot
wideLeftShift(rep_t * hi,rep_t * lo,int count)247*7c3d14c8STreehugger Robot static __inline void wideLeftShift(rep_t *hi, rep_t *lo, int count) {
248*7c3d14c8STreehugger Robot *hi = *hi << count | *lo >> (typeWidth - count);
249*7c3d14c8STreehugger Robot *lo = *lo << count;
250*7c3d14c8STreehugger Robot }
251*7c3d14c8STreehugger Robot
wideRightShiftWithSticky(rep_t * hi,rep_t * lo,unsigned int count)252*7c3d14c8STreehugger Robot static __inline void wideRightShiftWithSticky(rep_t *hi, rep_t *lo, unsigned int count) {
253*7c3d14c8STreehugger Robot if (count < typeWidth) {
254*7c3d14c8STreehugger Robot const bool sticky = *lo << (typeWidth - count);
255*7c3d14c8STreehugger Robot *lo = *hi << (typeWidth - count) | *lo >> count | sticky;
256*7c3d14c8STreehugger Robot *hi = *hi >> count;
257*7c3d14c8STreehugger Robot }
258*7c3d14c8STreehugger Robot else if (count < 2*typeWidth) {
259*7c3d14c8STreehugger Robot const bool sticky = *hi << (2*typeWidth - count) | *lo;
260*7c3d14c8STreehugger Robot *lo = *hi >> (count - typeWidth) | sticky;
261*7c3d14c8STreehugger Robot *hi = 0;
262*7c3d14c8STreehugger Robot } else {
263*7c3d14c8STreehugger Robot const bool sticky = *hi | *lo;
264*7c3d14c8STreehugger Robot *lo = sticky;
265*7c3d14c8STreehugger Robot *hi = 0;
266*7c3d14c8STreehugger Robot }
267*7c3d14c8STreehugger Robot }
268*7c3d14c8STreehugger Robot #endif
269*7c3d14c8STreehugger Robot
270*7c3d14c8STreehugger Robot #endif // FP_LIB_HEADER
271