fp_extend.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //===-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. #ifndef FP_EXTEND_HEADER
  14. #define FP_EXTEND_HEADER
  15. #include "int_lib.h"
  16. #if defined SRC_SINGLE
  17. typedef float src_t;
  18. typedef uint32_t src_rep_t;
  19. #define SRC_REP_C UINT32_C
  20. static const int srcSigBits = 23;
  21. #define src_rep_t_clz __builtin_clz
  22. #elif defined SRC_DOUBLE
  23. typedef double src_t;
  24. typedef uint64_t src_rep_t;
  25. #define SRC_REP_C UINT64_C
  26. static const int srcSigBits = 52;
  27. static inline int src_rep_t_clz(src_rep_t a) {
  28. #if defined __LP64__
  29. return __builtin_clzl(a);
  30. #else
  31. if (a & REP_C(0xffffffff00000000))
  32. return __builtin_clz(a >> 32);
  33. else
  34. return 32 + __builtin_clz(a & REP_C(0xffffffff));
  35. #endif
  36. }
  37. #else
  38. #error Source should be single precision or double precision!
  39. #endif //end source precision
  40. #if defined DST_DOUBLE
  41. typedef double dst_t;
  42. typedef uint64_t dst_rep_t;
  43. #define DST_REP_C UINT64_C
  44. static const int dstSigBits = 52;
  45. #elif defined DST_QUAD
  46. typedef long double dst_t;
  47. typedef __uint128_t dst_rep_t;
  48. #define DST_REP_C (__uint128_t)
  49. static const int dstSigBits = 112;
  50. #else
  51. #error Destination should be double precision or quad precision!
  52. #endif //end destination precision
  53. // End of specialization parameters. Two helper routines for conversion to and
  54. // from the representation of floating-point data as integer values follow.
  55. static inline src_rep_t srcToRep(src_t x) {
  56. const union { src_t f; src_rep_t i; } rep = {.f = x};
  57. return rep.i;
  58. }
  59. static 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. // End helper routines. Conversion implementation follows.
  64. #endif //FP_EXTEND_HEADER