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