fp_extend_impl.inc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //=-lib/fp_extend_impl.inc - low precision -> high precision conversion -*-- -//
  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. // This file implements a fairly generic conversion from a narrower to a wider
  11. // IEEE-754 floating-point type. The constants and types defined following the
  12. // includes below parameterize the conversion.
  13. //
  14. // It does not support types that don't use the usual IEEE-754 interchange
  15. // formats; specifically, some work would be needed to adapt it to
  16. // (for example) the Intel 80-bit format or PowerPC double-double format.
  17. //
  18. // Note please, however, that this implementation is only intended to support
  19. // *widening* operations; if you need to convert to a *narrower* floating-point
  20. // type (e.g. double -> float), then this routine will not do what you want it
  21. // to.
  22. //
  23. // It also requires that integer types at least as large as both formats
  24. // are available on the target platform; this may pose a problem when trying
  25. // to add support for quad on some 32-bit systems, for example. You also may
  26. // run into trouble finding an appropriate CLZ function for wide source types;
  27. // you will likely need to roll your own on some platforms.
  28. //
  29. // Finally, the following assumptions are made:
  30. //
  31. // 1. floating-point types and integer types have the same endianness on the
  32. // target platform
  33. //
  34. // 2. quiet NaNs, if supported, are indicated by the leading bit of the
  35. // significand field being set
  36. //
  37. //===----------------------------------------------------------------------===//
  38. #include "fp_extend.h"
  39. static __inline dst_t __extendXfYf2__(src_t a) {
  40. // Various constants whose values follow from the type parameters.
  41. // Any reasonable optimizer will fold and propagate all of these.
  42. const int srcBits = sizeof(src_t)*CHAR_BIT;
  43. const int srcExpBits = srcBits - srcSigBits - 1;
  44. const int srcInfExp = (1 << srcExpBits) - 1;
  45. const int srcExpBias = srcInfExp >> 1;
  46. const src_rep_t srcMinNormal = SRC_REP_C(1) << srcSigBits;
  47. const src_rep_t srcInfinity = (src_rep_t)srcInfExp << srcSigBits;
  48. const src_rep_t srcSignMask = SRC_REP_C(1) << (srcSigBits + srcExpBits);
  49. const src_rep_t srcAbsMask = srcSignMask - 1;
  50. const src_rep_t srcQNaN = SRC_REP_C(1) << (srcSigBits - 1);
  51. const src_rep_t srcNaNCode = srcQNaN - 1;
  52. const int dstBits = sizeof(dst_t)*CHAR_BIT;
  53. const int dstExpBits = dstBits - dstSigBits - 1;
  54. const int dstInfExp = (1 << dstExpBits) - 1;
  55. const int dstExpBias = dstInfExp >> 1;
  56. const dst_rep_t dstMinNormal = DST_REP_C(1) << dstSigBits;
  57. // Break a into a sign and representation of the absolute value
  58. const src_rep_t aRep = srcToRep(a);
  59. const src_rep_t aAbs = aRep & srcAbsMask;
  60. const src_rep_t sign = aRep & srcSignMask;
  61. dst_rep_t absResult;
  62. // If sizeof(src_rep_t) < sizeof(int), the subtraction result is promoted
  63. // to (signed) int. To avoid that, explicitly cast to src_rep_t.
  64. if ((src_rep_t)(aAbs - srcMinNormal) < srcInfinity - srcMinNormal) {
  65. // a is a normal number.
  66. // Extend to the destination type by shifting the significand and
  67. // exponent into the proper position and rebiasing the exponent.
  68. absResult = (dst_rep_t)aAbs << (dstSigBits - srcSigBits);
  69. absResult += (dst_rep_t)(dstExpBias - srcExpBias) << dstSigBits;
  70. }
  71. else if (aAbs >= srcInfinity) {
  72. // a is NaN or infinity.
  73. // Conjure the result by beginning with infinity, then setting the qNaN
  74. // bit (if needed) and right-aligning the rest of the trailing NaN
  75. // payload field.
  76. absResult = (dst_rep_t)dstInfExp << dstSigBits;
  77. absResult |= (dst_rep_t)(aAbs & srcQNaN) << (dstSigBits - srcSigBits);
  78. absResult |= (dst_rep_t)(aAbs & srcNaNCode) << (dstSigBits - srcSigBits);
  79. }
  80. else if (aAbs) {
  81. // a is denormal.
  82. // renormalize the significand and clear the leading bit, then insert
  83. // the correct adjusted exponent in the destination type.
  84. const int scale = src_rep_t_clz(aAbs) - src_rep_t_clz(srcMinNormal);
  85. absResult = (dst_rep_t)aAbs << (dstSigBits - srcSigBits + scale);
  86. absResult ^= dstMinNormal;
  87. const int resultExponent = dstExpBias - srcExpBias - scale + 1;
  88. absResult |= (dst_rep_t)resultExponent << dstSigBits;
  89. }
  90. else {
  91. // a is zero.
  92. absResult = 0;
  93. }
  94. // Apply the signbit to (dst_t)abs(a).
  95. const dst_rep_t result = absResult | (dst_rep_t)sign << (dstBits - srcBits);
  96. return dstFromRep(result);
  97. }