blob: 661119ae412a69f5fd65f867efc6ee2a377c0208 [file] [log] [blame]
Stephen Canon5c6d2ec2010-07-01 17:58:24 +00001//===-- lib/fp_lib.h - Floating-point utilities -------------------*- C -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Howard Hinnant9ad441f2010-11-16 22:13:33 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Stephen Canon5c6d2ec2010-07-01 17:58:24 +00007//
8//===----------------------------------------------------------------------===//
9//
Stephen Canone5086322010-07-01 15:52:42 +000010// This file is a configuration header for soft-float routines in compiler-rt.
Stephen Canon5c6d2ec2010-07-01 17:58:24 +000011// This file does not provide any part of the compiler-rt interface, but defines
12// many useful constants and utility routines that are used in the
13// implementation of the soft-float routines in compiler-rt.
14//
Stephen Canone5086322010-07-01 15:52:42 +000015// Assumes that float and double correspond to the IEEE-754 binary32 and
Stephen Canon5c6d2ec2010-07-01 17:58:24 +000016// binary64 types, respectively, and that integer endianness matches floating
17// point endianness on the target platform.
18//
19//===----------------------------------------------------------------------===//
Stephen Canone5086322010-07-01 15:52:42 +000020
21#ifndef FP_LIB_HEADER
22#define FP_LIB_HEADER
23
24#include <stdint.h>
25#include <stdbool.h>
26#include <limits.h>
Daniel Dunbar0ae9d252011-11-15 18:34:44 +000027#include "int_lib.h"
Stephen Canone5086322010-07-01 15:52:42 +000028
29#if defined SINGLE_PRECISION
Stephen Canone5086322010-07-01 15:52:42 +000030
31typedef uint32_t rep_t;
32typedef int32_t srep_t;
33typedef float fp_t;
34#define REP_C UINT32_C
35#define significandBits 23
36
37static inline int rep_clz(rep_t a) {
38 return __builtin_clz(a);
39}
40
Stephen Canon12a7d092010-07-04 16:53:39 +000041// 32x32 --> 64 bit multiply
42static inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {
43 const uint64_t product = (uint64_t)a*b;
44 *hi = product >> 32;
45 *lo = product;
46}
47
Stephen Canone5086322010-07-01 15:52:42 +000048#elif defined DOUBLE_PRECISION
Stephen Canone5086322010-07-01 15:52:42 +000049
50typedef uint64_t rep_t;
51typedef int64_t srep_t;
52typedef double fp_t;
53#define REP_C UINT64_C
54#define significandBits 52
55
56static inline int rep_clz(rep_t a) {
57#if defined __LP64__
58 return __builtin_clzl(a);
59#else
60 if (a & REP_C(0xffffffff00000000))
Stephen Canonc41370b2010-07-26 18:17:00 +000061 return __builtin_clz(a >> 32);
Stephen Canone5086322010-07-01 15:52:42 +000062 else
Stephen Canonc41370b2010-07-26 18:17:00 +000063 return 32 + __builtin_clz(a & REP_C(0xffffffff));
Stephen Canone5086322010-07-01 15:52:42 +000064#endif
65}
66
Stephen Canon12a7d092010-07-04 16:53:39 +000067#define loWord(a) (a & 0xffffffffU)
68#define hiWord(a) (a >> 32)
69
70// 64x64 -> 128 wide multiply for platforms that don't have such an operation;
71// many 64-bit platforms have this operation, but they tend to have hardware
72// floating-point, so we don't bother with a special case for them here.
73static inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {
74 // Each of the component 32x32 -> 64 products
75 const uint64_t plolo = loWord(a) * loWord(b);
76 const uint64_t plohi = loWord(a) * hiWord(b);
77 const uint64_t philo = hiWord(a) * loWord(b);
78 const uint64_t phihi = hiWord(a) * hiWord(b);
79 // Sum terms that contribute to lo in a way that allows us to get the carry
80 const uint64_t r0 = loWord(plolo);
81 const uint64_t r1 = hiWord(plolo) + loWord(plohi) + loWord(philo);
82 *lo = r0 + (r1 << 32);
83 // Sum terms contributing to hi with the carry from lo
84 *hi = hiWord(plohi) + hiWord(philo) + hiWord(r1) + phihi;
85}
86
Stephen Canone5086322010-07-01 15:52:42 +000087#else
88#error Either SINGLE_PRECISION or DOUBLE_PRECISION must be defined.
89#endif
90
Stephen Canone5086322010-07-01 15:52:42 +000091#define typeWidth (sizeof(rep_t)*CHAR_BIT)
92#define exponentBits (typeWidth - significandBits - 1)
93#define maxExponent ((1 << exponentBits) - 1)
94#define exponentBias (maxExponent >> 1)
95
Stephen Canone5086322010-07-01 15:52:42 +000096#define implicitBit (REP_C(1) << significandBits)
97#define significandMask (implicitBit - 1U)
98#define signBit (REP_C(1) << (significandBits + exponentBits))
99#define absMask (signBit - 1U)
100#define exponentMask (absMask ^ significandMask)
101#define oneRep ((rep_t)exponentBias << significandBits)
102#define infRep exponentMask
103#define quietBit (implicitBit >> 1)
104#define qnanRep (exponentMask | quietBit)
105
Stephen Canone5086322010-07-01 15:52:42 +0000106static inline rep_t toRep(fp_t x) {
107 const union { fp_t f; rep_t i; } rep = {.f = x};
108 return rep.i;
109}
110
111static inline fp_t fromRep(rep_t x) {
112 const union { fp_t f; rep_t i; } rep = {.i = x};
113 return rep.f;
114}
115
116static inline int normalize(rep_t *significand) {
117 const int shift = rep_clz(*significand) - rep_clz(implicitBit);
118 *significand <<= shift;
119 return 1 - shift;
120}
121
122static inline void wideLeftShift(rep_t *hi, rep_t *lo, int count) {
123 *hi = *hi << count | *lo >> (typeWidth - count);
124 *lo = *lo << count;
125}
126
Joerg Sonnenberger0499b842012-06-18 18:51:13 +0000127static inline void wideRightShiftWithSticky(rep_t *hi, rep_t *lo, unsigned int count) {
Stephen Canone5086322010-07-01 15:52:42 +0000128 if (count < typeWidth) {
129 const bool sticky = *lo << (typeWidth - count);
130 *lo = *hi << (typeWidth - count) | *lo >> count | sticky;
131 *hi = *hi >> count;
132 }
133 else if (count < 2*typeWidth) {
134 const bool sticky = *hi << (2*typeWidth - count) | *lo;
135 *lo = *hi >> (count - typeWidth) | sticky;
136 *hi = 0;
137 } else {
138 const bool sticky = *hi | *lo;
139 *lo = sticky;
140 *hi = 0;
141 }
142}
143
144#endif // FP_LIB_HEADER