fp_extend.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //===-lib/fp_extend.h - low precision -> high precision conversion -*- C
  2. //-*-===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  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 clzsi
  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 clzsi(a >> 32);
  33. else
  34. return 32 + clzsi(a & REP_C(0xffffffff));
  35. #endif
  36. }
  37. #elif defined SRC_HALF
  38. #ifdef COMPILER_RT_HAS_FLOAT16
  39. typedef _Float16 src_t;
  40. #else
  41. typedef uint16_t src_t;
  42. #endif
  43. typedef uint16_t src_rep_t;
  44. #define SRC_REP_C UINT16_C
  45. static const int srcSigBits = 10;
  46. #define src_rep_t_clz __builtin_clz
  47. #else
  48. #error Source should be half, single, or double precision!
  49. #endif // end source precision
  50. #if defined DST_SINGLE
  51. typedef float dst_t;
  52. typedef uint32_t dst_rep_t;
  53. #define DST_REP_C UINT32_C
  54. static const int dstSigBits = 23;
  55. #elif defined DST_DOUBLE
  56. typedef double dst_t;
  57. typedef uint64_t dst_rep_t;
  58. #define DST_REP_C UINT64_C
  59. static const int dstSigBits = 52;
  60. #elif defined DST_QUAD
  61. typedef long double dst_t;
  62. typedef __uint128_t dst_rep_t;
  63. #define DST_REP_C (__uint128_t)
  64. static const int dstSigBits = 112;
  65. #else
  66. #error Destination should be single, double, or quad precision!
  67. #endif // end destination precision
  68. // End of specialization parameters. Two helper routines for conversion to and
  69. // from the representation of floating-point data as integer values follow.
  70. static __inline src_rep_t srcToRep(src_t x) {
  71. const union {
  72. src_t f;
  73. src_rep_t i;
  74. } rep = {.f = x};
  75. return rep.i;
  76. }
  77. static __inline dst_t dstFromRep(dst_rep_t x) {
  78. const union {
  79. dst_t f;
  80. dst_rep_t i;
  81. } rep = {.i = x};
  82. return rep.f;
  83. }
  84. // End helper routines. Conversion implementation follows.
  85. #endif // FP_EXTEND_HEADER