fp_trunc.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //=== lib/fp_trunc.h - high precision -> low 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 precision setting
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef FP_TRUNC_HEADER
  14. #define FP_TRUNC_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. #elif defined SRC_DOUBLE
  22. typedef double src_t;
  23. typedef uint64_t src_rep_t;
  24. #define SRC_REP_C UINT64_C
  25. static const int srcSigBits = 52;
  26. #elif defined SRC_QUAD
  27. typedef long double src_t;
  28. typedef __uint128_t src_rep_t;
  29. #define SRC_REP_C (__uint128_t)
  30. static const int srcSigBits = 112;
  31. #else
  32. #error Source should be double precision or quad precision!
  33. #endif //end source precision
  34. #if defined DST_DOUBLE
  35. typedef double dst_t;
  36. typedef uint64_t dst_rep_t;
  37. #define DST_REP_C UINT64_C
  38. static const int dstSigBits = 52;
  39. #elif defined DST_SINGLE
  40. typedef float dst_t;
  41. typedef uint32_t dst_rep_t;
  42. #define DST_REP_C UINT32_C
  43. static const int dstSigBits = 23;
  44. #elif defined DST_HALF
  45. typedef uint16_t dst_t;
  46. typedef uint16_t dst_rep_t;
  47. #define DST_REP_C UINT16_C
  48. static const int dstSigBits = 10;
  49. #else
  50. #error Destination should be single precision or double precision!
  51. #endif //end destination precision
  52. // End of specialization parameters. Two helper routines for conversion to and
  53. // from the representation of floating-point data as integer values follow.
  54. static __inline src_rep_t srcToRep(src_t x) {
  55. const union { src_t f; src_rep_t i; } rep = {.f = x};
  56. return rep.i;
  57. }
  58. static __inline dst_t dstFromRep(dst_rep_t x) {
  59. const union { dst_t f; dst_rep_t i; } rep = {.i = x};
  60. return rep.f;
  61. }
  62. #endif // FP_TRUNC_HEADER