flt1282mpn.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* Copyright (C) 1995,1996,1997,1998,1999,2002,2003
  2. Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #include <float.h>
  17. #include <math.h>
  18. #include <stdlib.h>
  19. #include "gmp-impl.h"
  20. /* Convert a `__float128' in IEEE854 quad-precision format to a
  21. multi-precision integer representing the significand scaled up by its
  22. number of bits (113 for long double) and an integral power of two
  23. (MPN frexpl). */
  24. mp_size_t
  25. mpn_extract_flt128 (mp_ptr res_ptr, mp_size_t size,
  26. int *expt, int *is_neg,
  27. __float128 value)
  28. {
  29. ieee854_float128 u;
  30. u.value = value;
  31. *is_neg = u.ieee.negative;
  32. *expt = (int) u.ieee.exponent - IEEE854_FLOAT128_BIAS;
  33. #if BITS_PER_MP_LIMB == 32
  34. res_ptr[0] = u.ieee.mant_low; /* Low-order 32 bits of fraction. */
  35. res_ptr[1] = (u.ieee.mant_low >> 32);
  36. res_ptr[2] = u.ieee.mant_high;
  37. res_ptr[3] = (u.ieee.mant_high >> 32); /* High-order 32 bits. */
  38. #define N 4
  39. #elif BITS_PER_MP_LIMB == 64
  40. res_ptr[0] = u.ieee.mant_low;
  41. res_ptr[1] = u.ieee.mant_high;
  42. #define N 2
  43. #else
  44. #error "mp_limb size " BITS_PER_MP_LIMB "not accounted for"
  45. #endif
  46. /* The format does not fill the last limb. There are some zeros. */
  47. #define NUM_LEADING_ZEROS (BITS_PER_MP_LIMB \
  48. - (FLT128_MANT_DIG - ((N - 1) * BITS_PER_MP_LIMB)))
  49. if (u.ieee.exponent == 0)
  50. {
  51. /* A biased exponent of zero is a special case.
  52. Either it is a zero or it is a denormal number. */
  53. if (res_ptr[0] == 0 && res_ptr[1] == 0
  54. && res_ptr[N - 2] == 0 && res_ptr[N - 1] == 0) /* Assumes N<=4. */
  55. /* It's zero. */
  56. *expt = 0;
  57. else
  58. {
  59. /* It is a denormal number, meaning it has no implicit leading
  60. one bit, and its exponent is in fact the format minimum. */
  61. int cnt;
  62. #if N == 2
  63. if (res_ptr[N - 1] != 0)
  64. {
  65. count_leading_zeros (cnt, res_ptr[N - 1]);
  66. cnt -= NUM_LEADING_ZEROS;
  67. res_ptr[N - 1] = res_ptr[N - 1] << cnt
  68. | (res_ptr[0] >> (BITS_PER_MP_LIMB - cnt));
  69. res_ptr[0] <<= cnt;
  70. *expt = FLT128_MIN_EXP - 1 - cnt;
  71. }
  72. else
  73. {
  74. count_leading_zeros (cnt, res_ptr[0]);
  75. if (cnt >= NUM_LEADING_ZEROS)
  76. {
  77. res_ptr[N - 1] = res_ptr[0] << (cnt - NUM_LEADING_ZEROS);
  78. res_ptr[0] = 0;
  79. }
  80. else
  81. {
  82. res_ptr[N - 1] = res_ptr[0] >> (NUM_LEADING_ZEROS - cnt);
  83. res_ptr[0] <<= BITS_PER_MP_LIMB - (NUM_LEADING_ZEROS - cnt);
  84. }
  85. *expt = FLT128_MIN_EXP - 1
  86. - (BITS_PER_MP_LIMB - NUM_LEADING_ZEROS) - cnt;
  87. }
  88. #else
  89. int j, k, l;
  90. for (j = N - 1; j > 0; j--)
  91. if (res_ptr[j] != 0)
  92. break;
  93. count_leading_zeros (cnt, res_ptr[j]);
  94. cnt -= NUM_LEADING_ZEROS;
  95. l = N - 1 - j;
  96. if (cnt < 0)
  97. {
  98. cnt += BITS_PER_MP_LIMB;
  99. l--;
  100. }
  101. if (!cnt)
  102. for (k = N - 1; k >= l; k--)
  103. res_ptr[k] = res_ptr[k-l];
  104. else
  105. {
  106. for (k = N - 1; k > l; k--)
  107. res_ptr[k] = res_ptr[k-l] << cnt
  108. | res_ptr[k-l-1] >> (BITS_PER_MP_LIMB - cnt);
  109. res_ptr[k--] = res_ptr[0] << cnt;
  110. }
  111. for (; k >= 0; k--)
  112. res_ptr[k] = 0;
  113. *expt = FLT128_MIN_EXP - 1 - l * BITS_PER_MP_LIMB - cnt;
  114. #endif
  115. }
  116. }
  117. else
  118. /* Add the implicit leading one bit for a normalized number. */
  119. res_ptr[N - 1] |= (mp_limb_t) 1 << (FLT128_MANT_DIG - 1
  120. - ((N - 1) * BITS_PER_MP_LIMB));
  121. return N;
  122. }