xref: /minix3/sys/external/bsd/compiler_rt/dist/lib/builtins/fp_extend.h (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc //===-lib/fp_extend.h - low precision -> high 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 setting
11*0a6a1f1dSLionel Sambuc //
12*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
13*0a6a1f1dSLionel Sambuc 
14*0a6a1f1dSLionel Sambuc #ifndef FP_EXTEND_HEADER
15*0a6a1f1dSLionel Sambuc #define FP_EXTEND_HEADER
16*0a6a1f1dSLionel Sambuc 
17*0a6a1f1dSLionel Sambuc #include "int_lib.h"
18*0a6a1f1dSLionel Sambuc 
19*0a6a1f1dSLionel Sambuc #if defined SRC_SINGLE
20*0a6a1f1dSLionel Sambuc typedef float src_t;
21*0a6a1f1dSLionel Sambuc typedef uint32_t src_rep_t;
22*0a6a1f1dSLionel Sambuc #define SRC_REP_C UINT32_C
23*0a6a1f1dSLionel Sambuc static const int srcSigBits = 23;
24*0a6a1f1dSLionel Sambuc #define src_rep_t_clz __builtin_clz
25*0a6a1f1dSLionel Sambuc 
26*0a6a1f1dSLionel Sambuc #elif defined SRC_DOUBLE
27*0a6a1f1dSLionel Sambuc typedef double src_t;
28*0a6a1f1dSLionel Sambuc typedef uint64_t src_rep_t;
29*0a6a1f1dSLionel Sambuc #define SRC_REP_C UINT64_C
30*0a6a1f1dSLionel Sambuc static const int srcSigBits = 52;
src_rep_t_clz(src_rep_t a)31*0a6a1f1dSLionel Sambuc static inline int src_rep_t_clz(src_rep_t a) {
32*0a6a1f1dSLionel Sambuc #if defined __LP64__
33*0a6a1f1dSLionel Sambuc     return __builtin_clzl(a);
34*0a6a1f1dSLionel Sambuc #else
35*0a6a1f1dSLionel Sambuc     if (a & REP_C(0xffffffff00000000))
36*0a6a1f1dSLionel Sambuc         return __builtin_clz(a >> 32);
37*0a6a1f1dSLionel Sambuc     else
38*0a6a1f1dSLionel Sambuc         return 32 + __builtin_clz(a & REP_C(0xffffffff));
39*0a6a1f1dSLionel Sambuc #endif
40*0a6a1f1dSLionel Sambuc }
41*0a6a1f1dSLionel Sambuc 
42*0a6a1f1dSLionel Sambuc #else
43*0a6a1f1dSLionel Sambuc #error Source should be single precision or double precision!
44*0a6a1f1dSLionel Sambuc #endif //end source precision
45*0a6a1f1dSLionel Sambuc 
46*0a6a1f1dSLionel Sambuc #if defined DST_DOUBLE
47*0a6a1f1dSLionel Sambuc typedef double dst_t;
48*0a6a1f1dSLionel Sambuc typedef uint64_t dst_rep_t;
49*0a6a1f1dSLionel Sambuc #define DST_REP_C UINT64_C
50*0a6a1f1dSLionel Sambuc static const int dstSigBits = 52;
51*0a6a1f1dSLionel Sambuc 
52*0a6a1f1dSLionel Sambuc #elif defined DST_QUAD
53*0a6a1f1dSLionel Sambuc typedef long double dst_t;
54*0a6a1f1dSLionel Sambuc typedef __uint128_t dst_rep_t;
55*0a6a1f1dSLionel Sambuc #define DST_REP_C (__uint128_t)
56*0a6a1f1dSLionel Sambuc static const int dstSigBits = 112;
57*0a6a1f1dSLionel Sambuc 
58*0a6a1f1dSLionel Sambuc #else
59*0a6a1f1dSLionel Sambuc #error Destination should be double precision or quad precision!
60*0a6a1f1dSLionel Sambuc #endif //end destination precision
61*0a6a1f1dSLionel Sambuc 
62*0a6a1f1dSLionel Sambuc // End of specialization parameters.  Two helper routines for conversion to and
63*0a6a1f1dSLionel Sambuc // from the representation of floating-point data as integer values follow.
64*0a6a1f1dSLionel Sambuc 
srcToRep(src_t x)65*0a6a1f1dSLionel Sambuc static inline src_rep_t srcToRep(src_t x) {
66*0a6a1f1dSLionel Sambuc     const union { src_t f; src_rep_t i; } rep = {.f = x};
67*0a6a1f1dSLionel Sambuc     return rep.i;
68*0a6a1f1dSLionel Sambuc }
69*0a6a1f1dSLionel Sambuc 
dstFromRep(dst_rep_t x)70*0a6a1f1dSLionel Sambuc static inline dst_t dstFromRep(dst_rep_t x) {
71*0a6a1f1dSLionel Sambuc     const union { dst_t f; dst_rep_t i; } rep = {.i = x};
72*0a6a1f1dSLionel Sambuc     return rep.f;
73*0a6a1f1dSLionel Sambuc }
74*0a6a1f1dSLionel Sambuc // End helper routines.  Conversion implementation follows.
75*0a6a1f1dSLionel Sambuc 
76*0a6a1f1dSLionel Sambuc #endif //FP_EXTEND_HEADER
77