Stephen Canon | 74eaf1f | 2010-07-01 17:58:24 +0000 | [diff] [blame] | 1 | //===-- lib/fp_lib.h - Floating-point utilities -------------------*- C -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Stephen Canon | b1fdde1 | 2010-07-01 15:52:42 +0000 | [diff] [blame] | 10 | // This file is a configuration header for soft-float routines in compiler-rt. |
Stephen Canon | 74eaf1f | 2010-07-01 17:58:24 +0000 | [diff] [blame] | 11 | // 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 Canon | b1fdde1 | 2010-07-01 15:52:42 +0000 | [diff] [blame] | 15 | // Assumes that float and double correspond to the IEEE-754 binary32 and |
Stephen Canon | 74eaf1f | 2010-07-01 17:58:24 +0000 | [diff] [blame] | 16 | // binary64 types, respectively, and that integer endianness matches floating |
| 17 | // point endianness on the target platform. |
| 18 | // |
| 19 | //===----------------------------------------------------------------------===// |
Stephen Canon | b1fdde1 | 2010-07-01 15:52:42 +0000 | [diff] [blame] | 20 | |
| 21 | #ifndef FP_LIB_HEADER |
| 22 | #define FP_LIB_HEADER |
| 23 | |
| 24 | #include <stdint.h> |
| 25 | #include <stdbool.h> |
| 26 | #include <limits.h> |
| 27 | |
| 28 | #if defined SINGLE_PRECISION |
Stephen Canon | b1fdde1 | 2010-07-01 15:52:42 +0000 | [diff] [blame] | 29 | |
| 30 | typedef uint32_t rep_t; |
| 31 | typedef int32_t srep_t; |
| 32 | typedef float fp_t; |
| 33 | #define REP_C UINT32_C |
| 34 | #define significandBits 23 |
| 35 | |
| 36 | static inline int rep_clz(rep_t a) { |
| 37 | return __builtin_clz(a); |
| 38 | } |
| 39 | |
Stephen Canon | 644ab08 | 2010-07-04 16:53:39 +0000 | [diff] [blame] | 40 | // 32x32 --> 64 bit multiply |
| 41 | static inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) { |
| 42 | const uint64_t product = (uint64_t)a*b; |
| 43 | *hi = product >> 32; |
| 44 | *lo = product; |
| 45 | } |
| 46 | |
Stephen Canon | b1fdde1 | 2010-07-01 15:52:42 +0000 | [diff] [blame] | 47 | #elif defined DOUBLE_PRECISION |
Stephen Canon | b1fdde1 | 2010-07-01 15:52:42 +0000 | [diff] [blame] | 48 | |
| 49 | typedef uint64_t rep_t; |
| 50 | typedef int64_t srep_t; |
| 51 | typedef double fp_t; |
| 52 | #define REP_C UINT64_C |
| 53 | #define significandBits 52 |
| 54 | |
| 55 | static inline int rep_clz(rep_t a) { |
| 56 | #if defined __LP64__ |
| 57 | return __builtin_clzl(a); |
| 58 | #else |
| 59 | if (a & REP_C(0xffffffff00000000)) |
Stephen Canon | 8770d3d | 2010-07-26 18:17:00 +0000 | [diff] [blame] | 60 | return __builtin_clz(a >> 32); |
Stephen Canon | b1fdde1 | 2010-07-01 15:52:42 +0000 | [diff] [blame] | 61 | else |
Stephen Canon | 8770d3d | 2010-07-26 18:17:00 +0000 | [diff] [blame] | 62 | return 32 + __builtin_clz(a & REP_C(0xffffffff)); |
Stephen Canon | b1fdde1 | 2010-07-01 15:52:42 +0000 | [diff] [blame] | 63 | #endif |
| 64 | } |
| 65 | |
Stephen Canon | 644ab08 | 2010-07-04 16:53:39 +0000 | [diff] [blame] | 66 | #define loWord(a) (a & 0xffffffffU) |
| 67 | #define hiWord(a) (a >> 32) |
| 68 | |
| 69 | // 64x64 -> 128 wide multiply for platforms that don't have such an operation; |
| 70 | // many 64-bit platforms have this operation, but they tend to have hardware |
| 71 | // floating-point, so we don't bother with a special case for them here. |
| 72 | static inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) { |
| 73 | // Each of the component 32x32 -> 64 products |
| 74 | const uint64_t plolo = loWord(a) * loWord(b); |
| 75 | const uint64_t plohi = loWord(a) * hiWord(b); |
| 76 | const uint64_t philo = hiWord(a) * loWord(b); |
| 77 | const uint64_t phihi = hiWord(a) * hiWord(b); |
| 78 | // Sum terms that contribute to lo in a way that allows us to get the carry |
| 79 | const uint64_t r0 = loWord(plolo); |
| 80 | const uint64_t r1 = hiWord(plolo) + loWord(plohi) + loWord(philo); |
| 81 | *lo = r0 + (r1 << 32); |
| 82 | // Sum terms contributing to hi with the carry from lo |
| 83 | *hi = hiWord(plohi) + hiWord(philo) + hiWord(r1) + phihi; |
| 84 | } |
| 85 | |
Stephen Canon | b1fdde1 | 2010-07-01 15:52:42 +0000 | [diff] [blame] | 86 | #else |
| 87 | #error Either SINGLE_PRECISION or DOUBLE_PRECISION must be defined. |
| 88 | #endif |
| 89 | |
Stephen Canon | b1fdde1 | 2010-07-01 15:52:42 +0000 | [diff] [blame] | 90 | #define typeWidth (sizeof(rep_t)*CHAR_BIT) |
| 91 | #define exponentBits (typeWidth - significandBits - 1) |
| 92 | #define maxExponent ((1 << exponentBits) - 1) |
| 93 | #define exponentBias (maxExponent >> 1) |
| 94 | |
Stephen Canon | b1fdde1 | 2010-07-01 15:52:42 +0000 | [diff] [blame] | 95 | #define implicitBit (REP_C(1) << significandBits) |
| 96 | #define significandMask (implicitBit - 1U) |
| 97 | #define signBit (REP_C(1) << (significandBits + exponentBits)) |
| 98 | #define absMask (signBit - 1U) |
| 99 | #define exponentMask (absMask ^ significandMask) |
| 100 | #define oneRep ((rep_t)exponentBias << significandBits) |
| 101 | #define infRep exponentMask |
| 102 | #define quietBit (implicitBit >> 1) |
| 103 | #define qnanRep (exponentMask | quietBit) |
| 104 | |
Stephen Canon | b1fdde1 | 2010-07-01 15:52:42 +0000 | [diff] [blame] | 105 | static inline rep_t toRep(fp_t x) { |
| 106 | const union { fp_t f; rep_t i; } rep = {.f = x}; |
| 107 | return rep.i; |
| 108 | } |
| 109 | |
| 110 | static inline fp_t fromRep(rep_t x) { |
| 111 | const union { fp_t f; rep_t i; } rep = {.i = x}; |
| 112 | return rep.f; |
| 113 | } |
| 114 | |
| 115 | static inline int normalize(rep_t *significand) { |
| 116 | const int shift = rep_clz(*significand) - rep_clz(implicitBit); |
| 117 | *significand <<= shift; |
| 118 | return 1 - shift; |
| 119 | } |
| 120 | |
| 121 | static inline void wideLeftShift(rep_t *hi, rep_t *lo, int count) { |
| 122 | *hi = *hi << count | *lo >> (typeWidth - count); |
| 123 | *lo = *lo << count; |
| 124 | } |
| 125 | |
| 126 | static inline void wideRightShiftWithSticky(rep_t *hi, rep_t *lo, int count) { |
| 127 | if (count < typeWidth) { |
| 128 | const bool sticky = *lo << (typeWidth - count); |
| 129 | *lo = *hi << (typeWidth - count) | *lo >> count | sticky; |
| 130 | *hi = *hi >> count; |
| 131 | } |
| 132 | else if (count < 2*typeWidth) { |
| 133 | const bool sticky = *hi << (2*typeWidth - count) | *lo; |
| 134 | *lo = *hi >> (count - typeWidth) | sticky; |
| 135 | *hi = 0; |
| 136 | } else { |
| 137 | const bool sticky = *hi | *lo; |
| 138 | *lo = sticky; |
| 139 | *hi = 0; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | #endif // FP_LIB_HEADER |