blob: 6d95a0680709369a6d1ce184e79939438f5bb12f [file] [log] [blame]
Joerg Sonnenberger7610e8c2014-05-29 00:54:26 +00001//===-lib/fp_extend.h - low precision -> high 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 setting
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef FP_EXTEND_HEADER
15#define FP_EXTEND_HEADER
16
17#include "int_lib.h"
18
19#if defined SRC_SINGLE
20typedef float src_t;
21typedef uint32_t src_rep_t;
22#define SRC_REP_C UINT32_C
23static const int srcSigBits = 23;
24#define src_rep_t_clz __builtin_clz
25
26#elif defined SRC_DOUBLE
27typedef double src_t;
28typedef uint64_t src_rep_t;
29#define SRC_REP_C UINT64_C
30static const int srcSigBits = 52;
Saleem Abdulrasool911cfc12015-10-10 21:21:28 +000031static __inline int src_rep_t_clz(src_rep_t a) {
Joerg Sonnenberger7610e8c2014-05-29 00:54:26 +000032#if defined __LP64__
33 return __builtin_clzl(a);
34#else
35 if (a & REP_C(0xffffffff00000000))
36 return __builtin_clz(a >> 32);
37 else
38 return 32 + __builtin_clz(a & REP_C(0xffffffff));
39#endif
40}
41
Ahmed Bougachaf1ac8502015-05-12 18:33:42 +000042#elif defined SRC_HALF
43typedef uint16_t src_t;
44typedef uint16_t src_rep_t;
45#define SRC_REP_C UINT16_C
46static const int srcSigBits = 10;
47#define src_rep_t_clz __builtin_clz
48
Joerg Sonnenberger7610e8c2014-05-29 00:54:26 +000049#else
Ahmed Bougachaf1ac8502015-05-12 18:33:42 +000050#error Source should be half, single, or double precision!
Joerg Sonnenberger7610e8c2014-05-29 00:54:26 +000051#endif //end source precision
52
Ahmed Bougachaf1ac8502015-05-12 18:33:42 +000053#if defined DST_SINGLE
54typedef float dst_t;
55typedef uint32_t dst_rep_t;
56#define DST_REP_C UINT32_C
57static const int dstSigBits = 23;
58
59#elif defined DST_DOUBLE
Joerg Sonnenberger7610e8c2014-05-29 00:54:26 +000060typedef double dst_t;
61typedef uint64_t dst_rep_t;
62#define DST_REP_C UINT64_C
63static const int dstSigBits = 52;
64
65#elif defined DST_QUAD
66typedef long double dst_t;
67typedef __uint128_t dst_rep_t;
68#define DST_REP_C (__uint128_t)
69static const int dstSigBits = 112;
70
71#else
Ahmed Bougachaf1ac8502015-05-12 18:33:42 +000072#error Destination should be single, double, or quad precision!
Joerg Sonnenberger7610e8c2014-05-29 00:54:26 +000073#endif //end destination precision
74
75// End of specialization parameters. Two helper routines for conversion to and
76// from the representation of floating-point data as integer values follow.
77
Saleem Abdulrasool911cfc12015-10-10 21:21:28 +000078static __inline src_rep_t srcToRep(src_t x) {
Joerg Sonnenberger7610e8c2014-05-29 00:54:26 +000079 const union { src_t f; src_rep_t i; } rep = {.f = x};
80 return rep.i;
81}
82
Saleem Abdulrasool911cfc12015-10-10 21:21:28 +000083static __inline dst_t dstFromRep(dst_rep_t x) {
Joerg Sonnenberger7610e8c2014-05-29 00:54:26 +000084 const union { dst_t f; dst_rep_t i; } rep = {.i = x};
85 return rep.f;
86}
87// End helper routines. Conversion implementation follows.
88
89#endif //FP_EXTEND_HEADER