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