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