fp_lib.h revision 222656
1214152Sed//===-- lib/fp_lib.h - Floating-point utilities -------------------*- C -*-===//
2214152Sed//
3214152Sed//                     The LLVM Compiler Infrastructure
4214152Sed//
5222656Sed// This file is dual licensed under the MIT and the University of Illinois Open
6222656Sed// Source Licenses. See LICENSE.TXT for details.
7214152Sed//
8214152Sed//===----------------------------------------------------------------------===//
9214152Sed//
10214152Sed// This file is a configuration header for soft-float routines in compiler-rt.
11214152Sed// This file does not provide any part of the compiler-rt interface, but defines
12214152Sed// many useful constants and utility routines that are used in the
13214152Sed// implementation of the soft-float routines in compiler-rt.
14214152Sed//
15214152Sed// Assumes that float and double correspond to the IEEE-754 binary32 and
16214152Sed// binary64 types, respectively, and that integer endianness matches floating
17214152Sed// point endianness on the target platform.
18214152Sed//
19214152Sed//===----------------------------------------------------------------------===//
20214152Sed
21214152Sed#ifndef FP_LIB_HEADER
22214152Sed#define FP_LIB_HEADER
23214152Sed
24214152Sed#include <stdint.h>
25214152Sed#include <stdbool.h>
26214152Sed#include <limits.h>
27214152Sed
28214152Sed#if defined SINGLE_PRECISION
29214152Sed
30214152Sedtypedef uint32_t rep_t;
31214152Sedtypedef int32_t srep_t;
32214152Sedtypedef float fp_t;
33214152Sed#define REP_C UINT32_C
34214152Sed#define significandBits 23
35214152Sed
36214152Sedstatic inline int rep_clz(rep_t a) {
37214152Sed    return __builtin_clz(a);
38214152Sed}
39214152Sed
40214152Sed// 32x32 --> 64 bit multiply
41214152Sedstatic inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {
42214152Sed    const uint64_t product = (uint64_t)a*b;
43214152Sed    *hi = product >> 32;
44214152Sed    *lo = product;
45214152Sed}
46214152Sed
47214152Sed#elif defined DOUBLE_PRECISION
48214152Sed
49214152Sedtypedef uint64_t rep_t;
50214152Sedtypedef int64_t srep_t;
51214152Sedtypedef double fp_t;
52214152Sed#define REP_C UINT64_C
53214152Sed#define significandBits 52
54214152Sed
55214152Sedstatic inline int rep_clz(rep_t a) {
56214152Sed#if defined __LP64__
57214152Sed    return __builtin_clzl(a);
58214152Sed#else
59214152Sed    if (a & REP_C(0xffffffff00000000))
60214152Sed        return __builtin_clz(a >> 32);
61214152Sed    else
62214152Sed        return 32 + __builtin_clz(a & REP_C(0xffffffff));
63214152Sed#endif
64214152Sed}
65214152Sed
66214152Sed#define loWord(a) (a & 0xffffffffU)
67214152Sed#define hiWord(a) (a >> 32)
68214152Sed
69214152Sed// 64x64 -> 128 wide multiply for platforms that don't have such an operation;
70214152Sed// many 64-bit platforms have this operation, but they tend to have hardware
71214152Sed// floating-point, so we don't bother with a special case for them here.
72214152Sedstatic inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {
73214152Sed    // Each of the component 32x32 -> 64 products
74214152Sed    const uint64_t plolo = loWord(a) * loWord(b);
75214152Sed    const uint64_t plohi = loWord(a) * hiWord(b);
76214152Sed    const uint64_t philo = hiWord(a) * loWord(b);
77214152Sed    const uint64_t phihi = hiWord(a) * hiWord(b);
78214152Sed    // Sum terms that contribute to lo in a way that allows us to get the carry
79214152Sed    const uint64_t r0 = loWord(plolo);
80214152Sed    const uint64_t r1 = hiWord(plolo) + loWord(plohi) + loWord(philo);
81214152Sed    *lo = r0 + (r1 << 32);
82214152Sed    // Sum terms contributing to hi with the carry from lo
83214152Sed    *hi = hiWord(plohi) + hiWord(philo) + hiWord(r1) + phihi;
84214152Sed}
85214152Sed
86214152Sed#else
87214152Sed#error Either SINGLE_PRECISION or DOUBLE_PRECISION must be defined.
88214152Sed#endif
89214152Sed
90214152Sed#define typeWidth       (sizeof(rep_t)*CHAR_BIT)
91214152Sed#define exponentBits    (typeWidth - significandBits - 1)
92214152Sed#define maxExponent     ((1 << exponentBits) - 1)
93214152Sed#define exponentBias    (maxExponent >> 1)
94214152Sed
95214152Sed#define implicitBit     (REP_C(1) << significandBits)
96214152Sed#define significandMask (implicitBit - 1U)
97214152Sed#define signBit         (REP_C(1) << (significandBits + exponentBits))
98214152Sed#define absMask         (signBit - 1U)
99214152Sed#define exponentMask    (absMask ^ significandMask)
100214152Sed#define oneRep          ((rep_t)exponentBias << significandBits)
101214152Sed#define infRep          exponentMask
102214152Sed#define quietBit        (implicitBit >> 1)
103214152Sed#define qnanRep         (exponentMask | quietBit)
104214152Sed
105214152Sedstatic inline rep_t toRep(fp_t x) {
106214152Sed    const union { fp_t f; rep_t i; } rep = {.f = x};
107214152Sed    return rep.i;
108214152Sed}
109214152Sed
110214152Sedstatic inline fp_t fromRep(rep_t x) {
111214152Sed    const union { fp_t f; rep_t i; } rep = {.i = x};
112214152Sed    return rep.f;
113214152Sed}
114214152Sed
115214152Sedstatic inline int normalize(rep_t *significand) {
116214152Sed    const int shift = rep_clz(*significand) - rep_clz(implicitBit);
117214152Sed    *significand <<= shift;
118214152Sed    return 1 - shift;
119214152Sed}
120214152Sed
121214152Sedstatic inline void wideLeftShift(rep_t *hi, rep_t *lo, int count) {
122214152Sed    *hi = *hi << count | *lo >> (typeWidth - count);
123214152Sed    *lo = *lo << count;
124214152Sed}
125214152Sed
126214152Sedstatic inline void wideRightShiftWithSticky(rep_t *hi, rep_t *lo, int count) {
127214152Sed    if (count < typeWidth) {
128214152Sed        const bool sticky = *lo << (typeWidth - count);
129214152Sed        *lo = *hi << (typeWidth - count) | *lo >> count | sticky;
130214152Sed        *hi = *hi >> count;
131214152Sed    }
132214152Sed    else if (count < 2*typeWidth) {
133214152Sed        const bool sticky = *hi << (2*typeWidth - count) | *lo;
134214152Sed        *lo = *hi >> (count - typeWidth) | sticky;
135214152Sed        *hi = 0;
136214152Sed    } else {
137214152Sed        const bool sticky = *hi | *lo;
138214152Sed        *lo = sticky;
139214152Sed        *hi = 0;
140214152Sed    }
141214152Sed}
142214152Sed
143214152Sed#endif // FP_LIB_HEADER
144