fp_extend_impl.inc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //=-lib/fp_extend_impl.inc - low precision -> high precision conversion -*-- -//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements a fairly generic conversion from a narrower to a wider
  10. // IEEE-754 floating-point type. The constants and types defined following the
  11. // includes below parameterize the conversion.
  12. //
  13. // It does not support types that don't use the usual IEEE-754 interchange
  14. // formats; specifically, some work would be needed to adapt it to
  15. // (for example) the Intel 80-bit format or PowerPC double-double format.
  16. //
  17. // Note please, however, that this implementation is only intended to support
  18. // *widening* operations; if you need to convert to a *narrower* floating-point
  19. // type (e.g. double -> float), then this routine will not do what you want it
  20. // to.
  21. //
  22. // It also requires that integer types at least as large as both formats
  23. // are available on the target platform; this may pose a problem when trying
  24. // to add support for quad on some 32-bit systems, for example. You also may
  25. // run into trouble finding an appropriate CLZ function for wide source types;
  26. // you will likely need to roll your own on some platforms.
  27. //
  28. // Finally, the following assumptions are made:
  29. //
  30. // 1. Floating-point types and integer types have the same endianness on the
  31. // target platform.
  32. //
  33. // 2. Quiet NaNs, if supported, are indicated by the leading bit of the
  34. // significand field being set.
  35. //
  36. //===----------------------------------------------------------------------===//
  37. #include "fp_extend.h"
  38. // The source type may use a usual IEEE-754 interchange format or Intel 80-bit
  39. // format. In particular, for the source type srcSigFracBits may be not equal to
  40. // srcSigBits. The destination type is assumed to be one of IEEE-754 standard
  41. // types.
  42. static __inline dst_t __extendXfYf2__(src_t a) {
  43. // Various constants whose values follow from the type parameters.
  44. // Any reasonable optimizer will fold and propagate all of these.
  45. const int srcInfExp = (1 << srcExpBits) - 1;
  46. const int srcExpBias = srcInfExp >> 1;
  47. const int dstInfExp = (1 << dstExpBits) - 1;
  48. const int dstExpBias = dstInfExp >> 1;
  49. // Break a into a sign and representation of the absolute value.
  50. const src_rep_t aRep = srcToRep(a);
  51. const src_rep_t srcSign = extract_sign_from_src(aRep);
  52. const src_rep_t srcExp = extract_exp_from_src(aRep);
  53. const src_rep_t srcSigFrac = extract_sig_frac_from_src(aRep);
  54. dst_rep_t dstSign = srcSign;
  55. dst_rep_t dstExp;
  56. dst_rep_t dstSigFrac;
  57. if (srcExp >= 1 && srcExp < (src_rep_t)srcInfExp) {
  58. // a is a normal number.
  59. dstExp = (dst_rep_t)srcExp + (dst_rep_t)(dstExpBias - srcExpBias);
  60. dstSigFrac = (dst_rep_t)srcSigFrac << (dstSigFracBits - srcSigFracBits);
  61. }
  62. else if (srcExp == srcInfExp) {
  63. // a is NaN or infinity.
  64. dstExp = dstInfExp;
  65. dstSigFrac = (dst_rep_t)srcSigFrac << (dstSigFracBits - srcSigFracBits);
  66. }
  67. else if (srcSigFrac) {
  68. // a is denormal.
  69. if (srcExpBits == dstExpBits) {
  70. // The exponent fields are identical and this is a denormal number, so all
  71. // the non-significand bits are zero. In particular, this branch is always
  72. // taken when we extend a denormal F80 to F128.
  73. dstExp = 0;
  74. dstSigFrac = ((dst_rep_t)srcSigFrac) << (dstSigFracBits - srcSigFracBits);
  75. } else {
  76. #ifndef src_rep_t_clz
  77. // If src_rep_t_clz is not defined this branch must be unreachable.
  78. __builtin_unreachable();
  79. #else
  80. // Renormalize the significand and clear the leading bit.
  81. // For F80 -> F128 this codepath is unused.
  82. const int scale = clz_in_sig_frac(srcSigFrac) + 1;
  83. dstExp = dstExpBias - srcExpBias - scale + 1;
  84. dstSigFrac = (dst_rep_t)srcSigFrac
  85. << (dstSigFracBits - srcSigFracBits + scale);
  86. const dst_rep_t dstMinNormal = DST_REP_C(1) << (dstBits - 1 - dstExpBits);
  87. dstSigFrac ^= dstMinNormal;
  88. #endif
  89. }
  90. }
  91. else {
  92. // a is zero.
  93. dstExp = 0;
  94. dstSigFrac = 0;
  95. }
  96. const dst_rep_t result = construct_dst_rep(dstSign, dstExp, dstSigFrac);
  97. return dstFromRep(result);
  98. }