1 /**
2 University of Illinois/NCSA
3 Open Source License
4 
5 Copyright (c) 2009-2014 by the contributors listed in CREDITS.TXT
6 
7 All rights reserved.
8 
9 Developed by:
10 
11     LLVM Team
12 
13     University of Illinois at Urbana-Champaign
14 
15     http://llvm.org
16 
17 Permission is hereby granted, free of charge, to any person obtaining a copy of
18 this software and associated documentation files (the "Software"), to deal with
19 the Software without restriction, including without limitation the rights to
20 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
21 of the Software, and to permit persons to whom the Software is furnished to do
22 so, subject to the following conditions:
23 
24     * Redistributions of source code must retain the above copyright notice,
25       this list of conditions and the following disclaimers.
26 
27     * Redistributions in binary form must reproduce the above copyright notice,
28       this list of conditions and the following disclaimers in the
29       documentation and/or other materials provided with the distribution.
30 
31     * Neither the names of the LLVM Team, University of Illinois at
32       Urbana-Champaign, nor the names of its contributors may be used to
33       endorse or promote products derived from this Software without specific
34       prior written permission.
35 
36 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
37 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
38 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
39 CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
40 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
41 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
42 SOFTWARE.
43 **/
44 
45 #ifndef FP_LIB_HEADER
46 #define FP_LIB_HEADER
47 
48 #include <stdint.h>
49 #include <stdbool.h>
50 #include <limits.h>
51 #include "int_lib.h"
52 
53 #if defined SINGLE_PRECISION
54 
55 typedef uint32_t rep_t;
56 typedef int32_t srep_t;
57 typedef float fp_t;
58 #define REP_C UINT32_C
59 #define significandBits 23
60 
rep_clz(rep_t a)61 static inline int rep_clz(rep_t a) {
62     return __builtin_clz(a);
63 }
64 
65 // 32x32 --> 64 bit multiply
wideMultiply(rep_t a,rep_t b,rep_t * hi,rep_t * lo)66 static inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {
67     const uint64_t product = (uint64_t)a*b;
68     *hi = product >> 32;
69     *lo = product;
70 }
71 COMPILER_RT_ABI fp_t __addsf3(fp_t a, fp_t b);
72 
73 #elif defined DOUBLE_PRECISION
74 
75 typedef uint64_t rep_t;
76 typedef int64_t srep_t;
77 typedef double fp_t;
78 #define REP_C UINT64_C
79 #define significandBits 52
80 
rep_clz(rep_t a)81 static inline int rep_clz(rep_t a) {
82 #if defined __LP64__
83     return __builtin_clzl(a);
84 #else
85     if (a & REP_C(0xffffffff00000000))
86         return __builtin_clz(a >> 32);
87     else
88         return 32 + __builtin_clz(a & REP_C(0xffffffff));
89 #endif
90 }
91 
92 #define loWord(a) (a & 0xffffffffU)
93 #define hiWord(a) (a >> 32)
94 
95 // 64x64 -> 128 wide multiply for platforms that don't have such an operation;
96 // many 64-bit platforms have this operation, but they tend to have hardware
97 // 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)98 static inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {
99     // Each of the component 32x32 -> 64 products
100     const uint64_t plolo = loWord(a) * loWord(b);
101     const uint64_t plohi = loWord(a) * hiWord(b);
102     const uint64_t philo = hiWord(a) * loWord(b);
103     const uint64_t phihi = hiWord(a) * hiWord(b);
104     // Sum terms that contribute to lo in a way that allows us to get the carry
105     const uint64_t r0 = loWord(plolo);
106     const uint64_t r1 = hiWord(plolo) + loWord(plohi) + loWord(philo);
107     *lo = r0 + (r1 << 32);
108     // Sum terms contributing to hi with the carry from lo
109     *hi = hiWord(plohi) + hiWord(philo) + hiWord(r1) + phihi;
110 }
111 #undef loWord
112 #undef hiWord
113 
114 COMPILER_RT_ABI fp_t __adddf3(fp_t a, fp_t b);
115 
116 #elif defined QUAD_PRECISION
117 #if __LDBL_MANT_DIG__ == 113
118 #define CRT_LDBL_128BIT
119 typedef __uint128_t rep_t;
120 typedef __int128_t srep_t;
121 typedef long double fp_t;
122 #define REP_C (__uint128_t)
123 // Note: Since there is no explicit way to tell compiler the constant is a
124 // 128-bit integer, we let the constant be casted to 128-bit integer
125 #define significandBits 112
126 
rep_clz(rep_t a)127 static inline int rep_clz(rep_t a) {
128     const union
129         {
130              __uint128_t ll;
131 #if _YUGA_BIG_ENDIAN
132              struct { uint64_t high, low; } s;
133 #else
134              struct { uint64_t low, high; } s;
135 #endif
136         } uu = { .ll = a };
137 
138     uint64_t word;
139     uint64_t add;
140 
141     if (uu.s.high){
142         word = uu.s.high;
143         add = 0;
144     }
145     else{
146         word = uu.s.low;
147         add = 64;
148     }
149     return __builtin_clzll(word) + add;
150 }
151 
152 #define Word_LoMask   UINT64_C(0x00000000ffffffff)
153 #define Word_HiMask   UINT64_C(0xffffffff00000000)
154 #define Word_FullMask UINT64_C(0xffffffffffffffff)
155 #define Word_1(a) (uint64_t)((a >> 96) & Word_LoMask)
156 #define Word_2(a) (uint64_t)((a >> 64) & Word_LoMask)
157 #define Word_3(a) (uint64_t)((a >> 32) & Word_LoMask)
158 #define Word_4(a) (uint64_t)(a & Word_LoMask)
159 
160 // 128x128 -> 256 wide multiply for platforms that don't have such an operation;
161 // many 64-bit platforms have this operation, but they tend to have hardware
162 // 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)163 static inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {
164 
165     const uint64_t product11 = Word_1(a) * Word_1(b);
166     const uint64_t product12 = Word_1(a) * Word_2(b);
167     const uint64_t product13 = Word_1(a) * Word_3(b);
168     const uint64_t product14 = Word_1(a) * Word_4(b);
169     const uint64_t product21 = Word_2(a) * Word_1(b);
170     const uint64_t product22 = Word_2(a) * Word_2(b);
171     const uint64_t product23 = Word_2(a) * Word_3(b);
172     const uint64_t product24 = Word_2(a) * Word_4(b);
173     const uint64_t product31 = Word_3(a) * Word_1(b);
174     const uint64_t product32 = Word_3(a) * Word_2(b);
175     const uint64_t product33 = Word_3(a) * Word_3(b);
176     const uint64_t product34 = Word_3(a) * Word_4(b);
177     const uint64_t product41 = Word_4(a) * Word_1(b);
178     const uint64_t product42 = Word_4(a) * Word_2(b);
179     const uint64_t product43 = Word_4(a) * Word_3(b);
180     const uint64_t product44 = Word_4(a) * Word_4(b);
181 
182     const __uint128_t sum0 = (__uint128_t)product44;
183     const __uint128_t sum1 = (__uint128_t)product34 +
184                              (__uint128_t)product43;
185     const __uint128_t sum2 = (__uint128_t)product24 +
186                              (__uint128_t)product33 +
187                              (__uint128_t)product42;
188     const __uint128_t sum3 = (__uint128_t)product14 +
189                              (__uint128_t)product23 +
190                              (__uint128_t)product32 +
191                              (__uint128_t)product41;
192     const __uint128_t sum4 = (__uint128_t)product13 +
193                              (__uint128_t)product22 +
194                              (__uint128_t)product31;
195     const __uint128_t sum5 = (__uint128_t)product12 +
196                              (__uint128_t)product21;
197     const __uint128_t sum6 = (__uint128_t)product11;
198 
199     const __uint128_t r0 = (sum0 & Word_FullMask) +
200                            ((sum1 & Word_LoMask) << 32);
201     const __uint128_t r1 = (sum0 >> 64) +
202                            ((sum1 >> 32) & Word_FullMask) +
203                            (sum2 & Word_FullMask) +
204                            ((sum3 << 32) & Word_HiMask);
205 
206     *lo = r0 + (r1 << 64);
207     *hi = (r1 >> 64) +
208           (sum1 >> 96) +
209           (sum2 >> 64) +
210           (sum3 >> 32) +
211           sum4 +
212           (sum5 << 32) +
213           (sum6 << 64);
214 }
215 #undef Word_1
216 #undef Word_2
217 #undef Word_3
218 #undef Word_4
219 #undef Word_HiMask
220 #undef Word_LoMask
221 #undef Word_FullMask
222 #endif // __LDBL_MANT_DIG__ == 113
223 #else
224 #error SINGLE_PRECISION, DOUBLE_PRECISION or QUAD_PRECISION must be defined.
225 #endif
226 
227 #if defined(SINGLE_PRECISION) || defined(DOUBLE_PRECISION) || defined(CRT_LDBL_128BIT)
228 #define typeWidth       (sizeof(rep_t)*CHAR_BIT)
229 #define exponentBits    (typeWidth - significandBits - 1)
230 #define maxExponent     ((1 << exponentBits) - 1)
231 #define exponentBias    (maxExponent >> 1)
232 
233 #define implicitBit     (REP_C(1) << significandBits)
234 #define significandMask (implicitBit - 1U)
235 #define signBit         (REP_C(1) << (significandBits + exponentBits))
236 #define absMask         (signBit - 1U)
237 #define exponentMask    (absMask ^ significandMask)
238 #define oneRep          ((rep_t)exponentBias << significandBits)
239 #define infRep          exponentMask
240 #define quietBit        (implicitBit >> 1)
241 #define qnanRep         (exponentMask | quietBit)
242 
toRep(fp_t x)243 static inline rep_t toRep(fp_t x) {
244     const union { fp_t f; rep_t i; } rep = {.f = x};
245     return rep.i;
246 }
247 
fromRep(rep_t x)248 static inline fp_t fromRep(rep_t x) {
249     const union { fp_t f; rep_t i; } rep = {.i = x};
250     return rep.f;
251 }
252 
normalize(rep_t * significand)253 static inline int normalize(rep_t *significand) {
254     const int shift = rep_clz(*significand) - rep_clz(implicitBit);
255     *significand <<= shift;
256     return 1 - shift;
257 }
258 
wideLeftShift(rep_t * hi,rep_t * lo,int count)259 static inline void wideLeftShift(rep_t *hi, rep_t *lo, int count) {
260     *hi = *hi << count | *lo >> (typeWidth - count);
261     *lo = *lo << count;
262 }
263 
wideRightShiftWithSticky(rep_t * hi,rep_t * lo,unsigned int count)264 static inline void wideRightShiftWithSticky(rep_t *hi, rep_t *lo, unsigned int count) {
265     if (count < typeWidth) {
266         const bool sticky = *lo << (typeWidth - count);
267         *lo = *hi << (typeWidth - count) | *lo >> count | sticky;
268         *hi = *hi >> count;
269     }
270     else if (count < 2*typeWidth) {
271         const bool sticky = *hi << (2*typeWidth - count) | *lo;
272         *lo = *hi >> (count - typeWidth) | sticky;
273         *hi = 0;
274     } else {
275         const bool sticky = *hi | *lo;
276         *lo = sticky;
277         *hi = 0;
278     }
279 }
280 #endif
281 
282 #endif // FP_LIB_HEADER
283