fp_trunc.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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_DOUBLE
  17. typedef double src_t;
  18. typedef uint64_t src_rep_t;
  19. #define SRC_REP_C UINT64_C
  20. static const int srcSigBits = 52;
  21. #elif defined SRC_QUAD
  22. typedef long double src_t;
  23. typedef __uint128_t src_rep_t;
  24. #define SRC_REP_C (__uint128_t)
  25. static const int srcSigBits = 112;
  26. #else
  27. #error Source should be double precision or quad precision!
  28. #endif //end source precision
  29. #if defined DST_DOUBLE
  30. typedef double dst_t;
  31. typedef uint64_t dst_rep_t;
  32. #define DST_REP_C UINT64_C
  33. static const int dstSigBits = 52;
  34. #elif defined DST_SINGLE
  35. typedef float dst_t;
  36. typedef uint32_t dst_rep_t;
  37. #define DST_REP_C UINT32_C
  38. static const int dstSigBits = 23;
  39. #else
  40. #error Destination should be single precision or double precision!
  41. #endif //end destination precision
  42. // End of specialization parameters. Two helper routines for conversion to and
  43. // from the representation of floating-point data as integer values follow.
  44. static inline src_rep_t srcToRep(src_t x) {
  45. const union { src_t f; src_rep_t i; } rep = {.f = x};
  46. return rep.i;
  47. }
  48. static inline dst_t dstFromRep(dst_rep_t x) {
  49. const union { dst_t f; dst_rep_t i; } rep = {.i = x};
  50. return rep.f;
  51. }
  52. #endif // FP_TRUNC_HEADER