1 //===-- lib/fp_lib.h - Floating-point utilities -------------------*- C -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
14 //
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.
18 //
19 //===----------------------------------------------------------------------===//
20 
21 #ifndef FP_LIB_HEADER
22 #define FP_LIB_HEADER
23 
24 #include <stdint.h>
25 #include <stdbool.h>
26 #include <limits.h>
27 #include "int_lib.h"
28 #include "int_math.h"
29 
30 // 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 
42 #if defined SINGLE_PRECISION
43 
44 typedef uint32_t rep_t;
45 typedef int32_t srep_t;
46 typedef float fp_t;
47 #define REP_C UINT32_C
48 #define significandBits 23
49 
rep_clz(rep_t a)50 static __inline int rep_clz(rep_t a) {
51     return __builtin_clz(a);
52 }
53 
54 // 32x32 --> 64 bit multiply
wideMultiply(rep_t a,rep_t b,rep_t * hi,rep_t * lo)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;
57     *hi = product >> 32;
58     *lo = product;
59 }
60 COMPILER_RT_ABI fp_t __addsf3(fp_t a, fp_t b);
61 
62 #elif defined DOUBLE_PRECISION
63 
64 typedef uint64_t rep_t;
65 typedef int64_t srep_t;
66 typedef double fp_t;
67 #define REP_C UINT64_C
68 #define significandBits 52
69 
rep_clz(rep_t a)70 static __inline int rep_clz(rep_t a) {
71 #if defined __LP64__
72     return __builtin_clzl(a);
73 #else
74     if (a & REP_C(0xffffffff00000000))
75         return __builtin_clz(a >> 32);
76     else
77         return 32 + __builtin_clz(a & REP_C(0xffffffff));
78 #endif
79 }
80 
81 #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.
wideMultiply(rep_t a,rep_t b,rep_t * hi,rep_t * lo)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;
99 }
100 #undef loWord
101 #undef hiWord
102 
103 COMPILER_RT_ABI fp_t __adddf3(fp_t a, fp_t b);
104 
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
115 
rep_clz(rep_t a)116 static __inline int rep_clz(rep_t a) {
117     const union
118         {
119              __uint128_t ll;
120 #if _YUGA_BIG_ENDIAN
121              struct { uint64_t high, low; } s;
122 #else
123              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.
wideMultiply(rep_t a,rep_t b,rep_t * hi,rep_t * lo)152 static __inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {
153 
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.
214 #endif
215 
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)
221 
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)
231 
toRep(fp_t x)232 static __inline rep_t toRep(fp_t x) {
233     const union { fp_t f; rep_t i; } rep = {.f = x};
234     return rep.i;
235 }
236 
fromRep(rep_t x)237 static __inline fp_t fromRep(rep_t x) {
238     const union { fp_t f; rep_t i; } rep = {.i = x};
239     return rep.f;
240 }
241 
normalize(rep_t * significand)242 static __inline int normalize(rep_t *significand) {
243     const int shift = rep_clz(*significand) - rep_clz(implicitBit);
244     *significand <<= shift;
245     return 1 - shift;
246 }
247 
wideLeftShift(rep_t * hi,rep_t * lo,int count)248 static __inline void wideLeftShift(rep_t *hi, rep_t *lo, int count) {
249     *hi = *hi << count | *lo >> (typeWidth - count);
250     *lo = *lo << count;
251 }
252 
wideRightShiftWithSticky(rep_t * hi,rep_t * lo,unsigned int 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;
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 }
269 
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.
__compiler_rt_logbX(fp_t x)274 static __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)
__compiler_rt_logbf(fp_t x)306 static __inline fp_t __compiler_rt_logbf(fp_t x) {
307   return __compiler_rt_logbX(x);
308 }
309 #elif defined(DOUBLE_PRECISION)
__compiler_rt_logb(fp_t x)310 static __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)
__compiler_rt_logbl(fp_t x)315 static __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.
__compiler_rt_logbl(long double x)321 static __inline long double __compiler_rt_logbl(long double x) {
322   return crt_logbl(x);
323 }
324   #endif
325 #endif
326 
327 #endif // FP_LIB_HEADER
328