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