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