fp_extend.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. #elif defined SRC_HALF
  38. typedef uint16_t src_t;
  39. typedef uint16_t src_rep_t;
  40. #define SRC_REP_C UINT16_C
  41. static const int srcSigBits = 10;
  42. #define src_rep_t_clz __builtin_clz
  43. #else
  44. #error Source should be half, single, or double precision!
  45. #endif //end source precision
  46. #if defined DST_SINGLE
  47. typedef float dst_t;
  48. typedef uint32_t dst_rep_t;
  49. #define DST_REP_C UINT32_C
  50. static const int dstSigBits = 23;
  51. #elif defined DST_DOUBLE
  52. typedef double dst_t;
  53. typedef uint64_t dst_rep_t;
  54. #define DST_REP_C UINT64_C
  55. static const int dstSigBits = 52;
  56. #elif defined DST_QUAD
  57. typedef long double dst_t;
  58. typedef __uint128_t dst_rep_t;
  59. #define DST_REP_C (__uint128_t)
  60. static const int dstSigBits = 112;
  61. #else
  62. #error Destination should be single, double, or quad precision!
  63. #endif //end destination precision
  64. // End of specialization parameters. Two helper routines for conversion to and
  65. // from the representation of floating-point data as integer values follow.
  66. static __inline src_rep_t srcToRep(src_t x) {
  67. const union { src_t f; src_rep_t i; } rep = {.f = x};
  68. return rep.i;
  69. }
  70. static __inline dst_t dstFromRep(dst_rep_t x) {
  71. const union { dst_t f; dst_rep_t i; } rep = {.i = x};
  72. return rep.f;
  73. }
  74. // End helper routines. Conversion implementation follows.
  75. #endif //FP_EXTEND_HEADER