1276789Sdim//===-- lib/fp_lib.h - Floating-point utilities -------------------*- C -*-===//
2276789Sdim//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6276789Sdim//
7276789Sdim//===----------------------------------------------------------------------===//
8276789Sdim//
9276789Sdim// This file is a configuration header for soft-float routines in compiler-rt.
10276789Sdim// This file does not provide any part of the compiler-rt interface, but defines
11276789Sdim// many useful constants and utility routines that are used in the
12276789Sdim// implementation of the soft-float routines in compiler-rt.
13276789Sdim//
14276789Sdim// Assumes that float, double and long double correspond to the IEEE-754
15276789Sdim// binary32, binary64 and binary 128 types, respectively, and that integer
16276789Sdim// endianness matches floating point endianness on the target platform.
17276789Sdim//
18276789Sdim//===----------------------------------------------------------------------===//
19276789Sdim
20276789Sdim#ifndef FP_LIB_HEADER
21276789Sdim#define FP_LIB_HEADER
22276789Sdim
23276789Sdim#include "int_lib.h"
24344779Sdim#include "int_math.h"
25353358Sdim#include <limits.h>
26353358Sdim#include <stdbool.h>
27353358Sdim#include <stdint.h>
28276789Sdim
29276789Sdim// x86_64 FreeBSD prior v9.3 define fixed-width types incorrectly in
30276789Sdim// 32-bit mode.
31276789Sdim#if defined(__FreeBSD__) && defined(__i386__)
32353358Sdim#include <sys/param.h>
33353358Sdim#if __FreeBSD_version < 903000 // v9.3
34353358Sdim#define uint64_t unsigned long long
35353358Sdim#define int64_t long long
36353358Sdim#undef UINT64_C
37353358Sdim#define UINT64_C(c) (c##ULL)
38276789Sdim#endif
39353358Sdim#endif
40276789Sdim
41276789Sdim#if defined SINGLE_PRECISION
42276789Sdim
43276789Sdimtypedef uint32_t rep_t;
44276789Sdimtypedef int32_t srep_t;
45276789Sdimtypedef float fp_t;
46276789Sdim#define REP_C UINT32_C
47276789Sdim#define significandBits 23
48276789Sdim
49353358Sdimstatic __inline int rep_clz(rep_t a) { return __builtin_clz(a); }
50276789Sdim
51276789Sdim// 32x32 --> 64 bit multiply
52296417Sdimstatic __inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {
53353358Sdim  const uint64_t product = (uint64_t)a * b;
54353358Sdim  *hi = product >> 32;
55353358Sdim  *lo = product;
56276789Sdim}
57276789SdimCOMPILER_RT_ABI fp_t __addsf3(fp_t a, fp_t b);
58276789Sdim
59276789Sdim#elif defined DOUBLE_PRECISION
60276789Sdim
61276789Sdimtypedef uint64_t rep_t;
62276789Sdimtypedef int64_t srep_t;
63276789Sdimtypedef double fp_t;
64276789Sdim#define REP_C UINT64_C
65276789Sdim#define significandBits 52
66276789Sdim
67296417Sdimstatic __inline int rep_clz(rep_t a) {
68276789Sdim#if defined __LP64__
69353358Sdim  return __builtin_clzl(a);
70276789Sdim#else
71353358Sdim  if (a & REP_C(0xffffffff00000000))
72353358Sdim    return __builtin_clz(a >> 32);
73353358Sdim  else
74353358Sdim    return 32 + __builtin_clz(a & REP_C(0xffffffff));
75276789Sdim#endif
76276789Sdim}
77276789Sdim
78276789Sdim#define loWord(a) (a & 0xffffffffU)
79276789Sdim#define hiWord(a) (a >> 32)
80276789Sdim
81276789Sdim// 64x64 -> 128 wide multiply for platforms that don't have such an operation;
82276789Sdim// many 64-bit platforms have this operation, but they tend to have hardware
83276789Sdim// floating-point, so we don't bother with a special case for them here.
84296417Sdimstatic __inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {
85353358Sdim  // Each of the component 32x32 -> 64 products
86353358Sdim  const uint64_t plolo = loWord(a) * loWord(b);
87353358Sdim  const uint64_t plohi = loWord(a) * hiWord(b);
88353358Sdim  const uint64_t philo = hiWord(a) * loWord(b);
89353358Sdim  const uint64_t phihi = hiWord(a) * hiWord(b);
90353358Sdim  // Sum terms that contribute to lo in a way that allows us to get the carry
91353358Sdim  const uint64_t r0 = loWord(plolo);
92353358Sdim  const uint64_t r1 = hiWord(plolo) + loWord(plohi) + loWord(philo);
93353358Sdim  *lo = r0 + (r1 << 32);
94353358Sdim  // Sum terms contributing to hi with the carry from lo
95353358Sdim  *hi = hiWord(plohi) + hiWord(philo) + hiWord(r1) + phihi;
96276789Sdim}
97276789Sdim#undef loWord
98276789Sdim#undef hiWord
99276789Sdim
100276789SdimCOMPILER_RT_ABI fp_t __adddf3(fp_t a, fp_t b);
101276789Sdim
102276789Sdim#elif defined QUAD_PRECISION
103353358Sdim#if __LDBL_MANT_DIG__ == 113 && defined(__SIZEOF_INT128__)
104276789Sdim#define CRT_LDBL_128BIT
105276789Sdimtypedef __uint128_t rep_t;
106276789Sdimtypedef __int128_t srep_t;
107276789Sdimtypedef long double fp_t;
108276789Sdim#define REP_C (__uint128_t)
109276789Sdim// Note: Since there is no explicit way to tell compiler the constant is a
110276789Sdim// 128-bit integer, we let the constant be casted to 128-bit integer
111276789Sdim#define significandBits 112
112276789Sdim
113296417Sdimstatic __inline int rep_clz(rep_t a) {
114353358Sdim  const union {
115353358Sdim    __uint128_t ll;
116276789Sdim#if _YUGA_BIG_ENDIAN
117353358Sdim    struct {
118353358Sdim      uint64_t high, low;
119353358Sdim    } s;
120276789Sdim#else
121353358Sdim    struct {
122353358Sdim      uint64_t low, high;
123353358Sdim    } s;
124276789Sdim#endif
125353358Sdim  } uu = {.ll = a};
126276789Sdim
127353358Sdim  uint64_t word;
128353358Sdim  uint64_t add;
129276789Sdim
130353358Sdim  if (uu.s.high) {
131353358Sdim    word = uu.s.high;
132353358Sdim    add = 0;
133353358Sdim  } else {
134353358Sdim    word = uu.s.low;
135353358Sdim    add = 64;
136353358Sdim  }
137353358Sdim  return __builtin_clzll(word) + add;
138276789Sdim}
139276789Sdim
140353358Sdim#define Word_LoMask UINT64_C(0x00000000ffffffff)
141353358Sdim#define Word_HiMask UINT64_C(0xffffffff00000000)
142276789Sdim#define Word_FullMask UINT64_C(0xffffffffffffffff)
143276789Sdim#define Word_1(a) (uint64_t)((a >> 96) & Word_LoMask)
144276789Sdim#define Word_2(a) (uint64_t)((a >> 64) & Word_LoMask)
145276789Sdim#define Word_3(a) (uint64_t)((a >> 32) & Word_LoMask)
146276789Sdim#define Word_4(a) (uint64_t)(a & Word_LoMask)
147276789Sdim
148276789Sdim// 128x128 -> 256 wide multiply for platforms that don't have such an operation;
149276789Sdim// many 64-bit platforms have this operation, but they tend to have hardware
150276789Sdim// floating-point, so we don't bother with a special case for them here.
151296417Sdimstatic __inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {
152276789Sdim
153353358Sdim  const uint64_t product11 = Word_1(a) * Word_1(b);
154353358Sdim  const uint64_t product12 = Word_1(a) * Word_2(b);
155353358Sdim  const uint64_t product13 = Word_1(a) * Word_3(b);
156353358Sdim  const uint64_t product14 = Word_1(a) * Word_4(b);
157353358Sdim  const uint64_t product21 = Word_2(a) * Word_1(b);
158353358Sdim  const uint64_t product22 = Word_2(a) * Word_2(b);
159353358Sdim  const uint64_t product23 = Word_2(a) * Word_3(b);
160353358Sdim  const uint64_t product24 = Word_2(a) * Word_4(b);
161353358Sdim  const uint64_t product31 = Word_3(a) * Word_1(b);
162353358Sdim  const uint64_t product32 = Word_3(a) * Word_2(b);
163353358Sdim  const uint64_t product33 = Word_3(a) * Word_3(b);
164353358Sdim  const uint64_t product34 = Word_3(a) * Word_4(b);
165353358Sdim  const uint64_t product41 = Word_4(a) * Word_1(b);
166353358Sdim  const uint64_t product42 = Word_4(a) * Word_2(b);
167353358Sdim  const uint64_t product43 = Word_4(a) * Word_3(b);
168353358Sdim  const uint64_t product44 = Word_4(a) * Word_4(b);
169276789Sdim
170353358Sdim  const __uint128_t sum0 = (__uint128_t)product44;
171353358Sdim  const __uint128_t sum1 = (__uint128_t)product34 + (__uint128_t)product43;
172353358Sdim  const __uint128_t sum2 =
173353358Sdim      (__uint128_t)product24 + (__uint128_t)product33 + (__uint128_t)product42;
174353358Sdim  const __uint128_t sum3 = (__uint128_t)product14 + (__uint128_t)product23 +
175353358Sdim                           (__uint128_t)product32 + (__uint128_t)product41;
176353358Sdim  const __uint128_t sum4 =
177353358Sdim      (__uint128_t)product13 + (__uint128_t)product22 + (__uint128_t)product31;
178353358Sdim  const __uint128_t sum5 = (__uint128_t)product12 + (__uint128_t)product21;
179353358Sdim  const __uint128_t sum6 = (__uint128_t)product11;
180276789Sdim
181353358Sdim  const __uint128_t r0 = (sum0 & Word_FullMask) + ((sum1 & Word_LoMask) << 32);
182353358Sdim  const __uint128_t r1 = (sum0 >> 64) + ((sum1 >> 32) & Word_FullMask) +
183353358Sdim                         (sum2 & Word_FullMask) + ((sum3 << 32) & Word_HiMask);
184276789Sdim
185353358Sdim  *lo = r0 + (r1 << 64);
186353358Sdim  *hi = (r1 >> 64) + (sum1 >> 96) + (sum2 >> 64) + (sum3 >> 32) + sum4 +
187353358Sdim        (sum5 << 32) + (sum6 << 64);
188276789Sdim}
189276789Sdim#undef Word_1
190276789Sdim#undef Word_2
191276789Sdim#undef Word_3
192276789Sdim#undef Word_4
193276789Sdim#undef Word_HiMask
194276789Sdim#undef Word_LoMask
195276789Sdim#undef Word_FullMask
196353358Sdim#endif // __LDBL_MANT_DIG__ == 113 && __SIZEOF_INT128__
197276789Sdim#else
198276789Sdim#error SINGLE_PRECISION, DOUBLE_PRECISION or QUAD_PRECISION must be defined.
199276789Sdim#endif
200276789Sdim
201353358Sdim#if defined(SINGLE_PRECISION) || defined(DOUBLE_PRECISION) ||                  \
202353358Sdim    defined(CRT_LDBL_128BIT)
203353358Sdim#define typeWidth (sizeof(rep_t) * CHAR_BIT)
204353358Sdim#define exponentBits (typeWidth - significandBits - 1)
205353358Sdim#define maxExponent ((1 << exponentBits) - 1)
206353358Sdim#define exponentBias (maxExponent >> 1)
207276789Sdim
208353358Sdim#define implicitBit (REP_C(1) << significandBits)
209276789Sdim#define significandMask (implicitBit - 1U)
210353358Sdim#define signBit (REP_C(1) << (significandBits + exponentBits))
211353358Sdim#define absMask (signBit - 1U)
212353358Sdim#define exponentMask (absMask ^ significandMask)
213353358Sdim#define oneRep ((rep_t)exponentBias << significandBits)
214353358Sdim#define infRep exponentMask
215353358Sdim#define quietBit (implicitBit >> 1)
216353358Sdim#define qnanRep (exponentMask | quietBit)
217276789Sdim
218296417Sdimstatic __inline rep_t toRep(fp_t x) {
219353358Sdim  const union {
220353358Sdim    fp_t f;
221353358Sdim    rep_t i;
222353358Sdim  } rep = {.f = x};
223353358Sdim  return rep.i;
224276789Sdim}
225276789Sdim
226296417Sdimstatic __inline fp_t fromRep(rep_t x) {
227353358Sdim  const union {
228353358Sdim    fp_t f;
229353358Sdim    rep_t i;
230353358Sdim  } rep = {.i = x};
231353358Sdim  return rep.f;
232276789Sdim}
233276789Sdim
234296417Sdimstatic __inline int normalize(rep_t *significand) {
235353358Sdim  const int shift = rep_clz(*significand) - rep_clz(implicitBit);
236353358Sdim  *significand <<= shift;
237353358Sdim  return 1 - shift;
238276789Sdim}
239276789Sdim
240296417Sdimstatic __inline void wideLeftShift(rep_t *hi, rep_t *lo, int count) {
241353358Sdim  *hi = *hi << count | *lo >> (typeWidth - count);
242353358Sdim  *lo = *lo << count;
243276789Sdim}
244276789Sdim
245353358Sdimstatic __inline void wideRightShiftWithSticky(rep_t *hi, rep_t *lo,
246353358Sdim                                              unsigned int count) {
247353358Sdim  if (count < typeWidth) {
248360784Sdim    const bool sticky = (*lo << (typeWidth - count)) != 0;
249353358Sdim    *lo = *hi << (typeWidth - count) | *lo >> count | sticky;
250353358Sdim    *hi = *hi >> count;
251353358Sdim  } else if (count < 2 * typeWidth) {
252353358Sdim    const bool sticky = *hi << (2 * typeWidth - count) | *lo;
253353358Sdim    *lo = *hi >> (count - typeWidth) | sticky;
254353358Sdim    *hi = 0;
255353358Sdim  } else {
256353358Sdim    const bool sticky = *hi | *lo;
257353358Sdim    *lo = sticky;
258353358Sdim    *hi = 0;
259353358Sdim  }
260276789Sdim}
261344779Sdim
262344779Sdim// Implements logb methods (logb, logbf, logbl) for IEEE-754. This avoids
263344779Sdim// pulling in a libm dependency from compiler-rt, but is not meant to replace
264344779Sdim// it (i.e. code calling logb() should get the one from libm, not this), hence
265344779Sdim// the __compiler_rt prefix.
266344779Sdimstatic __inline fp_t __compiler_rt_logbX(fp_t x) {
267344779Sdim  rep_t rep = toRep(x);
268344779Sdim  int exp = (rep & exponentMask) >> significandBits;
269344779Sdim
270344779Sdim  // Abnormal cases:
271344779Sdim  // 1) +/- inf returns +inf; NaN returns NaN
272344779Sdim  // 2) 0.0 returns -inf
273344779Sdim  if (exp == maxExponent) {
274344779Sdim    if (((rep & signBit) == 0) || (x != x)) {
275353358Sdim      return x; // NaN or +inf: return x
276344779Sdim    } else {
277353358Sdim      return -x; // -inf: return -x
278344779Sdim    }
279344779Sdim  } else if (x == 0.0) {
280344779Sdim    // 0.0: return -inf
281344779Sdim    return fromRep(infRep | signBit);
282344779Sdim  }
283344779Sdim
284344779Sdim  if (exp != 0) {
285344779Sdim    // Normal number
286353358Sdim    return exp - exponentBias; // Unbias exponent
287344779Sdim  } else {
288344779Sdim    // Subnormal number; normalize and repeat
289344779Sdim    rep &= absMask;
290344779Sdim    const int shift = 1 - normalize(&rep);
291344779Sdim    exp = (rep & exponentMask) >> significandBits;
292353358Sdim    return exp - exponentBias - shift; // Unbias exponent
293344779Sdim  }
294344779Sdim}
295276789Sdim#endif
296276789Sdim
297344779Sdim#if defined(SINGLE_PRECISION)
298344779Sdimstatic __inline fp_t __compiler_rt_logbf(fp_t x) {
299344779Sdim  return __compiler_rt_logbX(x);
300344779Sdim}
301344779Sdim#elif defined(DOUBLE_PRECISION)
302344779Sdimstatic __inline fp_t __compiler_rt_logb(fp_t x) {
303344779Sdim  return __compiler_rt_logbX(x);
304344779Sdim}
305344779Sdim#elif defined(QUAD_PRECISION)
306353358Sdim#if defined(CRT_LDBL_128BIT)
307344779Sdimstatic __inline fp_t __compiler_rt_logbl(fp_t x) {
308344779Sdim  return __compiler_rt_logbX(x);
309344779Sdim}
310353358Sdim#else
311344779Sdim// The generic implementation only works for ieee754 floating point. For other
312344779Sdim// floating point types, continue to rely on the libm implementation for now.
313344779Sdimstatic __inline long double __compiler_rt_logbl(long double x) {
314344779Sdim  return crt_logbl(x);
315344779Sdim}
316344779Sdim#endif
317353358Sdim#endif
318344779Sdim
319276789Sdim#endif // FP_LIB_HEADER
320