+++ /dev/null
-/**\r
-University of Illinois/NCSA\r
-Open Source License\r
-\r
-Copyright (c) 2009-2014 by the contributors listed in CREDITS.TXT\r
-\r
-All rights reserved.\r
-\r
-Developed by:\r
-\r
- LLVM Team\r
-\r
- University of Illinois at Urbana-Champaign\r
-\r
- http://llvm.org\r
-\r
-Permission is hereby granted, free of charge, to any person obtaining a copy of\r
-this software and associated documentation files (the "Software"), to deal with\r
-the Software without restriction, including without limitation the rights to\r
-use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies\r
-of the Software, and to permit persons to whom the Software is furnished to do\r
-so, subject to the following conditions:\r
-\r
- * Redistributions of source code must retain the above copyright notice,\r
- this list of conditions and the following disclaimers.\r
-\r
- * Redistributions in binary form must reproduce the above copyright notice,\r
- this list of conditions and the following disclaimers in the\r
- documentation and/or other materials provided with the distribution.\r
-\r
- * Neither the names of the LLVM Team, University of Illinois at\r
- Urbana-Champaign, nor the names of its contributors may be used to\r
- endorse or promote products derived from this Software without specific\r
- prior written permission.\r
-\r
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
-FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
-CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE\r
-SOFTWARE.\r
-**/\r
-\r
-#ifndef FP_LIB_HEADER\r
-#define FP_LIB_HEADER\r
-\r
-#include <stdint.h>\r
-#include <stdbool.h>\r
-#include <limits.h>\r
-#include "int_lib.h"\r
-\r
-#if defined SINGLE_PRECISION\r
-\r
-typedef uint32_t rep_t;\r
-typedef int32_t srep_t;\r
-typedef float fp_t;\r
-#define REP_C UINT32_C\r
-#define significandBits 23\r
-\r
-static inline int rep_clz(rep_t a) {\r
- return __builtin_clz(a);\r
-}\r
-\r
-// 32x32 --> 64 bit multiply\r
-static inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {\r
- const uint64_t product = (uint64_t)a*b;\r
- *hi = product >> 32;\r
- *lo = product;\r
-}\r
-COMPILER_RT_ABI fp_t __addsf3(fp_t a, fp_t b);\r
-\r
-#elif defined DOUBLE_PRECISION\r
-\r
-typedef uint64_t rep_t;\r
-typedef int64_t srep_t;\r
-typedef double fp_t;\r
-#define REP_C UINT64_C\r
-#define significandBits 52\r
-\r
-static inline int rep_clz(rep_t a) {\r
-#if defined __LP64__\r
- return __builtin_clzl(a);\r
-#else\r
- if (a & REP_C(0xffffffff00000000))\r
- return __builtin_clz(a >> 32);\r
- else\r
- return 32 + __builtin_clz(a & REP_C(0xffffffff));\r
-#endif\r
-}\r
-\r
-#define loWord(a) (a & 0xffffffffU)\r
-#define hiWord(a) (a >> 32)\r
-\r
-// 64x64 -> 128 wide multiply for platforms that don't have such an operation;\r
-// many 64-bit platforms have this operation, but they tend to have hardware\r
-// floating-point, so we don't bother with a special case for them here.\r
-static inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {\r
- // Each of the component 32x32 -> 64 products\r
- const uint64_t plolo = loWord(a) * loWord(b);\r
- const uint64_t plohi = loWord(a) * hiWord(b);\r
- const uint64_t philo = hiWord(a) * loWord(b);\r
- const uint64_t phihi = hiWord(a) * hiWord(b);\r
- // Sum terms that contribute to lo in a way that allows us to get the carry\r
- const uint64_t r0 = loWord(plolo);\r
- const uint64_t r1 = hiWord(plolo) + loWord(plohi) + loWord(philo);\r
- *lo = r0 + (r1 << 32);\r
- // Sum terms contributing to hi with the carry from lo\r
- *hi = hiWord(plohi) + hiWord(philo) + hiWord(r1) + phihi;\r
-}\r
-#undef loWord\r
-#undef hiWord\r
-\r
-COMPILER_RT_ABI fp_t __adddf3(fp_t a, fp_t b);\r
-\r
-#elif defined QUAD_PRECISION\r
-#if __LDBL_MANT_DIG__ == 113\r
-#define CRT_LDBL_128BIT\r
-typedef __uint128_t rep_t;\r
-typedef __int128_t srep_t;\r
-typedef long double fp_t;\r
-#define REP_C (__uint128_t)\r
-// Note: Since there is no explicit way to tell compiler the constant is a\r
-// 128-bit integer, we let the constant be casted to 128-bit integer\r
-#define significandBits 112\r
-\r
-static inline int rep_clz(rep_t a) {\r
- const union\r
- {\r
- __uint128_t ll;\r
-#if _YUGA_BIG_ENDIAN\r
- struct { uint64_t high, low; } s;\r
-#else\r
- struct { uint64_t low, high; } s;\r
-#endif\r
- } uu = { .ll = a };\r
-\r
- uint64_t word;\r
- uint64_t add;\r
-\r
- if (uu.s.high){\r
- word = uu.s.high;\r
- add = 0;\r
- }\r
- else{\r
- word = uu.s.low;\r
- add = 64;\r
- }\r
- return __builtin_clzll(word) + add;\r
-}\r
-\r
-#define Word_LoMask UINT64_C(0x00000000ffffffff)\r
-#define Word_HiMask UINT64_C(0xffffffff00000000)\r
-#define Word_FullMask UINT64_C(0xffffffffffffffff)\r
-#define Word_1(a) (uint64_t)((a >> 96) & Word_LoMask)\r
-#define Word_2(a) (uint64_t)((a >> 64) & Word_LoMask)\r
-#define Word_3(a) (uint64_t)((a >> 32) & Word_LoMask)\r
-#define Word_4(a) (uint64_t)(a & Word_LoMask)\r
-\r
-// 128x128 -> 256 wide multiply for platforms that don't have such an operation;\r
-// many 64-bit platforms have this operation, but they tend to have hardware\r
-// floating-point, so we don't bother with a special case for them here.\r
-static inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {\r
-\r
- const uint64_t product11 = Word_1(a) * Word_1(b);\r
- const uint64_t product12 = Word_1(a) * Word_2(b);\r
- const uint64_t product13 = Word_1(a) * Word_3(b);\r
- const uint64_t product14 = Word_1(a) * Word_4(b);\r
- const uint64_t product21 = Word_2(a) * Word_1(b);\r
- const uint64_t product22 = Word_2(a) * Word_2(b);\r
- const uint64_t product23 = Word_2(a) * Word_3(b);\r
- const uint64_t product24 = Word_2(a) * Word_4(b);\r
- const uint64_t product31 = Word_3(a) * Word_1(b);\r
- const uint64_t product32 = Word_3(a) * Word_2(b);\r
- const uint64_t product33 = Word_3(a) * Word_3(b);\r
- const uint64_t product34 = Word_3(a) * Word_4(b);\r
- const uint64_t product41 = Word_4(a) * Word_1(b);\r
- const uint64_t product42 = Word_4(a) * Word_2(b);\r
- const uint64_t product43 = Word_4(a) * Word_3(b);\r
- const uint64_t product44 = Word_4(a) * Word_4(b);\r
-\r
- const __uint128_t sum0 = (__uint128_t)product44;\r
- const __uint128_t sum1 = (__uint128_t)product34 +\r
- (__uint128_t)product43;\r
- const __uint128_t sum2 = (__uint128_t)product24 +\r
- (__uint128_t)product33 +\r
- (__uint128_t)product42;\r
- const __uint128_t sum3 = (__uint128_t)product14 +\r
- (__uint128_t)product23 +\r
- (__uint128_t)product32 +\r
- (__uint128_t)product41;\r
- const __uint128_t sum4 = (__uint128_t)product13 +\r
- (__uint128_t)product22 +\r
- (__uint128_t)product31;\r
- const __uint128_t sum5 = (__uint128_t)product12 +\r
- (__uint128_t)product21;\r
- const __uint128_t sum6 = (__uint128_t)product11;\r
-\r
- const __uint128_t r0 = (sum0 & Word_FullMask) +\r
- ((sum1 & Word_LoMask) << 32);\r
- const __uint128_t r1 = (sum0 >> 64) +\r
- ((sum1 >> 32) & Word_FullMask) +\r
- (sum2 & Word_FullMask) +\r
- ((sum3 << 32) & Word_HiMask);\r
-\r
- *lo = r0 + (r1 << 64);\r
- *hi = (r1 >> 64) +\r
- (sum1 >> 96) +\r
- (sum2 >> 64) +\r
- (sum3 >> 32) +\r
- sum4 +\r
- (sum5 << 32) +\r
- (sum6 << 64);\r
-}\r
-#undef Word_1\r
-#undef Word_2\r
-#undef Word_3\r
-#undef Word_4\r
-#undef Word_HiMask\r
-#undef Word_LoMask\r
-#undef Word_FullMask\r
-#endif // __LDBL_MANT_DIG__ == 113\r
-#else\r
-#error SINGLE_PRECISION, DOUBLE_PRECISION or QUAD_PRECISION must be defined.\r
-#endif\r
-\r
-#if defined(SINGLE_PRECISION) || defined(DOUBLE_PRECISION) || defined(CRT_LDBL_128BIT)\r
-#define typeWidth (sizeof(rep_t)*CHAR_BIT)\r
-#define exponentBits (typeWidth - significandBits - 1)\r
-#define maxExponent ((1 << exponentBits) - 1)\r
-#define exponentBias (maxExponent >> 1)\r
-\r
-#define implicitBit (REP_C(1) << significandBits)\r
-#define significandMask (implicitBit - 1U)\r
-#define signBit (REP_C(1) << (significandBits + exponentBits))\r
-#define absMask (signBit - 1U)\r
-#define exponentMask (absMask ^ significandMask)\r
-#define oneRep ((rep_t)exponentBias << significandBits)\r
-#define infRep exponentMask\r
-#define quietBit (implicitBit >> 1)\r
-#define qnanRep (exponentMask | quietBit)\r
-\r
-static inline rep_t toRep(fp_t x) {\r
- const union { fp_t f; rep_t i; } rep = {.f = x};\r
- return rep.i;\r
-}\r
-\r
-static inline fp_t fromRep(rep_t x) {\r
- const union { fp_t f; rep_t i; } rep = {.i = x};\r
- return rep.f;\r
-}\r
-\r
-static inline int normalize(rep_t *significand) {\r
- const int shift = rep_clz(*significand) - rep_clz(implicitBit);\r
- *significand <<= shift;\r
- return 1 - shift;\r
-}\r
-\r
-static inline void wideLeftShift(rep_t *hi, rep_t *lo, int count) {\r
- *hi = *hi << count | *lo >> (typeWidth - count);\r
- *lo = *lo << count;\r
-}\r
-\r
-static inline void wideRightShiftWithSticky(rep_t *hi, rep_t *lo, unsigned int count) {\r
- if (count < typeWidth) {\r
- const bool sticky = *lo << (typeWidth - count);\r
- *lo = *hi << (typeWidth - count) | *lo >> count | sticky;\r
- *hi = *hi >> count;\r
- }\r
- else if (count < 2*typeWidth) {\r
- const bool sticky = *hi << (2*typeWidth - count) | *lo;\r
- *lo = *hi >> (count - typeWidth) | sticky;\r
- *hi = 0;\r
- } else {\r
- const bool sticky = *hi | *lo;\r
- *lo = sticky;\r
- *hi = 0;\r
- }\r
-}\r
-#endif\r
-\r
-#endif // FP_LIB_HEADER\r