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> 27229135Sed#include "int_lib.h" 28214152Sed 29214152Sed#if defined SINGLE_PRECISION 30214152Sed 31214152Sedtypedef uint32_t rep_t; 32214152Sedtypedef int32_t srep_t; 33214152Sedtypedef float fp_t; 34214152Sed#define REP_C UINT32_C 35214152Sed#define significandBits 23 36214152Sed 37214152Sedstatic inline int rep_clz(rep_t a) { 38214152Sed return __builtin_clz(a); 39214152Sed} 40214152Sed 41214152Sed// 32x32 --> 64 bit multiply 42214152Sedstatic inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) { 43214152Sed const uint64_t product = (uint64_t)a*b; 44214152Sed *hi = product >> 32; 45214152Sed *lo = product; 46214152Sed} 47214152Sed 48214152Sed#elif defined DOUBLE_PRECISION 49214152Sed 50214152Sedtypedef uint64_t rep_t; 51214152Sedtypedef int64_t srep_t; 52214152Sedtypedef double fp_t; 53214152Sed#define REP_C UINT64_C 54214152Sed#define significandBits 52 55214152Sed 56214152Sedstatic inline int rep_clz(rep_t a) { 57214152Sed#if defined __LP64__ 58214152Sed return __builtin_clzl(a); 59214152Sed#else 60214152Sed if (a & REP_C(0xffffffff00000000)) 61214152Sed return __builtin_clz(a >> 32); 62214152Sed else 63214152Sed return 32 + __builtin_clz(a & REP_C(0xffffffff)); 64214152Sed#endif 65214152Sed} 66214152Sed 67214152Sed#define loWord(a) (a & 0xffffffffU) 68214152Sed#define hiWord(a) (a >> 32) 69214152Sed 70214152Sed// 64x64 -> 128 wide multiply for platforms that don't have such an operation; 71214152Sed// many 64-bit platforms have this operation, but they tend to have hardware 72214152Sed// floating-point, so we don't bother with a special case for them here. 73214152Sedstatic inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) { 74214152Sed // Each of the component 32x32 -> 64 products 75214152Sed const uint64_t plolo = loWord(a) * loWord(b); 76214152Sed const uint64_t plohi = loWord(a) * hiWord(b); 77214152Sed const uint64_t philo = hiWord(a) * loWord(b); 78214152Sed const uint64_t phihi = hiWord(a) * hiWord(b); 79214152Sed // Sum terms that contribute to lo in a way that allows us to get the carry 80214152Sed const uint64_t r0 = loWord(plolo); 81214152Sed const uint64_t r1 = hiWord(plolo) + loWord(plohi) + loWord(philo); 82214152Sed *lo = r0 + (r1 << 32); 83214152Sed // Sum terms contributing to hi with the carry from lo 84214152Sed *hi = hiWord(plohi) + hiWord(philo) + hiWord(r1) + phihi; 85214152Sed} 86214152Sed 87214152Sed#else 88214152Sed#error Either SINGLE_PRECISION or DOUBLE_PRECISION must be defined. 89214152Sed#endif 90214152Sed 91214152Sed#define typeWidth (sizeof(rep_t)*CHAR_BIT) 92214152Sed#define exponentBits (typeWidth - significandBits - 1) 93214152Sed#define maxExponent ((1 << exponentBits) - 1) 94214152Sed#define exponentBias (maxExponent >> 1) 95214152Sed 96214152Sed#define implicitBit (REP_C(1) << significandBits) 97214152Sed#define significandMask (implicitBit - 1U) 98214152Sed#define signBit (REP_C(1) << (significandBits + exponentBits)) 99214152Sed#define absMask (signBit - 1U) 100214152Sed#define exponentMask (absMask ^ significandMask) 101214152Sed#define oneRep ((rep_t)exponentBias << significandBits) 102214152Sed#define infRep exponentMask 103214152Sed#define quietBit (implicitBit >> 1) 104214152Sed#define qnanRep (exponentMask | quietBit) 105214152Sed 106214152Sedstatic inline rep_t toRep(fp_t x) { 107214152Sed const union { fp_t f; rep_t i; } rep = {.f = x}; 108214152Sed return rep.i; 109214152Sed} 110214152Sed 111214152Sedstatic inline fp_t fromRep(rep_t x) { 112214152Sed const union { fp_t f; rep_t i; } rep = {.i = x}; 113214152Sed return rep.f; 114214152Sed} 115214152Sed 116214152Sedstatic inline int normalize(rep_t *significand) { 117214152Sed const int shift = rep_clz(*significand) - rep_clz(implicitBit); 118214152Sed *significand <<= shift; 119214152Sed return 1 - shift; 120214152Sed} 121214152Sed 122214152Sedstatic inline void wideLeftShift(rep_t *hi, rep_t *lo, int count) { 123214152Sed *hi = *hi << count | *lo >> (typeWidth - count); 124214152Sed *lo = *lo << count; 125214152Sed} 126214152Sed 127239138Sandrewstatic inline void wideRightShiftWithSticky(rep_t *hi, rep_t *lo, unsigned int count) { 128214152Sed if (count < typeWidth) { 129214152Sed const bool sticky = *lo << (typeWidth - count); 130214152Sed *lo = *hi << (typeWidth - count) | *lo >> count | sticky; 131214152Sed *hi = *hi >> count; 132214152Sed } 133214152Sed else if (count < 2*typeWidth) { 134214152Sed const bool sticky = *hi << (2*typeWidth - count) | *lo; 135214152Sed *lo = *hi >> (count - typeWidth) | sticky; 136214152Sed *hi = 0; 137214152Sed } else { 138214152Sed const bool sticky = *hi | *lo; 139214152Sed *lo = sticky; 140214152Sed *hi = 0; 141214152Sed } 142214152Sed} 143214152Sed 144214152Sed#endif // FP_LIB_HEADER 145