xref: /freebsd/contrib/llvm-project/compiler-rt/lib/builtins/fp_extend.h (revision 43a5ec4eb41567cc92586503212743d89686d78f)
1 //===-lib/fp_extend.h - low precision -> high precision conversion -*- C
2 //-*-===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
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
20 typedef float src_t;
21 typedef uint32_t src_rep_t;
22 #define SRC_REP_C UINT32_C
23 static const int srcSigBits = 23;
24 #define src_rep_t_clz clzsi
25 
26 #elif defined SRC_DOUBLE
27 typedef double src_t;
28 typedef uint64_t src_rep_t;
29 #define SRC_REP_C UINT64_C
30 static const int srcSigBits = 52;
31 static __inline int src_rep_t_clz(src_rep_t a) {
32 #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 
42 #elif defined SRC_HALF
43 #ifdef COMPILER_RT_HAS_FLOAT16
44 typedef _Float16 src_t;
45 #else
46 typedef uint16_t src_t;
47 #endif
48 typedef uint16_t src_rep_t;
49 #define SRC_REP_C UINT16_C
50 static const int srcSigBits = 10;
51 #define src_rep_t_clz __builtin_clz
52 
53 #else
54 #error Source should be half, single, or double precision!
55 #endif // end source precision
56 
57 #if defined DST_SINGLE
58 typedef float dst_t;
59 typedef uint32_t dst_rep_t;
60 #define DST_REP_C UINT32_C
61 static const int dstSigBits = 23;
62 
63 #elif defined DST_DOUBLE
64 typedef double dst_t;
65 typedef uint64_t dst_rep_t;
66 #define DST_REP_C UINT64_C
67 static const int dstSigBits = 52;
68 
69 #elif defined DST_QUAD
70 typedef long double dst_t;
71 typedef __uint128_t dst_rep_t;
72 #define DST_REP_C (__uint128_t)
73 static const int dstSigBits = 112;
74 
75 #else
76 #error Destination should be single, double, or quad precision!
77 #endif // end destination precision
78 
79 // End of specialization parameters.  Two helper routines for conversion to and
80 // from the representation of floating-point data as integer values follow.
81 
82 static __inline src_rep_t srcToRep(src_t x) {
83   const union {
84     src_t f;
85     src_rep_t i;
86   } rep = {.f = x};
87   return rep.i;
88 }
89 
90 static __inline dst_t dstFromRep(dst_rep_t x) {
91   const union {
92     dst_t f;
93     dst_rep_t i;
94   } rep = {.i = x};
95   return rep.f;
96 }
97 // End helper routines.  Conversion implementation follows.
98 
99 #endif // FP_EXTEND_HEADER
100