xref: /freebsd/contrib/llvm-project/compiler-rt/lib/builtins/fp_lib.h (revision a03411e84728e9b267056fd31c7d1d9d1dc1b01e)
1 //===-- lib/fp_lib.h - Floating-point utilities -------------------*- C -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is a configuration header for soft-float routines in compiler-rt.
10 // This file does not provide any part of the compiler-rt interface, but defines
11 // many useful constants and utility routines that are used in the
12 // implementation of the soft-float routines in compiler-rt.
13 //
14 // Assumes that float, double and long double correspond to the IEEE-754
15 // binary32, binary64 and binary 128 types, respectively, and that integer
16 // endianness matches floating point endianness on the target platform.
17 //
18 //===----------------------------------------------------------------------===//
19 
20 #ifndef FP_LIB_HEADER
21 #define FP_LIB_HEADER
22 
23 #include "int_lib.h"
24 #include "int_math.h"
25 #include <limits.h>
26 #include <stdbool.h>
27 #include <stdint.h>
28 
29 // x86_64 FreeBSD prior v9.3 define fixed-width types incorrectly in
30 // 32-bit mode.
31 #if defined(__FreeBSD__) && defined(__i386__)
32 #include <sys/param.h>
33 #if __FreeBSD_version < 903000 // v9.3
34 #define uint64_t unsigned long long
35 #define int64_t long long
36 #undef UINT64_C
37 #define UINT64_C(c) (c##ULL)
38 #endif
39 #endif
40 
41 #if defined SINGLE_PRECISION
42 
43 typedef uint16_t half_rep_t;
44 typedef uint32_t rep_t;
45 typedef uint64_t twice_rep_t;
46 typedef int32_t srep_t;
47 typedef float fp_t;
48 #define HALF_REP_C UINT16_C
49 #define REP_C UINT32_C
50 #define significandBits 23
51 
52 static __inline int rep_clz(rep_t a) { return clzsi(a); }
53 
54 // 32x32 --> 64 bit multiply
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 uint32_t half_rep_t;
65 typedef uint64_t rep_t;
66 typedef int64_t srep_t;
67 typedef double fp_t;
68 #define HALF_REP_C UINT32_C
69 #define REP_C UINT64_C
70 #define significandBits 52
71 
72 static __inline int rep_clz(rep_t a) {
73 #if defined __LP64__
74   return __builtin_clzl(a);
75 #else
76   if (a & REP_C(0xffffffff00000000))
77     return clzsi(a >> 32);
78   else
79     return 32 + clzsi(a & REP_C(0xffffffff));
80 #endif
81 }
82 
83 #define loWord(a) (a & 0xffffffffU)
84 #define hiWord(a) (a >> 32)
85 
86 // 64x64 -> 128 wide multiply for platforms that don't have such an operation;
87 // many 64-bit platforms have this operation, but they tend to have hardware
88 // floating-point, so we don't bother with a special case for them here.
89 static __inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {
90   // Each of the component 32x32 -> 64 products
91   const uint64_t plolo = loWord(a) * loWord(b);
92   const uint64_t plohi = loWord(a) * hiWord(b);
93   const uint64_t philo = hiWord(a) * loWord(b);
94   const uint64_t phihi = hiWord(a) * hiWord(b);
95   // Sum terms that contribute to lo in a way that allows us to get the carry
96   const uint64_t r0 = loWord(plolo);
97   const uint64_t r1 = hiWord(plolo) + loWord(plohi) + loWord(philo);
98   *lo = r0 + (r1 << 32);
99   // Sum terms contributing to hi with the carry from lo
100   *hi = hiWord(plohi) + hiWord(philo) + hiWord(r1) + phihi;
101 }
102 #undef loWord
103 #undef hiWord
104 
105 COMPILER_RT_ABI fp_t __adddf3(fp_t a, fp_t b);
106 
107 #elif defined QUAD_PRECISION
108 #if __LDBL_MANT_DIG__ == 113 && defined(__SIZEOF_INT128__)
109 // TODO: Availability of the *tf functions should not depend on long double
110 // being IEEE 128, but instead on being able to use a 128-bit floating-point
111 // type, which includes __float128.
112 // Right now this (incorrectly) stops the builtins from being used for x86.
113 #define CRT_LDBL_128BIT
114 #define CRT_HAS_TF_MODE
115 #define TF_C(c) c##L
116 typedef uint64_t half_rep_t;
117 typedef __uint128_t rep_t;
118 typedef __int128_t srep_t;
119 typedef long double fp_t;
120 #define HALF_REP_C UINT64_C
121 #define REP_C (__uint128_t)
122 // Note: Since there is no explicit way to tell compiler the constant is a
123 // 128-bit integer, we let the constant be casted to 128-bit integer
124 #define significandBits 112
125 #define TF_MANT_DIG (significandBits + 1)
126 
127 static __inline int rep_clz(rep_t a) {
128   const union {
129     __uint128_t ll;
130 #if _YUGA_BIG_ENDIAN
131     struct {
132       uint64_t high, low;
133     } s;
134 #else
135     struct {
136       uint64_t low, high;
137     } s;
138 #endif
139   } uu = {.ll = a};
140 
141   uint64_t word;
142   uint64_t add;
143 
144   if (uu.s.high) {
145     word = uu.s.high;
146     add = 0;
147   } else {
148     word = uu.s.low;
149     add = 64;
150   }
151   return __builtin_clzll(word) + add;
152 }
153 
154 #define Word_LoMask UINT64_C(0x00000000ffffffff)
155 #define Word_HiMask UINT64_C(0xffffffff00000000)
156 #define Word_FullMask UINT64_C(0xffffffffffffffff)
157 #define Word_1(a) (uint64_t)((a >> 96) & Word_LoMask)
158 #define Word_2(a) (uint64_t)((a >> 64) & Word_LoMask)
159 #define Word_3(a) (uint64_t)((a >> 32) & Word_LoMask)
160 #define Word_4(a) (uint64_t)(a & Word_LoMask)
161 
162 // 128x128 -> 256 wide multiply for platforms that don't have such an operation;
163 // many 64-bit platforms have this operation, but they tend to have hardware
164 // floating-point, so we don't bother with a special case for them here.
165 static __inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {
166 
167   const uint64_t product11 = Word_1(a) * Word_1(b);
168   const uint64_t product12 = Word_1(a) * Word_2(b);
169   const uint64_t product13 = Word_1(a) * Word_3(b);
170   const uint64_t product14 = Word_1(a) * Word_4(b);
171   const uint64_t product21 = Word_2(a) * Word_1(b);
172   const uint64_t product22 = Word_2(a) * Word_2(b);
173   const uint64_t product23 = Word_2(a) * Word_3(b);
174   const uint64_t product24 = Word_2(a) * Word_4(b);
175   const uint64_t product31 = Word_3(a) * Word_1(b);
176   const uint64_t product32 = Word_3(a) * Word_2(b);
177   const uint64_t product33 = Word_3(a) * Word_3(b);
178   const uint64_t product34 = Word_3(a) * Word_4(b);
179   const uint64_t product41 = Word_4(a) * Word_1(b);
180   const uint64_t product42 = Word_4(a) * Word_2(b);
181   const uint64_t product43 = Word_4(a) * Word_3(b);
182   const uint64_t product44 = Word_4(a) * Word_4(b);
183 
184   const __uint128_t sum0 = (__uint128_t)product44;
185   const __uint128_t sum1 = (__uint128_t)product34 + (__uint128_t)product43;
186   const __uint128_t sum2 =
187       (__uint128_t)product24 + (__uint128_t)product33 + (__uint128_t)product42;
188   const __uint128_t sum3 = (__uint128_t)product14 + (__uint128_t)product23 +
189                            (__uint128_t)product32 + (__uint128_t)product41;
190   const __uint128_t sum4 =
191       (__uint128_t)product13 + (__uint128_t)product22 + (__uint128_t)product31;
192   const __uint128_t sum5 = (__uint128_t)product12 + (__uint128_t)product21;
193   const __uint128_t sum6 = (__uint128_t)product11;
194 
195   const __uint128_t r0 = (sum0 & Word_FullMask) + ((sum1 & Word_LoMask) << 32);
196   const __uint128_t r1 = (sum0 >> 64) + ((sum1 >> 32) & Word_FullMask) +
197                          (sum2 & Word_FullMask) + ((sum3 << 32) & Word_HiMask);
198 
199   *lo = r0 + (r1 << 64);
200   *hi = (r1 >> 64) + (sum1 >> 96) + (sum2 >> 64) + (sum3 >> 32) + sum4 +
201         (sum5 << 32) + (sum6 << 64);
202 }
203 #undef Word_1
204 #undef Word_2
205 #undef Word_3
206 #undef Word_4
207 #undef Word_HiMask
208 #undef Word_LoMask
209 #undef Word_FullMask
210 #endif // __LDBL_MANT_DIG__ == 113 && __SIZEOF_INT128__
211 #else
212 #error SINGLE_PRECISION, DOUBLE_PRECISION or QUAD_PRECISION must be defined.
213 #endif
214 
215 #if defined(SINGLE_PRECISION) || defined(DOUBLE_PRECISION) ||                  \
216     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 
232 static __inline rep_t toRep(fp_t x) {
233   const union {
234     fp_t f;
235     rep_t i;
236   } rep = {.f = x};
237   return rep.i;
238 }
239 
240 static __inline fp_t fromRep(rep_t x) {
241   const union {
242     fp_t f;
243     rep_t i;
244   } rep = {.i = x};
245   return rep.f;
246 }
247 
248 static __inline int normalize(rep_t *significand) {
249   const int shift = rep_clz(*significand) - rep_clz(implicitBit);
250   *significand <<= shift;
251   return 1 - shift;
252 }
253 
254 static __inline void wideLeftShift(rep_t *hi, rep_t *lo, int count) {
255   *hi = *hi << count | *lo >> (typeWidth - count);
256   *lo = *lo << count;
257 }
258 
259 static __inline void wideRightShiftWithSticky(rep_t *hi, rep_t *lo,
260                                               unsigned int count) {
261   if (count < typeWidth) {
262     const bool sticky = (*lo << (typeWidth - count)) != 0;
263     *lo = *hi << (typeWidth - count) | *lo >> count | sticky;
264     *hi = *hi >> count;
265   } else if (count < 2 * typeWidth) {
266     const bool sticky = *hi << (2 * typeWidth - count) | *lo;
267     *lo = *hi >> (count - typeWidth) | sticky;
268     *hi = 0;
269   } else {
270     const bool sticky = *hi | *lo;
271     *lo = sticky;
272     *hi = 0;
273   }
274 }
275 
276 // Implements logb methods (logb, logbf, logbl) for IEEE-754. This avoids
277 // pulling in a libm dependency from compiler-rt, but is not meant to replace
278 // it (i.e. code calling logb() should get the one from libm, not this), hence
279 // the __compiler_rt prefix.
280 static __inline fp_t __compiler_rt_logbX(fp_t x) {
281   rep_t rep = toRep(x);
282   int exp = (rep & exponentMask) >> significandBits;
283 
284   // Abnormal cases:
285   // 1) +/- inf returns +inf; NaN returns NaN
286   // 2) 0.0 returns -inf
287   if (exp == maxExponent) {
288     if (((rep & signBit) == 0) || (x != x)) {
289       return x; // NaN or +inf: return x
290     } else {
291       return -x; // -inf: return -x
292     }
293   } else if (x == 0.0) {
294     // 0.0: return -inf
295     return fromRep(infRep | signBit);
296   }
297 
298   if (exp != 0) {
299     // Normal number
300     return exp - exponentBias; // Unbias exponent
301   } else {
302     // Subnormal number; normalize and repeat
303     rep &= absMask;
304     const int shift = 1 - normalize(&rep);
305     exp = (rep & exponentMask) >> significandBits;
306     return exp - exponentBias - shift; // Unbias exponent
307   }
308 }
309 
310 // Avoid using scalbn from libm. Unlike libc/libm scalbn, this function never
311 // sets errno on underflow/overflow.
312 static __inline fp_t __compiler_rt_scalbnX(fp_t x, int y) {
313   const rep_t rep = toRep(x);
314   int exp = (rep & exponentMask) >> significandBits;
315 
316   if (x == 0.0 || exp == maxExponent)
317     return x; // +/- 0.0, NaN, or inf: return x
318 
319   // Normalize subnormal input.
320   rep_t sig = rep & significandMask;
321   if (exp == 0) {
322     exp += normalize(&sig);
323     sig &= ~implicitBit; // clear the implicit bit again
324   }
325 
326   if (__builtin_sadd_overflow(exp, y, &exp)) {
327     // Saturate the exponent, which will guarantee an underflow/overflow below.
328     exp = (y >= 0) ? INT_MAX : INT_MIN;
329   }
330 
331   // Return this value: [+/-] 1.sig * 2 ** (exp - exponentBias).
332   const rep_t sign = rep & signBit;
333   if (exp >= maxExponent) {
334     // Overflow, which could produce infinity or the largest-magnitude value,
335     // depending on the rounding mode.
336     return fromRep(sign | ((rep_t)(maxExponent - 1) << significandBits)) * 2.0f;
337   } else if (exp <= 0) {
338     // Subnormal or underflow. Use floating-point multiply to handle truncation
339     // correctly.
340     fp_t tmp = fromRep(sign | (REP_C(1) << significandBits) | sig);
341     exp += exponentBias - 1;
342     if (exp < 1)
343       exp = 1;
344     tmp *= fromRep((rep_t)exp << significandBits);
345     return tmp;
346   } else
347     return fromRep(sign | ((rep_t)exp << significandBits) | sig);
348 }
349 
350 // Avoid using fmax from libm.
351 static __inline fp_t __compiler_rt_fmaxX(fp_t x, fp_t y) {
352   // If either argument is NaN, return the other argument. If both are NaN,
353   // arbitrarily return the second one. Otherwise, if both arguments are +/-0,
354   // arbitrarily return the first one.
355   return (crt_isnan(x) || x < y) ? y : x;
356 }
357 
358 #endif
359 
360 #if defined(SINGLE_PRECISION)
361 
362 static __inline fp_t __compiler_rt_logbf(fp_t x) {
363   return __compiler_rt_logbX(x);
364 }
365 static __inline fp_t __compiler_rt_scalbnf(fp_t x, int y) {
366   return __compiler_rt_scalbnX(x, y);
367 }
368 static __inline fp_t __compiler_rt_fmaxf(fp_t x, fp_t y) {
369 #if defined(__aarch64__)
370   // Use __builtin_fmaxf which turns into an fmaxnm instruction on AArch64.
371   return __builtin_fmaxf(x, y);
372 #else
373   // __builtin_fmaxf frequently turns into a libm call, so inline the function.
374   return __compiler_rt_fmaxX(x, y);
375 #endif
376 }
377 
378 #elif defined(DOUBLE_PRECISION)
379 
380 static __inline fp_t __compiler_rt_logb(fp_t x) {
381   return __compiler_rt_logbX(x);
382 }
383 static __inline fp_t __compiler_rt_scalbn(fp_t x, int y) {
384   return __compiler_rt_scalbnX(x, y);
385 }
386 static __inline fp_t __compiler_rt_fmax(fp_t x, fp_t y) {
387 #if defined(__aarch64__)
388   // Use __builtin_fmax which turns into an fmaxnm instruction on AArch64.
389   return __builtin_fmax(x, y);
390 #else
391   // __builtin_fmax frequently turns into a libm call, so inline the function.
392   return __compiler_rt_fmaxX(x, y);
393 #endif
394 }
395 
396 #elif defined(QUAD_PRECISION)
397 
398 #if defined(CRT_LDBL_128BIT)
399 static __inline fp_t __compiler_rt_logbl(fp_t x) {
400   return __compiler_rt_logbX(x);
401 }
402 static __inline fp_t __compiler_rt_scalbnl(fp_t x, int y) {
403   return __compiler_rt_scalbnX(x, y);
404 }
405 static __inline fp_t __compiler_rt_fmaxl(fp_t x, fp_t y) {
406   return __compiler_rt_fmaxX(x, y);
407 }
408 #else
409 // The generic implementation only works for ieee754 floating point. For other
410 // floating point types, continue to rely on the libm implementation for now.
411 static __inline long double __compiler_rt_logbl(long double x) {
412   return crt_logbl(x);
413 }
414 static __inline long double __compiler_rt_scalbnl(long double x, int y) {
415   return crt_scalbnl(x, y);
416 }
417 static __inline long double __compiler_rt_fmaxl(long double x, long double y) {
418   return crt_fmaxl(x, y);
419 }
420 #endif // CRT_LDBL_128BIT
421 
422 #endif // *_PRECISION
423 
424 #endif // FP_LIB_HEADER
425