1*0a6a1f1dSLionel Sambuc //=== lib/fp_trunc.h - high precision -> low precision conversion *- C -*-===// 2*0a6a1f1dSLionel Sambuc // 3*0a6a1f1dSLionel Sambuc // The LLVM Compiler Infrastructure 4*0a6a1f1dSLionel Sambuc // 5*0a6a1f1dSLionel Sambuc // This file is dual licensed under the MIT and the University of Illinois Open 6*0a6a1f1dSLionel Sambuc // Source Licenses. See LICENSE.TXT for details. 7*0a6a1f1dSLionel Sambuc // 8*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===// 9*0a6a1f1dSLionel Sambuc // 10*0a6a1f1dSLionel Sambuc // Set source and destination precision setting 11*0a6a1f1dSLionel Sambuc // 12*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===// 13*0a6a1f1dSLionel Sambuc 14*0a6a1f1dSLionel Sambuc #ifndef FP_TRUNC_HEADER 15*0a6a1f1dSLionel Sambuc #define FP_TRUNC_HEADER 16*0a6a1f1dSLionel Sambuc 17*0a6a1f1dSLionel Sambuc #include "int_lib.h" 18*0a6a1f1dSLionel Sambuc 19*0a6a1f1dSLionel Sambuc #if defined SRC_DOUBLE 20*0a6a1f1dSLionel Sambuc typedef double src_t; 21*0a6a1f1dSLionel Sambuc typedef uint64_t src_rep_t; 22*0a6a1f1dSLionel Sambuc #define SRC_REP_C UINT64_C 23*0a6a1f1dSLionel Sambuc static const int srcSigBits = 52; 24*0a6a1f1dSLionel Sambuc 25*0a6a1f1dSLionel Sambuc #elif defined SRC_QUAD 26*0a6a1f1dSLionel Sambuc typedef long double src_t; 27*0a6a1f1dSLionel Sambuc typedef __uint128_t src_rep_t; 28*0a6a1f1dSLionel Sambuc #define SRC_REP_C (__uint128_t) 29*0a6a1f1dSLionel Sambuc static const int srcSigBits = 112; 30*0a6a1f1dSLionel Sambuc 31*0a6a1f1dSLionel Sambuc #else 32*0a6a1f1dSLionel Sambuc #error Source should be double precision or quad precision! 33*0a6a1f1dSLionel Sambuc #endif //end source precision 34*0a6a1f1dSLionel Sambuc 35*0a6a1f1dSLionel Sambuc #if defined DST_DOUBLE 36*0a6a1f1dSLionel Sambuc typedef double dst_t; 37*0a6a1f1dSLionel Sambuc typedef uint64_t dst_rep_t; 38*0a6a1f1dSLionel Sambuc #define DST_REP_C UINT64_C 39*0a6a1f1dSLionel Sambuc static const int dstSigBits = 52; 40*0a6a1f1dSLionel Sambuc 41*0a6a1f1dSLionel Sambuc #elif defined DST_SINGLE 42*0a6a1f1dSLionel Sambuc typedef float dst_t; 43*0a6a1f1dSLionel Sambuc typedef uint32_t dst_rep_t; 44*0a6a1f1dSLionel Sambuc #define DST_REP_C UINT32_C 45*0a6a1f1dSLionel Sambuc static const int dstSigBits = 23; 46*0a6a1f1dSLionel Sambuc 47*0a6a1f1dSLionel Sambuc #else 48*0a6a1f1dSLionel Sambuc #error Destination should be single precision or double precision! 49*0a6a1f1dSLionel Sambuc #endif //end destination precision 50*0a6a1f1dSLionel Sambuc 51*0a6a1f1dSLionel Sambuc // End of specialization parameters. Two helper routines for conversion to and 52*0a6a1f1dSLionel Sambuc // from the representation of floating-point data as integer values follow. 53*0a6a1f1dSLionel Sambuc srcToRep(src_t x)54*0a6a1f1dSLionel Sambucstatic inline src_rep_t srcToRep(src_t x) { 55*0a6a1f1dSLionel Sambuc const union { src_t f; src_rep_t i; } rep = {.f = x}; 56*0a6a1f1dSLionel Sambuc return rep.i; 57*0a6a1f1dSLionel Sambuc } 58*0a6a1f1dSLionel Sambuc dstFromRep(dst_rep_t x)59*0a6a1f1dSLionel Sambucstatic inline dst_t dstFromRep(dst_rep_t x) { 60*0a6a1f1dSLionel Sambuc const union { dst_t f; dst_rep_t i; } rep = {.i = x}; 61*0a6a1f1dSLionel Sambuc return rep.f; 62*0a6a1f1dSLionel Sambuc } 63*0a6a1f1dSLionel Sambuc 64*0a6a1f1dSLionel Sambuc #endif // FP_TRUNC_HEADER 65