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